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.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_service_flag_fetcher.h"
21 #include "components/signin/core/browser/signin_manager_base.h"
22 #include "net/base/backoff_entry.h"
28 namespace user_prefs
{
29 class PrefRegistrySyncable
;
34 // This class handles detection of child accounts (on sign-in as well as on
35 // browser restart), and triggers the appropriate behavior (e.g. enable the
36 // supervised user experience, fetch information about the parent(s)).
37 class ChildAccountService
: public KeyedService
,
38 public FamilyInfoFetcher::Consumer
,
39 public SigninManagerBase::Observer
,
40 public SupervisedUserService::Delegate
{
42 ~ChildAccountService() override
;
44 static bool IsChildAccountDetectionEnabled();
46 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
50 // Sets whether the signed-in account is a child account.
51 // Public so it can be called on platforms where child account detection
52 // happens outside of this class (like Android).
53 void SetIsChildAccount(bool is_child_account
);
55 // Responds whether at least one request for child status was successful.
56 // And we got answer whether the profile belongs to a child account or not.
57 bool IsChildAccountStatusKnown();
60 void Shutdown() override
;
62 void AddChildStatusReceivedCallback(const base::Closure
& callback
);
65 friend class ChildAccountServiceFactory
;
66 // Use |ChildAccountServiceFactory::GetForProfile(...)| to get an instance of
68 explicit ChildAccountService(Profile
* profile
);
70 // SupervisedUserService::Delegate implementation.
71 bool SetActive(bool active
) override
;
72 base::FilePath
GetBlacklistPath() const override
;
73 GURL
GetBlacklistURL() const override
;
74 std::string
GetSafeSitesCx() const override
;
76 // SigninManagerBase::Observer implementation.
77 void GoogleSigninSucceeded(const std::string
& account_id
,
78 const std::string
& username
,
79 const std::string
& password
) override
;
80 void GoogleSignedOut(const std::string
& account_id
,
81 const std::string
& username
) override
;
83 // FamilyInfoFetcher::Consumer implementation.
84 void OnGetFamilyMembersSuccess(
85 const std::vector
<FamilyInfoFetcher::FamilyMember
>& members
) override
;
86 void OnFailure(FamilyInfoFetcher::ErrorCode error
) override
;
88 void StartFetchingFamilyInfo();
89 void CancelFetchingFamilyInfo();
90 void ScheduleNextFamilyInfoUpdate(base::TimeDelta delay
);
92 void StartFetchingServiceFlags();
93 void CancelFetchingServiceFlags();
94 void OnFlagsFetched(AccountServiceFlagFetcher::ResultCode
,
95 const std::vector
<std::string
>& flags
);
96 void ScheduleNextStatusFlagUpdate(base::TimeDelta delay
);
98 void PropagateChildStatusToUser(bool is_child
);
100 void SetFirstCustodianPrefs(const FamilyInfoFetcher::FamilyMember
& custodian
);
101 void SetSecondCustodianPrefs(
102 const FamilyInfoFetcher::FamilyMember
& custodian
);
103 void ClearFirstCustodianPrefs();
104 void ClearSecondCustodianPrefs();
106 // Owns us via the KeyedService mechanism.
111 // The user for which we are currently trying to fetch the child account flag.
112 // Empty when we are not currently fetching.
113 std::string account_id_
;
115 scoped_ptr
<AccountServiceFlagFetcher
> flag_fetcher_
;
116 // If fetching the account service flag fails, retry with exponential backoff.
117 base::OneShotTimer
<ChildAccountService
> flag_fetch_timer_
;
118 net::BackoffEntry flag_fetch_backoff_
;
120 scoped_ptr
<FamilyInfoFetcher
> family_fetcher_
;
121 // If fetching the family info fails, retry with exponential backoff.
122 base::OneShotTimer
<ChildAccountService
> family_fetch_timer_
;
123 net::BackoffEntry family_fetch_backoff_
;
125 // Callbacks to run when the user status becomes known.
126 std::vector
<base::Closure
> status_received_callback_list_
;
128 base::WeakPtrFactory
<ChildAccountService
> weak_ptr_factory_
;
130 DISALLOW_COPY_AND_ASSIGN(ChildAccountService
);
133 #endif // CHROME_BROWSER_SUPERVISED_USER_CHILD_ACCOUNTS_CHILD_ACCOUNT_SERVICE_H_