[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] | dd9cc62 | 2013-04-17 17:49:19 | [diff] [blame] | 8 | #include <map> |
[email protected] | a081ddf | 2013-05-26 02:24:08 | [diff] [blame] | 9 | #include <set> |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 10 | #include <string> |
[email protected] | a081ddf | 2013-05-26 02:24:08 | [diff] [blame] | 11 | #include <utility> |
[email protected] | 9a3d9a0 | 2013-04-12 02:18:48 | [diff] [blame] | 12 | #include <vector> |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 13 | |
Colin Blundell | c841b28 | 2017-09-26 05:26:59 | [diff] [blame] | 14 | #include "base/callback_list.h" |
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 15 | #include "base/feature_list.h" |
thestig | a0e18cd | 2015-09-25 04:58:36 | [diff] [blame] | 16 | #include "base/gtest_prod_util.h" |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 19 | #include "base/observer_list.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 20 | #include "build/build_config.h" |
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 21 | #include "build/buildflag.h" |
[email protected] | 51c5e6c | 2013-12-03 03:28:44 | [diff] [blame] | 22 | #include "chrome/browser/extensions/api/identity/extension_token_key.h" |
[email protected] | 1249a6e | 2013-05-17 01:12:54 | [diff] [blame] | 23 | #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
blundell | e1b2a78 | 2017-02-03 15:00:05 | [diff] [blame] | 24 | #include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h" |
blundell | e1b2ceb | 2017-02-03 13:03:15 | [diff] [blame] | 25 | #include "chrome/browser/extensions/api/identity/identity_get_auth_token_function.h" |
blundell | 06fb548 | 2017-02-01 17:27:27 | [diff] [blame] | 26 | #include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_function.h" |
blundell | 1915164 | 2017-01-27 08:50:56 | [diff] [blame] | 27 | #include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_function.h" |
[email protected] | dd9cc62 | 2013-04-17 17:49:19 | [diff] [blame] | 28 | #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" |
blundell | fb7f1c57 | 2017-01-27 15:45:00 | [diff] [blame] | 29 | #include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_token_function.h" |
[email protected] | 1d9452d | 2012-05-19 16:39:46 | [diff] [blame] | 30 | #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 31 | #include "chrome/browser/extensions/chrome_extension_function.h" |
Colin Blundell | 3517170e | 2019-07-11 08:16:34 | [diff] [blame^] | 32 | #include "components/signin/public/base/signin_buildflags.h" |
Henrique Ferreiro | 94eb46f | 2019-07-03 14:38:56 | [diff] [blame] | 33 | #include "components/signin/public/identity_manager/identity_manager.h" |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 34 | #include "extensions/browser/browser_context_keyed_api_factory.h" |
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 35 | #include "extensions/browser/event_router.h" |
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 36 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 37 | namespace content { |
38 | class BrowserContext; | ||||
39 | } | ||||
40 | |||||
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 41 | class Profile; |
42 | |||||
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 43 | namespace extensions { |
44 | |||||
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 45 | #if BUILDFLAG(ENABLE_DICE_SUPPORT) |
46 | // Enables all accounts in extensions. | ||||
47 | extern const base::Feature kExtensionsAllAccountsFeature; | ||||
48 | #endif | ||||
49 | |||||
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 50 | class IdentityTokenCacheValue { |
51 | public: | ||||
52 | IdentityTokenCacheValue(); | ||||
53 | explicit IdentityTokenCacheValue(const IssueAdviceInfo& issue_advice); | ||||
54 | IdentityTokenCacheValue(const std::string& token, | ||||
55 | base::TimeDelta time_to_live); | ||||
vmpstr | b8aacbe | 2016-02-26 02:00:48 | [diff] [blame] | 56 | IdentityTokenCacheValue(const IdentityTokenCacheValue& other); |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 57 | ~IdentityTokenCacheValue(); |
58 | |||||
59 | // Order of these entries is used to determine whether or not new | ||||
60 | // entries supercede older ones in SetCachedToken. | ||||
61 | enum CacheValueStatus { | ||||
62 | CACHE_STATUS_NOTFOUND, | ||||
63 | CACHE_STATUS_ADVICE, | ||||
64 | CACHE_STATUS_TOKEN | ||||
65 | }; | ||||
66 | |||||
67 | CacheValueStatus status() const; | ||||
68 | const IssueAdviceInfo& issue_advice() const; | ||||
69 | const std::string& token() const; | ||||
70 | const base::Time& expiration_time() const; | ||||
71 | |||||
72 | private: | ||||
73 | bool is_expired() const; | ||||
74 | |||||
75 | CacheValueStatus status_; | ||||
76 | IssueAdviceInfo issue_advice_; | ||||
77 | std::string token_; | ||||
78 | base::Time expiration_time_; | ||||
79 | }; | ||||
80 | |||||
81 | class IdentityAPI : public BrowserContextKeyedAPI, | ||||
Antonio Gomes | d22ab99 | 2018-11-21 13:35:07 | [diff] [blame] | 82 | public identity::IdentityManager::Observer { |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 83 | public: |
84 | typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens; | ||||
85 | |||||
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 86 | explicit IdentityAPI(content::BrowserContext* context); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 87 | ~IdentityAPI() override; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 88 | |
89 | // Request serialization queue for getAuthToken. | ||||
90 | IdentityMintRequestQueue* mint_queue(); | ||||
91 | |||||
92 | // Token cache | ||||
93 | void SetCachedToken(const ExtensionTokenKey& key, | ||||
94 | const IdentityTokenCacheValue& token_data); | ||||
95 | void EraseCachedToken(const std::string& extension_id, | ||||
96 | const std::string& token); | ||||
97 | void EraseAllCachedTokens(); | ||||
98 | const IdentityTokenCacheValue& GetCachedToken(const ExtensionTokenKey& key); | ||||
99 | |||||
100 | const CachedTokens& GetAllCachedTokens(); | ||||
101 | |||||
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 102 | // BrowserContextKeyedAPI: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 103 | void Shutdown() override; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 104 | static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
105 | |||||
Colin Blundell | c841b28 | 2017-09-26 05:26:59 | [diff] [blame] | 106 | std::unique_ptr<base::CallbackList<void()>::Subscription> |
107 | RegisterOnShutdownCallback(const base::Closure& cb) { | ||||
108 | return on_shutdown_callback_list_.Add(cb); | ||||
blundell | 202f1212 | 2017-02-03 09:30:23 | [diff] [blame] | 109 | } |
110 | |||||
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 111 | // Callback that is used in testing contexts to test the implementation of |
112 | // the chrome.identity.onSignInChanged event. Note that the passed-in Event is | ||||
113 | // valid only for the duration of the callback. | ||||
114 | using OnSignInChangedCallback = base::RepeatingCallback<void(Event*)>; | ||||
115 | void set_on_signin_changed_callback_for_testing( | ||||
116 | const OnSignInChangedCallback& callback) { | ||||
117 | on_signin_changed_callback_for_testing_ = callback; | ||||
118 | } | ||||
119 | |||||
David Roger | 739d153 | 2018-10-11 17:21:25 | [diff] [blame] | 120 | // Whether the chrome.identity API should use all accounts or the primary |
121 | // account only. | ||||
122 | bool AreExtensionsRestrictedToPrimaryAccount(); | ||||
123 | |||||
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 124 | private: |
125 | friend class BrowserContextKeyedAPIFactory<IdentityAPI>; | ||||
126 | |||||
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 127 | // BrowserContextKeyedAPI: |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 128 | static const char* service_name() { return "IdentityAPI"; } |
129 | static const bool kServiceIsNULLWhileTesting = true; | ||||
130 | |||||
Antonio Gomes | d22ab99 | 2018-11-21 13:35:07 | [diff] [blame] | 131 | // identity::IdentityManager::Observer: |
Colin Blundell | bc1d0fc | 2018-11-30 14:56:20 | [diff] [blame] | 132 | void OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame] | 133 | const CoreAccountInfo& account_info) override; |
Julie Jeongeun Kim | 9490434 | 2019-01-25 00:47:22 | [diff] [blame] | 134 | // NOTE: This class must listen for this callback rather than |
135 | // OnRefreshTokenRemovedForAccount() to obtain the Gaia ID of the removed | ||||
136 | // account. | ||||
Gyuyoung Kim | 54ad0480 | 2019-02-12 02:06:50 | [diff] [blame] | 137 | void OnExtendedAccountInfoRemoved(const AccountInfo& info) override; |
Colin Blundell | 798a107 | 2018-03-22 15:43:47 | [diff] [blame] | 138 | |
139 | // Fires the chrome.identity.onSignInChanged event. | ||||
140 | void FireOnAccountSignInChanged(const std::string& gaia_id, | ||||
141 | bool is_signed_in); | ||||
142 | |||||
143 | Profile* profile_; | ||||
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 144 | IdentityMintRequestQueue mint_queue_; |
145 | CachedTokens token_cache_; | ||||
blundell | 202f1212 | 2017-02-03 09:30:23 | [diff] [blame] | 146 | |
Colin Blundell | a423b41 | 2017-08-01 17:15:23 | [diff] [blame] | 147 | OnSignInChangedCallback on_signin_changed_callback_for_testing_; |
148 | |||||
Colin Blundell | c841b28 | 2017-09-26 05:26:59 | [diff] [blame] | 149 | base::CallbackList<void()> on_shutdown_callback_list_; |
[email protected] | a31ea18 | 2014-04-02 01:37:26 | [diff] [blame] | 150 | }; |
151 | |||||
152 | template <> | ||||
153 | void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | ||||
154 | |||||
[email protected] | a79be1f | 2012-03-23 22:14:00 | [diff] [blame] | 155 | } // namespace extensions |
156 | |||||
157 | #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |