diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-01 16:20:19 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-01 16:20:19 +0000 |
commit | c97496b98c8493f96867b5281ca9a468c2896c13 (patch) | |
tree | 69634019af059b717339cf43eb632455fa807df2 | |
parent | c5363bc56306ea3e19dcfac834c9af564774b267 (diff) | |
download | chromium_src-c97496b98c8493f96867b5281ca9a468c2896c13.zip chromium_src-c97496b98c8493f96867b5281ca9a468c2896c13.tar.gz chromium_src-c97496b98c8493f96867b5281ca9a468c2896c13.tar.bz2 |
Don't allow OAuth2MintTokenFlow to outlive profile.
BUG=150911
Review URL: https://codereview.chromium.org/10966009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@159502 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/permissions_updater.cc | 71 | ||||
-rw-r--r-- | chrome/browser/extensions/permissions_updater.h | 3 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_mint_token_flow.cc | 13 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_mint_token_flow.h | 8 |
4 files changed, 56 insertions, 39 deletions
diff --git a/chrome/browser/extensions/permissions_updater.cc b/chrome/browser/extensions/permissions_updater.cc index 06e921d..2a61b99 100644 --- a/chrome/browser/extensions/permissions_updater.cc +++ b/chrome/browser/extensions/permissions_updater.cc @@ -18,6 +18,8 @@ #include "chrome/common/extensions/api/permissions.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_messages.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_process_host.h" #include "google_apis/gaia/oauth2_mint_token_flow.h" @@ -33,7 +35,59 @@ namespace { const char kOnAdded[] = "permissions.onAdded"; const char kOnRemoved[] = "permissions.onRemoved"; -} +// An object to link the lifetime of an OAuth2MintTokenFlow to a Profile. +// The flow should not outlive the profile because the request context will +// become invalid. +class OAuth2GrantRecorder : public OAuth2MintTokenFlow::Delegate, + public content::NotificationObserver { + public: + OAuth2GrantRecorder(Profile* profile, const Extension* extension) + : ALLOW_THIS_IN_INITIALIZER_LIST(flow_( + profile->GetRequestContext(), + this, + OAuth2MintTokenFlow::Parameters( + TokenServiceFactory::GetForProfile(profile)-> + GetOAuth2LoginRefreshToken(), + extension->id(), + extension->oauth2_info().client_id, + extension->oauth2_info().scopes, + OAuth2MintTokenFlow::MODE_RECORD_GRANT))) { + notification_registrar_.Add(this, + chrome::NOTIFICATION_PROFILE_DESTROYED, + content::Source<Profile>(profile)); + + flow_.Start(); + } + + // content::NotificationObserver: + void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE { + DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_DESTROYED); + delete this; + } + + // OAuth2MintTokenFlow::Delegate: + virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE { + delete this; + } + virtual void OnIssueAdviceSuccess( + const IssueAdviceInfo& issue_advice) OVERRIDE { + delete this; + } + virtual void OnMintTokenFailure( + const GoogleServiceAuthError& error) OVERRIDE { + delete this; + } + + private: + virtual ~OAuth2GrantRecorder() {} + + OAuth2MintTokenFlow flow_; + content::NotificationRegistrar notification_registrar_; +}; + +} // namespace PermissionsUpdater::PermissionsUpdater(Profile* profile) : profile_(profile) {} @@ -85,7 +139,7 @@ void PermissionsUpdater::GrantActivePermissions(const Extension* extension, return; if (record_oauth2_grant) - RecordOAuth2Grant(extension); + new OAuth2GrantRecorder(profile_, extension); GetExtensionPrefs()->AddGrantedPermissions(extension->id(), extension->GetActivePermissions()); @@ -97,19 +151,6 @@ void PermissionsUpdater::UpdateActivePermissions( extension->SetActivePermissions(permissions); } -void PermissionsUpdater::RecordOAuth2Grant(const Extension* extension) { - TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); - OAuth2MintTokenFlow* flow = new OAuth2MintTokenFlow( - profile_->GetRequestContext(), NULL, OAuth2MintTokenFlow::Parameters( - token_service->GetOAuth2LoginRefreshToken(), - extension->id(), - extension->oauth2_info().client_id, - extension->oauth2_info().scopes, - OAuth2MintTokenFlow::MODE_RECORD_GRANT)); - // |flow| will delete itself. - flow->FireAndForget(); -} - void PermissionsUpdater::DispatchEvent( const std::string& extension_id, const char* event_name, diff --git a/chrome/browser/extensions/permissions_updater.h b/chrome/browser/extensions/permissions_updater.h index 7b8ffd3..e8875bc 100644 --- a/chrome/browser/extensions/permissions_updater.h +++ b/chrome/browser/extensions/permissions_updater.h @@ -54,9 +54,6 @@ class PermissionsUpdater { REMOVED, }; - // Records the oauth2 grant for the scopes specified in |permissions|. - void RecordOAuth2Grant(const Extension* extension); - // Dispatches specified event to the extension. void DispatchEvent(const std::string& extension_id, const char* event_name, diff --git a/google_apis/gaia/oauth2_mint_token_flow.cc b/google_apis/gaia/oauth2_mint_token_flow.cc index 66dd1a0..88f2878a 100644 --- a/google_apis/gaia/oauth2_mint_token_flow.cc +++ b/google_apis/gaia/oauth2_mint_token_flow.cc @@ -96,23 +96,14 @@ OAuth2MintTokenFlow::OAuth2MintTokenFlow( : OAuth2ApiCallFlow( context, parameters.login_refresh_token, "", std::vector<std::string>()), - context_(context), delegate_(delegate), parameters_(parameters), - delete_when_done_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } OAuth2MintTokenFlow::~OAuth2MintTokenFlow() { } -void OAuth2MintTokenFlow::FireAndForget() { - delete_when_done_ = true; - Start(); -} - void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token) { - scoped_ptr<OAuth2MintTokenFlow> will_delete(delete_when_done_ ? this : NULL); - if (delegate_) delegate_->OnMintTokenSuccess(access_token); @@ -121,8 +112,6 @@ void OAuth2MintTokenFlow::ReportSuccess(const std::string& access_token) { void OAuth2MintTokenFlow::ReportIssueAdviceSuccess( const IssueAdviceInfo& issue_advice) { - scoped_ptr<OAuth2MintTokenFlow> will_delete(delete_when_done_ ? this : NULL); - if (delegate_) delegate_->OnIssueAdviceSuccess(issue_advice); @@ -131,8 +120,6 @@ void OAuth2MintTokenFlow::ReportIssueAdviceSuccess( void OAuth2MintTokenFlow::ReportFailure( const GoogleServiceAuthError& error) { - scoped_ptr<OAuth2MintTokenFlow> will_delete(delete_when_done_ ? this : NULL); - if (delegate_) delegate_->OnMintTokenFailure(error); diff --git a/google_apis/gaia/oauth2_mint_token_flow.h b/google_apis/gaia/oauth2_mint_token_flow.h index cdca9ec..40850b4 100644 --- a/google_apis/gaia/oauth2_mint_token_flow.h +++ b/google_apis/gaia/oauth2_mint_token_flow.h @@ -103,10 +103,6 @@ class OAuth2MintTokenFlow : public OAuth2ApiCallFlow { const Parameters& parameters); virtual ~OAuth2MintTokenFlow(); - // Starts the flow, and deletes |this| when done. Useful when the caller - // does not care about the response (|delegate_| is NULL). - void FireAndForget(); - protected: // Implementation of template methods in OAuth2ApiCallFlow. virtual GURL CreateApiCallUrl() OVERRIDE; @@ -139,12 +135,8 @@ class OAuth2MintTokenFlow : public OAuth2ApiCallFlow { static bool ParseMintTokenResponse( const base::DictionaryValue* dict, std::string* access_token); - net::URLRequestContextGetter* context_; Delegate* delegate_; Parameters parameters_; - // If true, |this| owns itself and will delete itself after reporting - // success or failure. - bool delete_when_done_; base::WeakPtrFactory<OAuth2MintTokenFlow> weak_factory_; DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow); |