summaryrefslogtreecommitdiffstats
path: root/google_apis/drive/gdata_wapi_url_generator.cc
blob: c1263b71f34b447d3471c5bb213850567ca64757 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// 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_wapi_url_generator.h"

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "net/base/escape.h"
#include "net/base/url_util.h"
#include "url/gurl.h"

namespace google_apis {
namespace {

// Content URL for modification or resource list retrieval in a particular
// directory specified by "%s" which will be replaced with its resource id.
const char kContentURLFormat[] = "/feeds/default/private/full/%s/contents";

// Content URL for removing a resource specified by the latter "%s" from the
// directory specified by the former "%s".
const char kResourceURLForRemovalFormat[] =
    "/feeds/default/private/full/%s/contents/%s";

// URL requesting single resource entry whose resource id is followed by this
// prefix.
const char kGetEditURLPrefix[] = "/feeds/default/private/full/";

// Root resource list url.
const char kResourceListRootURL[] = "/feeds/default/private/full";

// Metadata feed with things like user quota.
const char kAccountMetadataURL[] = "/feeds/metadata/default";

// URL to upload a new file under a particular directory specified by "%s".
const char kInitiateUploadNewFileURLFormat[] =
    "/feeds/upload/create-session/default/private/full/%s/contents";

// URL to upload a file content to overwrite a file whose resource id is
// followed by this prefix.
const char kInitiateUploadExistingFileURLPrefix[] =
    "/feeds/upload/create-session/default/private/full/";

// Maximum number of resource entries to include in a feed.
// Be careful not to use something too small because it might overload the
// server. Be careful not to use something too large because it makes the
// "fetched N items" UI less responsive.
const int kMaxDocumentsPerFeed = 500;
const int kMaxDocumentsPerSearchFeed = 50;

// URL requesting documents list of changes to documents collections.
const char kGetChangesListURL[] = "/feeds/default/private/changes";

}  // namespace

const char GDataWapiUrlGenerator::kBaseUrlForProduction[] =
    "https://docs.google.com/";

const char GDataWapiUrlGenerator::kBaseDownloadUrlForProduction[] =
    "https://www.googledrive.com/host/";

// static
GURL GDataWapiUrlGenerator::AddStandardUrlParams(const GURL& url) {
  GURL result = net::AppendOrReplaceQueryParameter(url, "v", "3");
  result = net::AppendOrReplaceQueryParameter(result, "alt", "json");
  result = net::AppendOrReplaceQueryParameter(result, "showroot", "true");
  return result;
}

// static
GURL GDataWapiUrlGenerator::AddInitiateUploadUrlParams(const GURL& url) {
  GURL result = net::AppendOrReplaceQueryParameter(url, "convert", "false");
  return AddStandardUrlParams(result);
}

// static
GURL GDataWapiUrlGenerator::AddFeedUrlParams(
    const GURL& url,
    int num_items_to_fetch) {
  GURL result = AddStandardUrlParams(url);
  result = net::AppendOrReplaceQueryParameter(result, "showfolders", "true");
  result = net::AppendOrReplaceQueryParameter(result, "include-shared", "true");
  result = net::AppendOrReplaceQueryParameter(
      result, "max-results", base::IntToString(num_items_to_fetch));
  return result;
}

GDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url,
                                             const GURL& base_download_url)
    : base_url_(base_url),
      base_download_url_(base_download_url) {
}

GDataWapiUrlGenerator::~GDataWapiUrlGenerator() {
}

GURL GDataWapiUrlGenerator::GenerateResourceListUrl(
    const GURL& override_url,
    int64 start_changestamp,
    const std::string& search_string,
    const std::string& directory_resource_id) const {
  DCHECK_LE(0, start_changestamp);

  int max_docs = search_string.empty() ? kMaxDocumentsPerFeed :
                                         kMaxDocumentsPerSearchFeed;
  GURL url;
  if (!override_url.is_empty()) {
    // |override_url| specifies the URL of the continuation feed when the feed
    // is broken up to multiple chunks. In this case we must not add the
    // |start_changestamp| that provides the original start point.
    start_changestamp = 0;
    url = override_url;
  } else if (start_changestamp > 0) {
    // The start changestamp shouldn't be used for a search.
    DCHECK(search_string.empty());
    url = base_url_.Resolve(kGetChangesListURL);
  } else if (!directory_resource_id.empty()) {
    url = base_url_.Resolve(
        base::StringPrintf(kContentURLFormat,
                           net::EscapePath(
                               directory_resource_id).c_str()));
  } else {
    url = base_url_.Resolve(kResourceListRootURL);
  }

  url = AddFeedUrlParams(url, max_docs);

  if (start_changestamp) {
    url = net::AppendOrReplaceQueryParameter(
        url, "start-index", base::Int64ToString(start_changestamp));
  }
  if (!search_string.empty()) {
    url = net::AppendOrReplaceQueryParameter(url, "q", search_string);
  }

  return url;
}

