diff options
author | gogerald <gogerald@chromium.org> | 2015-07-02 13:03:47 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-02 20:04:23 +0000 |
commit | 63376b55f05201ea493fe6c26d3b9a8f26bc2f16 (patch) | |
tree | 97bbc087ac0b9cb8c7ba960ef749124180d9a67b /extensions/shell | |
parent | 9997795a0ef93d1a1bab867ae2f1a21d3d55361c (diff) | |
download | chromium_src-63376b55f05201ea493fe6c26d3b9a8f26bc2f16.zip chromium_src-63376b55f05201ea493fe6c26d3b9a8f26bc2f16.tar.gz chromium_src-63376b55f05201ea493fe6c26d3b9a8f26bc2f16.tar.bz2 |
Refactor AO2TS to make it easier to componentize
This cl is dedicated to refactor token service to make it easier to componentize. It introduces a new class OAuth2TokenServiceDelegate for each platform to implement it. OAuth2TokenService and its subclasses will call into platform specific delegate and the user can get that delegate through public interface GetDelegate() of OAuth2TokenService.
BUG=490882
Review URL: https://codereview.chromium.org/1143323005
Cr-Commit-Position: refs/heads/master@{#337248}
Diffstat (limited to 'extensions/shell')
7 files changed, 127 insertions, 48 deletions
diff --git a/extensions/shell/app_shell.gypi b/extensions/shell/app_shell.gypi index 4c6bc8d..caab3bb 100644 --- a/extensions/shell/app_shell.gypi +++ b/extensions/shell/app_shell.gypi @@ -63,6 +63,8 @@ 'browser/shell_network_controller_chromeos.h', 'browser/shell_network_delegate.cc', 'browser/shell_network_delegate.h', + 'browser/shell_oauth2_token_service_delegate.cc', + 'browser/shell_oauth2_token_service_delegate.h', 'browser/shell_oauth2_token_service.cc', 'browser/shell_oauth2_token_service.h', 'browser/shell_prefs.cc', diff --git a/extensions/shell/browser/api/identity/identity_api.cc b/extensions/shell/browser/api/identity/identity_api.cc index 8fb4fbb..bf8ad1f 100644 --- a/extensions/shell/browser/api/identity/identity_api.cc +++ b/extensions/shell/browser/api/identity/identity_api.cc @@ -65,7 +65,7 @@ ExtensionFunction::ResponseAction IdentityGetAuthTokenFunction::Run() { EXTENSION_FUNCTION_VALIDATE(params.get()); ShellOAuth2TokenService* service = ShellOAuth2TokenService::GetInstance(); - std::string account_id = service->account_id(); + std::string account_id = service->AccountId(); if (account_id.empty()) return RespondNow(Error(kErrorNoUserAccount)); @@ -85,7 +85,7 @@ ExtensionFunction::ResponseAction IdentityGetAuthTokenFunction::Run() { // that will be returned to the app. std::set<std::string> no_scopes; access_token_request_ = - service->StartRequest(service->account_id(), no_scopes, this); + service->StartRequest(service->AccountId(), no_scopes, this); return RespondLater(); } diff --git a/extensions/shell/browser/shell_oauth2_token_service.cc b/extensions/shell/browser/shell_oauth2_token_service.cc index a742255..b2a7c4c 100644 --- a/extensions/shell/browser/shell_oauth2_token_service.cc +++ b/extensions/shell/browser/shell_oauth2_token_service.cc @@ -7,7 +7,7 @@ #include "base/logging.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" -#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" +#include "extensions/shell/browser/shell_oauth2_token_service_delegate.h" namespace extensions { namespace { @@ -20,9 +20,9 @@ ShellOAuth2TokenService::ShellOAuth2TokenService( content::BrowserContext* browser_context, std::string account_id, std::string refresh_token) - : browser_context_(browser_context), - account_id_(account_id), - refresh_token_(refresh_token) { + : OAuth2TokenService(new ShellOAuth2TokenServiceDelegate(browser_context, + account_id, + refresh_token)) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK(!g_instance); g_instance = this; @@ -43,28 +43,11 @@ ShellOAuth2TokenService* ShellOAuth2TokenService::GetInstance() { void ShellOAuth2TokenService::SetRefreshToken( const std::string& account_id, const std::string& refresh_token) { - account_id_ = account_id; - refresh_token_ = refresh_token; + GetDelegate()->UpdateCredentials(account_id, refresh_token); } -bool ShellOAuth2TokenService::RefreshTokenIsAvailable( - const std::string& account_id) const { - if (account_id != account_id_) - return false; - - return !refresh_token_.empty(); -} - -OAuth2AccessTokenFetcher* ShellOAuth2TokenService::CreateAccessTokenFetcher( - const std::string& account_id, - net::URLRequestContextGetter* getter, - OAuth2AccessTokenConsumer* consumer) { - DCHECK(!refresh_token_.empty()); - return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token_); -} - -net::URLRequestContextGetter* ShellOAuth2TokenService::GetRequestContext() { - return browser_context_->GetRequestContext(); +std::string ShellOAuth2TokenService::AccountId() const { + return GetAccounts()[0]; } } // namespace extensions diff --git a/extensions/shell/browser/shell_oauth2_token_service.h b/extensions/shell/browser/shell_oauth2_token_service.h index 208c349..c93c9f20 100644 --- a/extensions/shell/browser/shell_oauth2_token_service.h +++ b/extensions/shell/browser/shell_oauth2_token_service.h @@ -30,29 +30,10 @@ class ShellOAuth2TokenService : public OAuth2TokenService { // Returns the single instance for app_shell. static ShellOAuth2TokenService* GetInstance(); - std::string account_id() const { return account_id_; } - - // Sets the current user account and refresh token. void SetRefreshToken(const std::string& account_id, const std::string& refresh_token); - // OAuth2TokenService: - bool RefreshTokenIsAvailable(const std::string& account_id) const override; - OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( - const std::string& account_id, - net::URLRequestContextGetter* getter, - OAuth2AccessTokenConsumer* consumer) override; - net::URLRequestContextGetter* GetRequestContext() override; - - private: - // Not owned. - content::BrowserContext* browser_context_; - - // User account id, such as "foo@gmail.com". - std::string account_id_; - - // Cached copy of an OAuth2 refresh token. Not stored on disk. - std::string refresh_token_; + std::string AccountId() const; DISALLOW_COPY_AND_ASSIGN(ShellOAuth2TokenService); }; diff --git a/extensions/shell/browser/shell_oauth2_token_service_delegate.cc b/extensions/shell/browser/shell_oauth2_token_service_delegate.cc new file mode 100644 index 0000000..89eb212 --- /dev/null +++ b/extensions/shell/browser/shell_oauth2_token_service_delegate.cc @@ -0,0 +1,59 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "extensions/shell/browser/shell_oauth2_token_service_delegate.h" + +#include <vector> + +namespace extensions { + +ShellOAuth2TokenServiceDelegate::ShellOAuth2TokenServiceDelegate( + content::BrowserContext* browser_context, + std::string account_id, + std::string refresh_token) + : browser_context_(browser_context), + account_id_(account_id), + refresh_token_(refresh_token) { +} + +ShellOAuth2TokenServiceDelegate::~ShellOAuth2TokenServiceDelegate() { +} + +bool ShellOAuth2TokenServiceDelegate::RefreshTokenIsAvailable( + const std::string& account_id) const { + if (account_id != account_id_) + return false; + + return !refresh_token_.empty(); +} + +OAuth2AccessTokenFetcher* +ShellOAuth2TokenServiceDelegate::CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) { + DCHECK_EQ(account_id, account_id_); + DCHECK(!refresh_token_.empty()); + return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token_); +} + +net::URLRequestContextGetter* +ShellOAuth2TokenServiceDelegate::GetRequestContext() const { + return browser_context_->GetRequestContext(); +} + +std::vector<std::string> ShellOAuth2TokenServiceDelegate::GetAccounts() { + std::vector<std::string> accounts; + accounts.push_back(account_id_); + return accounts; +} + +void ShellOAuth2TokenServiceDelegate::UpdateCredentials( + const std::string& account_id, + const std::string& refresh_token) { + account_id_ = account_id; + refresh_token_ = refresh_token; +} + +} // namespace extensions diff --git a/extensions/shell/browser/shell_oauth2_token_service_delegate.h b/extensions/shell/browser/shell_oauth2_token_service_delegate.h new file mode 100644 index 0000000..d9add4a --- /dev/null +++ b/extensions/shell/browser/shell_oauth2_token_service_delegate.h @@ -0,0 +1,54 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXTENSIONS_SHELL_BROWSER_SHELL_OAUTH2_TOKEN_SERVICE_DELEGATE_H_ +#define EXTENSIONS_SHELL_BROWSER_SHELL_OAUTH2_TOKEN_SERVICE_DELEGATE_H_ + +#include <string> + +#include "content/public/browser/browser_context.h" +#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" +#include "google_apis/gaia/oauth2_token_service_delegate.h" + +namespace content { +class BrowserContext; +} + +namespace extensions { + +class ShellOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate { + public: + ShellOAuth2TokenServiceDelegate(content::BrowserContext* browser_context, + std::string account_id, + std::string refresh_token); + ~ShellOAuth2TokenServiceDelegate() override; + + bool RefreshTokenIsAvailable(const std::string& account_id) const override; + + OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) override; + net::URLRequestContextGetter* GetRequestContext() const override; + + std::vector<std::string> GetAccounts() override; + + void UpdateCredentials(const std::string& account_id, + const std::string& refresh_token) override; + + private: + // Not owned. + content::BrowserContext* browser_context_; + + // User account id, such as "foo@gmail.com". + std::string account_id_; + + // Cached copy of an OAuth2 refresh token. Not stored on disk. + std::string refresh_token_; + + DISALLOW_COPY_AND_ASSIGN(ShellOAuth2TokenServiceDelegate); +}; + +} // namespace extensions +#endif diff --git a/extensions/shell/browser/shell_oauth2_token_service_unittest.cc b/extensions/shell/browser/shell_oauth2_token_service_unittest.cc index 8b9dc72..649fd3a 100644 --- a/extensions/shell/browser/shell_oauth2_token_service_unittest.cc +++ b/extensions/shell/browser/shell_oauth2_token_service_unittest.cc @@ -23,14 +23,14 @@ TEST_F(ShellOAuth2TokenServiceTest, SetRefreshToken) { ShellOAuth2TokenService service(nullptr, "larry@google.com", "token123"); // Only has a token for the account in the constructor. - EXPECT_EQ("larry@google.com", service.account_id()); + EXPECT_EQ("larry@google.com", service.AccountId()); EXPECT_TRUE(service.RefreshTokenIsAvailable("larry@google.com")); EXPECT_FALSE(service.RefreshTokenIsAvailable("sergey@google.com")); service.SetRefreshToken("sergey@google.com", "token456"); // The account and refresh token have updated. - EXPECT_EQ("sergey@google.com", service.account_id()); + EXPECT_EQ("sergey@google.com", service.AccountId()); EXPECT_FALSE(service.RefreshTokenIsAvailable("larry@google.com")); EXPECT_TRUE(service.RefreshTokenIsAvailable("sergey@google.com")); } |