Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / profile_info_watcher.cc
blob18c9b9ef1ba36b6c1be719bfb3dfa0124a7fd75a
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"
7 #include "base/bind.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) {
21 DCHECK(profile_);
22 DCHECK(!callback_.is_null());
24 ProfileManager* profile_manager = g_browser_process->profile_manager();
25 // The profile_manager might be NULL in testing environments.
26 if (profile_manager)
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.
36 if (profile_manager)
37 profile_manager->GetProfileInfoCache().RemoveObserver(this);
40 void ProfileInfoWatcher::OnProfileAuthInfoChanged(
41 const base::FilePath& profile_path) {
42 RunCallback();
45 std::string ProfileInfoWatcher::GetAuthenticatedUsername() const {
46 std::string username;
47 SigninManagerBase* signin_manager = GetSigninManager();
48 if (signin_manager)
49 username = signin_manager->GetAuthenticatedAccountInfo().email;
50 return username;
53 SigninManagerBase* ProfileInfoWatcher::GetSigninManager() const {
54 return SigninManagerFactory::GetForProfile(profile_);
57 void ProfileInfoWatcher::RunCallback() {
58 if (GetSigninManager())
59 callback_.Run();