Home | History | Annotate | Download | only in contacts
      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)->contact_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::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;
     57 }
     58 
     59 void FakeContactStore::AddObserver(ContactStoreObserver* observer) {
     60   CHECK(observer);
     61   observers_.AddObserver(observer);
     62 }
     63 
     64 void FakeContactStore::RemoveObserver(ContactStoreObserver* observer) {
     65   CHECK(observer);
     66   observers_.RemoveObserver(observer);
     67 }
     68 
     69 FakeContactStoreFactory::FakeContactStoreFactory()
     70     : permit_store_creation_(true) {
     71 }
     72 
     73 FakeContactStoreFactory::~FakeContactStoreFactory() {
     74   CHECK(stores_.empty());
     75 }
     76 
     77 FakeContactStore* FakeContactStoreFactory::GetContactStoreForProfile(
     78     Profile* profile) {
     79   CHECK(profile);
     80   ProfileStoreMap::const_iterator it = stores_.find(profile);
     81   return it != stores_.end() ? it->second : NULL;
     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
    113