Index: chrome/browser/chromeos/contacts/contact_database.cc |
diff --git a/chrome/browser/chromeos/contacts/contact_database.cc b/chrome/browser/chromeos/contacts/contact_database.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b89451121e8dbfe9dfca1464ff294ccdc07b0bc2 |
--- /dev/null |
+++ b/chrome/browser/chromeos/contacts/contact_database.cc |
@@ -0,0 +1,635 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/contacts/contact_database.h" |
+ |
+#include <map> |
+ |
+#include "chrome/browser/chromeos/contacts/contact.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "sql/meta_table.h" |
+#include "sql/statement.h" |
+#include "sql/transaction.h" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "ui/gfx/size.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace contacts { |
+ |
+namespace { |
+ |
+const int kCurrentVersion = 1; |
+const int kCompatibleVersion = 1; |
+ |
+} // namespace |
+ |
+ContactDatabase::ContactDatabase() |
+ : db_(new sql::Connection), |
+ meta_table_(new sql::MetaTable), |
+ condition_variable_(&lock_), |
+ num_outstanding_file_operations_(0) { |
+} |
+ |
+ContactDatabase::~ContactDatabase() { |
+ base::AutoLock auto_lock(lock_); |
+ while (num_outstanding_file_operations_ > 0) |
+ condition_variable_.Wait(); |
+} |
+ |
+void ContactDatabase::Init(const FilePath& database_path, |
+ InitCallback callback) { |
+ IncrementOutstandingFileOperations(); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&ContactDatabase::InitOnFileThread, base::Unretained(this), |
+ database_path, callback)); |
+} |
+ |
+void ContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, |
+ bool is_full_update, |
+ SaveCallback callback) { |
+ IncrementOutstandingFileOperations(); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&ContactDatabase::SaveContactsOnFileThread, |
+ base::Unretained(this), |
+ base::Passed(contacts.Pass()), is_full_update, callback)); |
+} |
+ |
+void ContactDatabase::LoadContacts(LoadCallback callback) { |
+ IncrementOutstandingFileOperations(); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&ContactDatabase::LoadContactsOnFileThread, |
+ base::Unretained(this), |
+ callback)); |
+} |
+ |
+void ContactDatabase::InitOnFileThread(const FilePath& database_path, |
+ InitCallback callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ bool success = (InitInternal(database_path) == sql::INIT_OK); |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, base::Bind(callback, success)); |
+ DecrementOutstandingFileOperations(); |
+} |
+ |
+sql::InitStatus ContactDatabase::InitInternal(const FilePath& database_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
+ VLOG(1) << "Opening " << database_path.value(); |
+ if (!db_->Open(database_path)) |
+ return sql::INIT_FAILURE; |
+ |
+ sql::Transaction transaction(db_.get()); |
+ if (!transaction.Begin()) |
+ return sql::INIT_FAILURE; |
+ |
+ VLOG(2) << "Initializing meta table"; |
+ if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) |
+ return sql::INIT_FAILURE; |
+ |
+ if (meta_table_->GetCompatibleVersionNumber() > kCurrentVersion) { |
+ LOG(ERROR) << "Database minimum compatible version is " |
+ << meta_table_->GetCompatibleVersionNumber() |
+ << ", but we are version " << kCurrentVersion; |
+ return sql::INIT_FAILURE; |
+ } |
+ |
+ VLOG(2) << "Creating tables"; |
+ if (!CreateTablesIfNeeded()) |
+ return sql::INIT_FAILURE; |
+ |
+ return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
+} |
+ |
+bool ContactDatabase::CreateTablesIfNeeded() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ if (!db_->DoesTableExist("contacts")) { |
+ if (!db_->Execute("CREATE TABLE contacts " |
+ "(contact_id INTEGER PRIMARY KEY, " |
+ "provider_id LONGVARCHAR NOT NULL UNIQUE, " |
+ "update_time INTEGER NOT NULL, " |
+ "deleted BOOLEAN NOT NULL, " |
+ "full_name VARCHAR NOT NULL, " |
+ "given_name VARCHAR NOT NULL, " |
+ "additional_name VARCHAR NOT NULL, " |
+ "family_name VARCHAR NOT NULL, " |
+ "name_prefix VARCHAR NOT NULL, " |
+ "name_suffix VARCHAR NOT NULL)") || |
+ !db_->Execute("CREATE INDEX provider_id ON contacts (provider_id)")) { |
+ return false; |
+ } |
+ } |
+ |
+ if (!db_->DoesTableExist("email_addresses")) { |
+ if (!db_->Execute("CREATE TABLE email_addresses " |
+ "(contact_id INTEGER NOT NULL, " |
+ "address VARCHAR NOT NULL, " |
+ "relation INTEGER NOT NULL, " |
+ "label VARCHAR NOT NULL, " |
+ "is_primary BOOLEAN NOT NULL)") || |
+ !db_->Execute("CREATE INDEX email_addresses_contact_id " |
+ "ON email_addresses (contact_id)")) { |
+ return false; |
+ } |
+ } |
+ |
+ if (!db_->DoesTableExist("phone_numbers")) { |
+ if (!db_->Execute("CREATE TABLE phone_numbers " |
+ "(contact_id INTEGER NOT NULL, " |
+ "number VARCHAR NOT NULL, " |
+ "relation INTEGER NOT NULL, " |
+ "label VARCHAR NOT NULL, " |
+ "is_primary BOOLEAN NOT NULL)") || |
+ !db_->Execute("CREATE INDEX phone_numbers_contact_id " |
+ "ON phone_numbers (contact_id)")) { |
+ return false; |
+ } |
+ } |
+ |
+ if (!db_->DoesTableExist("postal_addresses")) { |
+ if (!db_->Execute("CREATE TABLE postal_addresses " |
+ "(contact_id INTEGER NOT NULL, " |
+ "address VARCHAR NOT NULL, " |
+ "relation INTEGER NOT NULL, " |
+ "label VARCHAR NOT NULL, " |
+ "is_primary BOOLEAN NOT NULL)") || |
+ !db_->Execute("CREATE INDEX postal_addresses_contact_id " |
+ "ON postal_addresses (contact_id)")) { |
+ return false; |
+ } |
+ } |
+ |
+ if (!db_->DoesTableExist("instant_messaging_addresses")) { |
+ if (!db_->Execute("CREATE TABLE instant_messaging_addresses " |
+ "(contact_id INTEGER NOT NULL, " |
+ "address VARCHAR NOT NULL, " |
+ "protocol INTEGER NOT NULL, " |
+ "relation INTEGER NOT NULL, " |
+ "label VARCHAR NOT NULL, " |
+ "is_primary BOOLEAN NOT NULL)") || |
+ !db_->Execute("CREATE INDEX instant_messaging_addresses_contact_id " |
+ "ON instant_messaging_addresses (contact_id)")) { |
+ return false; |
+ } |
+ } |
+ |
+ if (!db_->DoesTableExist("photos")) { |
+ if (!db_->Execute("CREATE TABLE photos " |
+ "(contact_id INTEGER PRIMARY KEY, " |
+ "png_data BLOB NOT NULL)")) { |
+ return false; |
+ } |
+ } |
+ |
+ return true; |
+} |
+ |
+int64 ContactDatabase::GetContactId(const Contact& contact) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ sql::Statement statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id FROM contacts WHERE provider_id = ?")); |
+ statement.BindString(0, contact.provider_id); |
+ return statement.Step() ? statement.ColumnInt64(0) : -1; |
+} |
+ |
+void ContactDatabase::SaveContactsOnFileThread( |
+ scoped_ptr<ContactPointers> contacts, |
+ bool is_full_update, |
+ SaveCallback callback) { |
+ bool success = SaveContactsInternal(*(contacts.get()), is_full_update); |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(callback, success)); |
+ DecrementOutstandingFileOperations(); |
+} |
+ |
+bool ContactDatabase::SaveContactsInternal(const ContactPointers& contacts, |
+ bool is_full_update) { |
+ VLOG(1) << "Saving " << contacts.size() << " contact(s) to database as " |
+ << (is_full_update ? "full" : "partial") << " update"; |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ sql::Transaction transaction(db_.get()); |
+ if (!transaction.Begin()) |
+ return false; |
+ |
+ if (is_full_update) { |
+ if (!ClearContacts()) |
+ return false; |
+ } |
+ |
+ for (ContactPointers::const_iterator it = contacts.begin(); |
+ it != contacts.end(); ++it) { |
+ if (!SaveContact(**it, !is_full_update)) |
+ return false; |
+ } |
+ |
+ return transaction.Commit(); |
+} |
+ |
+bool ContactDatabase::ClearContacts() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ DCHECK_GE(db_->transaction_nesting(), 1) << "Not running within transaction"; |
+ |
+ sql::Statement clear_contacts_statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM contacts")); |
+ if (!clear_contacts_statement.Run()) |
+ return false; |
+ |
+ sql::Statement clear_email_statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM email_addresses")); |
+ if (!clear_email_statement.Run()) |
+ return false; |
+ |
+ sql::Statement clear_phone_statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM phone_numbers")); |
+ if (!clear_phone_statement.Run()) |
+ return false; |
+ |
+ sql::Statement clear_postal_statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM postal_addresses")); |
+ if (!clear_postal_statement.Run()) |
+ return false; |
+ |
+ sql::Statement clear_im_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, "DELETE FROM instant_messaging_addresses")); |
+ if (!clear_im_statement.Run()) |
+ return false; |
+ |
+ sql::Statement clear_photos_statement( |
+ db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM photos")); |
+ if (!clear_photos_statement.Run()) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+bool ContactDatabase::SaveContact(const Contact& contact, |
+ bool possibly_exists) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ DCHECK_GE(db_->transaction_nesting(), 1) << "Not running within transaction"; |
+ |
+ int64 contact_id = possibly_exists ? GetContactId(contact) : -1; |
+ if (contact_id == -1) { |
+ VLOG(2) << "Inserting contact with provider ID " << contact.provider_id; |
+ sql::Statement insert_contact_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO contacts " |
+ "(provider_id, update_time, deleted, full_name, " |
+ "given_name, additional_name, family_name, " |
+ "name_prefix, name_suffix) " |
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
+ insert_contact_statement.BindString(0, contact.provider_id); |
+ insert_contact_statement.BindInt64(1, contact.serialized_update_time()); |
+ insert_contact_statement.BindBool(2, contact.deleted); |
+ insert_contact_statement.BindString(3, contact.full_name); |
+ insert_contact_statement.BindString(4, contact.given_name); |
+ insert_contact_statement.BindString(5, contact.additional_name); |
+ insert_contact_statement.BindString(6, contact.family_name); |
+ insert_contact_statement.BindString(7, contact.name_prefix); |
+ insert_contact_statement.BindString(8, contact.name_suffix); |
+ if (!insert_contact_statement.Run()) |
+ return false; |
+ contact_id = db_->GetLastInsertRowId(); |
+ } else { |
+ VLOG(2) << "Updating contact " << contact_id |
+ << " with provider ID " << contact.provider_id; |
+ sql::Statement update_contact_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "UPDATE contacts " |
+ "SET update_time = ?, deleted = ?, full_name = ?, " |
+ "given_name = ?, additional_name = ?, family_name = ?, " |
+ "name_prefix = ?, name_suffix = ? WHERE contact_id = ?")); |
+ update_contact_statement.BindInt64(0, contact.serialized_update_time()); |
+ update_contact_statement.BindBool(1, contact.deleted); |
+ update_contact_statement.BindString(2, contact.full_name); |
+ update_contact_statement.BindString(3, contact.given_name); |
+ update_contact_statement.BindString(4, contact.additional_name); |
+ update_contact_statement.BindString(5, contact.family_name); |
+ update_contact_statement.BindString(6, contact.name_prefix); |
+ update_contact_statement.BindString(7, contact.name_suffix); |
+ update_contact_statement.BindInt64(8, contact_id); |
+ if (!update_contact_statement.Run()) |
+ return false; |
+ |
+ sql::Statement delete_email_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "DELETE FROM email_addresses WHERE contact_id = ?")); |
+ delete_email_statement.BindInt64(0, contact_id); |
+ if (!delete_email_statement.Run()) |
+ return false; |
+ |
+ sql::Statement delete_phone_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "DELETE FROM phone_numbers WHERE contact_id = ?")); |
+ delete_phone_statement.BindInt64(0, contact_id); |
+ if (!delete_phone_statement.Run()) |
+ return false; |
+ |
+ sql::Statement delete_postal_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "DELETE FROM postal_addresses WHERE contact_id = ?")); |
+ delete_postal_statement.BindInt64(0, contact_id); |
+ if (!delete_postal_statement.Run()) |
+ return false; |
+ |
+ sql::Statement delete_im_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "DELETE FROM instant_messaging_addresses WHERE contact_id = ?")); |
+ delete_im_statement.BindInt64(0, contact_id); |
+ if (!delete_im_statement.Run()) |
+ return false; |
+ |
+ sql::Statement delete_photo_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "DELETE FROM photos WHERE contact_id = ?")); |
+ delete_photo_statement.BindInt64(0, contact_id); |
+ if (!delete_photo_statement.Run()) |
+ return false; |
+ } |
+ |
+ for (size_t i = 0; i < contact.email_addresses.size(); ++i) { |
+ sql::Statement insert_email_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO email_addresses " |
+ "(contact_id, address, relation, label, is_primary) " |
+ "VALUES(?, ?, ?, ?, ?)")); |
+ const Contact::EmailAddress& email = contact.email_addresses[i]; |
+ insert_email_statement.BindInt64(0, contact_id); |
+ insert_email_statement.BindString(1, email.address); |
+ insert_email_statement.BindInt(2, email.type.relation); |
+ insert_email_statement.BindString(3, email.type.label); |
+ insert_email_statement.BindBool(4, email.primary); |
+ if (!insert_email_statement.Run()) |
+ return false; |
+ } |
+ |
+ for (size_t i = 0; i < contact.phone_numbers.size(); ++i) { |
+ sql::Statement insert_phone_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO phone_numbers " |
+ "(contact_id, number, relation, label, is_primary) " |
+ "VALUES(?, ?, ?, ?, ?)")); |
+ const Contact::PhoneNumber& phone = contact.phone_numbers[i]; |
+ insert_phone_statement.BindInt64(0, contact_id); |
+ insert_phone_statement.BindString(1, phone.number); |
+ insert_phone_statement.BindInt(2, phone.type.relation); |
+ insert_phone_statement.BindString(3, phone.type.label); |
+ insert_phone_statement.BindBool(4, phone.primary); |
+ if (!insert_phone_statement.Run()) |
+ return false; |
+ } |
+ |
+ for (size_t i = 0; i < contact.postal_addresses.size(); ++i) { |
+ sql::Statement insert_postal_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO postal_addresses " |
+ "(contact_id, address, relation, label, is_primary) " |
+ "VALUES(?, ?, ?, ?, ?)")); |
+ const Contact::PostalAddress& postal = contact.postal_addresses[i]; |
+ insert_postal_statement.BindInt64(0, contact_id); |
+ insert_postal_statement.BindString(1, postal.address); |
+ insert_postal_statement.BindInt(2, postal.type.relation); |
+ insert_postal_statement.BindString(3, postal.type.label); |
+ insert_postal_statement.BindBool(4, postal.primary); |
+ if (!insert_postal_statement.Run()) |
+ return false; |
+ } |
+ |
+ for (size_t i = 0; i < contact.instant_messaging_addresses.size(); ++i) { |
+ sql::Statement insert_im_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO instant_messaging_addresses " |
+ "(contact_id, address, protocol, relation, label, is_primary) " |
+ "VALUES(?, ?, ?, ?, ?, ?)")); |
+ const Contact::InstantMessagingAddress& im = |
+ contact.instant_messaging_addresses[i]; |
+ insert_im_statement.BindInt64(0, contact_id); |
+ insert_im_statement.BindString(1, im.address); |
+ insert_im_statement.BindInt(2, im.protocol); |
+ insert_im_statement.BindInt(3, im.type.relation); |
+ insert_im_statement.BindString(4, im.type.label); |
+ insert_im_statement.BindBool(5, im.primary); |
+ if (!insert_im_statement.Run()) |
+ return false; |
+ } |
+ |
+ if (!contact.photo.empty() && !contact.photo.isNull()) { |
+ std::vector<unsigned char> png_data; |
+ if (!gfx::PNGCodec::EncodeBGRASkBitmap(contact.photo, false, &png_data)) { |
+ LOG(ERROR) << "Failed to encode " << contact.photo.width() << "x" |
+ << contact.photo.height() << " photo for " |
+ << contact.provider_id; |
+ return false; |
+ } |
+ sql::Statement insert_photo_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "INSERT INTO photos (contact_id, png_data) VALUES(?, ?)")); |
+ insert_photo_statement.BindInt64(0, contact_id); |
+ insert_photo_statement.BindBlob(1, png_data.data(), png_data.size()); |
+ if (!insert_photo_statement.Run()) |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+void ContactDatabase::LoadContactsOnFileThread(LoadCallback callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); |
+ bool success = LoadContactsInternal(contacts.get()); |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(callback, success, base::Passed(contacts.Pass()))); |
+ DecrementOutstandingFileOperations(); |
+} |
+ |
+bool ContactDatabase::LoadContactsInternal(ScopedVector<Contact>* contacts) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ DCHECK(contacts); |
+ contacts->clear(); |
+ |
+ // Keyed by contact_id. |contacts| owns the values. |
+ typedef std::map<int64, Contact*> ContactMap; |
+ ContactMap contact_map; |
+ |
+ sql::Statement contacts_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, provider_id, update_time, deleted, " |
+ "full_name, given_name, additional_name, family_name, " |
+ "name_prefix, name_suffix FROM contacts")); |
+ while (contacts_statement.Step()) { |
+ int64 contact_id = contacts_statement.ColumnInt64(0); |
+ Contact* contact = new Contact; |
+ contacts->push_back(contact); |
+ contact->provider_id = contacts_statement.ColumnString(1); |
+ contact->set_serialized_update_time(contacts_statement.ColumnInt64(2)); |
+ contact->deleted = contacts_statement.ColumnBool(3); |
+ contact->full_name = contacts_statement.ColumnString(4); |
+ contact->given_name = contacts_statement.ColumnString(5); |
+ contact->additional_name = contacts_statement.ColumnString(6); |
+ contact->family_name = contacts_statement.ColumnString(7); |
+ contact->name_prefix = contacts_statement.ColumnString(8); |
+ contact->name_suffix = contacts_statement.ColumnString(9); |
+ contact_map.insert(std::make_pair(contact_id, contact)); |
+ } |
+ if (!contacts_statement.Succeeded()) |
+ return false; |
+ |
+ sql::Statement email_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, address, relation, label, is_primary " |
+ "FROM email_addresses")); |
+ while (email_statement.Step()) { |
+ int64 contact_id = email_statement.ColumnInt64(0); |
+ ContactMap::const_iterator it = contact_map.find(contact_id); |
+ if (it == contact_map.end()) { |
+ LOG(DFATAL) << "Got email address for missing contact " << contact_id; |
+ continue; |
+ } |
+ Contact::EmailAddress email; |
+ email.address = email_statement.ColumnString(1); |
+ email.type.relation = static_cast<Contact::AddressType::Relation>( |
+ email_statement.ColumnInt(2)); |
+ email.type.label = email_statement.ColumnString(3); |
+ email.primary = email_statement.ColumnBool(4); |
+ it->second->email_addresses.push_back(email); |
+ } |
+ if (!email_statement.Succeeded()) |
+ return false; |
+ |
+ sql::Statement phone_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, number, relation, label, is_primary " |
+ "FROM phone_numbers")); |
+ while (phone_statement.Step()) { |
+ int64 contact_id = phone_statement.ColumnInt64(0); |
+ ContactMap::const_iterator it = contact_map.find(contact_id); |
+ if (it == contact_map.end()) { |
+ LOG(DFATAL) << "Got phone number for missing contact " << contact_id; |
+ continue; |
+ } |
+ Contact::PhoneNumber phone; |
+ phone.number = phone_statement.ColumnString(1); |
+ phone.type.relation = static_cast<Contact::AddressType::Relation>( |
+ phone_statement.ColumnInt(2)); |
+ phone.type.label = phone_statement.ColumnString(3); |
+ phone.primary = phone_statement.ColumnBool(4); |
+ it->second->phone_numbers.push_back(phone); |
+ } |
+ if (!phone_statement.Succeeded()) |
+ return false; |
+ |
+ sql::Statement postal_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, address, relation, label, is_primary " |
+ "FROM postal_addresses")); |
+ while (postal_statement.Step()) { |
+ int64 contact_id = postal_statement.ColumnInt64(0); |
+ ContactMap::const_iterator it = contact_map.find(contact_id); |
+ if (it == contact_map.end()) { |
+ LOG(DFATAL) << "Got postal address for missing contact " << contact_id; |
+ continue; |
+ } |
+ Contact::PostalAddress postal; |
+ postal.address = postal_statement.ColumnString(1); |
+ postal.type.relation = static_cast<Contact::AddressType::Relation>( |
+ postal_statement.ColumnInt(2)); |
+ postal.type.label = postal_statement.ColumnString(3); |
+ postal.primary = postal_statement.ColumnBool(4); |
+ it->second->postal_addresses.push_back(postal); |
+ } |
+ if (!postal_statement.Succeeded()) |
+ return false; |
+ |
+ sql::Statement im_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, address, protocol, relation, label, is_primary " |
+ "FROM instant_messaging_addresses")); |
+ while (im_statement.Step()) { |
+ int64 contact_id = im_statement.ColumnInt64(0); |
+ ContactMap::const_iterator it = contact_map.find(contact_id); |
+ if (it == contact_map.end()) { |
+ LOG(DFATAL) << "Got IM address for missing contact " << contact_id; |
+ continue; |
+ } |
+ Contact::InstantMessagingAddress im; |
+ im.address = im_statement.ColumnString(1); |
+ im.protocol = static_cast<Contact::InstantMessagingAddress::Protocol>( |
+ im_statement.ColumnInt(2)); |
+ im.type.relation = static_cast<Contact::AddressType::Relation>( |
+ im_statement.ColumnInt(3)); |
+ im.type.label = im_statement.ColumnString(4); |
+ im.primary = im_statement.ColumnBool(5); |
+ it->second->instant_messaging_addresses.push_back(im); |
+ } |
+ if (!im_statement.Succeeded()) |
+ return false; |
+ |
+ sql::Statement photo_statement( |
+ db_->GetCachedStatement( |
+ SQL_FROM_HERE, |
+ "SELECT contact_id, png_data FROM photos")); |
+ while (photo_statement.Step()) { |
+ int64 contact_id = photo_statement.ColumnInt64(0); |
+ ContactMap::const_iterator it = contact_map.find(contact_id); |
+ if (it == contact_map.end()) { |
+ LOG(DFATAL) << "Got photo for missing contact " << contact_id; |
+ continue; |
+ } |
+ int png_data_size = photo_statement.ColumnByteLength(1); |
+ const unsigned char* png_data = |
+ reinterpret_cast<const unsigned char*>(photo_statement.ColumnBlob(1)); |
+ if (png_data_size > 0 && png_data) { |
+ if (!gfx::PNGCodec::Decode(png_data, png_data_size, |
+ &(it->second->photo))) { |
+ LOG(ERROR) << "Unable to decode " << png_data_size << "-byte photo for " |
+ << "contact " << contact_id; |
+ } else { |
+ VLOG(2) << "Loaded " << it->second->photo.width() << "x" |
+ << it->second->photo.height() << " photo for contact " |
+ << contact_id; |
+ } |
+ } |
+ } |
+ if (!photo_statement.Succeeded()) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+void ContactDatabase::IncrementOutstandingFileOperations() { |
+ base::AutoLock auto_lock(lock_); |
+ num_outstanding_file_operations_++; |
+} |
+ |
+void ContactDatabase::DecrementOutstandingFileOperations() { |
+ base::AutoLock auto_lock(lock_); |
+ DCHECK_GT(num_outstanding_file_operations_, 0); |
+ num_outstanding_file_operations_--; |
+ condition_variable_.Signal(); |
+} |
+ |
+} // namespace contacts |