Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: chrome/browser/chromeos/contacts/contact_manager.cc

Issue 10831162: contacts: Add ContactManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: apply review feedback and merge Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 namespace {
24
25 // Singleton instance.
26 ContactManager* instance = NULL;
satorux1 2012/08/03 22:18:28 please add g_ or global_ prefix per the style guid
Daniel Erat 2012/08/03 22:32:02 Sure (wasn't sure if this applies to variables in
27
28 } // namespace
29
30 // static
31 ContactManager* ContactManager::GetInstance() {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
33 DCHECK(instance);
34 return instance;
35 }
36
37 ContactManager::ContactManager()
38 : profile_observers_deleter_(&profile_observers_),
39 contact_store_factory_(new GoogleContactStoreFactory),
40 contact_stores_deleter_(&contact_stores_) {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
42 DCHECK(!instance);
43 instance = this;
44 }
45
46 ContactManager::~ContactManager() {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48 DCHECK_EQ(instance, this);
49 instance = NULL;
50 for (ContactStoreMap::const_iterator it = contact_stores_.begin();
51 it != contact_stores_.end(); ++it) {
52 it->second->RemoveObserver(this);
53 }
54 }
55
56 void ContactManager::SetContactStoreForTesting(
57 scoped_ptr<ContactStoreFactory> factory) {
58 DCHECK(contact_stores_.empty());
59 contact_store_factory_.swap(factory);
60 }
61
62 void ContactManager::Init() {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
64 registrar_.Add(
65 this,
66 chrome::NOTIFICATION_PROFILE_CREATED,
67 content::NotificationService::AllSources());
68 registrar_.Add(
69 this,
70 chrome::NOTIFICATION_PROFILE_DESTROYED,
71 content::NotificationService::AllSources());
72
73 // Notify about any already-existing profiles.
74 std::vector<Profile*> profiles(
75 g_browser_process->profile_manager()->GetLoadedProfiles());
76 for (size_t i = 0; i < profiles.size(); ++i)
77 HandleProfileCreated(profiles[i]);
78 }
79
80 void ContactManager::AddObserver(ContactManagerObserver* observer,
81 Profile* profile) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 DCHECK(observer);
84 DCHECK(profile);
85 Observers* observers = GetObserversForProfile(profile, true);
86 observers->AddObserver(observer);
87 }
88
89 void ContactManager::RemoveObserver(ContactManagerObserver* observer,
90 Profile* profile) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 DCHECK(observer);
93 DCHECK(profile);
94 Observers* observers = GetObserversForProfile(profile, false);
95 if (observers)
96 observers->RemoveObserver(observer);
97 }
98
99 scoped_ptr<ContactPointers> ContactManager::GetAllContacts(Profile* profile) {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 DCHECK(profile);
102 scoped_ptr<ContactPointers> contacts(new ContactPointers);
103 ContactStoreMap::const_iterator it = contact_stores_.find(profile);
104 if (it != contact_stores_.end())
105 it->second->AppendContacts(contacts.get());
106 return contacts.Pass();
107 }
108
109 const Contact* ContactManager::GetContactByProviderId(
110 Profile* profile,
111 const std::string& provider_id) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 DCHECK(profile);
114 ContactStoreMap::const_iterator it = contact_stores_.find(profile);
115 return it != contact_stores_.end() ?
116 it->second->GetContactByProviderId(provider_id) :
117 NULL;
118 }
119
120 void ContactManager::OnContactsUpdated(ContactStore* store) {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122 for (ContactStoreMap::const_iterator it = contact_stores_.begin();
123 it != contact_stores_.end(); ++it) {
124 if (it->second == store) {
125 Profile* profile = it->first;
126 Observers* observers = GetObserversForProfile(profile, false);
127 if (observers) {
128 FOR_EACH_OBSERVER(ContactManagerObserver,
129 *observers,
130 OnContactsUpdated(profile));
131 }
132 return;
133 }
134 }
135 NOTREACHED() << "Got update from unknown contact store " << store;
136 }
137
138 void ContactManager::Observe(int type,
139 const content::NotificationSource& source,
140 const content::NotificationDetails& details) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 switch (type) {
143 case chrome::NOTIFICATION_PROFILE_CREATED:
144 HandleProfileCreated(content::Source<Profile>(source).ptr());
145 break;
146 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
147 Profile* profile = content::Details<Profile>(details).ptr();
148 if (profile)
149 HandleProfileDestroyed(profile);
150 break;
151 }
152 default:
153 NOTREACHED() << "Unexpected notification " << type;
154 }
155 }
156
157 ContactManager::Observers* ContactManager::GetObserversForProfile(
158 Profile* profile,
159 bool create) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
161 ProfileObserversMap::const_iterator it = profile_observers_.find(profile);
162 if (it != profile_observers_.end())
163 return it->second;
164 if (!create)
165 return NULL;
166
167 Observers* observers = new Observers;
168 profile_observers_[profile] = observers;
169 return observers;
170 }
171
172 void ContactManager::HandleProfileCreated(Profile* profile) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
174 DCHECK(profile);
175
176 ContactStoreMap::iterator it = contact_stores_.find(profile);
177 if (it != contact_stores_.end())
178 return;
179
180 if (!contact_store_factory_->CanCreateContactStoreForProfile(profile))
181 return;
182
183 VLOG(1) << "Adding profile " << profile->GetProfileName();
184 ContactStore* store = contact_store_factory_->CreateContactStore(profile);
185 DCHECK(store);
186 store->AddObserver(this);
187 store->Init();
188 DCHECK_EQ(contact_stores_.count(profile), static_cast<size_t>(0));
189 contact_stores_[profile] = store;
190 }
191
192 void ContactManager::HandleProfileDestroyed(Profile* profile) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 DCHECK(profile);
195
196 ContactStoreMap::iterator store_it = contact_stores_.find(profile);
197 if (store_it != contact_stores_.end()) {
198 store_it->second->RemoveObserver(this);
199 delete store_it->second;
200 contact_stores_.erase(store_it);
201 }
202
203 ProfileObserversMap::iterator observer_it = profile_observers_.find(profile);
204 if (observer_it != profile_observers_.end()) {
205 delete observer_it->second;
206 profile_observers_.erase(observer_it);
207 }
208 }
209
210 } // namespace contacts
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/contacts/contact_manager.h ('k') | chrome/browser/chromeos/contacts/contact_manager_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698