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 #include "chrome/browser/google_apis/gdata_contacts_operations.h"
7 #include "chrome/browser/google_apis/time_util.h"
8 #include "googleurl/src/gurl.h"
9 #include "net/base/url_util.h"
11 namespace google_apis
{
15 // URL requesting all contact groups.
16 const char kGetContactGroupsURL
[] =
17 "https://www.google.com/m8/feeds/groups/default/full?alt=json";
19 // URL requesting all contacts.
20 // TODO(derat): Per https://goo.gl/AufHP, "The feed may not contain all of the
21 // user's contacts, because there's a default limit on the number of results
22 // returned." Decide if 10000 is reasonable or not.
23 const char kGetContactsURL
[] =
24 "https://www.google.com/m8/feeds/contacts/default/full"
25 "?alt=json&showdeleted=true&max-results=10000";
27 // Query parameter optionally appended to |kGetContactsURL| to return contacts
28 // from a specific group (as opposed to all contacts).
29 const char kGetContactsGroupParam
[] = "group";
31 // Query parameter optionally appended to |kGetContactsURL| to return only
32 // recently-updated contacts.
33 const char kGetContactsUpdatedMinParam
[] = "updated-min";
37 //========================== GetContactGroupsOperation =========================
39 GetContactGroupsOperation::GetContactGroupsOperation(
40 OperationRegistry
* registry
,
41 net::URLRequestContextGetter
* url_request_context_getter
,
42 const GetDataCallback
& callback
)
43 : GetDataOperation(registry
, url_request_context_getter
, callback
) {
46 GetContactGroupsOperation::~GetContactGroupsOperation() {}
48 GURL
GetContactGroupsOperation::GetURL() const {
49 return !feed_url_for_testing_
.is_empty() ?
50 feed_url_for_testing_
:
51 GURL(kGetContactGroupsURL
);
54 //============================ GetContactsOperation ============================
56 GetContactsOperation::GetContactsOperation(
57 OperationRegistry
* registry
,
58 net::URLRequestContextGetter
* url_request_context_getter
,
59 const std::string
& group_id
,
60 const base::Time
& min_update_time
,
61 const GetDataCallback
& callback
)
62 : GetDataOperation(registry
, url_request_context_getter
, callback
),
64 min_update_time_(min_update_time
) {
67 GetContactsOperation::~GetContactsOperation() {}
69 GURL
GetContactsOperation::GetURL() const {
70 if (!feed_url_for_testing_
.is_empty())
71 return GURL(feed_url_for_testing_
);
73 GURL
url(kGetContactsURL
);
75 if (!group_id_
.empty()) {
76 url
= net::AppendQueryParameter(url
, kGetContactsGroupParam
, group_id_
);
78 if (!min_update_time_
.is_null()) {
79 std::string time_rfc3339
= util::FormatTimeAsString(min_update_time_
);
80 url
= net::AppendQueryParameter(
81 url
, kGetContactsUpdatedMinParam
, time_rfc3339
);
86 //========================== GetContactPhotoOperation ==========================
88 GetContactPhotoOperation::GetContactPhotoOperation(
89 OperationRegistry
* registry
,
90 net::URLRequestContextGetter
* url_request_context_getter
,
91 const GURL
& photo_url
,
92 const GetContentCallback
& callback
)
93 : UrlFetchOperationBase(registry
, url_request_context_getter
),
94 photo_url_(photo_url
),
98 GetContactPhotoOperation::~GetContactPhotoOperation() {}
100 GURL
GetContactPhotoOperation::GetURL() const {
104 void GetContactPhotoOperation::ProcessURLFetchResults(
105 const net::URLFetcher
* source
) {
106 GDataErrorCode code
= static_cast<GDataErrorCode
>(source
->GetResponseCode());
107 scoped_ptr
<std::string
> data(new std::string
);
108 source
->GetResponseAsString(data
.get());
109 callback_
.Run(code
, data
.Pass());
110 OnProcessURLFetchResultsComplete(code
== HTTP_SUCCESS
);
113 void GetContactPhotoOperation::RunCallbackOnPrematureFailure(
114 GDataErrorCode code
) {
115 scoped_ptr
<std::string
> data(new std::string
);
116 callback_
.Run(code
, data
.Pass());
119 } // namespace google_apis