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/gdata_contacts_service.h" | 5 #include "chrome/browser/chromeos/contacts/gdata_contacts_service.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 // Individual contacts::Contact objects are created using the data from the | 429 // Individual contacts::Contact objects are created using the data from the |
430 // feed. | 430 // feed. |
431 // | 431 // |
432 // Finally, GetContactPhotoRequests are created and used to start downloading | 432 // Finally, GetContactPhotoRequests are created and used to start downloading |
433 // contacts' photos in parallel. When all photos have been downloaded, the | 433 // contacts' photos in parallel. When all photos have been downloaded, the |
434 // contacts are passed to the passed-in callback. | 434 // contacts are passed to the passed-in callback. |
435 class GDataContactsService::DownloadContactsRequest { | 435 class GDataContactsService::DownloadContactsRequest { |
436 public: | 436 public: |
437 DownloadContactsRequest( | 437 DownloadContactsRequest( |
438 GDataContactsService* service, | 438 GDataContactsService* service, |
439 google_apis::RequestSender* runner, | 439 google_apis::RequestSender* sender, |
440 net::URLRequestContextGetter* url_request_context_getter, | |
441 SuccessCallback success_callback, | 440 SuccessCallback success_callback, |
442 FailureCallback failure_callback, | 441 FailureCallback failure_callback, |
443 const base::Time& min_update_time) | 442 const base::Time& min_update_time) |
444 : service_(service), | 443 : service_(service), |
445 sender_(runner), | 444 sender_(sender), |
446 url_request_context_getter_(url_request_context_getter), | |
447 success_callback_(success_callback), | 445 success_callback_(success_callback), |
448 failure_callback_(failure_callback), | 446 failure_callback_(failure_callback), |
449 min_update_time_(min_update_time), | 447 min_update_time_(min_update_time), |
450 contacts_(new ScopedVector<contacts::Contact>), | 448 contacts_(new ScopedVector<contacts::Contact>), |
451 my_contacts_group_id_(service->cached_my_contacts_group_id_), | 449 my_contacts_group_id_(service->cached_my_contacts_group_id_), |
452 num_in_progress_photo_downloads_(0), | 450 num_in_progress_photo_downloads_(0), |
453 photo_download_failed_(false), | 451 photo_download_failed_(false), |
454 num_photo_download_404_errors_(0), | 452 num_photo_download_404_errors_(0), |
455 total_photo_bytes_(0), | 453 total_photo_bytes_(0), |
456 weak_ptr_factory_(this) { | 454 weak_ptr_factory_(this) { |
(...skipping 17 matching lines...) Expand all Loading... |
474 // Otherwise, the contact groups download is started. | 472 // Otherwise, the contact groups download is started. |
475 void Run() { | 473 void Run() { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
477 download_start_time_ = base::TimeTicks::Now(); | 475 download_start_time_ = base::TimeTicks::Now(); |
478 if (!my_contacts_group_id_.empty()) { | 476 if (!my_contacts_group_id_.empty()) { |
479 StartContactsDownload(); | 477 StartContactsDownload(); |
480 } else { | 478 } else { |
481 google_apis::GetContactGroupsRequest* operation = | 479 google_apis::GetContactGroupsRequest* operation = |
482 new google_apis::GetContactGroupsRequest( | 480 new google_apis::GetContactGroupsRequest( |
483 sender_, | 481 sender_, |
484 url_request_context_getter_, | |
485 base::Bind(&DownloadContactsRequest::HandleGroupsFeedData, | 482 base::Bind(&DownloadContactsRequest::HandleGroupsFeedData, |
486 weak_ptr_factory_.GetWeakPtr())); | 483 weak_ptr_factory_.GetWeakPtr())); |
487 if (!service_->groups_feed_url_for_testing_.is_empty()) { | 484 if (!service_->groups_feed_url_for_testing_.is_empty()) { |
488 operation->set_feed_url_for_testing( | 485 operation->set_feed_url_for_testing( |
489 service_->groups_feed_url_for_testing_); | 486 service_->groups_feed_url_for_testing_); |
490 } | 487 } |
491 sender_->StartRequestWithRetry(operation); | 488 sender_->StartRequestWithRetry(operation); |
492 } | 489 } |
493 } | 490 } |
494 | 491 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 ReportFailure(HISTOGRAM_RESULT_MY_CONTACTS_GROUP_NOT_FOUND); | 579 ReportFailure(HISTOGRAM_RESULT_MY_CONTACTS_GROUP_NOT_FOUND); |
583 } | 580 } |
584 } | 581 } |
585 | 582 |
586 // Starts a download of the contacts from the "My Contacts" group. | 583 // Starts a download of the contacts from the "My Contacts" group. |
587 void StartContactsDownload() { | 584 void StartContactsDownload() { |
588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
589 google_apis::GetContactsRequest* operation = | 586 google_apis::GetContactsRequest* operation = |
590 new google_apis::GetContactsRequest( | 587 new google_apis::GetContactsRequest( |
591 sender_, | 588 sender_, |
592 url_request_context_getter_, | |
593 my_contacts_group_id_, | 589 my_contacts_group_id_, |
594 min_update_time_, | 590 min_update_time_, |
595 base::Bind(&DownloadContactsRequest::HandleContactsFeedData, | 591 base::Bind(&DownloadContactsRequest::HandleContactsFeedData, |
596 weak_ptr_factory_.GetWeakPtr())); | 592 weak_ptr_factory_.GetWeakPtr())); |
597 if (!service_->contacts_feed_url_for_testing_.is_empty()) { | 593 if (!service_->contacts_feed_url_for_testing_.is_empty()) { |
598 operation->set_feed_url_for_testing( | 594 operation->set_feed_url_for_testing( |
599 service_->contacts_feed_url_for_testing_); | 595 service_->contacts_feed_url_for_testing_); |
600 } | 596 } |
601 sender_->StartRequestWithRetry(operation); | 597 sender_->StartRequestWithRetry(operation); |
602 } | 598 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); | 735 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); |
740 contacts_needing_photo_downloads_.pop_back(); | 736 contacts_needing_photo_downloads_.pop_back(); |
741 DCHECK(contact_photo_urls_.count(contact)); | 737 DCHECK(contact_photo_urls_.count(contact)); |
742 std::string url = contact_photo_urls_[contact]; | 738 std::string url = contact_photo_urls_[contact]; |
743 | 739 |
744 VLOG(1) << "Starting download of photo " << url << " for " | 740 VLOG(1) << "Starting download of photo " << url << " for " |
745 << contact->contact_id(); | 741 << contact->contact_id(); |
746 sender_->StartRequestWithRetry( | 742 sender_->StartRequestWithRetry( |
747 new google_apis::GetContactPhotoRequest( | 743 new google_apis::GetContactPhotoRequest( |
748 sender_, | 744 sender_, |
749 url_request_context_getter_, | |
750 GURL(url), | 745 GURL(url), |
751 base::Bind(&DownloadContactsRequest::HandlePhotoData, | 746 base::Bind(&DownloadContactsRequest::HandlePhotoData, |
752 weak_ptr_factory_.GetWeakPtr(), | 747 weak_ptr_factory_.GetWeakPtr(), |
753 contact))); | 748 contact))); |
754 num_in_progress_photo_downloads_++; | 749 num_in_progress_photo_downloads_++; |
755 } | 750 } |
756 } | 751 } |
757 | 752 |
758 // Callback for GetContactPhotoRequest calls. Updates the associated | 753 // Callback for GetContactPhotoRequest calls. Updates the associated |
759 // Contact and checks for completion. | 754 // Contact and checks for completion. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 | 791 |
797 total_photo_bytes_ += download_data->size(); | 792 total_photo_bytes_ += download_data->size(); |
798 contact->set_raw_untrusted_photo(*download_data); | 793 contact->set_raw_untrusted_photo(*download_data); |
799 CheckCompletion(); | 794 CheckCompletion(); |
800 } | 795 } |
801 | 796 |
802 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; | 797 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; |
803 | 798 |
804 GDataContactsService* service_; // not owned | 799 GDataContactsService* service_; // not owned |
805 google_apis::RequestSender* sender_; // not owned | 800 google_apis::RequestSender* sender_; // not owned |
806 net::URLRequestContextGetter* url_request_context_getter_; // not owned | |
807 | 801 |
808 SuccessCallback success_callback_; | 802 SuccessCallback success_callback_; |
809 FailureCallback failure_callback_; | 803 FailureCallback failure_callback_; |
810 | 804 |
811 base::Time min_update_time_; | 805 base::Time min_update_time_; |
812 | 806 |
813 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; | 807 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; |
814 | 808 |
815 // ID of the "My Contacts" contacts group. | 809 // ID of the "My Contacts" contacts group. |
816 std::string my_contacts_group_id_; | 810 std::string my_contacts_group_id_; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 // Note: This should remain the last member so it'll be destroyed and | 844 // Note: This should remain the last member so it'll be destroyed and |
851 // invalidate its weak pointers before any other members are destroyed. | 845 // invalidate its weak pointers before any other members are destroyed. |
852 base::WeakPtrFactory<DownloadContactsRequest> weak_ptr_factory_; | 846 base::WeakPtrFactory<DownloadContactsRequest> weak_ptr_factory_; |
853 | 847 |
854 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); | 848 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); |
855 }; | 849 }; |
856 | 850 |
857 GDataContactsService::GDataContactsService( | 851 GDataContactsService::GDataContactsService( |
858 net::URLRequestContextGetter* url_request_context_getter, | 852 net::URLRequestContextGetter* url_request_context_getter, |
859 Profile* profile) | 853 Profile* profile) |
860 : url_request_context_getter_(url_request_context_getter), | 854 : max_photo_downloads_per_second_(kMaxPhotoDownloadsPerSecond), |
861 max_photo_downloads_per_second_(kMaxPhotoDownloadsPerSecond), | |
862 photo_download_timer_interval_(base::TimeDelta::FromSeconds(1)) { | 855 photo_download_timer_interval_(base::TimeDelta::FromSeconds(1)) { |
863 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 856 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
864 std::vector<std::string> scopes; | 857 std::vector<std::string> scopes; |
865 scopes.push_back(kContactsScope); | 858 scopes.push_back(kContactsScope); |
866 sender_.reset(new google_apis::RequestSender(profile, | 859 sender_.reset(new google_apis::RequestSender(profile, |
867 url_request_context_getter_, | 860 url_request_context_getter, |
868 scopes, | 861 scopes, |
869 "" /* custom_user_agent */)); | 862 "" /* custom_user_agent */)); |
870 } | 863 } |
871 | 864 |
872 GDataContactsService::~GDataContactsService() { | 865 GDataContactsService::~GDataContactsService() { |
873 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 866 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
874 STLDeleteContainerPointers(requests_.begin(), requests_.end()); | 867 STLDeleteContainerPointers(requests_.begin(), requests_.end()); |
875 requests_.clear(); | 868 requests_.clear(); |
876 } | 869 } |
877 | 870 |
878 google_apis::AuthService* GDataContactsService::auth_service_for_testing() { | 871 google_apis::AuthService* GDataContactsService::auth_service_for_testing() { |
879 return sender_->auth_service(); | 872 return sender_->auth_service(); |
880 } | 873 } |
881 | 874 |
882 void GDataContactsService::Initialize() { | 875 void GDataContactsService::Initialize() { |
883 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 876 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
884 sender_->Initialize(); | 877 sender_->Initialize(); |
885 } | 878 } |
886 | 879 |
887 void GDataContactsService::DownloadContacts(SuccessCallback success_callback, | 880 void GDataContactsService::DownloadContacts(SuccessCallback success_callback, |
888 FailureCallback failure_callback, | 881 FailureCallback failure_callback, |
889 const base::Time& min_update_time) { | 882 const base::Time& min_update_time) { |
890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 883 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
891 DownloadContactsRequest* request = | 884 DownloadContactsRequest* request = |
892 new DownloadContactsRequest(this, | 885 new DownloadContactsRequest(this, |
893 sender_.get(), | 886 sender_.get(), |
894 url_request_context_getter_, | |
895 success_callback, | 887 success_callback, |
896 failure_callback, | 888 failure_callback, |
897 min_update_time); | 889 min_update_time); |
898 VLOG(1) << "Starting contacts download with request " << request; | 890 VLOG(1) << "Starting contacts download with request " << request; |
899 requests_.insert(request); | 891 requests_.insert(request); |
900 request->Run(); | 892 request->Run(); |
901 } | 893 } |
902 | 894 |
903 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { | 895 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { |
904 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 896 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
905 DCHECK(request); | 897 DCHECK(request); |
906 VLOG(1) << "Download request " << request << " complete"; | 898 VLOG(1) << "Download request " << request << " complete"; |
907 if (!request->my_contacts_group_id().empty()) | 899 if (!request->my_contacts_group_id().empty()) |
908 cached_my_contacts_group_id_ = request->my_contacts_group_id(); | 900 cached_my_contacts_group_id_ = request->my_contacts_group_id(); |
909 requests_.erase(request); | 901 requests_.erase(request); |
910 delete request; | 902 delete request; |
911 } | 903 } |
912 | 904 |
913 } // namespace contacts | 905 } // namespace contacts |
OLD | NEW |