File: child_account_service.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (321 lines) | stat: -rw-r--r-- 12,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/supervised_user/child_accounts/child_account_service.h"

#include "base/callback.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/supervised_user/child_accounts/permission_request_creator_apiary.h"
#include "chrome/browser/supervised_user/supervised_user_constants.h"
#include "chrome/browser/supervised_user/supervised_user_service.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "components/user_manager/user_manager.h"
#endif

const char kIsChildAccountServiceFlagName[] = "uca";

ChildAccountService::ChildAccountService(Profile* profile)
    : profile_(profile), active_(false), weak_ptr_factory_(this) {}

ChildAccountService::~ChildAccountService() {}

void ChildAccountService::SetIsChildAccount(bool is_child_account) {
  if (profile_->IsChild() == is_child_account)
    return;

  if (is_child_account) {
    profile_->GetPrefs()->SetString(prefs::kSupervisedUserId,
                                    supervised_users::kChildAccountSUID);
  } else {
    profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserId);
  }
  PropagateChildStatusToUser(is_child_account);
}

void ChildAccountService::Init() {
  SigninManagerFactory::GetForProfile(profile_)->AddObserver(this);
  SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(this);

  PropagateChildStatusToUser(profile_->IsChild());

  // If we're already signed in, fetch the flag again just to be sure.
  // (Previously, the browser might have been closed before we got the flag.
  // This also handles the graduation use case in a basic way.)
  std::string account_id = SigninManagerFactory::GetForProfile(profile_)
      ->GetAuthenticatedAccountId();
  if (!account_id.empty())
    StartFetchingServiceFlags(account_id);
}

void ChildAccountService::Shutdown() {
  CancelFetchingServiceFlags();
  SupervisedUserServiceFactory::GetForProfile(profile_)->SetDelegate(NULL);
  DCHECK(!active_);
  SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this);
}

bool ChildAccountService::SetActive(bool active) {
  if (!profile_->IsChild() && !active_)
    return false;
  if (active_ == active)
    return true;
  active_ = active;

  if (active_) {
    // In contrast to local SUs, child account SUs must sign in.
    scoped_ptr<base::Value> allow_signin(new base::FundamentalValue(true));
    SupervisedUserSettingsService* settings_service =
        SupervisedUserSettingsServiceFactory::GetForProfile(profile_);
    settings_service->SetLocalSetting(supervised_users::kSigninAllowed,
                                      allow_signin.Pass());
#if !defined(OS_CHROMEOS)
    // This is also used by user policies (UserPolicySigninService), but since
    // child accounts can not also be Dasher accounts, there shouldn't be any
    // problems.
    SigninManagerFactory::GetForProfile(profile_)->ProhibitSignout(true);
#endif

    // TODO(treib): Maybe only fetch the parents on the first start, and then
    // refresh occasionally (like once every 24h)? That's what
    // GAIAInfoUpdateService does.
    family_fetcher_.reset(new FamilyInfoFetcher(
        this,
        SigninManagerFactory::GetForProfile(profile_)
            ->GetAuthenticatedAccountId(),
        ProfileOAuth2TokenServiceFactory::GetForProfile(profile_),
        profile_->GetRequestContext()));
    family_fetcher_->StartGetFamilyMembers();

    SupervisedUserService* service =
        SupervisedUserServiceFactory::GetForProfile(profile_);
    service->AddPermissionRequestCreator(
        PermissionRequestCreatorApiary::CreateWithProfile(profile_));

    EnableExperimentalFiltering();
  } else {
    SupervisedUserSettingsService* settings_service =
        SupervisedUserSettingsServiceFactory::GetForProfile(profile_);
    settings_service->SetLocalSetting(supervised_users::kSigninAllowed,
                                      scoped_ptr<base::Value>());
#if !defined(OS_CHROMEOS)
    SigninManagerFactory::GetForProfile(profile_)->ProhibitSignout(false);
#endif

    ClearFirstCustodianPrefs();
    ClearSecondCustodianPrefs();
  }

  // Trigger a sync reconfig to enable/disable the right SU data types.
  // The logic to do this lives in the SupervisedUserSyncDataTypeController.
  ProfileSyncService* sync_service =
      ProfileSyncServiceFactory::GetForProfile(profile_);
  if (sync_service->HasSyncSetupCompleted())
    sync_service->ReconfigureDatatypeManager();

  return true;
}

base::FilePath ChildAccountService::GetBlacklistPath() const {
  if (!active_)
    return base::FilePath();
  base::FilePath blacklist_path;
  PathService::Get(chrome::DIR_USER_DATA, &blacklist_path);
  blacklist_path = blacklist_path.AppendASCII("su-blacklist.bin");
  return blacklist_path;
}

GURL ChildAccountService::GetBlacklistURL() const {
  if (!active_)
    return GURL();
  return GURL("https://www.gstatic.com/chrome/supervised_user/"
              "blacklist-20141001-1k.bin");
}

std::string ChildAccountService::GetSafeSitesCx() const {
  if (!active_)
    return std::string();
  return "017993620680222980993%3A1wdumejvx5i";
}

void ChildAccountService::GoogleSigninSucceeded(const std::string& account_id,
                                                const std::string& username,
                                                const std::string& password) {
  DCHECK(!account_id.empty());

  StartFetchingServiceFlags(account_id);
}

void ChildAccountService::GoogleSignedOut(const std::string& account_id,
                                          const std::string& username) {
  DCHECK(!profile_->IsChild());
  CancelFetchingServiceFlags();
}

