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/gdata/gdata_contacts_service.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_contacts_service.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 #include <string> | 8 #include <string> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
| 12 #include "base/json/json_value_converter.h" |
12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/time.h" | 17 #include "base/time.h" |
17 #include "base/timer.h" | 18 #include "base/timer.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 20 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
20 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h" | 21 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h" |
21 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h" | 22 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h" |
22 #include "chrome/browser/chromeos/gdata/gdata_operations.h" | 23 #include "chrome/browser/chromeos/gdata/gdata_operations.h" |
23 #include "chrome/browser/chromeos/gdata/gdata_params.h" | 24 #include "chrome/browser/chromeos/gdata/gdata_params.h" |
24 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 25 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 | 27 |
27 using content::BrowserThread; | 28 using content::BrowserThread; |
28 | 29 |
29 namespace gdata { | 30 namespace gdata { |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 // Maximum number of profile photos that we'll download per second. | 34 // Maximum number of profile photos that we'll download per second. |
34 // At values above 10, Google starts returning 503 errors. | 35 // At values above 10, Google starts returning 503 errors. |
35 const int kMaxPhotoDownloadsPerSecond = 10; | 36 const int kMaxPhotoDownloadsPerSecond = 10; |
36 | 37 |
| 38 // Hardcoded system group ID for the "My Contacts" group, per |
| 39 // https://developers.google.com/google-apps/contacts/v3/#contact_group_entry. |
| 40 const char kMyContactsSystemGroupId[] = "Contacts"; |
| 41 |
| 42 // Top-level field in a contact groups feed containing the list of entries. |
| 43 const char kGroupEntryField[] = "feed.entry"; |
| 44 |
| 45 // Field in group entries containing the system group ID (e.g. ID "Contacts" |
| 46 // for the "My Contacts" system group). See |
| 47 // https://developers.google.com/google-apps/contacts/v3/#contact_group_entry |
| 48 // for more details. |
| 49 const char kSystemGroupIdField[] = "gContact$systemGroup.id"; |
| 50 |
37 // Field in the top-level object containing the contacts feed. | 51 // Field in the top-level object containing the contacts feed. |
38 const char kFeedField[] = "feed"; | 52 const char kFeedField[] = "feed"; |
39 | 53 |
40 // Field in the contacts feed containing a list of category information, along | 54 // Field in the contacts feed containing a list of category information, along |
41 // with fields within the dictionaries contained in the list and expected | 55 // with fields within the dictionaries contained in the list and expected |
42 // values. | 56 // values. |
43 const char kCategoryField[] = "category"; | 57 const char kCategoryField[] = "category"; |
44 const char kCategorySchemeField[] = "scheme"; | 58 const char kCategorySchemeField[] = "scheme"; |
45 const char kCategorySchemeValue[] = "http://schemas.google.com/g/2005#kind"; | 59 const char kCategorySchemeValue[] = "http://schemas.google.com/g/2005#kind"; |
46 const char kCategoryTermField[] = "term"; | 60 const char kCategoryTermField[] = "term"; |
47 const char kCategoryTermValue[] = | 61 const char kCategoryTermValue[] = |
48 "http://schemas.google.com/contact/2008#contact"; | 62 "http://schemas.google.com/contact/2008#contact"; |
49 | 63 |
50 // Field in the contacts feed containing a list of contact entries. | 64 // Field in the contacts feed containing a list of contact entries. |
51 const char kEntryField[] = "entry"; | 65 const char kEntryField[] = "entry"; |
52 | 66 |
| 67 // Field in group and contact entries containing the item's ID. |
| 68 const char kIdField[] = "id.$t"; |
| 69 |
53 // Top-level fields in contact entries. | 70 // Top-level fields in contact entries. |
54 const char kIdField[] = "id.$t"; | |
55 const char kDeletedField[] = "gd$deleted"; | 71 const char kDeletedField[] = "gd$deleted"; |
56 const char kFullNameField[] = "gd$name.gd$fullName.$t"; | 72 const char kFullNameField[] = "gd$name.gd$fullName.$t"; |
57 const char kGivenNameField[] = "gd$name.gd$givenName.$t"; | 73 const char kGivenNameField[] = "gd$name.gd$givenName.$t"; |
58 const char kAdditionalNameField[] = "gd$name.gd$additionalName.$t"; | 74 const char kAdditionalNameField[] = "gd$name.gd$additionalName.$t"; |
59 const char kFamilyNameField[] = "gd$name.gd$familyName.$t"; | 75 const char kFamilyNameField[] = "gd$name.gd$familyName.$t"; |
60 const char kNamePrefixField[] = "gd$name.gd$namePrefix.$t"; | 76 const char kNamePrefixField[] = "gd$name.gd$namePrefix.$t"; |
61 const char kNameSuffixField[] = "gd$name.gd$nameSuffix.$t"; | 77 const char kNameSuffixField[] = "gd$name.gd$nameSuffix.$t"; |
62 const char kEmailField[] = "gd$email"; | 78 const char kEmailField[] = "gd$email"; |
63 const char kPhoneField[] = "gd$phoneNumber"; | 79 const char kPhoneField[] = "gd$phoneNumber"; |
64 const char kPostalAddressField[] = "gd$structuredPostalAddress"; | 80 const char kPostalAddressField[] = "gd$structuredPostalAddress"; |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 } | 315 } |
300 im->set_primary(IsAddressPrimary(*im_dict)); | 316 im->set_primary(IsAddressPrimary(*im_dict)); |
301 InitAddressType(*im_dict, im->mutable_type()); | 317 InitAddressType(*im_dict, im->mutable_type()); |
302 im->set_protocol(GetInstantMessagingProtocol(*im_dict)); | 318 im->set_protocol(GetInstantMessagingProtocol(*im_dict)); |
303 } | 319 } |
304 } | 320 } |
305 | 321 |
306 return true; | 322 return true; |
307 } | 323 } |
308 | 324 |
| 325 // Structure into which we parse the contact groups feed using |
| 326 // JSONValueConverter. |
| 327 struct ContactGroups { |
| 328 struct ContactGroup { |
| 329 // Group ID, e.g. |
| 330 // "http://www.google.com/m8/feeds/groups/user%40gmail.com/base/6". |
| 331 std::string group_id; |
| 332 |
| 333 // System group ID (e.g. "Contacts" for the "My Contacts" system group) if |
| 334 // this is a system group, and empty otherwise. See http://goo.gl/oWVnN |
| 335 // for more details. |
| 336 std::string system_group_id; |
| 337 }; |
| 338 |
| 339 // Given a system group ID, returns the corresponding group ID or an empty |
| 340 // string if the requested system group wasn't present. |
| 341 std::string GetGroupIdForSystemGroup(const std::string& system_group_id) { |
| 342 for (size_t i = 0; i < groups.size(); ++i) { |
| 343 const ContactGroup& group = *groups[i]; |
| 344 if (group.system_group_id == system_group_id) |
| 345 return group.group_id; |
| 346 } |
| 347 return std::string(); |
| 348 } |
| 349 |
| 350 // Given |value| corresponding to a dictionary in a contact group feed's |
| 351 // "entry" list, fills |result| with information about the group. |
| 352 static bool GetContactGroup(const base::Value* value, ContactGroup* result) { |
| 353 DCHECK(value); |
| 354 DCHECK(result); |
| 355 const base::DictionaryValue* dict = NULL; |
| 356 if (!value->GetAsDictionary(&dict)) |
| 357 return false; |
| 358 |
| 359 dict->GetString(kIdField, &result->group_id); |
| 360 dict->GetString(kSystemGroupIdField, &result->system_group_id); |
| 361 return true; |
| 362 } |
| 363 |
| 364 static void RegisterJSONConverter( |
| 365 base::JSONValueConverter<ContactGroups>* converter) { |
| 366 DCHECK(converter); |
| 367 converter->RegisterRepeatedCustomValue<ContactGroup>( |
| 368 kGroupEntryField, &ContactGroups::groups, &GetContactGroup); |
| 369 } |
| 370 |
| 371 ScopedVector<ContactGroup> groups; |
| 372 }; |
| 373 |
309 } // namespace | 374 } // namespace |
310 | 375 |
311 // This class handles a single request to download all of a user's contacts. | 376 // This class handles a single request to download all of a user's contacts. |
312 // | 377 // |
313 // First, the contacts feed is downloaded via GetContactsOperation and parsed. | 378 // First, the feed containing the user's contact groups is downloaded via |
| 379 // GetContactGroupsOperation and examined to find the ID for the "My Contacts" |
| 380 // group (by default, the contacts API also returns suggested contacts). The |
| 381 // group ID is cached in GDataContactsService so that this step can be skipped |
| 382 // by later DownloadContactRequests. |
| 383 // |
| 384 // Next, the contacts feed is downloaded via GetContactsOperation and parsed. |
314 // Individual contacts::Contact objects are created using the data from the | 385 // Individual contacts::Contact objects are created using the data from the |
315 // feed. Next, GetContactPhotoOperations are created and used to start | 386 // feed. |
316 // downloading contacts' photos in parallel. When all photos have been | 387 // |
317 // downloaded, the contacts are passed to the passed-in callback. | 388 // Finally, GetContactPhotoOperations are created and used to start downloading |
318 class GDataContactsService::DownloadContactsRequest | 389 // contacts' photos in parallel. When all photos have been downloaded, the |
319 : public base::SupportsWeakPtr<DownloadContactsRequest> { | 390 // contacts are passed to the passed-in callback. |
| 391 class GDataContactsService::DownloadContactsRequest { |
320 public: | 392 public: |
321 DownloadContactsRequest(GDataContactsService* service, | 393 DownloadContactsRequest(GDataContactsService* service, |
322 GDataOperationRunner* runner, | 394 GDataOperationRunner* runner, |
323 SuccessCallback success_callback, | 395 SuccessCallback success_callback, |
324 FailureCallback failure_callback, | 396 FailureCallback failure_callback, |
325 const base::Time& min_update_time) | 397 const base::Time& min_update_time) |
326 : service_(service), | 398 : service_(service), |
327 runner_(runner), | 399 runner_(runner), |
328 success_callback_(success_callback), | 400 success_callback_(success_callback), |
329 failure_callback_(failure_callback), | 401 failure_callback_(failure_callback), |
330 min_update_time_(min_update_time), | 402 min_update_time_(min_update_time), |
331 contacts_(new ScopedVector<contacts::Contact>), | 403 contacts_(new ScopedVector<contacts::Contact>), |
| 404 my_contacts_group_id_(service->cached_my_contacts_group_id_), |
332 num_in_progress_photo_downloads_(0), | 405 num_in_progress_photo_downloads_(0), |
333 photo_download_failed_(false) { | 406 photo_download_failed_(false), |
| 407 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 408 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
334 DCHECK(service_); | 409 DCHECK(service_); |
335 DCHECK(runner_); | 410 DCHECK(runner_); |
336 } | 411 } |
337 | 412 |
338 ~DownloadContactsRequest() { | 413 ~DownloadContactsRequest() { |
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
340 service_ = NULL; | 415 service_ = NULL; |
341 runner_ = NULL; | 416 runner_ = NULL; |
342 } | 417 } |
343 | 418 |
344 // Issues the initial request to download the contact feed. | 419 const std::string my_contacts_group_id() const { |
| 420 return my_contacts_group_id_; |
| 421 } |
| 422 |
| 423 // Begins the contacts-downloading process. If the ID for the "My Contacts" |
| 424 // group has previously been cached, then the contacts download is started. |
| 425 // Otherwise, the contact groups download is started. |
345 void Run() { | 426 void Run() { |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 428 if (!my_contacts_group_id_.empty()) { |
| 429 StartContactsDownload(); |
| 430 } else { |
| 431 GetContactGroupsOperation* operation = |
| 432 new GetContactGroupsOperation( |
| 433 runner_->operation_registry(), |
| 434 base::Bind(&DownloadContactsRequest::HandleGroupsFeedData, |
| 435 weak_ptr_factory_.GetWeakPtr())); |
| 436 if (!service_->groups_feed_url_for_testing_.is_empty()) { |
| 437 operation->set_feed_url_for_testing( |
| 438 service_->groups_feed_url_for_testing_); |
| 439 } |
| 440 runner_->StartOperationWithRetry(operation); |
| 441 } |
| 442 } |
| 443 |
| 444 private: |
| 445 // Invokes the failure callback and notifies GDataContactsService that the |
| 446 // request is done. |
| 447 void ReportFailure() { |
| 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 449 failure_callback_.Run(); |
| 450 service_->OnRequestComplete(this); |
| 451 } |
| 452 |
| 453 // Callback for GetContactGroupsOperation calls. Starts downloading the |
| 454 // actual contacts after finding the "My Contacts" group ID. |
| 455 void HandleGroupsFeedData(GDataErrorCode error, |
| 456 scoped_ptr<base::Value> feed_data) { |
| 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 458 if (error != HTTP_SUCCESS) { |
| 459 LOG(WARNING) << "Got error " << error << " while downloading groups"; |
| 460 ReportFailure(); |
| 461 return; |
| 462 } |
| 463 |
| 464 VLOG(2) << "Got groups feed data:\n" |
| 465 << PrettyPrintValue(*(feed_data.get())); |
| 466 ContactGroups groups; |
| 467 base::JSONValueConverter<ContactGroups> converter; |
| 468 if (!converter.Convert(*feed_data, &groups)) { |
| 469 LOG(WARNING) << "Unable to parse groups feed"; |
| 470 ReportFailure(); |
| 471 return; |
| 472 } |
| 473 |
| 474 my_contacts_group_id_ = |
| 475 groups.GetGroupIdForSystemGroup(kMyContactsSystemGroupId); |
| 476 if (!my_contacts_group_id_.empty()) { |
| 477 StartContactsDownload(); |
| 478 } else { |
| 479 LOG(WARNING) << "Unable to find ID for \"My Contacts\" group"; |
| 480 ReportFailure(); |
| 481 } |
| 482 } |
| 483 |
| 484 // Starts a download of the contacts from the "My Contacts" group. |
| 485 void StartContactsDownload() { |
| 486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
346 GetContactsOperation* operation = | 487 GetContactsOperation* operation = |
347 new GetContactsOperation( | 488 new GetContactsOperation( |
348 runner_->operation_registry(), | 489 runner_->operation_registry(), |
| 490 my_contacts_group_id_, |
349 min_update_time_, | 491 min_update_time_, |
350 base::Bind(&DownloadContactsRequest::HandleFeedData, | 492 base::Bind(&DownloadContactsRequest::HandleContactsFeedData, |
351 base::Unretained(this))); | 493 weak_ptr_factory_.GetWeakPtr())); |
352 if (!service_->feed_url_for_testing_.is_empty()) | 494 if (!service_->contacts_feed_url_for_testing_.is_empty()) { |
353 operation->set_feed_url_for_testing(service_->feed_url_for_testing_); | 495 operation->set_feed_url_for_testing( |
354 | 496 service_->contacts_feed_url_for_testing_); |
| 497 } |
355 runner_->StartOperationWithRetry(operation); | 498 runner_->StartOperationWithRetry(operation); |
356 } | 499 } |
357 | 500 |
358 private: | |
359 // Callback for GetContactsOperation calls. | 501 // Callback for GetContactsOperation calls. |
360 void HandleFeedData(GDataErrorCode error, | 502 void HandleContactsFeedData(GDataErrorCode error, |
361 scoped_ptr<base::Value> feed_data) { | 503 scoped_ptr<base::Value> feed_data) { |
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
363 if (error != HTTP_SUCCESS) { | 505 if (error != HTTP_SUCCESS) { |
364 LOG(WARNING) << "Got error " << error << " while downloading contacts"; | 506 LOG(WARNING) << "Got error " << error << " while downloading contacts"; |
365 failure_callback_.Run(); | 507 ReportFailure(); |
366 service_->OnRequestComplete(this); | |
367 return; | 508 return; |
368 } | 509 } |
369 | 510 |
370 VLOG(2) << "Got feed data:\n" << PrettyPrintValue(*(feed_data.get())); | 511 VLOG(2) << "Got contacts feed data:\n" |
371 if (!ProcessFeedData(*feed_data.get())) { | 512 << PrettyPrintValue(*(feed_data.get())); |
372 LOG(WARNING) << "Unable to process feed data"; | 513 if (!ProcessContactsFeedData(*feed_data.get())) { |
373 failure_callback_.Run(); | 514 LOG(WARNING) << "Unable to process contacts feed data"; |
374 service_->OnRequestComplete(this); | 515 ReportFailure(); |
375 return; | 516 return; |
376 } | 517 } |
377 | 518 |
378 StartPhotoDownloads(); | 519 StartPhotoDownloads(); |
379 photo_download_timer_.Start( | 520 photo_download_timer_.Start( |
380 FROM_HERE, service_->photo_download_timer_interval_, | 521 FROM_HERE, service_->photo_download_timer_interval_, |
381 this, &DownloadContactsRequest::StartPhotoDownloads); | 522 this, &DownloadContactsRequest::StartPhotoDownloads); |
382 CheckCompletion(); | 523 CheckCompletion(); |
383 } | 524 } |
384 | 525 |
385 // Processes the raw contacts feed from |feed_data| and fills |contacts_|. | 526 // Processes the raw contacts feed from |feed_data| and fills |contacts_|. |
386 // Returns true on success. | 527 // Returns true on success. |
387 bool ProcessFeedData(const base::Value& feed_data) { | 528 bool ProcessContactsFeedData(const base::Value& feed_data) { |
| 529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
388 const DictionaryValue* toplevel_dict = NULL; | 530 const DictionaryValue* toplevel_dict = NULL; |
389 if (!feed_data.GetAsDictionary(&toplevel_dict)) { | 531 if (!feed_data.GetAsDictionary(&toplevel_dict)) { |
390 LOG(WARNING) << "Top-level object is not a dictionary"; | 532 LOG(WARNING) << "Top-level object is not a dictionary"; |
391 return false; | 533 return false; |
392 } | 534 } |
393 | 535 |
394 const DictionaryValue* feed_dict = NULL; | 536 const DictionaryValue* feed_dict = NULL; |
395 if (!toplevel_dict->GetDictionary(kFeedField, &feed_dict)) { | 537 if (!toplevel_dict->GetDictionary(kFeedField, &feed_dict)) { |
396 LOG(WARNING) << "Feed dictionary missing"; | 538 LOG(WARNING) << "Feed dictionary missing"; |
397 return false; | 539 return false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 608 |
467 // If we're done downloading photos, invokes a callback and deletes |this|. | 609 // If we're done downloading photos, invokes a callback and deletes |this|. |
468 // Otherwise, starts one or more downloads of URLs from | 610 // Otherwise, starts one or more downloads of URLs from |
469 // |contacts_needing_photo_downloads_|. | 611 // |contacts_needing_photo_downloads_|. |
470 void CheckCompletion() { | 612 void CheckCompletion() { |
471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 613 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
472 if (contacts_needing_photo_downloads_.empty() && | 614 if (contacts_needing_photo_downloads_.empty() && |
473 num_in_progress_photo_downloads_ == 0) { | 615 num_in_progress_photo_downloads_ == 0) { |
474 VLOG(1) << "Done downloading photos; invoking callback"; | 616 VLOG(1) << "Done downloading photos; invoking callback"; |
475 photo_download_timer_.Stop(); | 617 photo_download_timer_.Stop(); |
476 if (photo_download_failed_) | 618 if (photo_download_failed_) { |
477 failure_callback_.Run(); | 619 ReportFailure(); |
478 else | 620 } else { |
479 success_callback_.Run(contacts_.Pass()); | 621 success_callback_.Run(contacts_.Pass()); |
480 service_->OnRequestComplete(this); | 622 service_->OnRequestComplete(this); |
| 623 } |
481 return; | 624 return; |
482 } | 625 } |
483 } | 626 } |
484 | 627 |
485 // Starts photo downloads for contacts in |contacts_needing_photo_downloads_|. | 628 // Starts photo downloads for contacts in |contacts_needing_photo_downloads_|. |
486 // Should be invoked only once per second. | 629 // Should be invoked only once per second. |
487 void StartPhotoDownloads() { | 630 void StartPhotoDownloads() { |
| 631 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
488 while (!contacts_needing_photo_downloads_.empty() && | 632 while (!contacts_needing_photo_downloads_.empty() && |
489 (num_in_progress_photo_downloads_ < | 633 (num_in_progress_photo_downloads_ < |
490 service_->max_photo_downloads_per_second_)) { | 634 service_->max_photo_downloads_per_second_)) { |
491 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); | 635 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); |
492 contacts_needing_photo_downloads_.pop_back(); | 636 contacts_needing_photo_downloads_.pop_back(); |
493 DCHECK(contact_photo_urls_.count(contact)); | 637 DCHECK(contact_photo_urls_.count(contact)); |
494 std::string url = contact_photo_urls_[contact]; | 638 std::string url = contact_photo_urls_[contact]; |
495 | 639 |
496 VLOG(1) << "Starting download of photo " << url << " for " | 640 VLOG(1) << "Starting download of photo " << url << " for " |
497 << contact->provider_id(); | 641 << contact->provider_id(); |
498 runner_->StartOperationWithRetry( | 642 runner_->StartOperationWithRetry( |
499 new GetContactPhotoOperation( | 643 new GetContactPhotoOperation( |
500 runner_->operation_registry(), | 644 runner_->operation_registry(), |
501 GURL(url), | 645 GURL(url), |
502 base::Bind(&DownloadContactsRequest::HandlePhotoData, | 646 base::Bind(&DownloadContactsRequest::HandlePhotoData, |
503 AsWeakPtr(), contact))); | 647 weak_ptr_factory_.GetWeakPtr(), |
| 648 contact))); |
504 num_in_progress_photo_downloads_++; | 649 num_in_progress_photo_downloads_++; |
505 } | 650 } |
506 } | 651 } |
507 | 652 |
508 // Callback for GetContactPhotoOperation calls. Updates the associated | 653 // Callback for GetContactPhotoOperation calls. Updates the associated |
509 // Contact and checks for completion. | 654 // Contact and checks for completion. |
510 void HandlePhotoData(contacts::Contact* contact, | 655 void HandlePhotoData(contacts::Contact* contact, |
511 GDataErrorCode error, | 656 GDataErrorCode error, |
512 scoped_ptr<std::string> download_data) { | 657 scoped_ptr<std::string> download_data) { |
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 23 matching lines...) Expand all Loading... |
537 // Make sure we don't start any more downloads. | 682 // Make sure we don't start any more downloads. |
538 contacts_needing_photo_downloads_.clear(); | 683 contacts_needing_photo_downloads_.clear(); |
539 CheckCompletion(); | 684 CheckCompletion(); |
540 return; | 685 return; |
541 } | 686 } |
542 | 687 |
543 contact->set_raw_untrusted_photo(*download_data); | 688 contact->set_raw_untrusted_photo(*download_data); |
544 CheckCompletion(); | 689 CheckCompletion(); |
545 } | 690 } |
546 | 691 |
547 private: | |
548 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; | 692 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; |
549 | 693 |
550 GDataContactsService* service_; // not owned | 694 GDataContactsService* service_; // not owned |
551 GDataOperationRunner* runner_; // not owned | 695 GDataOperationRunner* runner_; // not owned |
552 | 696 |
553 SuccessCallback success_callback_; | 697 SuccessCallback success_callback_; |
554 FailureCallback failure_callback_; | 698 FailureCallback failure_callback_; |
555 | 699 |
556 base::Time min_update_time_; | 700 base::Time min_update_time_; |
557 | 701 |
558 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; | 702 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; |
559 | 703 |
| 704 // ID of the "My Contacts" contacts group. |
| 705 std::string my_contacts_group_id_; |
| 706 |
560 // Map from a contact to the URL at which its photo is located. | 707 // Map from a contact to the URL at which its photo is located. |
561 // Contacts without photos do not appear in this map. | 708 // Contacts without photos do not appear in this map. |
562 ContactPhotoUrls contact_photo_urls_; | 709 ContactPhotoUrls contact_photo_urls_; |
563 | 710 |
564 // Invokes StartPhotoDownloads() once per second. | 711 // Invokes StartPhotoDownloads() once per second. |
565 base::RepeatingTimer<DownloadContactsRequest> photo_download_timer_; | 712 base::RepeatingTimer<DownloadContactsRequest> photo_download_timer_; |
566 | 713 |
567 // Contacts that have photos that we still need to start downloading. | 714 // Contacts that have photos that we still need to start downloading. |
568 // When we start a download, the contact is removed from this list. | 715 // When we start a download, the contact is removed from this list. |
569 std::vector<contacts::Contact*> contacts_needing_photo_downloads_; | 716 std::vector<contacts::Contact*> contacts_needing_photo_downloads_; |
570 | 717 |
571 // Number of in-progress photo downloads. | 718 // Number of in-progress photo downloads. |
572 int num_in_progress_photo_downloads_; | 719 int num_in_progress_photo_downloads_; |
573 | 720 |
574 // Did we encounter a fatal error while downloading a photo? | 721 // Did we encounter a fatal error while downloading a photo? |
575 bool photo_download_failed_; | 722 bool photo_download_failed_; |
576 | 723 |
| 724 // Note: This should remain the last member so it'll be destroyed and |
| 725 // invalidate its weak pointers before any other members are destroyed. |
| 726 base::WeakPtrFactory<DownloadContactsRequest> weak_ptr_factory_; |
| 727 |
577 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); | 728 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); |
578 }; | 729 }; |
579 | 730 |
580 GDataContactsService::GDataContactsService(Profile* profile) | 731 GDataContactsService::GDataContactsService(Profile* profile) |
581 : runner_(new GDataOperationRunner(profile)), | 732 : runner_(new GDataOperationRunner(profile)), |
582 max_photo_downloads_per_second_(kMaxPhotoDownloadsPerSecond), | 733 max_photo_downloads_per_second_(kMaxPhotoDownloadsPerSecond), |
583 photo_download_timer_interval_(base::TimeDelta::FromSeconds(1)) { | 734 photo_download_timer_interval_(base::TimeDelta::FromSeconds(1)) { |
584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 735 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
585 } | 736 } |
586 | 737 |
(...skipping 25 matching lines...) Expand all Loading... |
612 min_update_time); | 763 min_update_time); |
613 VLOG(1) << "Starting contacts download with request " << request; | 764 VLOG(1) << "Starting contacts download with request " << request; |
614 requests_.insert(request); | 765 requests_.insert(request); |
615 request->Run(); | 766 request->Run(); |
616 } | 767 } |
617 | 768 |
618 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { | 769 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { |
619 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
620 DCHECK(request); | 771 DCHECK(request); |
621 VLOG(1) << "Download request " << request << " complete"; | 772 VLOG(1) << "Download request " << request << " complete"; |
| 773 if (!request->my_contacts_group_id().empty()) |
| 774 cached_my_contacts_group_id_ = request->my_contacts_group_id(); |
622 requests_.erase(request); | 775 requests_.erase(request); |
623 delete request; | 776 delete request; |
624 } | 777 } |
625 | 778 |
626 } // namespace contacts | 779 } // namespace contacts |
OLD | NEW |