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/chromeos/contacts/fake_contact_store.h"
9 #include "chrome/browser/chromeos/contacts/contact.pb.h"
10 #include "chrome/browser/chromeos/contacts/contact_store_observer.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/browser_thread.h"
14 using content::BrowserThread
;
18 FakeContactStore::FakeContactStore(FakeContactStoreFactory
* factory
)
20 contacts_deleter_(&contacts_
) {
23 FakeContactStore::~FakeContactStore() {
24 factory_
->RemoveStore(this);
27 void FakeContactStore::SetContacts(const ContactPointers
& contacts
) {
28 STLDeleteValues(&contacts_
);
30 for (ContactPointers::const_iterator it
= contacts
.begin();
31 it
!= contacts
.end(); ++it
) {
32 contacts_
[(*it
)->contact_id()] = new Contact(**it
);
36 void FakeContactStore::NotifyObserversAboutContactsUpdate() {
37 FOR_EACH_OBSERVER(ContactStoreObserver
,
39 OnContactsUpdated(this));
42 void FakeContactStore::Init() {
45 void FakeContactStore::AppendContacts(ContactPointers
* contacts_out
) {
47 for (ContactMap::const_iterator it
= contacts_
.begin();
48 it
!= contacts_
.end(); ++it
) {
49 if (!it
->second
->deleted())
50 contacts_out
->push_back(it
->second
);
54 const Contact
* FakeContactStore::GetContactById(const std::string
& contact_id
) {
55 ContactMap::const_iterator it
= contacts_
.find(contact_id
);
56 return (it
!= contacts_
.end() && !it
->second
->deleted()) ? it
->second
: NULL
;
59 void FakeContactStore::AddObserver(ContactStoreObserver
* observer
) {
61 observers_
.AddObserver(observer
);
64 void FakeContactStore::RemoveObserver(ContactStoreObserver
* observer
) {
66 observers_
.RemoveObserver(observer
);
69 FakeContactStoreFactory::FakeContactStoreFactory()
70 : permit_store_creation_(true) {
73 FakeContactStoreFactory::~FakeContactStoreFactory() {
74 CHECK(stores_
.empty());
77 FakeContactStore
* FakeContactStoreFactory::GetContactStoreForProfile(
80 ProfileStoreMap::const_iterator it
= stores_
.find(profile
);
81 return it
!= stores_
.end() ? it
->second
: NULL
;
84 void FakeContactStoreFactory::RemoveStore(FakeContactStore
* store
) {
86 for (ProfileStoreMap::iterator it
= stores_
.begin();
87 it
!= stores_
.end(); ++it
) {
88 if (it
->second
== store
) {
93 NOTREACHED() << "No record of destroyed FakeContactStore " << store
;
96 bool FakeContactStoreFactory::CanCreateContactStoreForProfile(
99 return permit_store_creation_
;
102 ContactStore
* FakeContactStoreFactory::CreateContactStore(Profile
* profile
) {
104 CHECK(CanCreateContactStoreForProfile(profile
));
105 FakeContactStore
* store
= new FakeContactStore(this);
106 CHECK(stores_
.insert(std::make_pair(profile
, store
)).second
)
107 << "Got request to create second FakeContactStore for profile "
108 << profile
<< " (" << profile
->GetProfileName() << ")";
112 } // namespace contacts