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.
5 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_
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.h"
17 #include "googleurl/src/gurl.h"
25 namespace google_apis
{
27 class OperationRunner
;
34 // Interface for fetching a user's Google contacts via the Contacts API
35 // (described at https://developers.google.com/google-apps/contacts/v3/).
36 class GDataContactsServiceInterface
{
38 typedef base::Callback
<void(scoped_ptr
<ScopedVector
<contacts::Contact
> >)>
40 typedef base::Closure FailureCallback
;
42 virtual ~GDataContactsServiceInterface() {}
44 virtual void Initialize() = 0;
46 // Downloads all contacts changed at or after |min_update_time| and invokes
47 // the appropriate callback asynchronously on the UI thread when complete. If
48 // min_update_time.is_null() is true, all contacts will be returned.
49 virtual void DownloadContacts(SuccessCallback success_callback
,
50 FailureCallback failure_callback
,
51 const base::Time
& min_update_time
) = 0;
54 GDataContactsServiceInterface() {}
57 DISALLOW_COPY_AND_ASSIGN(GDataContactsServiceInterface
);
60 class GDataContactsService
: public GDataContactsServiceInterface
{
62 typedef base::Callback
<std::string(const std::string
&)>
63 RewritePhotoUrlCallback
;
65 explicit GDataContactsService(Profile
* profile
);
66 virtual ~GDataContactsService();
68 google_apis::AuthService
* auth_service_for_testing();
70 const std::string
& cached_my_contacts_group_id_for_testing() const {
71 return cached_my_contacts_group_id_
;
73 void clear_cached_my_contacts_group_id_for_testing() {
74 cached_my_contacts_group_id_
.clear();
77 void set_max_photo_downloads_per_second_for_testing(int max_downloads
) {
78 max_photo_downloads_per_second_
= max_downloads
;
80 void set_photo_download_timer_interval_for_testing(base::TimeDelta interval
) {
81 photo_download_timer_interval_
= interval
;
83 void set_groups_feed_url_for_testing(const GURL
& url
) {
84 groups_feed_url_for_testing_
= url
;
86 void set_contacts_feed_url_for_testing(const GURL
& url
) {
87 contacts_feed_url_for_testing_
= url
;
89 void set_rewrite_photo_url_callback_for_testing(RewritePhotoUrlCallback cb
) {
90 rewrite_photo_url_callback_for_testing_
= cb
;
93 // Overridden from GDataContactsServiceInterface:
94 virtual void Initialize() OVERRIDE
;
95 virtual void DownloadContacts(SuccessCallback success_callback
,
96 FailureCallback failure_callback
,
97 const base::Time
& min_update_time
) OVERRIDE
;
100 class DownloadContactsRequest
;
102 // Invoked by a download request once it's finished (either successfully or
104 void OnRequestComplete(DownloadContactsRequest
* request
);
106 Profile
* profile_
; // not owned
108 scoped_ptr
<google_apis::OperationRunner
> runner_
;
110 // Group ID for the "My Contacts" system contacts group.
111 // Cached after a DownloadContactsRequest has completed.
112 std::string cached_my_contacts_group_id_
;
114 // In-progress download requests. Pointers are owned by this class.
115 std::set
<DownloadContactsRequest
*> requests_
;
117 // Maximum number of photos we'll try to download per second (per
118 // DownloadContacts() request).
119 int max_photo_downloads_per_second_
;
121 // Amount of time that we'll wait between waves of photo download requests.
122 // This is usually one second (see |max_photo_downloads_per_second_|) but can
123 // be set to a lower value for tests to make them complete more quickly.
124 base::TimeDelta photo_download_timer_interval_
;
126 // If non-empty, URLs that will be used to fetch feeds.
127 GURL groups_feed_url_for_testing_
;
128 GURL contacts_feed_url_for_testing_
;
130 // Callback that's invoked to rewrite photo URLs for tests.
131 // This is needed for tests that serve static feed data from a host/port
132 // that's only known at runtime.
133 RewritePhotoUrlCallback rewrite_photo_url_callback_for_testing_
;
135 DISALLOW_COPY_AND_ASSIGN(GDataContactsService
);
138 } // namespace contacts
140 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GDATA_CONTACTS_SERVICE_H_