summaryrefslogtreecommitdiff
path: root/chromium/google_apis/drive/gdata_contacts_requests.cc
blob: 11419af449b3dd411daa98e58f3cf6a0cf1f285c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "google_apis/drive/gdata_contacts_requests.h"

#include "google_apis/drive/time_util.h"
#include "net/base/url_util.h"
#include "url/gurl.h"

namespace google_apis {

namespace {

// URL requesting all contact groups.
const char kGetContactGroupsURL[] =
    "https://www.google.com/m8/feeds/groups/default/full?alt=json";

// URL requesting all contacts.
// TODO(derat): Per https://goo.gl/AufHP, "The feed may not contain all of the
// user's contacts, because there's a default limit on the number of results
// returned."  Decide if 10000 is reasonable or not.
const char kGetContactsURL[] =
    "https://www.google.com/m8/feeds/contacts/default/full"
    "?alt=json&showdeleted=true&max-results=10000";

// Query parameter optionally appended to |kGetContactsURL| to return contacts
// from a specific group (as opposed to all contacts).
const char kGetContactsGroupParam[] = "group";

// Query parameter optionally appended to |kGetContactsURL| to return only
// recently-updated contacts.
const char kGetContactsUpdatedMinParam[] = "updated-min";

}  // namespace

//========================== GetContactGroupsRequest =========================

GetContactGroupsRequest::GetContactGroupsRequest(
    RequestSender* runner,
    const GetDataCallback& callback)
    : GetDataRequest(runner, callback) {
}

GetContactGroupsRequest::~GetContactGroupsRequest() {}

GURL GetContactGroupsRequest::GetURL() const {
  return !feed_url_for_testing_.is_empty() ?
         feed_url_for_testing_ :
         GURL(kGetContactGroupsURL);
}

//============================ GetContactsRequest ============================

GetContactsRequest::GetContactsRequest(
    RequestSender* runner,
    const std::string& group_id,
    const base::Time& min_update_time,
    const GetDataCallback& callback)
    : GetDataRequest(runner, callback),
      group_id_(group_id),
      min_update_time_(min_update_time) {
}

GetContactsRequest::~GetContactsRequest() {}

GURL GetContactsRequest::GetURL() const {
  if (!feed_url_for_testing_.is_empty())
    return GURL(feed_url_for_testing_);

  GURL url(kGetContactsURL);

  if (!group_id_.empty()) {
    url = net::AppendQueryParameter(url, kGetContactsGroupParam, group_id_);
  }
  if (!min_update_time_.is_null()) {
    std::string time_rfc3339 = util::FormatTimeAsString(min_update_time_);
    url = net::AppendQueryParameter(
        url, kGetContactsUpdatedMinParam, time_rfc3339);
  }
  return url;
}

//========================== GetContactPhotoRequest ==========================

GetContactPhotoRequest::GetContactPhotoRequest(
    RequestSender* runner,
    const GURL& photo_url,
    const GetContentCallback& callback)
    : UrlFetchRequestBase(runner),
      photo_url_(photo_url),
      callback_(callback) {
}

GetContactPhotoRequest::~GetContactPhotoRequest() {}

GURL GetContactPhotoRequest::GetURL() const {
  return photo_url_;
}

void GetContactPhotoRequest::ProcessURLFetchResults(
    const net::URLFetcher* source) {
  GDataErrorCode code = GetErrorCode();
  scoped_ptr<std::string> data(new std::string(response_writer()->data()));
  callback_.Run(code, data.Pass());
  OnProcessURLFetchResultsComplete();
}

void GetContactPhotoRequest::RunCallbackOnPrematureFailure(
    GDataErrorCode code) {
  scoped_ptr<std::string> data(new std::string);
  callback_.Run(code, data.Pass());
}

}  // namespace google_apis