Home | History | Annotate | Download | only in contacts
      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 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_
      6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/scoped_vector.h"
     16 #include "base/time/time.h"
     17 #include "url/gurl.h"
     18 
     19 class Profile;
     20 
     21 namespace base {
     22 class Value;
     23 }  // namespace base
     24 
     25 namespace google_apis {
     26 class AuthServiceInterface;
     27 class RequestSender;
     28 }  // namespace google_apis
     29 
     30 namespace net {
     31 class URLRequestContextGetter;
     32 }  // namespace net
     33 
     34 namespace contacts {
     35 
     36 class Contact;
     37 
     38 // Interface for fetching a user's Google contacts via the Contacts API
     39 // (described at https://developers.google.com/google-apps/contacts/v3/).
     40 class GDataContactsServiceInterface {
     41  public:
     42   typedef base::Callback<void(scoped_ptr<ScopedVector<contacts::Contact> >)>
     43       SuccessCallback;
     44   typedef base::Closure FailureCallback;
     45 
     46   virtual ~GDataContactsServiceInterface() {}
     47 
     48   // Downloads all contacts changed at or after |min_update_time| and invokes
     49   // the appropriate callback asynchronously on the UI thread when complete.  If
     50   // min_update_time.is_null() is true, all contacts will be returned.
     51   virtual void DownloadContacts(SuccessCallback success_callback,
     52                                 FailureCallback failure_callback,
     53                                 const base::Time& min_update_time) = 0;
     54 
     55  protected:
     56   GDataContactsServiceInterface() {}
     57 
     58  private:
     59   DISALLOW_COPY_AND_ASSIGN(GDataContactsServiceInterface);
     60 };
     61 
     62 class GDataContactsService : public GDataContactsServiceInterface {
     63  public:
     64   typedef base::Callback<std::string(const std::string&)>
     65       RewritePhotoUrlCallback;
     66 
     67   GDataContactsService(
     68       net::URLRequestContextGetter* url_request_context_getter,
     69       google_apis::AuthServiceInterface* auth_service);
     70   virtual ~GDataContactsService();
     71 
     72   const std::string& cached_my_contacts_group_id_for_testing() const {
     73     return cached_my_contacts_group_id_;
     74   }
     75   void clear_cached_my_contacts_group_id_for_testing() {
     76     cached_my_contacts_group_id_.clear();
     77   }
     78 
     79   void set_max_photo_downloads_per_second_for_testing(int max_downloads) {
     80     max_photo_downloads_per_second_ = max_downloads;
     81   }
     82   void set_photo_download_timer_interval_for_testing(base::TimeDelta interval) {
     83     photo_download_timer_interval_ = interval;
     84   }
     85   void set_groups_feed_url_for_testing(const GURL& url) {
     86     groups_feed_url_for_testing_ = url;
     87   }
     88   void set_contacts_feed_url_for_testing(const GURL& url) {
     89     contacts_feed_url_for_testing_ = url;
     90   }
     91   void set_rewrite_photo_url_callback_for_testing(RewritePhotoUrlCallback cb) {
     92     rewrite_photo_url_callback_for_testing_ = cb;
     93   }
     94 
     95   // Overridden from GDataContactsServiceInterface:
     96   virtual void DownloadContacts(SuccessCallback success_callback,
     97                                 FailureCallback failure_callback,
     98                                 const base::Time& min_update_time) OVERRIDE;
     99 
    100  private:
    101   class DownloadContactsRequest;
    102 
    103   // Invoked by a download request once it's finished (either successfully or
    104   // unsuccessfully).
    105   void OnRequestComplete(DownloadContactsRequest* request);
    106 
    107   scoped_ptr<google_apis::RequestSender> sender_;
    108 
    109   // Group ID for the "My Contacts" system contacts group.
    110   // Cached after a DownloadContactsRequest has completed.
    111   std::string cached_my_contacts_group_id_;
    112 
    113   // In-progress download requests.  Pointers are owned by this class.
    114   std::set<DownloadContactsRequest*> requests_;
    115 
    116   // Maximum number of photos we'll try to download per second (per
    117   // DownloadContacts() request).
    118   int max_photo_downloads_per_second_;
    119 
    120   // Amount of time that we'll wait between waves of photo download requests.
    121   // This is usually one second (see |max_photo_downloads_per_second_|) but can
    122   // be set to a lower value for tests to make them complete more quickly.
    123   base::TimeDelta photo_download_timer_interval_;
    124 
    125   // If non-empty, URLs that will be used to fetch feeds.
    126   GURL groups_feed_url_for_testing_;
    127   GURL contacts_feed_url_for_testing_;
    128 
    129   // Callback that's invoked to rewrite photo URLs for tests.
    130   // This is needed for tests that serve static feed data from a host/port
    131   // that's only known at runtime.
    132   RewritePhotoUrlCallback rewrite_photo_url_callback_for_testing_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(GDataContactsService);
    135 };
    136 
    137 }  // namespace contacts
    138 
    139 #endif  // CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_
    140