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_database.h"
      6 
      7 #include "chrome/browser/chromeos/contacts/contact.pb.h"
      8 #include "content/public/browser/browser_thread.h"
      9 
     10 using content::BrowserThread;
     11 
     12 namespace contacts {
     13 
     14 FakeContactDatabase::FakeContactDatabase()
     15     : init_success_(true),
     16       save_success_(true),
     17       load_success_(true),
     18       num_saved_contacts_(0) {
     19   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     20 }
     21 
     22 void FakeContactDatabase::Init(const base::FilePath& database_dir,
     23                                InitCallback callback) {
     24   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     25   callback.Run(init_success_);
     26 }
     27 
     28 void FakeContactDatabase::SetContacts(const ContactPointers& contacts,
     29                                       const UpdateMetadata& metadata) {
     30   contacts_.Clear();
     31   MergeContacts(contacts, ContactIds());
     32   metadata_ = metadata;
     33 }
     34 
     35 void FakeContactDatabase::DestroyOnUIThread() {
     36   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     37   delete this;
     38 }
     39 
     40 void FakeContactDatabase::SaveContacts(
     41     scoped_ptr<ContactPointers> contacts_to_save,
     42     scoped_ptr<ContactIds> contact_ids_to_delete,
     43     scoped_ptr<UpdateMetadata> metadata,
     44     bool is_full_update,
     45     SaveCallback callback) {
     46   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     47   if (save_success_) {
     48     num_saved_contacts_ += contacts_to_save->size();
     49     if (is_full_update)
     50       contacts_.Clear();
     51     MergeContacts(*contacts_to_save, *contact_ids_to_delete);
     52     metadata_ = *metadata;
     53   }
     54   callback.Run(save_success_);
     55 }
     56 
     57 void FakeContactDatabase::LoadContacts(LoadCallback callback) {
     58   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     59   scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>());
     60   scoped_ptr<UpdateMetadata> metadata(new UpdateMetadata);
     61   if (load_success_) {
     62     for (ContactMap::const_iterator it = contacts_.begin();
     63          it != contacts_.end(); ++it) {
     64       contacts->push_back(new Contact(*it->second));
     65     }
     66     *metadata = metadata_;
     67   }
     68   callback.Run(load_success_, contacts.Pass(), metadata.Pass());
     69 }
     70 
     71 FakeContactDatabase::~FakeContactDatabase() {
     72 }
     73 
     74 void FakeContactDatabase::MergeContacts(
     75     const ContactPointers& updated_contacts,
     76     const ContactIds& contact_ids_to_delete) {
     77   scoped_ptr<ScopedVector<Contact> > copied_contacts(new ScopedVector<Contact>);
     78   for (size_t i = 0; i < updated_contacts.size(); ++i)
     79     copied_contacts->push_back(new Contact(*updated_contacts[i]));
     80   contacts_.Merge(copied_contacts.Pass(), ContactMap::KEEP_DELETED_CONTACTS);
     81   for (ContactIds::const_iterator it = contact_ids_to_delete.begin();
     82        it != contact_ids_to_delete.end(); ++it) {
     83     contacts_.Erase(*it);
     84   }
     85 }
     86 
     87 }  // namespace contacts
     88