[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 7 | |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 8 | #include <string> |
[email protected] | a081ddf | 2013-05-26 02:24:08 | [diff] [blame] | 9 | #include <utility> |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 10 | |
Colin Blundell | c841b28 | 2017-09-26 05:26:59 | [diff] [blame] | 11 | #include "base/callback_list.h" |
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 12 | #include "base/feature_list.h" |
thestig | a0e18cd | 2015-09-25 04:58:36 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 16 | #include "base/observer_list.h" |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 17 | #include "base/optional.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 18 | #include "build/build_config.h" |
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 19 | #include "build/buildflag.h" |
[email protected] | 1249a6e | 2013-05-17 01:12:54 | [diff] [blame] | 20 | #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
David Roger | 4f5cf7a | 2020-10-02 08:41:36 | [diff] [blame] | 21 | #include "chrome/browser/extensions/api/identity/identity_clear_all_cached_auth_tokens_function.h" |
blundell | e1b2a78 | 2017-02-03 15:00:05 | [diff] [blame] | 22 | #include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h" |
blundell | e1b2ceb | 2017-02-03 13:03:15 | [diff] [blame] | 23 | #include "chrome/browser/extensions/api/identity/identity_get_auth_token_function.h" |
blundell | 06fb548 | 2017-02-01 17:27:27 | [diff] [blame] | 24 | #include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_function.h" |
blundell | 1915164 | 2017-01-27 08:50:56 | [diff] [blame] | 25 | #include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_function.h" |
[email protected] | dd9cc62 | 2013-04-17 17:49:19 | [diff] [blame] | 26 | #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" |
blundell | fb7f1c57 | 2017-01-27 15:45:00 | [diff] [blame] | 27 | #include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_token_function.h" |
William Lin | cb0e4af | 2020-08-13 01:42:46 | [diff] [blame] | 28 | #include "chrome/browser/extensions/api/identity/identity_token_cache.h" |
[email protected] | 1d9452d | 2012-05-19 16:39:46 | [diff] [blame] | 29 | #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
Colin Blundell | 3517170e | 2019-07-11 08:16:34 | [diff] [blame] | 30 | #include "components/signin/public/base/signin_buildflags.h" |
Henrique Ferreiro | 94eb46f | 2019-07-03 14:38:56 | [diff] [blame] | 31 | #include "components/signin/public/identity_manager/identity_manager.h" |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 32 | #include "extensions/browser/browser_context_keyed_api_factory.h" |
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 33 | #include "extensions/browser/event_router.h" |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 34 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 35 | namespace content { |
| 36 | class BrowserContext; |
| 37 | } |
| 38 | |
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 39 | class Profile; |
| 40 | |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 41 | namespace extensions { |
| 42 | |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 43 | class IdentityAPI : public BrowserContextKeyedAPI, |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 44 | public signin::IdentityManager::Observer { |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 45 | public: |
Alex Ilin | 3779f5c7 | 2020-01-21 20:58:48 | [diff] [blame] | 46 | using OnSetConsentResultSignature = void(const std::string&, |
| 47 | const std::string&); |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 48 | |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 49 | explicit IdentityAPI(content::BrowserContext* context); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 50 | ~IdentityAPI() override; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 51 | |
| 52 | // Request serialization queue for getAuthToken. |
| 53 | IdentityMintRequestQueue* mint_queue(); |
| 54 | |
William Lin | cb0e4af | 2020-08-13 01:42:46 | [diff] [blame] | 55 | IdentityTokenCache* token_cache(); |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 56 | |
Alex Ilin | b1a0b0c7 | 2020-03-31 15:36:30 | [diff] [blame] | 57 | // GAIA id cache. |
Alex Ilin | b1a0b0c7 | 2020-03-31 15:36:30 | [diff] [blame] | 58 | void SetGaiaIdForExtension(const std::string& extension_id, |
| 59 | const std::string& gaia_id); |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 60 | // Returns |base::nullopt| if no GAIA id is saved for |extension_id|. |
| 61 | // Otherwise, returns GAIA id previously saved via SetGaiaIdForExtension(). |
| 62 | base::Optional<std::string> GetGaiaIdForExtension( |
| 63 | const std::string& extension_id); |
| 64 | void EraseGaiaIdForExtension(const std::string& extension_id); |
| 65 | // If refresh tokens have been loaded, erases GAIA ids of accounts that are no |
| 66 | // longer signed in to Chrome for all extensions. |
| 67 | void EraseStaleGaiaIdsForAllExtensions(); |
Alex Ilin | b1a0b0c7 | 2020-03-31 15:36:30 | [diff] [blame] | 68 | |
Alex Ilin | 3779f5c7 | 2020-01-21 20:58:48 | [diff] [blame] | 69 | // Consent result. |
| 70 | void SetConsentResult(const std::string& result, |
| 71 | const std::string& window_id); |
Clark DuVall | 1476549 | 2020-07-09 09:49:58 | [diff] [blame] | 72 | std::unique_ptr< |
| 73 | base::RepeatingCallbackList<OnSetConsentResultSignature>::Subscription> |
Alex Ilin | 3779f5c7 | 2020-01-21 20:58:48 | [diff] [blame] | 74 | RegisterOnSetConsentResultCallback( |
| 75 | const base::RepeatingCallback<OnSetConsentResultSignature>& callback); |
| 76 | |
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 77 | // BrowserContextKeyedAPI: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 78 | void Shutdown() override; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 79 | static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
| 80 | |
Clark DuVall | 1476549 | 2020-07-09 09:49:58 | [diff] [blame] | 81 | std::unique_ptr<base::OnceCallbackList<void()>::Subscription> |
| 82 | RegisterOnShutdownCallback(base::OnceClosure cb); |
blundell | 202f1212 | 2017-02-03 09:30:23 | [diff] [blame] | 83 | |
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 84 | // Callback that is used in testing contexts to test the implementation of |
| 85 | // the chrome.identity.onSignInChanged event. Note that the passed-in Event is |
| 86 | // valid only for the duration of the callback. |
| 87 | using OnSignInChangedCallback = base::RepeatingCallback<void(Event*)>; |
| 88 | void set_on_signin_changed_callback_for_testing( |
| 89 | const OnSignInChangedCallback& callback) { |
| 90 | on_signin_changed_callback_for_testing_ = callback; |
| 91 | } |
| 92 | |
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 93 | // Whether the chrome.identity API should use all accounts or the primary |
| 94 | // account only. |
| 95 | bool AreExtensionsRestrictedToPrimaryAccount(); |
| 96 | |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 97 | private: |
| 98 | friend class BrowserContextKeyedAPIFactory<IdentityAPI>; |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 99 | friend class IdentityAPITest; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 100 | |
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 101 | // BrowserContextKeyedAPI: |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 102 | static const char* service_name() { return "IdentityAPI"; } |
| 103 | static const bool kServiceIsNULLWhileTesting = true; |
| 104 | |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 105 | // This constructor allows to mock keyed services in tests. |
| 106 | IdentityAPI(Profile* profile, |
| 107 | signin::IdentityManager* identity_manager, |
| 108 | ExtensionPrefs* extension_prefs, |
| 109 | EventRouter* event_router); |
| 110 | |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 111 | // signin::IdentityManager::Observer: |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 112 | void OnRefreshTokensLoaded() override; |
Colin Blundell | bc1d0fc | 2018-11-30 14:56:20 | [diff] [blame] | 113 | void OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame] | 114 | const CoreAccountInfo& account_info) override; |
Julie Jeongeun Kim | 9490434 | 2019-01-25 00:47:22 | [diff] [blame] | 115 | // NOTE: This class must listen for this callback rather than |
| 116 | // OnRefreshTokenRemovedForAccount() to obtain the Gaia ID of the removed |
| 117 | // account. |
Gyuyoung Kim | 54ad0480 | 2019-02-12 02:06:50 | [diff] [blame] | 118 | void OnExtendedAccountInfoRemoved(const AccountInfo& info) override; |
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 119 | |
| 120 | // Fires the chrome.identity.onSignInChanged event. |
| 121 | void FireOnAccountSignInChanged(const std::string& gaia_id, |
| 122 | bool is_signed_in); |
| 123 | |
Alex Ilin | d8d084f | 2020-04-02 12:14:52 | [diff] [blame] | 124 | Profile* const profile_; |
| 125 | signin::IdentityManager* const identity_manager_; |
| 126 | ExtensionPrefs* const extension_prefs_; |
| 127 | EventRouter* const event_router_; |
| 128 | |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 129 | IdentityMintRequestQueue mint_queue_; |
William Lin | cb0e4af | 2020-08-13 01:42:46 | [diff] [blame] | 130 | IdentityTokenCache token_cache_; |
blundell | 202f1212 | 2017-02-03 09:30:23 | [diff] [blame] | 131 | |
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 132 | OnSignInChangedCallback on_signin_changed_callback_for_testing_; |
| 133 | |
Clark DuVall | 1476549 | 2020-07-09 09:49:58 | [diff] [blame] | 134 | base::RepeatingCallbackList<OnSetConsentResultSignature> |
Alex Ilin | 3779f5c7 | 2020-01-21 20:58:48 | [diff] [blame] | 135 | on_set_consent_result_callback_list_; |
Clark DuVall | 1476549 | 2020-07-09 09:49:58 | [diff] [blame] | 136 | base::OnceCallbackList<void()> on_shutdown_callback_list_; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | template <> |
| 140 | void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
| 141 | |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 142 | } // namespace extensions |
| 143 | |
| 144 | #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |