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/contact_database.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "chrome/browser/chromeos/contacts/contact.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "sql/meta_table.h" |
| 12 #include "sql/statement.h" |
| 13 #include "sql/transaction.h" |
| 14 #include "ui/gfx/codec/png_codec.h" |
| 15 #include "ui/gfx/size.h" |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 namespace contacts { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const int kCurrentVersion = 1; |
| 24 const int kCompatibleVersion = 1; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 ContactDatabase::ContactDatabase() |
| 29 : db_(new sql::Connection), |
| 30 meta_table_(new sql::MetaTable), |
| 31 condition_variable_(&lock_), |
| 32 num_outstanding_file_operations_(0) { |
| 33 } |
| 34 |
| 35 ContactDatabase::~ContactDatabase() { |
| 36 base::AutoLock auto_lock(lock_); |
| 37 while (num_outstanding_file_operations_ > 0) |
| 38 condition_variable_.Wait(); |
| 39 } |
| 40 |
| 41 void ContactDatabase::Init(const FilePath& database_path, |
| 42 InitCallback callback) { |
| 43 IncrementOutstandingFileOperations(); |
| 44 BrowserThread::PostTask( |
| 45 BrowserThread::FILE, FROM_HERE, |
| 46 base::Bind(&ContactDatabase::InitOnFileThread, base::Unretained(this), |
| 47 database_path, callback)); |
| 48 } |
| 49 |
| 50 void ContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 51 bool is_full_update, |
| 52 SaveCallback callback) { |
| 53 IncrementOutstandingFileOperations(); |
| 54 BrowserThread::PostTask( |
| 55 BrowserThread::FILE, FROM_HERE, |
| 56 base::Bind(&ContactDatabase::SaveContactsOnFileThread, |
| 57 base::Unretained(this), |
| 58 base::Passed(contacts.Pass()), is_full_update, callback)); |
| 59 } |
| 60 |
| 61 void ContactDatabase::LoadContacts(LoadCallback callback) { |
| 62 IncrementOutstandingFileOperations(); |
| 63 BrowserThread::PostTask( |
| 64 BrowserThread::FILE, FROM_HERE, |
| 65 base::Bind(&ContactDatabase::LoadContactsOnFileThread, |
| 66 base::Unretained(this), |
| 67 callback)); |
| 68 } |
| 69 |
| 70 void ContactDatabase::InitOnFileThread(const FilePath& database_path, |
| 71 InitCallback callback) { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 73 bool success = (InitInternal(database_path) == sql::INIT_OK); |
| 74 BrowserThread::PostTask( |
| 75 BrowserThread::UI, FROM_HERE, base::Bind(callback, success)); |
| 76 DecrementOutstandingFileOperations(); |
| 77 } |
| 78 |
| 79 sql::InitStatus ContactDatabase::InitInternal(const FilePath& database_path) { |
| 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 81 |
| 82 VLOG(1) << "Opening " << database_path.value(); |
| 83 if (!db_->Open(database_path)) |
| 84 return sql::INIT_FAILURE; |
| 85 |
| 86 sql::Transaction transaction(db_.get()); |
| 87 if (!transaction.Begin()) |
| 88 return sql::INIT_FAILURE; |
| 89 |
| 90 VLOG(2) << "Initializing meta table"; |
| 91 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) |
| 92 return sql::INIT_FAILURE; |
| 93 |
| 94 if (meta_table_->GetCompatibleVersionNumber() > kCurrentVersion) { |
| 95 LOG(ERROR) << "Database minimum compatible version is " |
| 96 << meta_table_->GetCompatibleVersionNumber() |
| 97 << ", but we are version " << kCurrentVersion; |
| 98 return sql::INIT_FAILURE; |
| 99 } |
| 100 |
| 101 VLOG(2) << "Creating tables"; |
| 102 if (!CreateTablesIfNeeded()) |
| 103 return sql::INIT_FAILURE; |
| 104 |
| 105 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
| 106 } |
| 107 |
| 108 bool ContactDatabase::CreateTablesIfNeeded() { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 110 if (!db_->DoesTableExist("contacts")) { |
| 111 if (!db_->Execute("CREATE TABLE contacts " |
| 112 "(contact_id INTEGER PRIMARY KEY, " |
| 113 "provider_id LONGVARCHAR NOT NULL UNIQUE, " |
| 114 "update_time INTEGER NOT NULL, " |
| 115 "deleted BOOLEAN NOT NULL, " |
| 116 "full_name VARCHAR NOT NULL, " |
| 117 "given_name VARCHAR NOT NULL, " |
| 118 "additional_name VARCHAR NOT NULL, " |
| 119 "family_name VARCHAR NOT NULL, " |
| 120 "name_prefix VARCHAR NOT NULL, " |
| 121 "name_suffix VARCHAR NOT NULL)") || |
| 122 !db_->Execute("CREATE INDEX provider_id ON contacts (provider_id)")) { |
| 123 return false; |
| 124 } |
| 125 } |
| 126 |
| 127 if (!db_->DoesTableExist("email_addresses")) { |
| 128 if (!db_->Execute("CREATE TABLE email_addresses " |
| 129 "(contact_id INTEGER NOT NULL, " |
| 130 "address VARCHAR NOT NULL, " |
| 131 "relation INTEGER NOT NULL, " |
| 132 "label VARCHAR NOT NULL, " |
| 133 "is_primary BOOLEAN NOT NULL)") || |
| 134 !db_->Execute("CREATE INDEX email_addresses_contact_id " |
| 135 "ON email_addresses (contact_id)")) { |
| 136 return false; |
| 137 } |
| 138 } |
| 139 |
| 140 if (!db_->DoesTableExist("phone_numbers")) { |
| 141 if (!db_->Execute("CREATE TABLE phone_numbers " |
| 142 "(contact_id INTEGER NOT NULL, " |
| 143 "number VARCHAR NOT NULL, " |
| 144 "relation INTEGER NOT NULL, " |
| 145 "label VARCHAR NOT NULL, " |
| 146 "is_primary BOOLEAN NOT NULL)") || |
| 147 !db_->Execute("CREATE INDEX phone_numbers_contact_id " |
| 148 "ON phone_numbers (contact_id)")) { |
| 149 return false; |
| 150 } |
| 151 } |
| 152 |
| 153 if (!db_->DoesTableExist("postal_addresses")) { |
| 154 if (!db_->Execute("CREATE TABLE postal_addresses " |
| 155 "(contact_id INTEGER NOT NULL, " |
| 156 "address VARCHAR NOT NULL, " |
| 157 "relation INTEGER NOT NULL, " |
| 158 "label VARCHAR NOT NULL, " |
| 159 "is_primary BOOLEAN NOT NULL)") || |
| 160 !db_->Execute("CREATE INDEX postal_addresses_contact_id " |
| 161 "ON postal_addresses (contact_id)")) { |
| 162 return false; |
| 163 } |
| 164 } |
| 165 |
| 166 if (!db_->DoesTableExist("instant_messaging_addresses")) { |
| 167 if (!db_->Execute("CREATE TABLE instant_messaging_addresses " |
| 168 "(contact_id INTEGER NOT NULL, " |
| 169 "address VARCHAR NOT NULL, " |
| 170 "protocol INTEGER NOT NULL, " |
| 171 "relation INTEGER NOT NULL, " |
| 172 "label VARCHAR NOT NULL, " |
| 173 "is_primary BOOLEAN NOT NULL)") || |
| 174 !db_->Execute("CREATE INDEX instant_messaging_addresses_contact_id " |
| 175 "ON instant_messaging_addresses (contact_id)")) { |
| 176 return false; |
| 177 } |
| 178 } |
| 179 |
| 180 if (!db_->DoesTableExist("photos")) { |
| 181 if (!db_->Execute("CREATE TABLE photos " |
| 182 "(contact_id INTEGER PRIMARY KEY, " |
| 183 "png_data BLOB NOT NULL)")) { |
| 184 return false; |
| 185 } |
| 186 } |
| 187 |
| 188 return true; |
| 189 } |
| 190 |
| 191 int64 ContactDatabase::GetContactId(const Contact& contact) { |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 193 sql::Statement statement( |
| 194 db_->GetCachedStatement( |
| 195 SQL_FROM_HERE, |
| 196 "SELECT contact_id FROM contacts WHERE provider_id = ?")); |
| 197 statement.BindString(0, contact.provider_id); |
| 198 return statement.Step() ? statement.ColumnInt64(0) : -1; |
| 199 } |
| 200 |
| 201 void ContactDatabase::SaveContactsOnFileThread( |
| 202 scoped_ptr<ContactPointers> contacts, |
| 203 bool is_full_update, |
| 204 SaveCallback callback) { |
| 205 bool success = SaveContactsInternal(*(contacts.get()), is_full_update); |
| 206 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 207 base::Bind(callback, success)); |
| 208 DecrementOutstandingFileOperations(); |
| 209 } |
| 210 |
| 211 bool ContactDatabase::SaveContactsInternal(const ContactPointers& contacts, |
| 212 bool is_full_update) { |
| 213 VLOG(1) << "Saving " << contacts.size() << " contact(s) to database as " |
| 214 << (is_full_update ? "full" : "partial") << " update"; |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 216 sql::Transaction transaction(db_.get()); |
| 217 if (!transaction.Begin()) |
| 218 return false; |
| 219 |
| 220 if (is_full_update) { |
| 221 if (!ClearContacts()) |
| 222 return false; |
| 223 } |
| 224 |
| 225 for (ContactPointers::const_iterator it = contacts.begin(); |
| 226 it != contacts.end(); ++it) { |
| 227 if (!SaveContact(**it, !is_full_update)) |
| 228 return false; |
| 229 } |
| 230 |
| 231 return transaction.Commit(); |
| 232 } |
| 233 |
| 234 bool ContactDatabase::ClearContacts() { |
| 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 236 DCHECK_GE(db_->transaction_nesting(), 1) << "Not running within transaction"; |
| 237 |
| 238 sql::Statement clear_contacts_statement( |
| 239 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM contacts")); |
| 240 if (!clear_contacts_statement.Run()) |
| 241 return false; |
| 242 |
| 243 sql::Statement clear_email_statement( |
| 244 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM email_addresses")); |
| 245 if (!clear_email_statement.Run()) |
| 246 return false; |
| 247 |
| 248 sql::Statement clear_phone_statement( |
| 249 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM phone_numbers")); |
| 250 if (!clear_phone_statement.Run()) |
| 251 return false; |
| 252 |
| 253 sql::Statement clear_postal_statement( |
| 254 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM postal_addresses")); |
| 255 if (!clear_postal_statement.Run()) |
| 256 return false; |
| 257 |
| 258 sql::Statement clear_im_statement( |
| 259 db_->GetCachedStatement( |
| 260 SQL_FROM_HERE, "DELETE FROM instant_messaging_addresses")); |
| 261 if (!clear_im_statement.Run()) |
| 262 return false; |
| 263 |
| 264 sql::Statement clear_photos_statement( |
| 265 db_->GetCachedStatement(SQL_FROM_HERE, "DELETE FROM photos")); |
| 266 if (!clear_photos_statement.Run()) |
| 267 return false; |
| 268 |
| 269 return true; |
| 270 } |
| 271 |
| 272 bool ContactDatabase::SaveContact(const Contact& contact, |
| 273 bool possibly_exists) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 275 DCHECK_GE(db_->transaction_nesting(), 1) << "Not running within transaction"; |
| 276 |
| 277 int64 contact_id = possibly_exists ? GetContactId(contact) : -1; |
| 278 if (contact_id == -1) { |
| 279 VLOG(2) << "Inserting contact with provider ID " << contact.provider_id; |
| 280 sql::Statement insert_contact_statement( |
| 281 db_->GetCachedStatement( |
| 282 SQL_FROM_HERE, |
| 283 "INSERT INTO contacts " |
| 284 "(provider_id, update_time, deleted, full_name, " |
| 285 "given_name, additional_name, family_name, " |
| 286 "name_prefix, name_suffix) " |
| 287 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 288 insert_contact_statement.BindString(0, contact.provider_id); |
| 289 insert_contact_statement.BindInt64(1, contact.serialized_update_time()); |
| 290 insert_contact_statement.BindBool(2, contact.deleted); |
| 291 insert_contact_statement.BindString(3, contact.full_name); |
| 292 insert_contact_statement.BindString(4, contact.given_name); |
| 293 insert_contact_statement.BindString(5, contact.additional_name); |
| 294 insert_contact_statement.BindString(6, contact.family_name); |
| 295 insert_contact_statement.BindString(7, contact.name_prefix); |
| 296 insert_contact_statement.BindString(8, contact.name_suffix); |
| 297 if (!insert_contact_statement.Run()) |
| 298 return false; |
| 299 contact_id = db_->GetLastInsertRowId(); |
| 300 } else { |
| 301 VLOG(2) << "Updating contact " << contact_id |
| 302 << " with provider ID " << contact.provider_id; |
| 303 sql::Statement update_contact_statement( |
| 304 db_->GetCachedStatement( |
| 305 SQL_FROM_HERE, |
| 306 "UPDATE contacts " |
| 307 "SET update_time = ?, deleted = ?, full_name = ?, " |
| 308 "given_name = ?, additional_name = ?, family_name = ?, " |
| 309 "name_prefix = ?, name_suffix = ? WHERE contact_id = ?")); |
| 310 update_contact_statement.BindInt64(0, contact.serialized_update_time()); |
| 311 update_contact_statement.BindBool(1, contact.deleted); |
| 312 update_contact_statement.BindString(2, contact.full_name); |
| 313 update_contact_statement.BindString(3, contact.given_name); |
| 314 update_contact_statement.BindString(4, contact.additional_name); |
| 315 update_contact_statement.BindString(5, contact.family_name); |
| 316 update_contact_statement.BindString(6, contact.name_prefix); |
| 317 update_contact_statement.BindString(7, contact.name_suffix); |
| 318 update_contact_statement.BindInt64(8, contact_id); |
| 319 if (!update_contact_statement.Run()) |
| 320 return false; |
| 321 |
| 322 sql::Statement delete_email_statement( |
| 323 db_->GetCachedStatement( |
| 324 SQL_FROM_HERE, |
| 325 "DELETE FROM email_addresses WHERE contact_id = ?")); |
| 326 delete_email_statement.BindInt64(0, contact_id); |
| 327 if (!delete_email_statement.Run()) |
| 328 return false; |
| 329 |
| 330 sql::Statement delete_phone_statement( |
| 331 db_->GetCachedStatement( |
| 332 SQL_FROM_HERE, |
| 333 "DELETE FROM phone_numbers WHERE contact_id = ?")); |
| 334 delete_phone_statement.BindInt64(0, contact_id); |
| 335 if (!delete_phone_statement.Run()) |
| 336 return false; |
| 337 |
| 338 sql::Statement delete_postal_statement( |
| 339 db_->GetCachedStatement( |
| 340 SQL_FROM_HERE, |
| 341 "DELETE FROM postal_addresses WHERE contact_id = ?")); |
| 342 delete_postal_statement.BindInt64(0, contact_id); |
| 343 if (!delete_postal_statement.Run()) |
| 344 return false; |
| 345 |
| 346 sql::Statement delete_im_statement( |
| 347 db_->GetCachedStatement( |
| 348 SQL_FROM_HERE, |
| 349 "DELETE FROM instant_messaging_addresses WHERE contact_id = ?")); |
| 350 delete_im_statement.BindInt64(0, contact_id); |
| 351 if (!delete_im_statement.Run()) |
| 352 return false; |
| 353 |
| 354 sql::Statement delete_photo_statement( |
| 355 db_->GetCachedStatement( |
| 356 SQL_FROM_HERE, |
| 357 "DELETE FROM photos WHERE contact_id = ?")); |
| 358 delete_photo_statement.BindInt64(0, contact_id); |
| 359 if (!delete_photo_statement.Run()) |
| 360 return false; |
| 361 } |
| 362 |
| 363 for (size_t i = 0; i < contact.email_addresses.size(); ++i) { |
| 364 sql::Statement insert_email_statement( |
| 365 db_->GetCachedStatement( |
| 366 SQL_FROM_HERE, |
| 367 "INSERT INTO email_addresses " |
| 368 "(contact_id, address, relation, label, is_primary) " |
| 369 "VALUES(?, ?, ?, ?, ?)")); |
| 370 const Contact::EmailAddress& email = contact.email_addresses[i]; |
| 371 insert_email_statement.BindInt64(0, contact_id); |
| 372 insert_email_statement.BindString(1, email.address); |
| 373 insert_email_statement.BindInt(2, email.type.relation); |
| 374 insert_email_statement.BindString(3, email.type.label); |
| 375 insert_email_statement.BindBool(4, email.primary); |
| 376 if (!insert_email_statement.Run()) |
| 377 return false; |
| 378 } |
| 379 |
| 380 for (size_t i = 0; i < contact.phone_numbers.size(); ++i) { |
| 381 sql::Statement insert_phone_statement( |
| 382 db_->GetCachedStatement( |
| 383 SQL_FROM_HERE, |
| 384 "INSERT INTO phone_numbers " |
| 385 "(contact_id, number, relation, label, is_primary) " |
| 386 "VALUES(?, ?, ?, ?, ?)")); |
| 387 const Contact::PhoneNumber& phone = contact.phone_numbers[i]; |
| 388 insert_phone_statement.BindInt64(0, contact_id); |
| 389 insert_phone_statement.BindString(1, phone.number); |
| 390 insert_phone_statement.BindInt(2, phone.type.relation); |
| 391 insert_phone_statement.BindString(3, phone.type.label); |
| 392 insert_phone_statement.BindBool(4, phone.primary); |
| 393 if (!insert_phone_statement.Run()) |
| 394 return false; |
| 395 } |
| 396 |
| 397 for (size_t i = 0; i < contact.postal_addresses.size(); ++i) { |
| 398 sql::Statement insert_postal_statement( |
| 399 db_->GetCachedStatement( |
| 400 SQL_FROM_HERE, |
| 401 "INSERT INTO postal_addresses " |
| 402 "(contact_id, address, relation, label, is_primary) " |
| 403 "VALUES(?, ?, ?, ?, ?)")); |
| 404 const Contact::PostalAddress& postal = contact.postal_addresses[i]; |
| 405 insert_postal_statement.BindInt64(0, contact_id); |
| 406 insert_postal_statement.BindString(1, postal.address); |
| 407 insert_postal_statement.BindInt(2, postal.type.relation); |
| 408 insert_postal_statement.BindString(3, postal.type.label); |
| 409 insert_postal_statement.BindBool(4, postal.primary); |
| 410 if (!insert_postal_statement.Run()) |
| 411 return false; |
| 412 } |
| 413 |
| 414 for (size_t i = 0; i < contact.instant_messaging_addresses.size(); ++i) { |
| 415 sql::Statement insert_im_statement( |
| 416 db_->GetCachedStatement( |
| 417 SQL_FROM_HERE, |
| 418 "INSERT INTO instant_messaging_addresses " |
| 419 "(contact_id, address, protocol, relation, label, is_primary) " |
| 420 "VALUES(?, ?, ?, ?, ?, ?)")); |
| 421 const Contact::InstantMessagingAddress& im = |
| 422 contact.instant_messaging_addresses[i]; |
| 423 insert_im_statement.BindInt64(0, contact_id); |
| 424 insert_im_statement.BindString(1, im.address); |
| 425 insert_im_statement.BindInt(2, im.protocol); |
| 426 insert_im_statement.BindInt(3, im.type.relation); |
| 427 insert_im_statement.BindString(4, im.type.label); |
| 428 insert_im_statement.BindBool(5, im.primary); |
| 429 if (!insert_im_statement.Run()) |
| 430 return false; |
| 431 } |
| 432 |
| 433 if (!contact.photo.empty() && !contact.photo.isNull()) { |
| 434 std::vector<unsigned char> png_data; |
| 435 if (!gfx::PNGCodec::EncodeBGRASkBitmap(contact.photo, false, &png_data)) { |
| 436 LOG(ERROR) << "Failed to encode " << contact.photo.width() << "x" |
| 437 << contact.photo.height() << " photo for " |
| 438 << contact.provider_id; |
| 439 return false; |
| 440 } |
| 441 sql::Statement insert_photo_statement( |
| 442 db_->GetCachedStatement( |
| 443 SQL_FROM_HERE, |
| 444 "INSERT INTO photos (contact_id, png_data) VALUES(?, ?)")); |
| 445 insert_photo_statement.BindInt64(0, contact_id); |
| 446 insert_photo_statement.BindBlob(1, png_data.data(), png_data.size()); |
| 447 if (!insert_photo_statement.Run()) |
| 448 return false; |
| 449 } |
| 450 |
| 451 return true; |
| 452 } |
| 453 |
| 454 void ContactDatabase::LoadContactsOnFileThread(LoadCallback callback) { |
| 455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 456 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>()); |
| 457 bool success = LoadContactsInternal(contacts.get()); |
| 458 BrowserThread::PostTask( |
| 459 BrowserThread::UI, FROM_HERE, |
| 460 base::Bind(callback, success, base::Passed(contacts.Pass()))); |
| 461 DecrementOutstandingFileOperations(); |
| 462 } |
| 463 |
| 464 bool ContactDatabase::LoadContactsInternal(ScopedVector<Contact>* contacts) { |
| 465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 466 DCHECK(contacts); |
| 467 contacts->clear(); |
| 468 |
| 469 // Keyed by contact_id. |contacts| owns the values. |
| 470 typedef std::map<int64, Contact*> ContactMap; |
| 471 ContactMap contact_map; |
| 472 |
| 473 sql::Statement contacts_statement( |
| 474 db_->GetCachedStatement( |
| 475 SQL_FROM_HERE, |
| 476 "SELECT contact_id, provider_id, update_time, deleted, " |
| 477 "full_name, given_name, additional_name, family_name, " |
| 478 "name_prefix, name_suffix FROM contacts")); |
| 479 while (contacts_statement.Step()) { |
| 480 int64 contact_id = contacts_statement.ColumnInt64(0); |
| 481 Contact* contact = new Contact; |
| 482 contacts->push_back(contact); |
| 483 contact->provider_id = contacts_statement.ColumnString(1); |
| 484 contact->set_serialized_update_time(contacts_statement.ColumnInt64(2)); |
| 485 contact->deleted = contacts_statement.ColumnBool(3); |
| 486 contact->full_name = contacts_statement.ColumnString(4); |
| 487 contact->given_name = contacts_statement.ColumnString(5); |
| 488 contact->additional_name = contacts_statement.ColumnString(6); |
| 489 contact->family_name = contacts_statement.ColumnString(7); |
| 490 contact->name_prefix = contacts_statement.ColumnString(8); |
| 491 contact->name_suffix = contacts_statement.ColumnString(9); |
| 492 contact_map.insert(std::make_pair(contact_id, contact)); |
| 493 } |
| 494 if (!contacts_statement.Succeeded()) |
| 495 return false; |
| 496 |
| 497 sql::Statement email_statement( |
| 498 db_->GetCachedStatement( |
| 499 SQL_FROM_HERE, |
| 500 "SELECT contact_id, address, relation, label, is_primary " |
| 501 "FROM email_addresses")); |
| 502 while (email_statement.Step()) { |
| 503 int64 contact_id = email_statement.ColumnInt64(0); |
| 504 ContactMap::const_iterator it = contact_map.find(contact_id); |
| 505 if (it == contact_map.end()) { |
| 506 LOG(DFATAL) << "Got email address for missing contact " << contact_id; |
| 507 continue; |
| 508 } |
| 509 Contact::EmailAddress email; |
| 510 email.address = email_statement.ColumnString(1); |
| 511 email.type.relation = static_cast<Contact::AddressType::Relation>( |
| 512 email_statement.ColumnInt(2)); |
| 513 email.type.label = email_statement.ColumnString(3); |
| 514 email.primary = email_statement.ColumnBool(4); |
| 515 it->second->email_addresses.push_back(email); |
| 516 } |
| 517 if (!email_statement.Succeeded()) |
| 518 return false; |
| 519 |
| 520 sql::Statement phone_statement( |
| 521 db_->GetCachedStatement( |
| 522 SQL_FROM_HERE, |
| 523 "SELECT contact_id, number, relation, label, is_primary " |
| 524 "FROM phone_numbers")); |
| 525 while (phone_statement.Step()) { |
| 526 int64 contact_id = phone_statement.ColumnInt64(0); |
| 527 ContactMap::const_iterator it = contact_map.find(contact_id); |
| 528 if (it == contact_map.end()) { |
| 529 LOG(DFATAL) << "Got phone number for missing contact " << contact_id; |
| 530 continue; |
| 531 } |
| 532 Contact::PhoneNumber phone; |
| 533 phone.number = phone_statement.ColumnString(1); |
| 534 phone.type.relation = static_cast<Contact::AddressType::Relation>( |
| 535 phone_statement.ColumnInt(2)); |
| 536 phone.type.label = phone_statement.ColumnString(3); |
| 537 phone.primary = phone_statement.ColumnBool(4); |
| 538 it->second->phone_numbers.push_back(phone); |
| 539 } |
| 540 if (!phone_statement.Succeeded()) |
| 541 return false; |
| 542 |
| 543 sql::Statement postal_statement( |
| 544 db_->GetCachedStatement( |
| 545 SQL_FROM_HERE, |
| 546 "SELECT contact_id, address, relation, label, is_primary " |
| 547 "FROM postal_addresses")); |
| 548 while (postal_statement.Step()) { |
| 549 int64 contact_id = postal_statement.ColumnInt64(0); |
| 550 ContactMap::const_iterator it = contact_map.find(contact_id); |
| 551 if (it == contact_map.end()) { |
| 552 LOG(DFATAL) << "Got postal address for missing contact " << contact_id; |
| 553 continue; |
| 554 } |
| 555 Contact::PostalAddress postal; |
| 556 postal.address = postal_statement.ColumnString(1); |
| 557 postal.type.relation = static_cast<Contact::AddressType::Relation>( |
| 558 postal_statement.ColumnInt(2)); |
| 559 postal.type.label = postal_statement.ColumnString(3); |
| 560 postal.primary = postal_statement.ColumnBool(4); |
| 561 it->second->postal_addresses.push_back(postal); |
| 562 } |
| 563 if (!postal_statement.Succeeded()) |
| 564 return false; |
| 565 |
| 566 sql::Statement im_statement( |
| 567 db_->GetCachedStatement( |
| 568 SQL_FROM_HERE, |
| 569 "SELECT contact_id, address, protocol, relation, label, is_primary " |
| 570 "FROM instant_messaging_addresses")); |
| 571 while (im_statement.Step()) { |
| 572 int64 contact_id = im_statement.ColumnInt64(0); |
| 573 ContactMap::const_iterator it = contact_map.find(contact_id); |
| 574 if (it == contact_map.end()) { |
| 575 LOG(DFATAL) << "Got IM address for missing contact " << contact_id; |
| 576 continue; |
| 577 } |
| 578 Contact::InstantMessagingAddress im; |
| 579 im.address = im_statement.ColumnString(1); |
| 580 im.protocol = static_cast<Contact::InstantMessagingAddress::Protocol>( |
| 581 im_statement.ColumnInt(2)); |
| 582 im.type.relation = static_cast<Contact::AddressType::Relation>( |
| 583 im_statement.ColumnInt(3)); |
| 584 im.type.label = im_statement.ColumnString(4); |
| 585 im.primary = im_statement.ColumnBool(5); |
| 586 it->second->instant_messaging_addresses.push_back(im); |
| 587 } |
| 588 if (!im_statement.Succeeded()) |
| 589 return false; |
| 590 |
| 591 sql::Statement photo_statement( |
| 592 db_->GetCachedStatement( |
| 593 SQL_FROM_HERE, |
| 594 "SELECT contact_id, png_data FROM photos")); |
| 595 while (photo_statement.Step()) { |
| 596 int64 contact_id = photo_statement.ColumnInt64(0); |
| 597 ContactMap::const_iterator it = contact_map.find(contact_id); |
| 598 if (it == contact_map.end()) { |
| 599 LOG(DFATAL) << "Got photo for missing contact " << contact_id; |
| 600 continue; |
| 601 } |
| 602 int png_data_size = photo_statement.ColumnByteLength(1); |
| 603 const unsigned char* png_data = |
| 604 reinterpret_cast<const unsigned char*>(photo_statement.ColumnBlob(1)); |
| 605 if (png_data_size > 0 && png_data) { |
| 606 if (!gfx::PNGCodec::Decode(png_data, png_data_size, |
| 607 &(it->second->photo))) { |
| 608 LOG(ERROR) << "Unable to decode " << png_data_size << "-byte photo for " |
| 609 << "contact " << contact_id; |
| 610 } else { |
| 611 VLOG(2) << "Loaded " << it->second->photo.width() << "x" |
| 612 << it->second->photo.height() << " photo for contact " |
| 613 << contact_id; |
| 614 } |
| 615 } |
| 616 } |
| 617 if (!photo_statement.Succeeded()) |
| 618 return false; |
| 619 |
| 620 return true; |
| 621 } |
| 622 |
| 623 void ContactDatabase::IncrementOutstandingFileOperations() { |
| 624 base::AutoLock auto_lock(lock_); |
| 625 num_outstanding_file_operations_++; |
| 626 } |
| 627 |
| 628 void ContactDatabase::DecrementOutstandingFileOperations() { |
| 629 base::AutoLock auto_lock(lock_); |
| 630 DCHECK_GT(num_outstanding_file_operations_, 0); |
| 631 num_outstanding_file_operations_--; |
| 632 condition_variable_.Signal(); |
| 633 } |
| 634 |
| 635 } // namespace contacts |
OLD | NEW |