OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/supervised_user/child_accounts/child_account_service.h" | 5 #include "chrome/browser/supervised_user/child_accounts/child_account_service.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
18 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
19 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" | 19 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" |
20 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 20 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
21 #include "chrome/browser/supervised_user/supervised_user_service.h" | 21 #include "chrome/browser/supervised_user/supervised_user_service.h" |
22 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 22 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
23 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" | 23 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" |
24 #include "chrome/browser/supervised_user/supervised_user_settings_service_factor
y.h" | 24 #include "chrome/browser/supervised_user/supervised_user_settings_service_factor
y.h" |
25 #include "chrome/browser/sync/profile_sync_service.h" | 25 #include "chrome/browser/sync/profile_sync_service.h" |
26 #include "chrome/browser/sync/profile_sync_service_factory.h" | 26 #include "chrome/browser/sync/profile_sync_service_factory.h" |
27 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" |
30 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 31 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
31 #include "components/signin/core/browser/signin_manager.h" | 32 #include "components/signin/core/browser/signin_manager.h" |
32 | 33 |
33 #if defined(OS_CHROMEOS) | 34 #if defined(OS_CHROMEOS) |
34 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 35 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
35 #include "components/user_manager/user_manager.h" | 36 #include "components/user_manager/user_manager.h" |
36 #endif | 37 #endif |
37 | 38 |
38 const char kChildAccountDetectionFieldTrialName[] = "ChildAccountDetection"; | 39 const char kChildAccountDetectionFieldTrialName[] = "ChildAccountDetection"; |
39 | 40 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 88 |
88 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 89 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
89 if (command_line->HasSwitch(switches::kDisableChildAccountDetection)) | 90 if (command_line->HasSwitch(switches::kDisableChildAccountDetection)) |
90 return false; | 91 return false; |
91 if (command_line->HasSwitch(switches::kEnableChildAccountDetection)) | 92 if (command_line->HasSwitch(switches::kEnableChildAccountDetection)) |
92 return true; | 93 return true; |
93 | 94 |
94 return group_name == "Enabled"; | 95 return group_name == "Enabled"; |
95 } | 96 } |
96 | 97 |
| 98 void ChildAccountService::RegisterProfilePrefs( |
| 99 user_prefs::PrefRegistrySyncable* registry) { |
| 100 registry->RegisterBooleanPref( |
| 101 prefs::kChildAccountStatusKnown, |
| 102 false, |
| 103 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 104 } |
| 105 |
97 void ChildAccountService::SetIsChildAccount(bool is_child_account) { | 106 void ChildAccountService::SetIsChildAccount(bool is_child_account) { |
98 PropagateChildStatusToUser(is_child_account); | 107 PropagateChildStatusToUser(is_child_account); |
99 if (profile_->IsChild() == is_child_account) | 108 if (profile_->IsChild() == is_child_account) |
100 return; | 109 return; |
101 | 110 |
102 if (is_child_account) { | 111 if (is_child_account) { |
103 profile_->GetPrefs()->SetString(prefs::kSupervisedUserId, | 112 profile_->GetPrefs()->SetString(prefs::kSupervisedUserId, |
104 supervised_users::kChildAccountSUID); | 113 supervised_users::kChildAccountSUID); |
105 } else { | 114 } else { |
106 profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserId); | 115 profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserId); |
107 } | 116 } |
108 } | 117 } |
109 | 118 |
110 void ChildAccountService::Init() { | 119 void ChildAccountService::Init() { |
111 SigninManagerFactory::GetForProfile(profile_)->AddObserver(this); | 120 SigninManagerFactory::GetForProfile(profile_)->AddObserver(this); |
112 SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(this); | 121 SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(this); |
113 | 122 |
114 PropagateChildStatusToUser(profile_->IsChild()); | 123 PropagateChildStatusToUser(profile_->IsChild()); |
115 | 124 |
116 // If we're already signed in, fetch the flag again just to be sure. | 125 // If we're already signed in, fetch the flag again just to be sure. |
117 // (Previously, the browser might have been closed before we got the flag. | 126 // (Previously, the browser might have been closed before we got the flag. |
118 // This also handles the graduation use case in a basic way.) | 127 // This also handles the graduation use case in a basic way.) |
119 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) | 128 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) |
120 StartFetchingServiceFlags(); | 129 StartFetchingServiceFlags(); |
121 } | 130 } |
122 | 131 |
| 132 bool ChildAccountService::IsChildAccountStatusKnown() { |
| 133 return profile_->GetPrefs()->GetBoolean(prefs::kChildAccountStatusKnown); |
| 134 } |
| 135 |
123 void ChildAccountService::Shutdown() { | 136 void ChildAccountService::Shutdown() { |
124 family_fetcher_.reset(); | 137 family_fetcher_.reset(); |
125 CancelFetchingServiceFlags(); | 138 CancelFetchingServiceFlags(); |
126 SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(NULL); | 139 SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(NULL); |
127 DCHECK(!active_); | 140 DCHECK(!active_); |
128 SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this); | 141 SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this); |
129 } | 142 } |
130 | 143 |
| 144 |
| 145 void ChildAccountService::AddChildStatusReceivedCallback( |
| 146 const base::Closure& callback) { |
| 147 if (IsChildAccountStatusKnown()) |
| 148 callback.Run(); |
| 149 else |
| 150 status_received_callback_list_.push_back(callback); |
| 151 } |
| 152 |
131 bool ChildAccountService::SetActive(bool active) { | 153 bool ChildAccountService::SetActive(bool active) { |
132 if (!profile_->IsChild() && !active_) | 154 if (!profile_->IsChild() && !active_) |
133 return false; | 155 return false; |
134 if (active_ == active) | 156 if (active_ == active) |
135 return true; | 157 return true; |
136 active_ = active; | 158 active_ = active; |
137 | 159 |
138 if (active_) { | 160 if (active_) { |
139 // In contrast to local SUs, child account SUs must sign in. | 161 // In contrast to local SUs, child account SUs must sign in. |
140 scoped_ptr<base::Value> allow_signin(new base::FundamentalValue(true)); | 162 scoped_ptr<base::Value> allow_signin(new base::FundamentalValue(true)); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 FROM_HERE, delay, this, &ChildAccountService::StartFetchingFamilyInfo); | 299 FROM_HERE, delay, this, &ChildAccountService::StartFetchingFamilyInfo); |
278 } | 300 } |
279 | 301 |
280 void ChildAccountService::StartFetchingServiceFlags() { | 302 void ChildAccountService::StartFetchingServiceFlags() { |
281 if (!IsChildAccountDetectionEnabled()) { | 303 if (!IsChildAccountDetectionEnabled()) { |
282 SetIsChildAccount(false); | 304 SetIsChildAccount(false); |
283 return; | 305 return; |
284 } | 306 } |
285 account_id_ = SigninManagerFactory::GetForProfile(profile_) | 307 account_id_ = SigninManagerFactory::GetForProfile(profile_) |
286 ->GetAuthenticatedAccountId(); | 308 ->GetAuthenticatedAccountId(); |
287 flag_fetcher_.reset(new AccountServiceFlagFetcher( | |
288 account_id_, | |
289 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
290 profile_->GetRequestContext(), | |
291 base::Bind(&ChildAccountService::OnFlagsFetched, | |
292 weak_ptr_factory_.GetWeakPtr()))); | |
293 } | 309 } |
294 | 310 |
295 void ChildAccountService::CancelFetchingServiceFlags() { | 311 void ChildAccountService::CancelFetchingServiceFlags() { |
296 flag_fetcher_.reset(); | 312 flag_fetcher_.reset(); |
297 account_id_.clear(); | 313 account_id_.clear(); |
298 flag_fetch_timer_.Stop(); | 314 flag_fetch_timer_.Stop(); |
299 } | 315 } |
300 | 316 |
301 void ChildAccountService::OnFlagsFetched( | 317 void ChildAccountService::OnFlagsFetched( |
302 AccountServiceFlagFetcher::ResultCode result, | 318 AccountServiceFlagFetcher::ResultCode result, |
(...skipping 14 matching lines...) Expand all Loading... |
317 flag_fetch_backoff_.InformOfRequest(false); | 333 flag_fetch_backoff_.InformOfRequest(false); |
318 ScheduleNextStatusFlagUpdate(flag_fetch_backoff_.GetTimeUntilRelease()); | 334 ScheduleNextStatusFlagUpdate(flag_fetch_backoff_.GetTimeUntilRelease()); |
319 return; | 335 return; |
320 } | 336 } |
321 | 337 |
322 flag_fetch_backoff_.InformOfRequest(true); | 338 flag_fetch_backoff_.InformOfRequest(true); |
323 | 339 |
324 bool is_child_account = | 340 bool is_child_account = |
325 std::find(flags.begin(), flags.end(), | 341 std::find(flags.begin(), flags.end(), |
326 kIsChildAccountServiceFlagName) != flags.end(); | 342 kIsChildAccountServiceFlagName) != flags.end(); |
| 343 |
| 344 bool status_was_known = profile_->GetPrefs()->GetBoolean( |
| 345 prefs::kChildAccountStatusKnown); |
| 346 profile_->GetPrefs()->SetBoolean(prefs::kChildAccountStatusKnown, true); |
| 347 |
| 348 if (!status_was_known) { |
| 349 for (auto& callback : status_received_callback_list_) |
| 350 callback.Run(); |
| 351 status_received_callback_list_.clear(); |
| 352 } |
| 353 |
327 SetIsChildAccount(is_child_account); | 354 SetIsChildAccount(is_child_account); |
328 | 355 |
329 ScheduleNextStatusFlagUpdate( | 356 ScheduleNextStatusFlagUpdate( |
330 base::TimeDelta::FromSeconds(kUpdateIntervalSeconds)); | 357 base::TimeDelta::FromSeconds(kUpdateIntervalSeconds)); |
331 } | 358 } |
332 | 359 |
333 void ChildAccountService::ScheduleNextStatusFlagUpdate(base::TimeDelta delay) { | 360 void ChildAccountService::ScheduleNextStatusFlagUpdate(base::TimeDelta delay) { |
334 flag_fetch_timer_.Start( | 361 flag_fetch_timer_.Start( |
335 FROM_HERE, delay, this, &ChildAccountService::StartFetchingServiceFlags); | 362 FROM_HERE, delay, this, &ChildAccountService::StartFetchingServiceFlags); |
336 } | 363 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 command_line->AppendSwitch(switches::kEnableSupervisedUserBlacklist); | 431 command_line->AppendSwitch(switches::kEnableSupervisedUserBlacklist); |
405 | 432 |
406 // Query-based filtering also defaults to enabled. | 433 // Query-based filtering also defaults to enabled. |
407 bool has_enable_safesites = | 434 bool has_enable_safesites = |
408 command_line->HasSwitch(switches::kEnableSupervisedUserSafeSites); | 435 command_line->HasSwitch(switches::kEnableSupervisedUserSafeSites); |
409 bool has_disable_safesites = | 436 bool has_disable_safesites = |
410 command_line->HasSwitch(switches::kDisableSupervisedUserSafeSites); | 437 command_line->HasSwitch(switches::kDisableSupervisedUserSafeSites); |
411 if (!has_enable_safesites && !has_disable_safesites) | 438 if (!has_enable_safesites && !has_disable_safesites) |
412 command_line->AppendSwitch(switches::kEnableSupervisedUserSafeSites); | 439 command_line->AppendSwitch(switches::kEnableSupervisedUserSafeSites); |
413 } | 440 } |
OLD | NEW |