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" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 29 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
30 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
31 | 31 |
32 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
33 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 33 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
34 #include "components/user_manager/user_manager.h" | 34 #include "components/user_manager/user_manager.h" |
35 #endif | 35 #endif |
36 | 36 |
37 const char kIsChildAccountServiceFlagName[] = "uca"; | 37 const char kIsChildAccountServiceFlagName[] = "uca"; |
38 | 38 |
39 // Normally, re-check the child account flag once per day. | 39 // Normally, re-check the child account flag and the family info once per day. |
40 const int kStatusFlagUpdateIntervalSeconds = 60 * 60 * 24; | 40 const int kUpdateIntervalSeconds = 60 * 60 * 24; |
41 | 41 |
42 // In case of an error while getting the flag, retry with exponential backoff. | 42 // In case of an error while getting the flag or the family info, retry with |
43 const net::BackoffEntry::Policy kFlagFetchBackoffPolicy = { | 43 // exponential backoff. |
44 const net::BackoffEntry::Policy kBackoffPolicy = { | |
44 // Number of initial errors (in sequence) to ignore before applying | 45 // Number of initial errors (in sequence) to ignore before applying |
45 // exponential back-off rules. | 46 // exponential back-off rules. |
46 0, | 47 0, |
47 | 48 |
48 // Initial delay for exponential backoff in ms. | 49 // Initial delay for exponential backoff in ms. |
49 2000, | 50 2000, |
50 | 51 |
51 // Factor by which the waiting time will be multiplied. | 52 // Factor by which the waiting time will be multiplied. |
52 2, | 53 2, |
53 | 54 |
54 // Fuzzing percentage. ex: 10% will spread requests randomly | 55 // Fuzzing percentage. ex: 10% will spread requests randomly |
55 // between 90%-100% of the calculated time. | 56 // between 90%-100% of the calculated time. |
56 0.2, // 20% | 57 0.2, // 20% |
57 | 58 |
58 // Maximum amount of time we are willing to delay our request in ms. | 59 // Maximum amount of time we are willing to delay our request in ms. |
59 1000 * 60 * 60 * 4, // 4 hours. | 60 1000 * 60 * 60 * 4, // 4 hours. |
60 | 61 |
61 // Time to keep an entry from being discarded even when it | 62 // Time to keep an entry from being discarded even when it |
62 // has no significant state, -1 to never discard. | 63 // has no significant state, -1 to never discard. |
63 -1, | 64 -1, |
64 | 65 |
65 // Don't use initial delay unless the last request was an error. | 66 // Don't use initial delay unless the last request was an error. |
66 false, | 67 false, |
67 }; | 68 }; |
68 | 69 |
69 ChildAccountService::ChildAccountService(Profile* profile) | 70 ChildAccountService::ChildAccountService(Profile* profile) |
70 : profile_(profile), active_(false), | 71 : profile_(profile), active_(false), |
71 flag_fetch_backoff_(&kFlagFetchBackoffPolicy), | 72 flag_fetch_backoff_(&kBackoffPolicy), |
73 family_fetch_backoff_(&kBackoffPolicy), | |
72 weak_ptr_factory_(this) {} | 74 weak_ptr_factory_(this) {} |
73 | 75 |
74 ChildAccountService::~ChildAccountService() {} | 76 ChildAccountService::~ChildAccountService() {} |
75 | 77 |
76 void ChildAccountService::SetIsChildAccount(bool is_child_account) { | 78 void ChildAccountService::SetIsChildAccount(bool is_child_account) { |
77 if (profile_->IsChild() == is_child_account) | 79 if (profile_->IsChild() == is_child_account) |
78 return; | 80 return; |
79 | 81 |
80 if (is_child_account) { | 82 if (is_child_account) { |
81 profile_->GetPrefs()->SetString(prefs::kSupervisedUserId, | 83 profile_->GetPrefs()->SetString(prefs::kSupervisedUserId, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 SupervisedUserSettingsServiceFactory::GetForProfile(profile_); | 123 SupervisedUserSettingsServiceFactory::GetForProfile(profile_); |
122 settings_service->SetLocalSetting(supervised_users::kSigninAllowed, | 124 settings_service->SetLocalSetting(supervised_users::kSigninAllowed, |
123 allow_signin.Pass()); | 125 allow_signin.Pass()); |
124 #if !defined(OS_CHROMEOS) | 126 #if !defined(OS_CHROMEOS) |
125 // This is also used by user policies (UserPolicySigninService), but since | 127 // This is also used by user policies (UserPolicySigninService), but since |
126 // child accounts can not also be Dasher accounts, there shouldn't be any | 128 // child accounts can not also be Dasher accounts, there shouldn't be any |
127 // problems. | 129 // problems. |
128 SigninManagerFactory::GetForProfile(profile_)->ProhibitSignout(true); | 130 SigninManagerFactory::GetForProfile(profile_)->ProhibitSignout(true); |
129 #endif | 131 #endif |
130 | 132 |
131 // TODO(treib): Maybe only fetch the parents on the first start, and then | 133 // TODO(treib): Maybe store the last update time in a pref, so we don't |
Marc Treib
2015/01/26 13:19:31
WDYT?
Bernhard Bauer
2015/01/26 13:41:23
That seems like a good idea. We could check at eve
| |
132 // refresh occasionally (like once every 24h)? That's what | 134 // have to re-fetch on every start. |
133 // GAIAInfoUpdateService does. | 135 StartFetchingFamilyInfo(); |
134 family_fetcher_.reset(new FamilyInfoFetcher( | |
135 this, | |
136 SigninManagerFactory::GetForProfile(profile_) | |
137 ->GetAuthenticatedAccountId(), | |
138 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
139 profile_->GetRequestContext())); | |
140 family_fetcher_->StartGetFamilyMembers(); | |
141 | 136 |
142 SupervisedUserService* service = | 137 SupervisedUserService* service = |
143 SupervisedUserServiceFactory::GetForProfile(profile_); | 138 SupervisedUserServiceFactory::GetForProfile(profile_); |
144 service->AddPermissionRequestCreator( | 139 service->AddPermissionRequestCreator( |
145 PermissionRequestCreatorApiary::CreateWithProfile(profile_)); | 140 PermissionRequestCreatorApiary::CreateWithProfile(profile_)); |
146 | 141 |
147 EnableExperimentalFiltering(); | 142 EnableExperimentalFiltering(); |
148 } else { | 143 } else { |
149 SupervisedUserSettingsService* settings_service = | 144 SupervisedUserSettingsService* settings_service = |
150 SupervisedUserSettingsServiceFactory::GetForProfile(profile_); | 145 SupervisedUserSettingsServiceFactory::GetForProfile(profile_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 ->GetAuthenticatedAccountId(), | 193 ->GetAuthenticatedAccountId(), |
199 account_id); | 194 account_id); |
200 | 195 |
201 StartFetchingServiceFlags(); | 196 StartFetchingServiceFlags(); |
202 } | 197 } |
203 | 198 |
204 void ChildAccountService::GoogleSignedOut(const std::string& account_id, | 199 void ChildAccountService::GoogleSignedOut(const std::string& account_id, |
205 const std::string& username) { | 200 const std::string& username) { |
206 DCHECK(!profile_->IsChild()); | 201 DCHECK(!profile_->IsChild()); |
207 CancelFetchingServiceFlags(); | 202 CancelFetchingServiceFlags(); |
203 CancelFetchingFamilyInfo(); | |
208 } | 204 } |
209 | 205 |
210 void ChildAccountService::OnGetFamilyMembersSuccess( | 206 void ChildAccountService::OnGetFamilyMembersSuccess( |
211 const std::vector<FamilyInfoFetcher::FamilyMember>& members) { | 207 const std::vector<FamilyInfoFetcher::FamilyMember>& members) { |
212 bool hoh_found = false; | 208 bool hoh_found = false; |
213 bool parent_found = false; | 209 bool parent_found = false; |
214 for (const FamilyInfoFetcher::FamilyMember& member : members) { | 210 for (const FamilyInfoFetcher::FamilyMember& member : members) { |
215 if (member.role == FamilyInfoFetcher::HEAD_OF_HOUSEHOLD) { | 211 if (member.role == FamilyInfoFetcher::HEAD_OF_HOUSEHOLD) { |
216 hoh_found = true; | 212 hoh_found = true; |
217 SetFirstCustodianPrefs(member); | 213 SetFirstCustodianPrefs(member); |
218 } else if (member.role == FamilyInfoFetcher::PARENT) { | 214 } else if (member.role == FamilyInfoFetcher::PARENT) { |
219 parent_found = true; | 215 parent_found = true; |
220 SetSecondCustodianPrefs(member); | 216 SetSecondCustodianPrefs(member); |
221 } | 217 } |
222 if (hoh_found && parent_found) | 218 if (hoh_found && parent_found) |
223 break; | 219 break; |
224 } | 220 } |
225 if (!hoh_found) { | 221 if (!hoh_found) { |
226 DLOG(WARNING) << "GetFamilyMembers didn't return a HOH?!"; | 222 DLOG(WARNING) << "GetFamilyMembers didn't return a HOH?!"; |
227 ClearFirstCustodianPrefs(); | 223 ClearFirstCustodianPrefs(); |
228 } | 224 } |
229 if (!parent_found) | 225 if (!parent_found) |
230 ClearSecondCustodianPrefs(); | 226 ClearSecondCustodianPrefs(); |
231 family_fetcher_.reset(); | 227 family_fetcher_.reset(); |
228 | |
229 family_fetch_backoff_.InformOfRequest(true); | |
230 | |
231 ScheduleNextFamilyInfoUpdate( | |
232 base::TimeDelta::FromSeconds(kUpdateIntervalSeconds)); | |
232 } | 233 } |
233 | 234 |
234 void ChildAccountService::OnFailure(FamilyInfoFetcher::ErrorCode error) { | 235 void ChildAccountService::OnFailure(FamilyInfoFetcher::ErrorCode error) { |
235 DLOG(WARNING) << "GetFamilyMembers failed with code " << error; | 236 DLOG(WARNING) << "GetFamilyMembers failed with code " << error; |
237 family_fetch_backoff_.InformOfRequest(false); | |
238 ScheduleNextFamilyInfoUpdate(family_fetch_backoff_.GetTimeUntilRelease()); | |
239 } | |
240 | |
241 void ChildAccountService::StartFetchingFamilyInfo() { | |
242 family_fetcher_.reset(new FamilyInfoFetcher( | |
243 this, | |
244 SigninManagerFactory::GetForProfile(profile_) | |
245 ->GetAuthenticatedAccountId(), | |
246 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
247 profile_->GetRequestContext())); | |
248 family_fetcher_->StartGetFamilyMembers(); | |
249 } | |
250 | |
251 void ChildAccountService::CancelFetchingFamilyInfo() { | |
236 family_fetcher_.reset(); | 252 family_fetcher_.reset(); |
237 // TODO(treib): Retry after a while? | 253 family_fetch_timer_.Stop(); |
254 } | |
255 | |
256 void ChildAccountService::ScheduleNextFamilyInfoUpdate(base::TimeDelta delay) { | |
257 family_fetch_timer_.Start( | |
258 FROM_HERE, delay, this, &ChildAccountService::StartFetchingFamilyInfo); | |
238 } | 259 } |
239 | 260 |
240 void ChildAccountService::StartFetchingServiceFlags() { | 261 void ChildAccountService::StartFetchingServiceFlags() { |
241 account_id_ = SigninManagerFactory::GetForProfile(profile_) | 262 account_id_ = SigninManagerFactory::GetForProfile(profile_) |
242 ->GetAuthenticatedAccountId(); | 263 ->GetAuthenticatedAccountId(); |
243 flag_fetcher_.reset(new AccountServiceFlagFetcher( | 264 flag_fetcher_.reset(new AccountServiceFlagFetcher( |
244 account_id_, | 265 account_id_, |
245 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | 266 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), |
246 profile_->GetRequestContext(), | 267 profile_->GetRequestContext(), |
247 base::Bind(&ChildAccountService::OnFlagsFetched, | 268 base::Bind(&ChildAccountService::OnFlagsFetched, |
(...skipping 27 matching lines...) Expand all Loading... | |
275 return; | 296 return; |
276 } | 297 } |
277 | 298 |
278 flag_fetch_backoff_.InformOfRequest(true); | 299 flag_fetch_backoff_.InformOfRequest(true); |
279 | 300 |
280 bool is_child_account = | 301 bool is_child_account = |
281 std::find(flags.begin(), flags.end(), | 302 std::find(flags.begin(), flags.end(), |
282 kIsChildAccountServiceFlagName) != flags.end(); | 303 kIsChildAccountServiceFlagName) != flags.end(); |
283 SetIsChildAccount(is_child_account); | 304 SetIsChildAccount(is_child_account); |
284 | 305 |
285 ScheduleNextStatusFlagUpdate(base::TimeDelta::FromSeconds( | 306 ScheduleNextStatusFlagUpdate( |
286 kStatusFlagUpdateIntervalSeconds)); | 307 base::TimeDelta::FromSeconds(kUpdateIntervalSeconds)); |
287 } | 308 } |
288 | 309 |
289 void ChildAccountService::ScheduleNextStatusFlagUpdate(base::TimeDelta delay) { | 310 void ChildAccountService::ScheduleNextStatusFlagUpdate(base::TimeDelta delay) { |
290 flag_fetch_timer_.Start( | 311 flag_fetch_timer_.Start( |
291 FROM_HERE, delay, this, &ChildAccountService::StartFetchingServiceFlags); | 312 FROM_HERE, delay, this, &ChildAccountService::StartFetchingServiceFlags); |
292 } | 313 } |
293 | 314 |
294 void ChildAccountService::PropagateChildStatusToUser(bool is_child) { | 315 void ChildAccountService::PropagateChildStatusToUser(bool is_child) { |
295 #if defined(OS_CHROMEOS) | 316 #if defined(OS_CHROMEOS) |
296 user_manager::User* user = | 317 user_manager::User* user = |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 command_line->AppendSwitch(switches::kEnableSupervisedUserBlacklist); | 381 command_line->AppendSwitch(switches::kEnableSupervisedUserBlacklist); |
361 | 382 |
362 // Query-based filtering also defaults to enabled. | 383 // Query-based filtering also defaults to enabled. |
363 bool has_enable_safesites = | 384 bool has_enable_safesites = |
364 command_line->HasSwitch(switches::kEnableSupervisedUserSafeSites); | 385 command_line->HasSwitch(switches::kEnableSupervisedUserSafeSites); |
365 bool has_disable_safesites = | 386 bool has_disable_safesites = |
366 command_line->HasSwitch(switches::kDisableSupervisedUserSafeSites); | 387 command_line->HasSwitch(switches::kDisableSupervisedUserSafeSites); |
367 if (!has_enable_safesites && !has_disable_safesites) | 388 if (!has_enable_safesites && !has_disable_safesites) |
368 command_line->AppendSwitch(switches::kEnableSupervisedUserSafeSites); | 389 command_line->AppendSwitch(switches::kEnableSupervisedUserSafeSites); |
369 } | 390 } |
OLD | NEW |