1 // Copyright 2015 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.
5 #ifndef COMPONENTS_SIGNIN_IOS_BROWSER_ACCOUNT_CONSISTENCY_SERVICE_H_
6 #define COMPONENTS_SIGNIN_IOS_BROWSER_ACCOUNT_CONSISTENCY_SERVICE_H_
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "components/content_settings/core/browser/cookie_settings.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #import "components/signin/ios/browser/manage_accounts_delegate.h"
22 #include "ios/web/public/active_state_manager.h"
27 class WebStatePolicyDecider
;
32 @
class AccountConsistencyNavigationDelegate
;
35 // Handles actions necessary for Account Consistency to work on iOS. This
36 // includes setting the Account Consistency cookie (informing Gaia that the
37 // Account Consistency is on).
39 // This is currently only used when WKWebView is enabled.
40 class AccountConsistencyService
: public KeyedService
,
41 public GaiaCookieManagerService::Observer
,
42 public SigninManagerBase::Observer
,
43 public web::ActiveStateManager::Observer
{
45 // Name of the preference property that persists the domains that have a
46 // X-CHROME-CONNECTED cookie set by this service.
47 static const char kDomainsWithCookiePref
[];
49 AccountConsistencyService(
50 web::BrowserState
* browser_state
,
51 scoped_refptr
<content_settings::CookieSettings
> cookie_settings
,
52 GaiaCookieManagerService
* gaia_cookie_manager_service
,
53 SigninClient
* signin_client
,
54 SigninManager
* signin_manager
);
55 ~AccountConsistencyService() override
;
57 // Registers the preferences used by AccountConsistencyService.
58 static void RegisterPrefs(user_prefs::PrefRegistrySyncable
* registry
);
60 // Sets the handler for |web_state| that reacts on Gaia responses with the
61 // X-Chrome-Manage-Accounts header and notifies |delegate|.
62 void SetWebStateHandler(web::WebState
* web_state
,
63 id
<ManageAccountsDelegate
> delegate
);
64 // Removes the handler associated with |web_state|.
65 void RemoveWebStateHandler(web::WebState
* web_state
);
67 // Enqueues a request to add the X-CHROME-CONNECTED cookie to |domain|. Does
68 // nothing if the cookie is already on |domain|.
69 void AddXChromeConnectedCookieToDomain(const std::string
& domain
);
71 // Enqueues a request to remove the X-CHROME-CONNECTED cookie to |domain|.
72 // Does nothing if the cookie is not set on |domain|.
73 void RemoveXChromeConnectedCookieFromDomain(const std::string
& domain
);
76 friend class AccountConsistencyServiceTest
;
78 // The type of a cookie request.
79 enum CookieRequestType
{
80 ADD_X_CHROME_CONNECTED_COOKIE
,
81 REMOVE_X_CHROME_CONNECTED_COOKIE
84 // A X-CHROME-CONNECTED cookie request to be applied by the
85 // AccountConsistencyService.
86 struct CookieRequest
{
87 static CookieRequest
CreateAddCookieRequest(const std::string
& domain
);
88 static CookieRequest
CreateRemoveCookieRequest(const std::string
& domain
);
89 CookieRequestType request_type
;
93 // Loads the domains with a X-CHROME-CONNECTED cookie from the prefs.
96 // KeyedService implementation.
97 void Shutdown() override
;
99 // Applies the pending X-CHROME-CONNECTED cookie requests one by one.
100 void ApplyCookieRequests();
102 // Called when the current X-CHROME-CONNECTED cookie request is done.
103 void FinishedApplyingCookieRequest(bool success
);
105 // Returns the cached WKWebView if it exists, or creates one if necessary.
106 // Can return nil if the browser state is not active.
107 WKWebView
* GetWKWebView();
108 // Actually creates a WKWebView. Virtual for testing.
109 virtual WKWebView
* CreateWKWebView();
110 // Stops any page loading in the WKWebView currently in use and releases it.
111 void ResetWKWebView();
113 // Adds X-CHROME-CONNECTED cookies on all the main Google domains.
114 void AddXChromeConnectedCookies();
115 // Removes X-CHROME-CONNECTED cookies on all the Google domains where it was
117 void RemoveXChromeConnectedCookies();
119 // GaiaCookieManagerService::Observer implementation.
120 void OnAddAccountToCookieCompleted(
121 const std::string
& account_id
,
122 const GoogleServiceAuthError
& error
) override
;
123 void OnGaiaAccountsInCookieUpdated(
124 const std::vector
<gaia::ListedAccount
>& accounts
,
125 const GoogleServiceAuthError
& error
) override
;
127 // SigninManagerBase::Observer implementation.
128 void GoogleSigninSucceeded(const std::string
& account_id
,
129 const std::string
& username
,
130 const std::string
& password
) override
;
131 void GoogleSignedOut(const std::string
& account_id
,
132 const std::string
& username
) override
;
134 // ActiveStateManager::Observer implementation.
135 void OnActive() override
;
136 void OnInactive() override
;
138 // Browser state associated with the service, used to create WKWebViews.
139 web::BrowserState
* browser_state_
;
140 // Cookie settings currently in use for |browser_state_|, used to check if
141 // setting X-CHROME-CONNECTED cookies is valid.
142 scoped_refptr
<content_settings::CookieSettings
> cookie_settings_
;
143 // Service managing the Gaia cookies, observed to be notified of the state of
145 GaiaCookieManagerService
* gaia_cookie_manager_service_
;
146 // Signin client, used to access prefs.
147 SigninClient
* signin_client_
;
148 // Signin manager, observed to be notified of signin and signout events.
149 SigninManager
* signin_manager_
;
151 // Whether a X-CHROME-CONNECTED cookie request is currently being applied.
152 bool applying_cookie_requests_
;
153 // The queue of X-CHROME-CONNECTED cookie requests to be applied.
154 std::deque
<CookieRequest
> cookie_requests_
;
155 // The set of domains where a X-CHROME-CONNECTED cookie is present.
156 std::set
<std::string
> domains_with_cookies_
;
158 // Web view used to apply the X-CHROME-CONNECTED cookie requests.
159 base::scoped_nsobject
<WKWebView
> web_view_
;
160 // Navigation delegate of |web_view_| that informs the service when a cookie
161 // request has been applied.
162 base::scoped_nsobject
<AccountConsistencyNavigationDelegate
>
163 navigation_delegate_
;
165 // Handlers reacting on GAIA responses with the X-Chrome-Manage-Accounts
167 std::map
<web::WebState
*, scoped_ptr
<web::WebStatePolicyDecider
>>
170 DISALLOW_COPY_AND_ASSIGN(AccountConsistencyService
);
173 #endif // COMPONENTS_SIGNIN_IOS_BROWSER_ACCOUNT_CONSISTENCY_SERVICE_H_