OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/contacts/contact_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/chromeos/contacts/contact.h" |
| 9 #include "chrome/browser/chromeos/contacts/contact_manager_observer.h" |
| 10 #include "chrome/browser/chromeos/contacts/contact_source.h" |
| 11 #include "chrome/browser/chromeos/contacts/google_contact_source.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/notification_service.h" |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 namespace contacts { |
| 20 |
| 21 ContactManager* ContactManager::instance_ = NULL; |
| 22 |
| 23 // static |
| 24 ContactManager* ContactManager::GetInstance() { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 26 DCHECK(instance_); |
| 27 return instance_; |
| 28 } |
| 29 |
| 30 ContactManager::ContactManager() |
| 31 : google_contact_sources_deleter_(&google_contact_sources_) { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 DCHECK(!instance_); |
| 34 instance_ = this; |
| 35 } |
| 36 |
| 37 ContactManager::~ContactManager() { |
| 38 // This should also be running on the UI thread but we can't check it; the |
| 39 // message loop is already getting torn down at this point. |
| 40 DCHECK_EQ(instance_, this); |
| 41 instance_ = NULL; |
| 42 } |
| 43 |
| 44 void ContactManager::Init() { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 46 registrar_.Add( |
| 47 this, |
| 48 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 49 content::NotificationService::AllSources()); |
| 50 registrar_.Add( |
| 51 this, |
| 52 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 53 content::NotificationService::AllSources()); |
| 54 // FIXME: Handle startup in already-logged-in state (e.g. Chrome restarts). |
| 55 } |
| 56 |
| 57 void ContactManager::AddObserver(ContactManagerObserver* observer) { |
| 58 DCHECK(observer); |
| 59 observers_.AddObserver(observer); |
| 60 } |
| 61 |
| 62 void ContactManager::RemoveObserver(ContactManagerObserver* observer) { |
| 63 DCHECK(observer); |
| 64 observers_.RemoveObserver(observer); |
| 65 } |
| 66 |
| 67 void ContactManager::GetAllContacts(ContactPointers* contacts) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 69 DCHECK(contacts); |
| 70 contacts->clear(); |
| 71 for (GoogleContactSourceMap::const_iterator it = |
| 72 google_contact_sources_.begin(); |
| 73 it != google_contact_sources_.end(); ++it) { |
| 74 it->second->AppendContacts(contacts); |
| 75 } |
| 76 } |
| 77 |
| 78 const Contact* ContactManager::GetContactByProviderId( |
| 79 const std::string& provider_id) { |
| 80 for (GoogleContactSourceMap::const_iterator it = |
| 81 google_contact_sources_.begin(); |
| 82 it != google_contact_sources_.end(); ++it) { |
| 83 const Contact* contact = it->second->GetContactByProviderId(provider_id); |
| 84 if (contact) |
| 85 return contact; |
| 86 } |
| 87 return NULL; |
| 88 } |
| 89 |
| 90 void ContactManager::OnContactsUpdated(ContactSource* source) { |
| 91 FOR_EACH_OBSERVER(ContactManagerObserver, |
| 92 observers_, |
| 93 OnContactsUpdated(this)); |
| 94 } |
| 95 |
| 96 void ContactManager::Observe(int type, |
| 97 const content::NotificationSource& source, |
| 98 const content::NotificationDetails& details) { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 100 switch (type) { |
| 101 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
| 102 Profile* profile = content::Source<Profile>(source).ptr(); |
| 103 DCHECK(profile); |
| 104 VLOG(1) << "Signin successful for " << profile->GetProfileName(); |
| 105 GoogleContactSourceMap::iterator it = |
| 106 google_contact_sources_.find(profile); |
| 107 if (it == google_contact_sources_.end()) { |
| 108 GoogleContactSource* contact_source = new GoogleContactSource(profile); |
| 109 contact_source->AddObserver(this); |
| 110 contact_source->Init(); |
| 111 google_contact_sources_.insert(std::make_pair(profile, contact_source)); |
| 112 } |
| 113 break; |
| 114 } |
| 115 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 116 Profile* profile = content::Details<Profile>(details).ptr(); |
| 117 if (profile) { |
| 118 GoogleContactSourceMap::iterator it = |
| 119 google_contact_sources_.find(profile); |
| 120 if (it != google_contact_sources_.end()) { |
| 121 delete it->second; |
| 122 google_contact_sources_.erase(it); |
| 123 } |
| 124 } |
| 125 break; |
| 126 } |
| 127 default: |
| 128 NOTREACHED() << "Unexpected notification " << type; |
| 129 } |
| 130 } |
| 131 |
| 132 } // namespace contacts |
OLD | NEW |