Home | History | Annotate | Download | only in google_apis
      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 #include "chrome/browser/google_apis/gdata_contacts_requests.h"
      6 
      7 #include "chrome/browser/google_apis/time_util.h"
      8 #include "net/base/url_util.h"
      9 #include "url/gurl.h"
     10 
     11 namespace google_apis {
     12 
     13 namespace {
     14 
     15 // URL requesting all contact groups.
     16 const char kGetContactGroupsURL[] =
     17     "https://www.google.com/m8/feeds/groups/default/full?alt=json";
     18 
     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";
     26 
     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";
     30 
     31 // Query parameter optionally appended to |kGetContactsURL| to return only
     32 // recently-updated contacts.
     33 const char kGetContactsUpdatedMinParam[] = "updated-min";
     34 
     35 }  // namespace
     36 
     37 //========================== GetContactGroupsRequest =========================
     38 
     39 GetContactGroupsRequest::GetContactGroupsRequest(
     40     RequestSender* runner,
     41     const GetDataCallback& callback)
     42     : GetDataRequest(runner, callback) {
     43 }
     44 
     45 GetContactGroupsRequest::~GetContactGroupsRequest() {}
     46 
     47 GURL GetContactGroupsRequest::GetURL() const {
     48   return !feed_url_for_testing_.is_empty() ?
     49          feed_url_for_testing_ :
     50          GURL(kGetContactGroupsURL);
     51 }
     52 
     53 //============================ GetContactsRequest ============================
     54 
     55 GetContactsRequest::GetContactsRequest(
     56     RequestSender* runner,
     57     const std::string& group_id,
     58     const base::Time& min_update_time,
     59     const GetDataCallback& callback)
     60     : GetDataRequest(runner, callback),
     61       group_id_(group_id),
     62       min_update_time_(min_update_time) {
     63 }
     64 
     65 GetContactsRequest::~GetContactsRequest() {}
     66 
     67 GURL GetContactsRequest::GetURL() const {
     68   if (!feed_url_for_testing_.is_empty())
     69     return GURL(feed_url_for_testing_);
     70 
     71   GURL url(kGetContactsURL);
     72 
     73   if (!group_id_.empty()) {
     74     url = net::AppendQueryParameter(url, kGetContactsGroupParam, group_id_);
     75   }
     76   if (!min_update_time_.is_null()) {
     77     std::string time_rfc3339 = util::FormatTimeAsString(min_update_time_);
     78     url = net::AppendQueryParameter(
     79         url, kGetContactsUpdatedMinParam, time_rfc3339);
     80   }
     81   return url;
     82 }
     83 
     84 //========================== GetContactPhotoRequest ==========================
     85 
     86 GetContactPhotoRequest::GetContactPhotoRequest(
     87     RequestSender* runner,
     88     const GURL& photo_url,
     89     const GetContentCallback& callback)
     90     : UrlFetchRequestBase(runner),
     91       photo_url_(photo_url),
     92       callback_(callback) {
     93 }
     94 
     95 GetContactPhotoRequest::~GetContactPhotoRequest() {}
     96 
     97 GURL GetContactPhotoRequest::GetURL() const {
     98   return photo_url_;
     99 }
    100 
    101 void GetContactPhotoRequest::ProcessURLFetchResults(
    102     const net::URLFetcher* source) {
    103   GDataErrorCode code = GetErrorCode(source);
    104   scoped_ptr<std::string> data(new std::string);
    105   source->GetResponseAsString(data.get());
    106   callback_.Run(code, data.Pass());
    107   OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS);
    108 }
    109 
    110 void GetContactPhotoRequest::RunCallbackOnPrematureFailure(
    111     GDataErrorCode code) {
    112   scoped_ptr<std::string> data(new std::string);
    113   callback_.Run(code, data.Pass());
    114 }
    115 
    116 }  // namespace google_apis
    117