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_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 17 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
18 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h" |
19 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h" |
20 #include "chrome/browser/chromeos/gdata/gdata_operations.h" | 20 #include "chrome/browser/chromeos/gdata/gdata_operations.h" |
21 #include "chrome/browser/chromeos/gdata/gdata_params.h" | 21 #include "chrome/browser/chromeos/gdata/gdata_params.h" |
22 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 22 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
23 #include "chrome/browser/profiles/profile.h" | |
24 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
25 | 24 |
26 using content::BrowserThread; | 25 using content::BrowserThread; |
27 | 26 |
28 namespace gdata { | 27 namespace gdata { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 // Maximum number of profile photos that we'll download at once. | 31 // Maximum number of profile photos that we'll download at once. |
33 const int kMaxSimultaneousPhotoDownloads = 10; | 32 const int kMaxSimultaneousPhotoDownloads = 10; |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // | 309 // |
311 // First, the contacts feed is downloaded via GetContactsOperation and parsed. | 310 // First, the contacts feed is downloaded via GetContactsOperation and parsed. |
312 // Individual contacts::Contact objects are created using the data from the | 311 // Individual contacts::Contact objects are created using the data from the |
313 // feed. Next, GetContactPhotoOperations are created and used to start | 312 // feed. Next, GetContactPhotoOperations are created and used to start |
314 // downloading contacts' photos in parallel. When all photos have been | 313 // downloading contacts' photos in parallel. When all photos have been |
315 // downloaded, the contacts are passed to the passed-in callback. | 314 // downloaded, the contacts are passed to the passed-in callback. |
316 class GDataContactsService::DownloadContactsRequest | 315 class GDataContactsService::DownloadContactsRequest |
317 : public base::SupportsWeakPtr<DownloadContactsRequest> { | 316 : public base::SupportsWeakPtr<DownloadContactsRequest> { |
318 public: | 317 public: |
319 DownloadContactsRequest(GDataContactsService* service, | 318 DownloadContactsRequest(GDataContactsService* service, |
320 Profile* profile, | |
321 GDataOperationRunner* runner, | 319 GDataOperationRunner* runner, |
322 SuccessCallback success_callback, | 320 SuccessCallback success_callback, |
323 FailureCallback failure_callback, | 321 FailureCallback failure_callback, |
324 const base::Time& min_update_time, | 322 const base::Time& min_update_time, |
325 int max_simultaneous_photo_downloads) | 323 int max_simultaneous_photo_downloads) |
326 : service_(service), | 324 : service_(service), |
327 profile_(profile), | |
328 runner_(runner), | 325 runner_(runner), |
329 success_callback_(success_callback), | 326 success_callback_(success_callback), |
330 failure_callback_(failure_callback), | 327 failure_callback_(failure_callback), |
331 min_update_time_(min_update_time), | 328 min_update_time_(min_update_time), |
332 contacts_(new ScopedVector<contacts::Contact>), | 329 contacts_(new ScopedVector<contacts::Contact>), |
333 max_simultaneous_photo_downloads_(max_simultaneous_photo_downloads), | 330 max_simultaneous_photo_downloads_(max_simultaneous_photo_downloads), |
334 num_in_progress_photo_downloads_(0), | 331 num_in_progress_photo_downloads_(0), |
335 photo_download_failed_(false) { | 332 photo_download_failed_(false) { |
336 DCHECK(service_); | 333 DCHECK(service_); |
337 DCHECK(profile_); | |
338 DCHECK(runner_); | 334 DCHECK(runner_); |
339 } | 335 } |
340 | 336 |
341 ~DownloadContactsRequest() { | 337 ~DownloadContactsRequest() { |
342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
343 service_ = NULL; | 339 service_ = NULL; |
344 profile_ = NULL; | |
345 runner_ = NULL; | 340 runner_ = NULL; |
346 } | 341 } |
347 | 342 |
348 // Issues the initial request to download the contact feed. | 343 // Issues the initial request to download the contact feed. |
349 void Run() { | 344 void Run() { |
350 GetContactsOperation* operation = | 345 GetContactsOperation* operation = |
351 new GetContactsOperation( | 346 new GetContactsOperation( |
352 runner_->operation_registry(), | 347 runner_->operation_registry(), |
353 profile_, | |
354 min_update_time_, | 348 min_update_time_, |
355 base::Bind(&DownloadContactsRequest::HandleFeedData, | 349 base::Bind(&DownloadContactsRequest::HandleFeedData, |
356 base::Unretained(this))); | 350 base::Unretained(this))); |
357 if (!service_->feed_url_for_testing_.is_empty()) | 351 if (!service_->feed_url_for_testing_.is_empty()) |
358 operation->set_feed_url_for_testing(service_->feed_url_for_testing_); | 352 operation->set_feed_url_for_testing(service_->feed_url_for_testing_); |
359 | 353 |
360 runner_->StartOperationWithRetry(operation); | 354 runner_->StartOperationWithRetry(operation); |
361 } | 355 } |
362 | 356 |
363 private: | 357 private: |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); | 481 contacts::Contact* contact = contacts_needing_photo_downloads_.back(); |
488 contacts_needing_photo_downloads_.pop_back(); | 482 contacts_needing_photo_downloads_.pop_back(); |
489 DCHECK(contact_photo_urls_.count(contact)); | 483 DCHECK(contact_photo_urls_.count(contact)); |
490 std::string url = contact_photo_urls_[contact]; | 484 std::string url = contact_photo_urls_[contact]; |
491 | 485 |
492 VLOG(1) << "Starting download of photo " << url << " for " | 486 VLOG(1) << "Starting download of photo " << url << " for " |
493 << contact->provider_id(); | 487 << contact->provider_id(); |
494 runner_->StartOperationWithRetry( | 488 runner_->StartOperationWithRetry( |
495 new GetContactPhotoOperation( | 489 new GetContactPhotoOperation( |
496 runner_->operation_registry(), | 490 runner_->operation_registry(), |
497 profile_, | |
498 GURL(url), | 491 GURL(url), |
499 base::Bind(&DownloadContactsRequest::HandlePhotoData, | 492 base::Bind(&DownloadContactsRequest::HandlePhotoData, |
500 AsWeakPtr(), contact))); | 493 AsWeakPtr(), contact))); |
501 num_in_progress_photo_downloads_++; | 494 num_in_progress_photo_downloads_++; |
502 } | 495 } |
503 } | 496 } |
504 | 497 |
505 // Callback for GetContactPhotoOperation calls. Updates the associated | 498 // Callback for GetContactPhotoOperation calls. Updates the associated |
506 // Contact and checks for completion. | 499 // Contact and checks for completion. |
507 void HandlePhotoData(contacts::Contact* contact, | 500 void HandlePhotoData(contacts::Contact* contact, |
(...skipping 16 matching lines...) Expand all Loading... |
524 } | 517 } |
525 | 518 |
526 contact->set_raw_untrusted_photo(*download_data); | 519 contact->set_raw_untrusted_photo(*download_data); |
527 CheckCompletion(); | 520 CheckCompletion(); |
528 } | 521 } |
529 | 522 |
530 private: | 523 private: |
531 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; | 524 typedef std::map<contacts::Contact*, std::string> ContactPhotoUrls; |
532 | 525 |
533 GDataContactsService* service_; // not owned | 526 GDataContactsService* service_; // not owned |
534 Profile* profile_; // not owned | |
535 GDataOperationRunner* runner_; // not owned | 527 GDataOperationRunner* runner_; // not owned |
536 | 528 |
537 SuccessCallback success_callback_; | 529 SuccessCallback success_callback_; |
538 FailureCallback failure_callback_; | 530 FailureCallback failure_callback_; |
539 | 531 |
540 base::Time min_update_time_; | 532 base::Time min_update_time_; |
541 | 533 |
542 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; | 534 scoped_ptr<ScopedVector<contacts::Contact> > contacts_; |
543 | 535 |
544 // Map from a contact to the URL at which its photo is located. | 536 // Map from a contact to the URL at which its photo is located. |
(...skipping 10 matching lines...) Expand all Loading... |
555 // Number of in-progress photo downloads. | 547 // Number of in-progress photo downloads. |
556 int num_in_progress_photo_downloads_; | 548 int num_in_progress_photo_downloads_; |
557 | 549 |
558 // Did we encounter a fatal error while downloading a photo? | 550 // Did we encounter a fatal error while downloading a photo? |
559 bool photo_download_failed_; | 551 bool photo_download_failed_; |
560 | 552 |
561 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); | 553 DISALLOW_COPY_AND_ASSIGN(DownloadContactsRequest); |
562 }; | 554 }; |
563 | 555 |
564 GDataContactsService::GDataContactsService(Profile* profile) | 556 GDataContactsService::GDataContactsService(Profile* profile) |
565 : profile_(profile), | 557 : runner_(new GDataOperationRunner(profile)), |
566 runner_(new GDataOperationRunner(profile)), | |
567 max_simultaneous_photo_downloads_(kMaxSimultaneousPhotoDownloads) { | 558 max_simultaneous_photo_downloads_(kMaxSimultaneousPhotoDownloads) { |
568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
569 DCHECK(profile_); | |
570 } | 560 } |
571 | 561 |
572 GDataContactsService::~GDataContactsService() { | 562 GDataContactsService::~GDataContactsService() { |
573 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
574 runner_->CancelAll(); | 564 runner_->CancelAll(); |
575 STLDeleteContainerPointers(requests_.begin(), requests_.end()); | 565 STLDeleteContainerPointers(requests_.begin(), requests_.end()); |
576 requests_.clear(); | 566 requests_.clear(); |
577 } | 567 } |
578 | 568 |
579 GDataAuthService* GDataContactsService::auth_service_for_testing() { | 569 GDataAuthService* GDataContactsService::auth_service_for_testing() { |
580 return runner_->auth_service(); | 570 return runner_->auth_service(); |
581 } | 571 } |
582 | 572 |
583 void GDataContactsService::Initialize() { | 573 void GDataContactsService::Initialize() { |
584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
585 runner_->Initialize(); | 575 runner_->Initialize(); |
586 } | 576 } |
587 | 577 |
588 void GDataContactsService::DownloadContacts(SuccessCallback success_callback, | 578 void GDataContactsService::DownloadContacts(SuccessCallback success_callback, |
589 FailureCallback failure_callback, | 579 FailureCallback failure_callback, |
590 const base::Time& min_update_time) { | 580 const base::Time& min_update_time) { |
591 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 581 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
592 DownloadContactsRequest* request = | 582 DownloadContactsRequest* request = |
593 new DownloadContactsRequest(this, | 583 new DownloadContactsRequest(this, |
594 profile_, | |
595 runner_.get(), | 584 runner_.get(), |
596 success_callback, | 585 success_callback, |
597 failure_callback, | 586 failure_callback, |
598 min_update_time, | 587 min_update_time, |
599 max_simultaneous_photo_downloads_); | 588 max_simultaneous_photo_downloads_); |
600 VLOG(1) << "Starting contacts download with request " << request; | 589 VLOG(1) << "Starting contacts download with request " << request; |
601 requests_.insert(request); | 590 requests_.insert(request); |
602 request->Run(); | 591 request->Run(); |
603 } | 592 } |
604 | 593 |
605 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { | 594 void GDataContactsService::OnRequestComplete(DownloadContactsRequest* request) { |
606 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 595 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
607 DCHECK(request); | 596 DCHECK(request); |
608 VLOG(1) << "Download request " << request << " complete"; | 597 VLOG(1) << "Download request " << request << " complete"; |
609 requests_.erase(request); | 598 requests_.erase(request); |
610 delete request; | 599 delete request; |
611 } | 600 } |
612 | 601 |
613 } // namespace contacts | 602 } // namespace contacts |
OLD | NEW |