void ChildAccountService::OnGetFamilyMembersSuccess(
    const std::vector<FamilyInfoFetcher::FamilyMember>& members) {
  bool hoh_found = false;
  bool parent_found = false;
  for (const FamilyInfoFetcher::FamilyMember& member : members) {
    if (member.role == FamilyInfoFetcher::HEAD_OF_HOUSEHOLD) {
      hoh_found = true;
      SetFirstCustodianPrefs(member);
    } else if (member.role == FamilyInfoFetcher::PARENT) {
      parent_found = true;
      SetSecondCustodianPrefs(member);
    }
    if (hoh_found && parent_found)
      break;
  }
  if (!hoh_found) {
    DLOG(WARNING) << "GetFamilyMembers didn't return a HOH?!";
    ClearFirstCustodianPrefs();
  }
  if (!parent_found)
    ClearSecondCustodianPrefs();
}

void ChildAccountService::OnFailure(FamilyInfoFetcher::ErrorCode error) {
  DLOG(WARNING) << "GetFamilyMembers failed with code " << error;
  // TODO(treib): Retry after a while?
}

void ChildAccountService::StartFetchingServiceFlags(
    const std::string& account_id) {
  account_id_ = account_id;
}

void ChildAccountService::CancelFetchingServiceFlags() {
  flag_fetcher_.reset();
  account_id_.clear();
}

void ChildAccountService::OnFlagsFetched(
    AccountServiceFlagFetcher::ResultCode result,
    const std::vector<std::string>& flags) {
  // If we've been signed out again (or signed in to a different account),
  // ignore the fetched flags.
  const std::string& new_account_id =
      SigninManagerFactory::GetForProfile(profile_)
          ->GetAuthenticatedAccountId();
  if (account_id_.empty() || account_id_ != new_account_id)
    return;

  // In case of an error, retry after a while.
  if (result != AccountServiceFlagFetcher::SUCCESS) {
    DLOG(WARNING) << "AccountServiceFlagFetcher returned error code " << result;
    base::MessageLoop::current()->PostDelayedTask(
        FROM_HERE,
        base::Bind(&ChildAccountService::StartFetchingServiceFlags,
                   weak_ptr_factory_.GetWeakPtr(),
                   account_id_),
        base::TimeDelta::FromSeconds(10));
    return;
  }

  account_id_.clear();

  bool is_child_account =
      std::find(flags.begin(), flags.end(),
                kIsChildAccountServiceFlagName) != flags.end();
  SetIsChildAccount(is_child_account);
}

void ChildAccountService::PropagateChildStatusToUser(bool is_child) {
#if defined(OS_CHROMEOS)
  user_manager::User* user =
      chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
  if (user) {
    user_manager::UserManager::Get()->ChangeUserChildStatus(user, is_child);
  } else {
    LOG(WARNING) <<
        "User instance wasn't found while setting child account flag.";
  }
#endif
}

void ChildAccountService::SetFirstCustodianPrefs(
    const FamilyInfoFetcher::FamilyMember& custodian) {
  profile_->GetPrefs()->SetString(prefs::kSupervisedUserCustodianName,
                                  custodian.display_name);
  profile_->GetPrefs()->SetString(prefs::kSupervisedUserCustodianEmail,
                                  custodian.email);
  profile_->GetPrefs()->SetString(prefs::kSupervisedUserCustodianProfileURL,
                                  custodian.profile_url);
  profile_->GetPrefs()->SetString(
      prefs::kSupervisedUserCustodianProfileImageURL,
      custodian.profile_image_url);
}

void ChildAccountService::SetSecondCustodianPrefs(
    const FamilyInfoFetcher::FamilyMember& custodian) {
  profile_->GetPrefs()->SetString(prefs::kSupervisedUserSecondCustodianName,
                                  custodian.display_name);
  profile_->GetPrefs()->SetString(prefs::kSupervisedUserSecondCustodianEmail,
                                  custodian.email);
  profile_->GetPrefs()->SetString(
      prefs::kSupervisedUserSecondCustodianProfileURL,
      custodian.profile_url);
  profile_->GetPrefs()->SetString(
      prefs::kSupervisedUserSecondCustodianProfileImageURL,
      custodian.profile_image_url);
}

void ChildAccountService::ClearFirstCustodianPrefs() {
  profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianName);
  profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianEmail);
  profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianProfileURL);
  profile_->GetPrefs()->ClearPref(
      prefs::kSupervisedUserCustodianProfileImageURL);
}

void ChildAccountService::ClearSecondCustodianPrefs() {
  profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserSecondCustodianName);
  profile_->GetPrefs()->ClearPref(prefs::kSupervisedUserSecondCustodianEmail);
  profile_->GetPrefs()->ClearPref(
      prefs::kSupervisedUserSecondCustodianProfileURL);
  profile_->GetPrefs()->ClearPref(
      prefs::kSupervisedUserSecondCustodianProfileImageURL);
}

void ChildAccountService::EnableExperimentalFiltering() {
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

  // Static blacklist defaults to enabled.
  bool has_enable_blacklist =
      command_line->HasSwitch(switches::kEnableSupervisedUserBlacklist);
  bool has_disable_blacklist =
      command_line->HasSwitch(switches::kDisableSupervisedUserBlacklist);
  if (!has_enable_blacklist && !has_disable_blacklist)
    command_line->AppendSwitch(switches::kEnableSupervisedUserBlacklist);

  // Query-based filtering also defaults to enabled.
  bool has_enable_safesites =
      command_line->HasSwitch(switches::kEnableSupervisedUserSafeSites);
  bool has_disable_safesites =
      command_line->HasSwitch(switches::kDisableSupervisedUserSafeSites);
  if (!has_enable_safesites && !has_disable_safesites)
    command_line->AppendSwitch(switches::kEnableSupervisedUserSafeSites);
}