| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" | 5 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 7 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 8 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | |
| 9 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 10 | 9 |
| 11 using content::BrowserThread; | 10 using content::BrowserThread; |
| 12 | 11 |
| 13 namespace contacts { | 12 namespace contacts { |
| 14 | 13 |
| 15 FakeContactDatabase::FakeContactDatabase() | 14 FakeContactDatabase::FakeContactDatabase() |
| 16 : init_success_(true), | 15 : init_success_(true), |
| 17 save_success_(true), | 16 save_success_(true), |
| 18 load_success_(true), | 17 load_success_(true), |
| 19 num_saved_contacts_(0) { | 18 num_saved_contacts_(0) { |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 21 } | 20 } |
| 22 | 21 |
| 23 void FakeContactDatabase::Init(const FilePath& database_dir, | 22 void FakeContactDatabase::Init(const FilePath& database_dir, |
| 24 InitCallback callback) { | 23 InitCallback callback) { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 26 callback.Run(init_success_); | 25 callback.Run(init_success_); |
| 27 } | 26 } |
| 28 | 27 |
| 29 void FakeContactDatabase::SetContacts(const ContactPointers& contacts, | 28 void FakeContactDatabase::SetContacts(const ContactPointers& contacts, |
| 30 const UpdateMetadata& metadata) { | 29 const UpdateMetadata& metadata) { |
| 31 test::CopyContacts(contacts, &contacts_); | 30 contacts_.Clear(); |
| 31 MergeContacts(contacts); |
| 32 metadata_ = metadata; | 32 metadata_ = metadata; |
| 33 } | 33 } |
| 34 | 34 |
| 35 void FakeContactDatabase::DestroyOnUIThread() { | 35 void FakeContactDatabase::DestroyOnUIThread() { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 37 delete this; | 37 delete this; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void FakeContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, | 40 void FakeContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 41 scoped_ptr<UpdateMetadata> metadata, | 41 scoped_ptr<UpdateMetadata> metadata, |
| 42 bool is_full_update, | 42 bool is_full_update, |
| 43 SaveCallback callback) { | 43 SaveCallback callback) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 45 if (save_success_) { | 45 if (save_success_) { |
| 46 num_saved_contacts_ += contacts->size(); | 46 num_saved_contacts_ += contacts->size(); |
| 47 if (is_full_update) | 47 if (is_full_update) |
| 48 test::CopyContacts(*contacts, &contacts_); | 48 contacts_.Clear(); |
| 49 else | 49 MergeContacts(*contacts); |
| 50 MergeContacts(*contacts); | |
| 51 metadata_ = *metadata; | 50 metadata_ = *metadata; |
| 52 } | 51 } |
| 53 callback.Run(save_success_); | 52 callback.Run(save_success_); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void FakeContactDatabase::LoadContacts(LoadCallback callback) { | 55 void FakeContactDatabase::LoadContacts(LoadCallback callback) { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); | 57 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); |
| 59 scoped_ptr<UpdateMetadata> metadata(new UpdateMetadata); | 58 scoped_ptr<UpdateMetadata> metadata(new UpdateMetadata); |
| 60 if (load_success_) { | 59 if (load_success_) { |
| 61 test::CopyContacts(contacts_, contacts.get()); | 60 for (ContactMap::const_iterator it = contacts_.begin(); |
| 61 it != contacts_.end(); ++it) { |
| 62 contacts->push_back(new Contact(*it->second)); |
| 63 } |
| 62 *metadata = metadata_; | 64 *metadata = metadata_; |
| 63 } | 65 } |
| 64 callback.Run(load_success_, contacts.Pass(), metadata.Pass()); | 66 callback.Run(load_success_, contacts.Pass(), metadata.Pass()); |
| 65 } | 67 } |
| 66 | 68 |
| 67 FakeContactDatabase::~FakeContactDatabase() { | 69 FakeContactDatabase::~FakeContactDatabase() { |
| 68 } | 70 } |
| 69 | 71 |
| 70 void FakeContactDatabase::MergeContacts( | 72 void FakeContactDatabase::MergeContacts( |
| 71 const ContactPointers& updated_contacts) { | 73 const ContactPointers& updated_contacts) { |
| 72 for (ContactPointers::const_iterator updated_it = updated_contacts.begin(); | 74 scoped_ptr<ScopedVector<Contact> > copied_contacts(new ScopedVector<Contact>); |
| 73 updated_it != updated_contacts.end(); ++updated_it) { | 75 for (size_t i = 0; i < updated_contacts.size(); ++i) |
| 74 const Contact& updated_contact = **updated_it; | 76 copied_contacts->push_back(new Contact(*updated_contacts[i])); |
| 75 bool found = false; | 77 contacts_.Merge(copied_contacts.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 76 for (ScopedVector<Contact>::const_iterator existing_it = contacts_.begin(); | |
| 77 existing_it != contacts_.end(); ++existing_it) { | |
| 78 Contact* existing_contact = *existing_it; | |
| 79 if (existing_contact->contact_id() == updated_contact.contact_id()) { | |
| 80 *existing_contact = updated_contact; | |
| 81 found = true; | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 if (!found) | |
| 86 contacts_.push_back(new Contact(updated_contact)); | |
| 87 } | |
| 88 } | 78 } |
| 89 | 79 |
| 90 } // namespace contacts | 80 } // namespace contacts |
| OLD | NEW |