123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file. 4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_vector.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/thread_checker.h" 10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h" 11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/webdata/common/web_data_service_base.h" 12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/webdata/common/web_data_service_consumer.h" 13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// A specialization of ProfileOAuth2TokenService that can can mutate its OAuth2 15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// tokens. 16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Note: This class is just a placeholder for now. Methods used to mutate 18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// the tokens are currently being migrated from ProfileOAuth2TokenService. 19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class MutableProfileOAuth2TokenService : public ProfileOAuth2TokenService, 20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public WebDataServiceConsumer { 21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public: 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // ProfileOAuth2TokenService overrides. 23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual void Shutdown() OVERRIDE; 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual std::vector<std::string> GetAccounts() OVERRIDE; 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // The below three methods should be called only on the thread on which this 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // object was created. 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void LoadCredentials(const std::string& primary_account_id) OVERRIDE; 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void UpdateCredentials(const std::string& account_id, 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& refresh_token) OVERRIDE; 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void RevokeAllCredentials() OVERRIDE; 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual bool RefreshTokenIsAvailable(const std::string& account_id) const 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) OVERRIDE; 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Revokes credentials related to |account_id|. 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void RevokeCredentials(const std::string& account_id); 37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) protected: 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) class AccountInfo : public SigninErrorController::AuthStatusProvider { 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AccountInfo(ProfileOAuth2TokenService* token_service, 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& account_id, 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& refresh_token); 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual ~AccountInfo(); 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& refresh_token() const { return refresh_token_; } 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_refresh_token(const std::string& token) { 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) refresh_token_ = token; 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetLastAuthError(const GoogleServiceAuthError& error); 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // SigninErrorController::AuthStatusProvider implementation. 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual std::string GetAccountId() const OVERRIDE; 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual std::string GetUsername() const OVERRIDE; 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileOAuth2TokenService* token_service_; 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string account_id_; 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string refresh_token_; 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GoogleServiceAuthError last_auth_error_; 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(AccountInfo); 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) }; 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Maps the |account_id| of accounts known to ProfileOAuth2TokenService 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to information about the account. 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typedef std::map<std::string, linked_ptr<AccountInfo> > AccountInfoMap; 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch friend class ProfileOAuth2TokenServiceFactory; 724ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch friend class MutableProfileOAuth2TokenServiceTest; 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) MutableProfileOAuth2TokenService(); 75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual ~MutableProfileOAuth2TokenService(); 76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // OAuth2TokenService implementation. 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const std::string& account_id, 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) net::URLRequestContextGetter* getter, 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) OAuth2AccessTokenConsumer* consumer) OVERRIDE; 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Updates the internal cache of the result from the most-recently-completed 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // auth request (used for reporting errors to the user). 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void UpdateAuthError(const std::string& account_id, 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const GoogleServiceAuthError& error) OVERRIDE; 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual std::string GetRefreshToken(const std::string& account_id) const; 90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AccountInfoMap& refresh_tokens() { return refresh_tokens_; } 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private: 94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) class RevokeServerRefreshToken; 95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) TokenServiceUpdateClearsCache); 98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) PersistenceDBUpgrade); 100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 101a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) PersistenceLoadCredentials); 102a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // WebDataServiceConsumer implementation: 104a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual void OnWebDataServiceRequestDone( 105a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) WebDataServiceBase::Handle handle, 106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const WDTypedResult* result) OVERRIDE; 107a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 108a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Loads credentials into in memory stucture. 109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void LoadAllCredentialsIntoMemory( 110a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const std::map<std::string, std::string>& db_tokens); 111a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Persists credentials for |account_id|. Enables overriding for 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // testing purposes, or other cases, when accessing the DB is not desired. 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void PersistCredentials(const std::string& account_id, 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& refresh_token); 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Clears credentials persisted for |account_id|. Enables overriding for 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // testing purposes, or other cases, when accessing the DB is not desired. 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void ClearPersistedCredentials(const std::string& account_id); 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Revokes the refresh token on the server. 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void RevokeCredentialsOnServer(const std::string& refresh_token); 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Cancels any outstanding fetch for tokens from the web database. 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void CancelWebTokenFetch(); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // In memory refresh token store mapping account_id to refresh_token. 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AccountInfoMap refresh_tokens_; 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Handle to the request reading tokens from database. 131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) WebDataServiceBase::Handle web_data_service_request_; 132a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The primary account id of this service's profile during the loading of 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // credentials. This member is empty otherwise. 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string loading_primary_account_id_; 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ScopedVector<RevokeServerRefreshToken> server_revokes_; 138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Used to verify that certain methods are called only on the thread on which 140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // this instance was created. 141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::ThreadChecker thread_checker_; 142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 143a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService); 144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}; 145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 146effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ 147