Commit d7c5f14d authored by yoshiki@chromium.org's avatar yoshiki@chromium.org

[Files.app] Fetch and use CWS OAuth token instead of Drive token in Suggest app dialog.

We need to pass the CWS OAuth token to the CWS widget in Suggest app dialog, instead of Drive OAuth token. This patch adds the method to fetch the CWS token and replace the drive token with it in Suggest app dialog.

BUG=none
TEST=manual

Review URL: https://chromiumcodereview.appspot.com/23549016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222004 0039d316-1c4b-4281-b951-d872f2087c98
parent 417b5efc
...@@ -7,20 +7,29 @@ ...@@ -7,20 +7,29 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h" #include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/logging.h" #include "chrome/browser/chromeos/drive/logging.h"
#include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
#include "chrome/browser/chromeos/file_manager/file_manager_installer.h" #include "chrome/browser/chromeos/file_manager/file_manager_installer.h"
#include "chrome/browser/chromeos/settings/cros_settings.h" #include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/google_apis/auth_service.h"
#include "chrome/browser/lifetime/application_lifetime.h" #include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/common/page_zoom.h" #include "content/public/common/page_zoom.h"
#include "google_apis/gaia/oauth2_token_service.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace extensions { namespace extensions {
namespace {
const char kCWSScope[] = "https://www.googleapis.com/auth/chromewebstore";
}
FileBrowserPrivateLogoutUserFunction::FileBrowserPrivateLogoutUserFunction() { FileBrowserPrivateLogoutUserFunction::FileBrowserPrivateLogoutUserFunction() {
} }
...@@ -254,4 +263,61 @@ void FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete( ...@@ -254,4 +263,61 @@ void FileBrowserPrivateInstallWebstoreItemFunction::OnInstallComplete(
SendResponse(success); SendResponse(success);
} }
FileBrowserPrivateRequestWebStoreAccessTokenFunction::
FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
}
FileBrowserPrivateRequestWebStoreAccessTokenFunction::
~FileBrowserPrivateRequestWebStoreAccessTokenFunction() {
}
bool FileBrowserPrivateRequestWebStoreAccessTokenFunction::RunImpl() {
std::vector<std::string> scopes;
scopes.push_back(kCWSScope);
OAuth2TokenService* oauth_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile());
net::URLRequestContextGetter* url_request_context_getter =
g_browser_process->system_request_context();
if (!oauth_service) {
drive::util::Log(logging::LOG_ERROR,
"CWS OAuth token fetch failed. OAuth2TokenService can't "
"be retrived.");
SetResult(base::Value::CreateNullValue());
return false;
}
auth_service_.reset(new google_apis::AuthService(
oauth_service,
url_request_context_getter,
scopes));
auth_service_->StartAuthentication(base::Bind(
&FileBrowserPrivateRequestWebStoreAccessTokenFunction::
OnAccessTokenFetched,
this));
return true;
}
void FileBrowserPrivateRequestWebStoreAccessTokenFunction::OnAccessTokenFetched(
google_apis::GDataErrorCode code,
const std::string& access_token) {
if (code == google_apis::HTTP_SUCCESS) {
DCHECK(auth_service_->HasAccessToken());
DCHECK(access_token == auth_service_->access_token());
drive::util::Log(logging::LOG_INFO,
"CWS OAuth token fetch succeeded. (token: %s)",
access_token.c_str());
SetResult(new base::StringValue(access_token));
SendResponse(true);
} else {
drive::util::Log(logging::LOG_ERROR,
"CWS OAuth token fetch failed. (GDataErrorCode: %s)",
google_apis::GDataErrorCodeToString(code).c_str());
SetResult(base::Value::CreateNullValue());
SendResponse(false);
}
}
} // namespace extensions } // namespace extensions
...@@ -10,6 +10,11 @@ ...@@ -10,6 +10,11 @@
#include "chrome/browser/chromeos/extensions/file_manager/private_api_base.h" #include "chrome/browser/chromeos/extensions/file_manager/private_api_base.h"
#include "chrome/browser/chromeos/file_manager/zip_file_creator.h" #include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
#include "chrome/browser/google_apis/gdata_errorcode.h"
namespace google_apis {
class AuthServiceInterface;
}
namespace extensions { namespace extensions {
...@@ -116,6 +121,26 @@ class FileBrowserPrivateInstallWebstoreItemFunction ...@@ -116,6 +121,26 @@ class FileBrowserPrivateInstallWebstoreItemFunction
std::string webstore_item_id_; std::string webstore_item_id_;
}; };
class FileBrowserPrivateRequestWebStoreAccessTokenFunction
: public LoggedAsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.requestWebStoreAccessToken",
FILEBROWSERPRIVATE_REQUESTWEBSTOREACCESSTOKEN);
FileBrowserPrivateRequestWebStoreAccessTokenFunction();
protected:
virtual ~FileBrowserPrivateRequestWebStoreAccessTokenFunction();
virtual bool RunImpl() OVERRIDE;
private:
scoped_ptr<google_apis::AuthServiceInterface> auth_service_;
void OnAccessTokenFetched(google_apis::GDataErrorCode code,
const std::string& access_token);
};
} // namespace extensions } // namespace extensions
#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_MISC_H_ #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_MISC_H_
...@@ -629,6 +629,7 @@ enum HistogramValue { ...@@ -629,6 +629,7 @@ enum HistogramValue {
USB_OPENDEVICE, USB_OPENDEVICE,
ACTIVITYLOGPRIVATE_DELETEDATABASE, ACTIVITYLOGPRIVATE_DELETEDATABASE,
ACTIVITYLOGPRIVATE_DELETEURLS, ACTIVITYLOGPRIVATE_DELETEURLS,
FILEBROWSERPRIVATE_REQUESTWEBSTOREACCESSTOKEN,
ENUM_BOUNDARY // Last entry: Add new entries above. ENUM_BOUNDARY // Last entry: Add new entries above.
}; };
......
...@@ -219,17 +219,16 @@ SuggestAppsDialog.prototype.authorizeRequest_ = function(e) { ...@@ -219,17 +219,16 @@ SuggestAppsDialog.prototype.authorizeRequest_ = function(e) {
* @private * @private
*/ */
SuggestAppsDialog.prototype.retrieveAuthorizeToken_ = function(callback) { SuggestAppsDialog.prototype.retrieveAuthorizeToken_ = function(callback) {
// TODO(yoshiki): Share the access token with ShareDialog.
if (this.accessToken_) { if (this.accessToken_) {
callback(); callback();
return; return;
} }
// Fetch or update the access token. // Fetch or update the access token.
chrome.fileBrowserPrivate.requestAccessToken( chrome.fileBrowserPrivate.requestWebStoreAccessToken(
false, // force_refresh function(accessToken) {
function(inAccessToken) { // In case of error, this.accessToken_ will be set to null.
this.accessToken_ = inAccessToken; this.accessToken_ = accessToken;
callback(); callback();
}.bind(this)); }.bind(this));
}; };
...@@ -256,6 +255,12 @@ SuggestAppsDialog.prototype.show = function(extension, mime, onDialogClosed) { ...@@ -256,6 +255,12 @@ SuggestAppsDialog.prototype.show = function(extension, mime, onDialogClosed) {
// Makes it sure that the initialization is completed. // Makes it sure that the initialization is completed.
this.initializationTask_.run(function() { this.initializationTask_.run(function() {
if (!this.accessToken_) {
this.state_ = SuggestAppsDialog.State.INITIALIZE_FAILED_CLOSING;
this.onHide_();
return;
}
var title = str('SUGGEST_DIALOG_TITLE'); var title = str('SUGGEST_DIALOG_TITLE');
// TODO(yoshiki): Remove this before ShareDialog launches. // TODO(yoshiki): Remove this before ShareDialog launches.
......
...@@ -1132,6 +1132,23 @@ ...@@ -1132,6 +1132,23 @@
} }
] ]
}, },
{
"name": "requestWebStoreAccessToken",
"description": "Requests a Webstore API OAuth2 access token.",
"parameters": [
{
"name": "callback",
"type": "function",
"parameters": [
{
"name": "accessToken",
"type": "string",
"description": "OAuth2 access token, or an empty string if failed to fetch."
}
]
}
]
},
{ {
"name": "getShareUrl", "name": "getShareUrl",
"description": "Requests a share dialog url for the specified file.", "description": "Requests a share dialog url for the specified file.",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment