1 // Copyright 2015 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 #include "chrome/browser/ui/webui/profile_info_watcher.h"
8 #include "base/bind_helpers.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/signin/core/browser/signin_manager.h"
18 ProfileInfoWatcher::ProfileInfoWatcher(
19 Profile
* profile
, const base::Closure
& callback
)
20 : profile_(profile
), callback_(callback
) {
22 DCHECK(!callback_
.is_null());
24 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
25 // The profile_manager might be NULL in testing environments.
27 profile_manager
->GetProfileInfoCache().AddObserver(this);
29 signin_allowed_pref_
.Init(prefs::kSigninAllowed
, profile_
->GetPrefs(),
30 base::Bind(&ProfileInfoWatcher::RunCallback
, base::Unretained(this)));
33 ProfileInfoWatcher::~ProfileInfoWatcher() {
34 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
35 // The profile_manager might be NULL in testing environments.
37 profile_manager
->GetProfileInfoCache().RemoveObserver(this);
40 void ProfileInfoWatcher::OnProfileAuthInfoChanged(
41 const base::FilePath
& profile_path
) {
45 std::string
ProfileInfoWatcher::GetAuthenticatedUsername() const {
47 SigninManagerBase
* signin_manager
= GetSigninManager();
49 username
= signin_manager
->GetAuthenticatedAccountInfo().email
;
53 SigninManagerBase
* ProfileInfoWatcher::GetSigninManager() const {
54 return SigninManagerFactory::GetForProfile(profile_
);
57 void ProfileInfoWatcher::RunCallback() {
58 if (GetSigninManager())