blob: 02b1162a6a855d6ec05d6bbc0c712d7499abcf60 [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]a79be1f2012-03-23 22:14:008#include <string>
[email protected]a081ddf2013-05-26 02:24:089#include <utility>
[email protected]a79be1f2012-03-23 22:14:0010
Colin Blundellc841b282017-09-26 05:26:5911#include "base/callback_list.h"
David Roger739d1532018-10-11 17:21:2512#include "base/feature_list.h"
thestiga0e18cd2015-09-25 04:58:3613#include "base/gtest_prod_util.h"
[email protected]b70a2d92012-06-28 19:51:2114#include "base/memory/ref_counted.h"
[email protected]a79be1f2012-03-23 22:14:0015#include "base/memory/weak_ptr.h"
[email protected]a31ea182014-04-02 01:37:2616#include "base/observer_list.h"
Alex Ilind8d084f2020-04-02 12:14:5217#include "base/optional.h"
avia2f4804a2015-12-24 23:11:1318#include "build/build_config.h"
David Roger739d1532018-10-11 17:21:2519#include "build/buildflag.h"
[email protected]1249a6e2013-05-17 01:12:5420#include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h"
David Roger4f5cf7a2020-10-02 08:41:3621#include "chrome/browser/extensions/api/identity/identity_clear_all_cached_auth_tokens_function.h"
blundelle1b2a782017-02-03 15:00:0522#include "chrome/browser/extensions/api/identity/identity_get_accounts_function.h"
blundelle1b2ceb2017-02-03 13:03:1523#include "chrome/browser/extensions/api/identity/identity_get_auth_token_function.h"
blundell06fb5482017-02-01 17:27:2724#include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_function.h"
blundell19151642017-01-27 08:50:5625#include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_function.h"
[email protected]dd9cc622013-04-17 17:49:1926#include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
blundellfb7f1c572017-01-27 15:45:0027#include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_token_function.h"
William Lincb0e4af2020-08-13 01:42:4628#include "chrome/browser/extensions/api/identity/identity_token_cache.h"
[email protected]1d9452d2012-05-19 16:39:4629#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
Colin Blundell3517170e2019-07-11 08:16:3430#include "components/signin/public/base/signin_buildflags.h"
Henrique Ferreiro94eb46f2019-07-03 14:38:5631#include "components/signin/public/identity_manager/identity_manager.h"
[email protected]4bf3bed2014-03-05 10:21:0232#include "extensions/browser/browser_context_keyed_api_factory.h"
Colin Blundella423b412017-08-01 17:15:2333#include "extensions/browser/event_router.h"
[email protected]a79be1f2012-03-23 22:14:0034
[email protected]21eaf3542014-02-25 17:47:5235namespace content {
36class BrowserContext;
37}
38
Colin Blundell798a1072018-03-22 15:43:4739class Profile;
40
[email protected]a79be1f2012-03-23 22:14:0041namespace extensions {
42
[email protected]a31ea182014-04-02 01:37:2643class IdentityAPI : public BrowserContextKeyedAPI,
Miyoung Shin23737f62019-07-23 15:43:3144 public signin::IdentityManager::Observer {
[email protected]a31ea182014-04-02 01:37:2645 public:
Alex Ilin3779f5c72020-01-21 20:58:4846 using OnSetConsentResultSignature = void(const std::string&,
47 const std::string&);
[email protected]a31ea182014-04-02 01:37:2648
[email protected]a31ea182014-04-02 01:37:2649 explicit IdentityAPI(content::BrowserContext* context);
dchengae36a4a2014-10-21 12:36:3650 ~IdentityAPI() override;
[email protected]a31ea182014-04-02 01:37:2651
52 // Request serialization queue for getAuthToken.
53 IdentityMintRequestQueue* mint_queue();
54
William Lincb0e4af2020-08-13 01:42:4655 IdentityTokenCache* token_cache();
[email protected]a31ea182014-04-02 01:37:2656
Alex Ilinb1a0b0c72020-03-31 15:36:3057 // GAIA id cache.
Alex Ilinb1a0b0c72020-03-31 15:36:3058 void SetGaiaIdForExtension(const std::string& extension_id,
59 const std::string& gaia_id);
Alex Ilind8d084f2020-04-02 12:14:5260 // 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 Ilinb1a0b0c72020-03-31 15:36:3068
Alex Ilin3779f5c72020-01-21 20:58:4869 // Consent result.
70 void SetConsentResult(const std::string& result,
71 const std::string& window_id);
Clark DuVall14765492020-07-09 09:49:5872 std::unique_ptr<
73 base::RepeatingCallbackList<OnSetConsentResultSignature>::Subscription>
Alex Ilin3779f5c72020-01-21 20:58:4874 RegisterOnSetConsentResultCallback(
75 const base::RepeatingCallback<OnSetConsentResultSignature>& callback);
76
Colin Blundell798a1072018-03-22 15:43:4777 // BrowserContextKeyedAPI:
dchengae36a4a2014-10-21 12:36:3678 void Shutdown() override;
[email protected]a31ea182014-04-02 01:37:2679 static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance();
80
Clark DuVall14765492020-07-09 09:49:5881 std::unique_ptr<base::OnceCallbackList<void()>::Subscription>
82 RegisterOnShutdownCallback(base::OnceClosure cb);
blundell202f12122017-02-03 09:30:2383
Colin Blundella423b412017-08-01 17:15:2384 // 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 Roger739d1532018-10-11 17:21:2593 // Whether the chrome.identity API should use all accounts or the primary
94 // account only.
95 bool AreExtensionsRestrictedToPrimaryAccount();
96
[email protected]a31ea182014-04-02 01:37:2697 private:
98 friend class BrowserContextKeyedAPIFactory<IdentityAPI>;
Alex Ilind8d084f2020-04-02 12:14:5299 friend class IdentityAPITest;
[email protected]a31ea182014-04-02 01:37:26100
Colin Blundell798a1072018-03-22 15:43:47101 // BrowserContextKeyedAPI:
[email protected]a31ea182014-04-02 01:37:26102 static const char* service_name() { return "IdentityAPI"; }
103 static const bool kServiceIsNULLWhileTesting = true;
104
Alex Ilind8d084f2020-04-02 12:14:52105 // 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 Shin23737f62019-07-23 15:43:31111 // signin::IdentityManager::Observer:
Alex Ilind8d084f2020-04-02 12:14:52112 void OnRefreshTokensLoaded() override;
Colin Blundellbc1d0fc2018-11-30 14:56:20113 void OnRefreshTokenUpdatedForAccount(
Gyuyoung Kimb70d3c72019-02-12 01:45:43114 const CoreAccountInfo& account_info) override;
Julie Jeongeun Kim94904342019-01-25 00:47:22115 // NOTE: This class must listen for this callback rather than
116 // OnRefreshTokenRemovedForAccount() to obtain the Gaia ID of the removed
117 // account.
Gyuyoung Kim54ad04802019-02-12 02:06:50118 void OnExtendedAccountInfoRemoved(const AccountInfo& info) override;
Colin Blundell798a1072018-03-22 15:43:47119
120 // Fires the chrome.identity.onSignInChanged event.
121 void FireOnAccountSignInChanged(const std::string& gaia_id,
122 bool is_signed_in);
123
Alex Ilind8d084f2020-04-02 12:14:52124 Profile* const profile_;
125 signin::IdentityManager* const identity_manager_;
126 ExtensionPrefs* const extension_prefs_;
127 EventRouter* const event_router_;
128
[email protected]a31ea182014-04-02 01:37:26129 IdentityMintRequestQueue mint_queue_;
William Lincb0e4af2020-08-13 01:42:46130 IdentityTokenCache token_cache_;
blundell202f12122017-02-03 09:30:23131
Colin Blundella423b412017-08-01 17:15:23132 OnSignInChangedCallback on_signin_changed_callback_for_testing_;
133
Clark DuVall14765492020-07-09 09:49:58134 base::RepeatingCallbackList<OnSetConsentResultSignature>
Alex Ilin3779f5c72020-01-21 20:58:48135 on_set_consent_result_callback_list_;
Clark DuVall14765492020-07-09 09:49:58136 base::OnceCallbackList<void()> on_shutdown_callback_list_;
[email protected]a31ea182014-04-02 01:37:26137};
138
139template <>
140void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies();
141
[email protected]a79be1f2012-03-23 22:14:00142} // namespace extensions
143
144#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_