1 // Copyright 2014 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 #include "chrome/browser/signin/chrome_signin_client.h"
7 #include "base/command_line.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/content_settings/cookie_settings.h"
13 #include "chrome/browser/net/chrome_cookie_notification_details.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profile_metrics.h"
17 #include "chrome/browser/profiles/profile_window.h"
18 #include "chrome/browser/signin/local_auth.h"
19 #include "chrome/browser/signin/signin_cookie_changed_subscription.h"
20 #include "chrome/browser/webdata/web_data_service_factory.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "components/metrics/metrics_service.h"
23 #include "components/signin/core/common/profile_management_switches.h"
24 #include "components/signin/core/common/signin_pref_names.h"
25 #include "components/signin/core/common/signin_switches.h"
26 #include "net/url_request/url_request_context_getter.h"
29 #if defined(ENABLE_SUPERVISED_USERS)
30 #include "chrome/browser/supervised_user/supervised_user_constants.h"
33 #if defined(OS_CHROMEOS)
34 #include "components/user_manager/user_manager.h"
37 #if !defined(OS_ANDROID)
38 #include "chrome/browser/first_run/first_run.h"
43 const char kGoogleAccountsUrl
[] = "https://accounts.google.com";
47 ChromeSigninClient::ChromeSigninClient(
48 Profile
* profile
, SigninErrorController
* signin_error_controller
)
50 signin_error_controller_(signin_error_controller
) {
51 signin_error_controller_
->AddObserver(this);
54 ChromeSigninClient::~ChromeSigninClient() {
55 signin_error_controller_
->RemoveObserver(this);
59 bool ChromeSigninClient::ProfileAllowsSigninCookies(Profile
* profile
) {
60 CookieSettings
* cookie_settings
=
61 CookieSettings::Factory::GetForProfile(profile
).get();
62 return SettingsAllowSigninCookies(cookie_settings
);
66 bool ChromeSigninClient::SettingsAllowSigninCookies(
67 CookieSettings
* cookie_settings
) {
68 return cookie_settings
&&
69 cookie_settings
->IsSettingCookieAllowed(GURL(kGoogleAccountsUrl
),
70 GURL(kGoogleAccountsUrl
));
73 PrefService
* ChromeSigninClient::GetPrefs() { return profile_
->GetPrefs(); }
75 scoped_refptr
<TokenWebData
> ChromeSigninClient::GetDatabase() {
76 return WebDataServiceFactory::GetTokenWebDataForProfile(
77 profile_
, ServiceAccessType::EXPLICIT_ACCESS
);
80 bool ChromeSigninClient::CanRevokeCredentials() {
81 #if defined(OS_CHROMEOS)
82 // UserManager may not exist in unit_tests.
83 if (user_manager::UserManager::IsInitialized() &&
84 user_manager::UserManager::Get()->IsLoggedInAsSupervisedUser()) {
85 // Don't allow revoking credentials for Chrome OS supervised users.
86 // See http://crbug.com/332032
87 LOG(ERROR
) << "Attempt to revoke supervised user refresh "
88 << "token detected, ignoring.";
92 // Don't allow revoking credentials for legacy supervised users.
93 // See http://crbug.com/332032
94 if (profile_
->IsLegacySupervised()) {
95 LOG(ERROR
) << "Attempt to revoke supervised user refresh "
96 << "token detected, ignoring.";
103 std::string
ChromeSigninClient::GetSigninScopedDeviceId() {
104 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kDisableSigninScopedDeviceId
)) {
106 return std::string();
109 std::string signin_scoped_device_id
=
110 GetPrefs()->GetString(prefs::kGoogleServicesSigninScopedDeviceId
);
111 if (signin_scoped_device_id
.empty()) {
112 // If device_id doesn't exist then generate new and save in prefs.
113 signin_scoped_device_id
= base::GenerateGUID();
114 DCHECK(!signin_scoped_device_id
.empty());
115 GetPrefs()->SetString(prefs::kGoogleServicesSigninScopedDeviceId
,
116 signin_scoped_device_id
);
118 return signin_scoped_device_id
;
121 void ChromeSigninClient::OnSignedOut() {
122 GetPrefs()->ClearPref(prefs::kGoogleServicesSigninScopedDeviceId
);
123 ProfileInfoCache
& cache
=
124 g_browser_process
->profile_manager()->GetProfileInfoCache();
125 size_t index
= cache
.GetIndexOfProfileWithPath(profile_
->GetPath());
127 // If sign out occurs because Sync setup was in progress and the Profile got
128 // deleted, then the profile's no longer in the ProfileInfoCache.
129 if (index
== std::string::npos
)
132 cache
.SetLocalAuthCredentialsOfProfileAtIndex(index
, std::string());
133 cache
.SetUserNameOfProfileAtIndex(index
, base::string16());
134 cache
.SetProfileSigninRequiredAtIndex(index
, false);
137 net::URLRequestContextGetter
* ChromeSigninClient::GetURLRequestContext() {
138 return profile_
->GetRequestContext();
141 bool ChromeSigninClient::ShouldMergeSigninCredentialsIntoCookieJar() {
142 return !switches::IsEnableAccountConsistency();
145 std::string
ChromeSigninClient::GetProductVersion() {
146 chrome::VersionInfo chrome_version
;
147 return chrome_version
.CreateVersionString();
150 bool ChromeSigninClient::IsFirstRun() const {
151 #if defined(OS_ANDROID)
154 return first_run::IsChromeFirstRun();
158 base::Time
ChromeSigninClient::GetInstallDate() {
159 return base::Time::FromTimeT(
160 g_browser_process
->metrics_service()->GetInstallDate());
163 bool ChromeSigninClient::AreSigninCookiesAllowed() {
164 return ProfileAllowsSigninCookies(profile_
);
167 void ChromeSigninClient::AddContentSettingsObserver(
168 content_settings::Observer
* observer
) {
169 profile_
->GetHostContentSettingsMap()->AddObserver(observer
);
172 void ChromeSigninClient::RemoveContentSettingsObserver(
173 content_settings::Observer
* observer
) {
174 profile_
->GetHostContentSettingsMap()->RemoveObserver(observer
);
177 scoped_ptr
<SigninClient::CookieChangedSubscription
>
178 ChromeSigninClient::AddCookieChangedCallback(
180 const std::string
& name
,
181 const net::CookieStore::CookieChangedCallback
& callback
) {
182 scoped_refptr
<net::URLRequestContextGetter
> context_getter
=
183 profile_
->GetRequestContext();
184 DCHECK(context_getter
.get());
185 scoped_ptr
<SigninCookieChangedSubscription
> subscription(
186 new SigninCookieChangedSubscription(context_getter
, url
, name
, callback
));
187 return subscription
.Pass();
190 void ChromeSigninClient::OnSignedIn(const std::string
& account_id
,
191 const std::string
& username
,
192 const std::string
& password
) {
193 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
194 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
195 size_t index
= cache
.GetIndexOfProfileWithPath(profile_
->GetPath());
196 if (index
!= std::string::npos
) {
197 cache
.SetUserNameOfProfileAtIndex(index
, base::UTF8ToUTF16(username
));
198 ProfileMetrics::UpdateReportedProfilesStatistics(profile_manager
);
202 void ChromeSigninClient::PostSignedIn(const std::string
& account_id
,
203 const std::string
& username
,
204 const std::string
& password
) {
205 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
206 // Don't store password hash except when lock is available for the user.
207 if (!password
.empty() && profiles::IsLockAvailable(profile_
))
208 LocalAuth::SetLocalAuthCredentials(profile_
, password
);
212 void ChromeSigninClient::OnErrorChanged() {
213 // Some tests don't have a ProfileManager.
214 if (g_browser_process
->profile_manager() == nullptr)
217 ProfileInfoCache
& cache
= g_browser_process
->profile_manager()->
218 GetProfileInfoCache();
219 size_t index
= cache
.GetIndexOfProfileWithPath(profile_
->GetPath());
220 if (index
== std::string::npos
)
223 cache
.SetProfileIsAuthErrorAtIndex(index
,
224 signin_error_controller_
->HasError());