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_GDATA_GDATA_CONTACTS_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CONTACTS_SERVICE_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/time.h"
18 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h"
19 #include "googleurl/src/gurl.h"
34 class OperationRunner
;
36 // Interface for fetching a user's Google contacts via the Contacts API
37 // (described at https://developers.google.com/google-apps/contacts/v3/).
38 class GDataContactsServiceInterface
{
40 typedef base::Callback
<void(scoped_ptr
<ScopedVector
<contacts::Contact
> >)>
42 typedef base::Closure FailureCallback
;
44 virtual ~GDataContactsServiceInterface() {}
46 virtual void Initialize() = 0;
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;
56 GDataContactsServiceInterface() {}
59 DISALLOW_COPY_AND_ASSIGN(GDataContactsServiceInterface
);
62 class GDataContactsService
: public GDataContactsServiceInterface
{
64 typedef base::Callback
<std::string(const std::string
&)>
65 RewritePhotoUrlCallback
;
67 explicit GDataContactsService(Profile
* profile
);
68 virtual ~GDataContactsService();
70 AuthService
* auth_service_for_testing();
72 const std::string
& cached_my_contacts_group_id_for_testing() const {
73 return cached_my_contacts_group_id_
;
75 void clear_cached_my_contacts_group_id_for_testing() {
76 cached_my_contacts_group_id_
.clear();
79 void set_max_photo_downloads_per_second_for_testing(int max_downloads
) {
80 max_photo_downloads_per_second_
= max_downloads
;
82 void set_photo_download_timer_interval_for_testing(base::TimeDelta interval
) {
83 photo_download_timer_interval_
= interval
;
85 void set_groups_feed_url_for_testing(const GURL
& url
) {
86 groups_feed_url_for_testing_
= url
;
88 void set_contacts_feed_url_for_testing(const GURL
& url
) {
89 contacts_feed_url_for_testing_
= url
;
91 void set_rewrite_photo_url_callback_for_testing(RewritePhotoUrlCallback cb
) {
92 rewrite_photo_url_callback_for_testing_
= cb
;
95 // Overridden from GDataContactsServiceInterface:
96 virtual void Initialize() OVERRIDE
;
97 virtual void DownloadContacts(SuccessCallback success_callback
,
98 FailureCallback failure_callback
,
99 const base::Time
& min_update_time
) OVERRIDE
;
102 class DownloadContactsRequest
;
104 // Invoked by a download request once it's finished (either successfully or
106 void OnRequestComplete(DownloadContactsRequest
* request
);
108 Profile
* profile_
; // not owned
110 scoped_ptr
<OperationRunner
> runner_
;
112 // Group ID for the "My Contacts" system contacts group.
113 // Cached after a DownloadContactsRequest has completed.
114 std::string cached_my_contacts_group_id_
;
116 // In-progress download requests. Pointers are owned by this class.
117 std::set
<DownloadContactsRequest
*> requests_
;
119 // Maximum number of photos we'll try to download per second (per
120 // DownloadContacts() request).
121 int max_photo_downloads_per_second_
;
123 // Amount of time that we'll wait between waves of photo download requests.
124 // This is usually one second (see |max_photo_downloads_per_second_|) but can
125 // be set to a lower value for tests to make them complete more quickly.
126 base::TimeDelta photo_download_timer_interval_
;
128 // If non-empty, URLs that will be used to fetch feeds.
129 GURL groups_feed_url_for_testing_
;
130 GURL contacts_feed_url_for_testing_
;
132 // Callback that's invoked to rewrite photo URLs for tests.
133 // This is needed for tests that serve static feed data from a host/port
134 // that's only known at runtime.
135 RewritePhotoUrlCallback rewrite_photo_url_callback_for_testing_
;
137 DISALLOW_COPY_AND_ASSIGN(GDataContactsService
);
142 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CONTACTS_SERVICE_H_