GURL GDataWapiUrlGenerator::GenerateSearchByTitleUrl(
    const std::string& title,
    const std::string& directory_resource_id) const {
  DCHECK(!title.empty());

  GURL url = directory_resource_id.empty() ?
      base_url_.Resolve(kResourceListRootURL) :
      base_url_.Resolve(base::StringPrintf(
          kContentURLFormat, net::EscapePath(directory_resource_id).c_str()));
  url = AddFeedUrlParams(url, kMaxDocumentsPerFeed);
  url = net::AppendOrReplaceQueryParameter(url, "title", title);
  url = net::AppendOrReplaceQueryParameter(url, "title-exact", "true");
  return url;
}

GURL GDataWapiUrlGenerator::GenerateEditUrl(
    const std::string& resource_id) const {
  return AddStandardUrlParams(GenerateEditUrlWithoutParams(resource_id));
}

GURL GDataWapiUrlGenerator::GenerateEditUrlWithoutParams(
    const std::string& resource_id) const {
  return base_url_.Resolve(kGetEditURLPrefix + net::EscapePath(resource_id));
}

GURL GDataWapiUrlGenerator::GenerateEditUrlWithEmbedOrigin(
    const std::string& resource_id, const GURL& embed_origin) const {
  GURL url = GenerateEditUrl(resource_id);
  if (!embed_origin.is_empty()) {
    // Construct a valid serialized embed origin from an url, according to
    // WD-html5-20110525. Such string has to be built manually, since
    // GURL::spec() always adds the trailing slash. Moreover, ports are
    // currently not supported.
    DCHECK(!embed_origin.has_port());
    DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
    const std::string serialized_embed_origin =
        embed_origin.scheme() + "://" + embed_origin.host();
    url = net::AppendOrReplaceQueryParameter(
        url, "embedOrigin", serialized_embed_origin);
  }
  return url;
}

GURL GDataWapiUrlGenerator::GenerateContentUrl(
    const std::string& resource_id) const {
  if (resource_id.empty()) {
    // |resource_id| must not be empty. Return an empty GURL as an error.
    return GURL();
  }

  GURL result = base_url_.Resolve(
      base::StringPrintf(kContentURLFormat,
                         net::EscapePath(resource_id).c_str()));
  return AddStandardUrlParams(result);
}

GURL GDataWapiUrlGenerator::GenerateResourceUrlForRemoval(
    const std::string& parent_resource_id,
    const std::string& resource_id) const {
  if (resource_id.empty() || parent_resource_id.empty()) {
    // Both |resource_id| and |parent_resource_id| must be non-empty.
    // Return an empty GURL as an error.
    return GURL();
  }

  GURL result = base_url_.Resolve(
      base::StringPrintf(kResourceURLForRemovalFormat,
                         net::EscapePath(parent_resource_id).c_str(),
                         net::EscapePath(resource_id).c_str()));
  return AddStandardUrlParams(result);
}

GURL GDataWapiUrlGenerator::GenerateInitiateUploadNewFileUrl(
    const std::string& parent_resource_id) const {
  GURL result = base_url_.Resolve(
      base::StringPrintf(kInitiateUploadNewFileURLFormat,
                         net::EscapePath(parent_resource_id).c_str()));
  return AddInitiateUploadUrlParams(result);
}

GURL GDataWapiUrlGenerator::GenerateInitiateUploadExistingFileUrl(
    const std::string& resource_id) const {
  GURL result = base_url_.Resolve(
      kInitiateUploadExistingFileURLPrefix + net::EscapePath(resource_id));
  return AddInitiateUploadUrlParams(result);
}

GURL GDataWapiUrlGenerator::GenerateResourceListRootUrl() const {
  return AddStandardUrlParams(base_url_.Resolve(kResourceListRootURL));
}

GURL GDataWapiUrlGenerator::GenerateAccountMetadataUrl(
    bool include_installed_apps) const {
  GURL result = AddStandardUrlParams(base_url_.Resolve(kAccountMetadataURL));
  if (include_installed_apps) {
    result = net::AppendOrReplaceQueryParameter(
        result, "include-installed-apps", "true");
  }
  return result;
}

GURL GDataWapiUrlGenerator::GenerateDownloadFileUrl(
    const std::string& resource_id) const {
  // Strip the file type prefix before the colon character.
  size_t colon = resource_id.find(':');
  return base_download_url_.Resolve(net::EscapePath(
      colon == std::string::npos ? resource_id
                                 : resource_id.substr(colon + 1)));
}

}  // namespace google_apis