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 #ifndef CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_CHILD_ACCOUNT_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_CHILD_ACCOUNT_SERVICE_H_
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/supervised_user/child_accounts/family_info_fetcher.h"
18 #include "chrome/browser/supervised_user/supervised_user_service.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "components/signin/core/browser/account_tracker_service.h"
21 #include "net/base/backoff_entry.h"
27 namespace user_prefs
{
28 class PrefRegistrySyncable
;
33 // This class handles detection of child accounts (on sign-in as well as on
34 // browser restart), and triggers the appropriate behavior (e.g. enable the
35 // supervised user experience, fetch information about the parent(s)).
36 class ChildAccountService
: public KeyedService
,
37 public FamilyInfoFetcher::Consumer
,
38 public AccountTrackerService::Observer
,
39 public SupervisedUserService::Delegate
{
41 ~ChildAccountService() override
;
43 static bool IsChildAccountDetectionEnabled();
45 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
49 // Responds whether at least one request for child status was successful.
50 // And we got answer whether the profile belongs to a child account or not.
51 bool IsChildAccountStatusKnown();
54 void Shutdown() override
;
56 void AddChildStatusReceivedCallback(const base::Closure
& callback
);
59 friend class ChildAccountServiceFactory
;
60 // Use |ChildAccountServiceFactory::GetForProfile(...)| to get an instance of
62 explicit ChildAccountService(Profile
* profile
);
64 // SupervisedUserService::Delegate implementation.
65 bool SetActive(bool active
) override
;
67 // Sets whether the signed-in account is a child account.
68 void SetIsChildAccount(bool is_child_account
);
70 // AccountTrackerService::Observer implementation.
71 void OnAccountUpdated(const AccountInfo
& info
) override
;
73 // FamilyInfoFetcher::Consumer implementation.
74 void OnGetFamilyMembersSuccess(
75 const std::vector
<FamilyInfoFetcher::FamilyMember
>& members
) override
;
76 void OnFailure(FamilyInfoFetcher::ErrorCode error
) override
;
78 void StartFetchingFamilyInfo();
79 void CancelFetchingFamilyInfo();
80 void ScheduleNextFamilyInfoUpdate(base::TimeDelta delay
);
82 void PropagateChildStatusToUser(bool is_child
);
84 void SetFirstCustodianPrefs(const FamilyInfoFetcher::FamilyMember
& custodian
);
85 void SetSecondCustodianPrefs(
86 const FamilyInfoFetcher::FamilyMember
& custodian
);
87 void ClearFirstCustodianPrefs();
88 void ClearSecondCustodianPrefs();
90 // Owns us via the KeyedService mechanism.
95 scoped_ptr
<FamilyInfoFetcher
> family_fetcher_
;
96 // If fetching the family info fails, retry with exponential backoff.
97 base::OneShotTimer
<ChildAccountService
> family_fetch_timer_
;
98 net::BackoffEntry family_fetch_backoff_
;
100 // Callbacks to run when the user status becomes known.
101 std::vector
<base::Closure
> status_received_callback_list_
;
103 base::WeakPtrFactory
<ChildAccountService
> weak_ptr_factory_
;
105 DISALLOW_COPY_AND_ASSIGN(ChildAccountService
);
108 #endif // CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_CHILD_ACCOUNT_SERVICE_H_