1 // Copyright (c) 2012 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/signin/signin_names_io_thread.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "components/signin/core/browser/signin_manager.h"
14 #include "content/public/browser/browser_thread.h"
16 SigninNamesOnIOThread::SigninNamesOnIOThread() : resources_released_(false) {
19 SigninManagerFactory::GetInstance()->AddObserver(this);
21 // Get list of profiles and record the email addresses of any connected
23 if (g_browser_process) {
24 ProfileManager* manager = g_browser_process->profile_manager();
26 const ProfileInfoCache& cache = manager->GetProfileInfoCache();
27 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
28 base::string16 email = cache.GetUserNameOfProfileAtIndex(i);
30 emails_.insert(email);
36 SigninNamesOnIOThread::~SigninNamesOnIOThread() {
38 DCHECK(resources_released_) << "Must call ReleaseResourcesOnUIThread() first";
39 DCHECK(!observed_managers_.size())
40 << "Shouldn't be observing any SigninManagers";
43 const SigninNamesOnIOThread::EmailSet&
44 SigninNamesOnIOThread::GetEmails() const {
49 void SigninNamesOnIOThread::ReleaseResourcesOnUIThread() {
51 DCHECK(!resources_released_);
52 SigninManagerFactory::GetInstance()->RemoveObserver(this);
54 for (std::set<SigninManagerBase*>::iterator i = observed_managers_.begin();
55 i != observed_managers_.end();
57 (*i)->RemoveObserver(this);
59 observed_managers_.clear();
61 resources_released_ = true;
64 void SigninNamesOnIOThread::SigninManagerCreated(SigninManagerBase* manager) {
65 manager->AddObserver(this);
66 observed_managers_.insert(manager);
69 void SigninNamesOnIOThread::SigninManagerShutdown(SigninManagerBase* manager) {
70 manager->RemoveObserver(this);
71 observed_managers_.erase(manager);
74 void SigninNamesOnIOThread::GoogleSigninSucceeded(const std::string& username,
75 const std::string& password) {
76 PostTaskToIOThread(true, base::UTF8ToUTF16(username));
79 void SigninNamesOnIOThread::GoogleSignedOut(const std::string& username) {
80 PostTaskToIOThread(false, base::UTF8ToUTF16(username));
83 void SigninNamesOnIOThread::CheckOnIOThread() const {
84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
87 void SigninNamesOnIOThread::CheckOnUIThread() const {
88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
91 void SigninNamesOnIOThread::PostTaskToIOThread(bool add,
92 const base::string16& email) {
93 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
94 UpdateOnIOThread(add, email);
96 bool may_run = content::BrowserThread::PostTask(
97 content::BrowserThread::IO,
99 base::Bind(&SigninNamesOnIOThread::UpdateOnIOThread,
100 base::Unretained(this),
107 void SigninNamesOnIOThread::UpdateOnIOThread(bool add,
108 const base::string16& email) {
111 emails_.insert(email);
113 emails_.erase(email);