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/fake_contact_store.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 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" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace contacts { |
| 17 |
| 18 FakeContactStore::FakeContactStore(FakeContactStoreFactory* factory) |
| 19 : factory_(factory), |
| 20 contacts_deleter_(&contacts_) { |
| 21 } |
| 22 |
| 23 FakeContactStore::~FakeContactStore() { |
| 24 factory_->RemoveStore(this); |
| 25 } |
| 26 |
| 27 void FakeContactStore::SetContacts(const ContactPointers& contacts) { |
| 28 STLDeleteValues(&contacts_); |
| 29 contacts_.clear(); |
| 30 for (ContactPointers::const_iterator it = contacts.begin(); |
| 31 it != contacts.end(); ++it) { |
| 32 contacts_[(*it)->provider_id()] = new Contact(**it); |
| 33 } |
| 34 } |
| 35 |
| 36 void FakeContactStore::NotifyObserversAboutContactsUpdate() { |
| 37 FOR_EACH_OBSERVER(ContactStoreObserver, |
| 38 observers_, |
| 39 OnContactsUpdated(this)); |
| 40 } |
| 41 |
| 42 void FakeContactStore::Init() { |
| 43 } |
| 44 |
| 45 void FakeContactStore::AppendContacts(ContactPointers* contacts_out) { |
| 46 CHECK(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); |
| 51 } |
| 52 } |
| 53 |
| 54 const Contact* FakeContactStore::GetContactByProviderId( |
| 55 const std::string& provider_id) { |
| 56 ContactMap::const_iterator it = contacts_.find(provider_id); |
| 57 return (it != contacts_.end() && !it->second->deleted()) ? it->second : NULL; |
| 58 } |
| 59 |
| 60 void FakeContactStore::AddObserver(ContactStoreObserver* observer) { |
| 61 CHECK(observer); |
| 62 observers_.AddObserver(observer); |
| 63 } |
| 64 |
| 65 void FakeContactStore::RemoveObserver(ContactStoreObserver* observer) { |
| 66 CHECK(observer); |
| 67 observers_.RemoveObserver(observer); |
| 68 } |
| 69 |
| 70 FakeContactStoreFactory::FakeContactStoreFactory() |
| 71 : permit_store_creation_(true) { |
| 72 } |
| 73 |
| 74 FakeContactStoreFactory::~FakeContactStoreFactory() { |
| 75 CHECK(stores_.empty()); |
| 76 } |
| 77 |
| 78 FakeContactStore* FakeContactStoreFactory::GetContactStoreForProfile( |
| 79 Profile* profile) { |
| 80 CHECK(profile); |
| 81 return stores_[profile]; |
| 82 } |
| 83 |
| 84 void FakeContactStoreFactory::RemoveStore(FakeContactStore* store) { |
| 85 CHECK(store); |
| 86 for (ProfileStoreMap::iterator it = stores_.begin(); |
| 87 it != stores_.end(); ++it) { |
| 88 if (it->second == store) { |
| 89 stores_.erase(it); |
| 90 return; |
| 91 } |
| 92 } |
| 93 NOTREACHED() << "No record of destroyed FakeContactStore " << store; |
| 94 } |
| 95 |
| 96 bool FakeContactStoreFactory::CanCreateContactStoreForProfile( |
| 97 Profile* profile) { |
| 98 CHECK(profile); |
| 99 return permit_store_creation_; |
| 100 } |
| 101 |
| 102 ContactStore* FakeContactStoreFactory::CreateContactStore(Profile* profile) { |
| 103 CHECK(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() << ")"; |
| 109 return store; |
| 110 } |
| 111 |
| 112 } // namespace contacts |
OLD | NEW |