blob: d76631949709f143321df92ac0866f2e4f9969a5 [file] [log] [blame]
[email protected]a79be1f2012-03-23 22:14:001// 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]a79be1f2012-03-23 22:14:007
[email protected]dd9cc622013-04-17 17:49:198#include <map>
[email protected]a081ddf2013-05-26 02:24:089#include <set>
[email protected]a79be1f2012-03-23 22:14:0010#include <string>
[email protected]a081ddf2013-05-26 02:24:0811#include <utility>
[email protected]9a3d9a02013-04-12 02:18:4812#include <vector>
[email protected]a79be1f2012-03-23 22:14:0013
Colin Blundellc841b282017-09-26 05:26:5914#include "base/callback_list.h"
David Roger739d1532018-10-11 17:21:2515#include "base/feature_list.h"
thestiga0e18cd2015-09-25 04:58:3616#include "base/gtest_prod_util.h"
[email protected]b70a2d92012-06-28 19:51:2117#include "base/memory/ref_counted.h"
[email protected]a79be1f2012-03-23 22:14:0018#include "base/memory/weak_ptr.h"
[email protected]a31ea182014-04-02 01:37:2619#include "base/observer_list.h"
avia2f4804a2015-12-24 23:11:1320#include "build/build_config.h"
David Roger739d1532018-10-11 17:21:2521#include "build/buildflag.h"
[email protected]51c5e6c2013-12-03 03:28:4422#include "chrome/browser/extensions/api/identity/extension_token_key.h"
[email protected]1249a6e2013-05-17 01:12:5423#include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h"
blundelle1b2a782017-02-03 15:00:0524#include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h"
blundelle1b2ceb2017-02-03 13:03:1525#include "chrome/browser/extensions/api/identity/identity_get_auth_token_function.h"
blundell06fb5482017-02-01 17:27:2726#include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_function.h"
blundell19151642017-01-27 08:50:5627#include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_function.h"
[email protected]dd9cc622013-04-17 17:49:1928#include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
blundellfb7f1c572017-01-27 15:45:0029#include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_token_function.h"
[email protected]1d9452d2012-05-19 16:39:4630#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
[email protected]21a40082013-10-28 21:19:2331#include "chrome/browser/extensions/chrome_extension_function.h"
Colin Blundell3517170e2019-07-11 08:16:3432#include "components/signin/public/base/signin_buildflags.h"
Henrique Ferreiro94eb46f2019-07-03 14:38:5633#include "components/signin/public/identity_manager/identity_manager.h"
[email protected]4bf3bed2014-03-05 10:21:0234#include "extensions/browser/browser_context_keyed_api_factory.h"
Colin Blundella423b412017-08-01 17:15:2335#include "extensions/browser/event_router.h"
[email protected]a79be1f2012-03-23 22:14:0036
[email protected]21eaf3542014-02-25 17:47:5237namespace content {
38class BrowserContext;
39}
40
Colin Blundell798a1072018-03-22 15:43:4741class Profile;
42
[email protected]a79be1f2012-03-23 22:14:0043namespace extensions {
44
David Roger739d1532018-10-11 17:21:2545#if BUILDFLAG(ENABLE_DICE_SUPPORT)
46// Enables all accounts in extensions.
47extern const base::Feature kExtensionsAllAccountsFeature;
48#endif
49
[email protected]a31ea182014-04-02 01:37:2650class IdentityTokenCacheValue {
51 public:
52 IdentityTokenCacheValue();
53 explicit IdentityTokenCacheValue(const IssueAdviceInfo& issue_advice);
54 IdentityTokenCacheValue(const std::string& token,
55 base::TimeDelta time_to_live);
vmpstrb8aacbe2016-02-26 02:00:4856 IdentityTokenCacheValue(const IdentityTokenCacheValue& other);
[email protected]a31ea182014-04-02 01:37:2657 ~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
81class IdentityAPI : public BrowserContextKeyedAPI,
Antonio Gomesd22ab992018-11-21 13:35:0782 public identity::IdentityManager::Observer {
[email protected]a31ea182014-04-02 01:37:2683 public:
84 typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens;
85
[email protected]a31ea182014-04-02 01:37:2686 explicit IdentityAPI(content::BrowserContext* context);
dchengae36a4a2014-10-21 12:36:3687 ~IdentityAPI() override;
[email protected]a31ea182014-04-02 01:37:2688
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 Blundell798a1072018-03-22 15:43:47102 // BrowserContextKeyedAPI:
dchengae36a4a2014-10-21 12:36:36103 void Shutdown() override;
[email protected]a31ea182014-04-02 01:37:26104 static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance();
105
Colin Blundellc841b282017-09-26 05:26:59106 std::unique_ptr<base::CallbackList<void()>::Subscription>
107 RegisterOnShutdownCallback(const base::Closure& cb) {
108 return on_shutdown_callback_list_.Add(cb);
blundell202f12122017-02-03 09:30:23109 }
110
Colin Blundella423b412017-08-01 17:15:23111 // 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 Roger739d1532018-10-11 17:21:25120 // Whether the chrome.identity API should use all accounts or the primary
121 // account only.
122 bool AreExtensionsRestrictedToPrimaryAccount();
123
[email protected]a31ea182014-04-02 01:37:26124 private:
125 friend class BrowserContextKeyedAPIFactory<IdentityAPI>;
126
Colin Blundell798a1072018-03-22 15:43:47127 // BrowserContextKeyedAPI:
[email protected]a31ea182014-04-02 01:37:26128 static const char* service_name() { return "IdentityAPI"; }
129 static const bool kServiceIsNULLWhileTesting = true;
130
Antonio Gomesd22ab992018-11-21 13:35:07131 // identity::IdentityManager::Observer:
Colin Blundellbc1d0fc2018-11-30 14:56:20132 void OnRefreshTokenUpdatedForAccount(
Gyuyoung Kimb70d3c72019-02-12 01:45:43133 const CoreAccountInfo& account_info) override;
Julie Jeongeun Kim94904342019-01-25 00:47:22134 // NOTE: This class must listen for this callback rather than
135 // OnRefreshTokenRemovedForAccount() to obtain the Gaia ID of the removed
136 // account.
Gyuyoung Kim54ad04802019-02-12 02:06:50137 void OnExtendedAccountInfoRemoved(const AccountInfo& info) override;
Colin Blundell798a1072018-03-22 15:43:47138
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]a31ea182014-04-02 01:37:26144 IdentityMintRequestQueue mint_queue_;
145 CachedTokens token_cache_;
blundell202f12122017-02-03 09:30:23146
Colin Blundella423b412017-08-01 17:15:23147 OnSignInChangedCallback on_signin_changed_callback_for_testing_;
148
Colin Blundellc841b282017-09-26 05:26:59149 base::CallbackList<void()> on_shutdown_callback_list_;
[email protected]a31ea182014-04-02 01:37:26150};
151
152template <>
153void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies();
154
[email protected]a79be1f2012-03-23 22:14:00155} // namespace extensions
156
157#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_