Index: chrome/browser/chromeos/contacts/contact_manager.cc |
diff --git a/chrome/browser/chromeos/contacts/contact_manager.cc b/chrome/browser/chromeos/contacts/contact_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b663bf7e81e87b747ec3f33f7655077ce6d6be1c |
--- /dev/null |
+++ b/chrome/browser/chromeos/contacts/contact_manager.cc |
@@ -0,0 +1,132 @@ |
+// Copyright (c) 2012 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/chromeos/contacts/contact_manager.h" |
+ |
+#include "base/logging.h" |
+#include "chrome/browser/chromeos/contacts/contact.h" |
+#include "chrome/browser/chromeos/contacts/contact_manager_observer.h" |
+#include "chrome/browser/chromeos/contacts/contact_source.h" |
+#include "chrome/browser/chromeos/contacts/google_contact_source.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/chrome_notification_types.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/notification_service.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace contacts { |
+ |
+ContactManager* ContactManager::instance_ = NULL; |
+ |
+// static |
+ContactManager* ContactManager::GetInstance() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(instance_); |
+ return instance_; |
+} |
+ |
+ContactManager::ContactManager() |
+ : google_contact_sources_deleter_(&google_contact_sources_) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(!instance_); |
+ instance_ = this; |
+} |
+ |
+ContactManager::~ContactManager() { |
+ // This should also be running on the UI thread but we can't check it; the |
+ // message loop is already getting torn down at this point. |
+ DCHECK_EQ(instance_, this); |
+ instance_ = NULL; |
+} |
+ |
+void ContactManager::Init() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ registrar_.Add( |
+ this, |
+ chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
+ content::NotificationService::AllSources()); |
+ registrar_.Add( |
+ this, |
+ chrome::NOTIFICATION_PROFILE_DESTROYED, |
+ content::NotificationService::AllSources()); |
+ // FIXME: Handle startup in already-logged-in state (e.g. Chrome restarts). |
+} |
+ |
+void ContactManager::AddObserver(ContactManagerObserver* observer) { |
+ DCHECK(observer); |
+ observers_.AddObserver(observer); |
+} |
+ |
+void ContactManager::RemoveObserver(ContactManagerObserver* observer) { |
+ DCHECK(observer); |
+ observers_.RemoveObserver(observer); |
+} |
+ |
+void ContactManager::GetAllContacts(ContactPointers* contacts) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(contacts); |
+ contacts->clear(); |
+ for (GoogleContactSourceMap::const_iterator it = |
+ google_contact_sources_.begin(); |
+ it != google_contact_sources_.end(); ++it) { |
+ it->second->AppendContacts(contacts); |
+ } |
+} |
+ |
+const Contact* ContactManager::GetContactByProviderId( |
+ const std::string& provider_id) { |
+ for (GoogleContactSourceMap::const_iterator it = |
+ google_contact_sources_.begin(); |
+ it != google_contact_sources_.end(); ++it) { |
+ const Contact* contact = it->second->GetContactByProviderId(provider_id); |
+ if (contact) |
+ return contact; |
+ } |
+ return NULL; |
+} |
+ |
+void ContactManager::OnContactsUpdated(ContactSource* source) { |
+ FOR_EACH_OBSERVER(ContactManagerObserver, |
+ observers_, |
+ OnContactsUpdated(this)); |
+} |
+ |
+void ContactManager::Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ switch (type) { |
+ case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
+ Profile* profile = content::Source<Profile>(source).ptr(); |
+ DCHECK(profile); |
+ VLOG(1) << "Signin successful for " << profile->GetProfileName(); |
+ GoogleContactSourceMap::iterator it = |
+ google_contact_sources_.find(profile); |
+ if (it == google_contact_sources_.end()) { |
+ GoogleContactSource* contact_source = new GoogleContactSource(profile); |
+ contact_source->AddObserver(this); |
+ contact_source->Init(); |
+ google_contact_sources_.insert(std::make_pair(profile, contact_source)); |
+ } |
+ break; |
+ } |
+ case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
+ Profile* profile = content::Details<Profile>(details).ptr(); |
+ if (profile) { |
+ GoogleContactSourceMap::iterator it = |
+ google_contact_sources_.find(profile); |
+ if (it != google_contact_sources_.end()) { |
+ delete it->second; |
+ google_contact_sources_.erase(it); |
+ } |
+ } |
+ break; |
+ } |
+ default: |
+ NOTREACHED() << "Unexpected notification " << type; |
+ } |
+} |
+ |
+} // namespace contacts |