Chromium Code Reviews| 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/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
| 10 #include "chrome/browser/chromeos/contacts/contact_manager_observer.h" | |
| 11 #include "chrome/browser/chromeos/contacts/contact_store.h" | |
| 12 #include "chrome/browser/chromeos/contacts/google_contact_store.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/common/chrome_notification_types.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace contacts { | |
| 22 | |
| 23 ContactManager* ContactManager::instance_ = NULL; | |
| 24 | |
| 25 // static | |
| 26 ContactManager* ContactManager::GetInstance() { | |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 28 DCHECK(instance_); | |
| 29 return instance_; | |
| 30 } | |
| 31 | |
| 32 ContactManager::ContactManager() | |
| 33 : profile_observers_deleter_(&profile_observers_), | |
| 34 contact_store_factory_(new GoogleContactStoreFactory), | |
| 35 contact_stores_deleter_(&contact_stores_) { | |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 37 DCHECK(!instance_); | |
| 38 instance_ = this; | |
| 39 } | |
| 40 | |
| 41 ContactManager::~ContactManager() { | |
| 42 // This should also be running on the UI thread but we can't check it; the | |
| 43 // message loop is already getting torn down at this point. | |
|
Daniel Erat
2012/08/03 22:12:07
I think that I can avoid this by just destroying t
| |
| 44 DCHECK_EQ(instance_, this); | |
| 45 instance_ = NULL; | |
| 46 } | |
| 47 | |
| 48 void ContactManager::set_contact_store_factory_for_testing( | |
| 49 scoped_ptr<ContactStoreFactory> factory) { | |
| 50 DCHECK(contact_stores_.empty()); | |
| 51 contact_store_factory_.swap(factory); | |
| 52 } | |
| 53 | |
| 54 void ContactManager::Init() { | |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 56 registrar_.Add( | |
| 57 this, | |
| 58 chrome::NOTIFICATION_PROFILE_CREATED, | |
| 59 content::NotificationService::AllSources()); | |
| 60 registrar_.Add( | |
| 61 this, | |
| 62 chrome::NOTIFICATION_PROFILE_DESTROYED, | |
| 63 content::NotificationService::AllSources()); | |
| 64 | |
| 65 // Notify about any already-existing profiles. | |
| 66 std::vector<Profile*> profiles( | |
| 67 g_browser_process->profile_manager()->GetLoadedProfiles()); | |
| 68 for (size_t i = 0; i < profiles.size(); ++i) | |
| 69 HandleProfileCreated(profiles[i]); | |
| 70 } | |
| 71 | |
| 72 void ContactManager::AddObserver(ContactManagerObserver* observer, | |
| 73 Profile* profile) { | |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 75 DCHECK(observer); | |
| 76 DCHECK(profile); | |
| 77 Observers* observers = GetObserversForProfile(profile, true); | |
| 78 observers->AddObserver(observer); | |
| 79 } | |
| 80 | |
| 81 void ContactManager::RemoveObserver(ContactManagerObserver* observer, | |
| 82 Profile* profile) { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 84 DCHECK(observer); | |
| 85 DCHECK(profile); | |
| 86 Observers* observers = GetObserversForProfile(profile, false); | |
| 87 if (observers) | |
| 88 observers->RemoveObserver(observer); | |
| 89 } | |
| 90 | |
| 91 scoped_ptr<ContactPointers> ContactManager::GetAllContacts(Profile* profile) { | |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 93 DCHECK(profile); | |
| 94 scoped_ptr<ContactPointers> contacts(new ContactPointers); | |
| 95 ContactStoreMap::const_iterator it = contact_stores_.find(profile); | |
| 96 if (it != contact_stores_.end()) | |
| 97 it->second->AppendContacts(contacts.get()); | |
| 98 return contacts.Pass(); | |
| 99 } | |
| 100 | |
| 101 const Contact* ContactManager::GetContactByProviderId( | |
| 102 Profile* profile, | |
| 103 const std::string& provider_id) { | |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 105 DCHECK(profile); | |
| 106 ContactStoreMap::const_iterator it = contact_stores_.find(profile); | |
| 107 return it != contact_stores_.end() ? | |
| 108 it->second->GetContactByProviderId(provider_id) : | |
| 109 NULL; | |
| 110 } | |
| 111 | |
| 112 void ContactManager::OnContactsUpdated(ContactStore* store) { | |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 114 for (ContactStoreMap::const_iterator it = contact_stores_.begin(); | |
| 115 it != contact_stores_.end(); ++it) { | |
| 116 if (it->second == store) { | |
| 117 Profile* profile = it->first; | |
| 118 Observers* observers = GetObserversForProfile(profile, false); | |
| 119 if (observers) { | |
| 120 FOR_EACH_OBSERVER(ContactManagerObserver, | |
| 121 *observers, | |
| 122 OnContactsUpdated(this, profile)); | |
| 123 } | |
| 124 return; | |
| 125 } | |
| 126 } | |
| 127 NOTREACHED() << "Got update from unknown contact store " << store; | |
| 128 } | |
| 129 | |
| 130 void ContactManager::Observe(int type, | |
| 131 const content::NotificationSource& source, | |
| 132 const content::NotificationDetails& details) { | |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 134 switch (type) { | |
| 135 case chrome::NOTIFICATION_PROFILE_CREATED: | |
| 136 HandleProfileCreated(content::Source<Profile>(source).ptr()); | |
| 137 break; | |
| 138 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | |
| 139 Profile* profile = content::Details<Profile>(details).ptr(); | |
| 140 if (profile) | |
| 141 HandleProfileDestroyed(profile); | |
| 142 break; | |
| 143 } | |
| 144 default: | |
| 145 NOTREACHED() << "Unexpected notification " << type; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 ContactManager::Observers* ContactManager::GetObserversForProfile( | |
| 150 Profile* profile, | |
| 151 bool create) { | |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 153 ProfileObserversMap::const_iterator it = profile_observers_.find(profile); | |
| 154 if (it != profile_observers_.end()) | |
| 155 return it->second; | |
| 156 if (!create) | |
| 157 return NULL; | |
| 158 | |
| 159 Observers* observers = new Observers; | |
| 160 profile_observers_[profile] = observers; | |
| 161 return observers; | |
| 162 } | |
| 163 | |
| 164 void ContactManager::HandleProfileCreated(Profile* profile) { | |
| 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 166 DCHECK(profile); | |
| 167 | |
| 168 ContactStoreMap::iterator it = contact_stores_.find(profile); | |
| 169 if (it != contact_stores_.end()) | |
| 170 return; | |
| 171 | |
| 172 if (!contact_store_factory_->CanCreateContactStoreForProfile(profile)) | |
| 173 return; | |
| 174 | |
| 175 VLOG(1) << "Adding profile " << profile->GetProfileName(); | |
| 176 ContactStore* store = contact_store_factory_->CreateContactStore(profile); | |
| 177 DCHECK(store); | |
| 178 store->AddObserver(this); | |
|
satorux1
2012/08/03 21:16:22
I don't see store->RemoveObserver(this); Usually t
Daniel Erat
2012/08/03 22:12:07
Heh, went back and forth on this. Updated.
| |
| 179 store->Init(); | |
| 180 DCHECK_EQ(contact_stores_.count(profile), static_cast<size_t>(0)); | |
| 181 contact_stores_[profile] = store; | |
| 182 } | |
| 183 | |
| 184 void ContactManager::HandleProfileDestroyed(Profile* profile) { | |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 186 DCHECK(profile); | |
| 187 | |
| 188 ContactStoreMap::iterator store_it = contact_stores_.find(profile); | |
| 189 if (store_it != contact_stores_.end()) { | |
| 190 delete store_it->second; | |
| 191 contact_stores_.erase(store_it); | |
| 192 } | |
| 193 | |
| 194 ProfileObserversMap::iterator observer_it = profile_observers_.find(profile); | |
| 195 if (observer_it != profile_observers_.end()) { | |
| 196 delete observer_it->second; | |
| 197 profile_observers_.erase(observer_it); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 } // namespace contacts | |
| OLD | NEW |