Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / google_apis / drive / drive_api_url_generator.cc
blob14da8a3ae7fb0bccb059061d2ea8667eba41d36c
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 "google_apis/drive/drive_api_url_generator.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "google_apis/google_api_keys.h"
11 #include "net/base/escape.h"
12 #include "net/base/url_util.h"
14 namespace google_apis {
16 namespace {
18 // Hard coded URLs for communication with a google drive server.
19 const char kDriveV2AboutUrl[] = "drive/v2/about";
20 const char kDriveV2AppsUrl[] = "drive/v2/apps";
21 const char kDriveV2ChangelistUrl[] = "drive/v2/changes";
22 const char kDriveV2FilesUrl[] = "drive/v2/files";
23 const char kDriveV2FileUrlPrefix[] = "drive/v2/files/";
24 const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children";
25 const char kDriveV2ChildrenUrlForRemovalFormat[] =
26 "drive/v2/files/%s/children/%s";
27 const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy";
28 const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s";
29 const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash";
30 const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files";
31 const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/";
32 const char kDriveV2BatchUploadUrl[] = "upload/drive";
33 const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions";
34 const char kDriveV2DownloadUrlFormat[] = "host/%s";
35 const char kDriveV2ThumbnailUrlFormat[] = "thumb/%s?width=%d&height=%d";
37 // apps.delete and file.authorize API is exposed through a special endpoint
38 // v2internal that is accessible only by the official API key for Chrome.
39 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps";
40 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s";
41 const char kDriveV2FilesAuthorizeUrlFormat[] =
42 "drive/v2internal/files/%s/authorize?appId=%s";
43 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/";
45 GURL AddResumableUploadParam(const GURL& url) {
46 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
49 GURL AddMultipartUploadParam(const GURL& url) {
50 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart");
53 } // namespace
55 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url,
56 const GURL& base_download_url)
57 : base_url_(base_url),
58 base_download_url_(base_download_url) {
59 // Do nothing.
62 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
63 // Do nothing.
66 const char DriveApiUrlGenerator::kBaseUrlForProduction[] =
67 "https://www.googleapis.com";
69 const char DriveApiUrlGenerator::kBaseDownloadUrlForProduction[] =
70 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
71 "https://www.googledrive.com/p/";
72 #else
73 "https://www.googledrive.com";
74 #endif
76 GURL DriveApiUrlGenerator::GetAboutGetUrl() const {
77 return base_url_.Resolve(kDriveV2AboutUrl);
80 GURL DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint) const {
81 return base_url_.Resolve(use_internal_endpoint ?
82 kDriveV2InternalAppsUrl : kDriveV2AppsUrl);
85 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const {
86 return base_url_.Resolve(base::StringPrintf(
87 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str()));
90 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id,
91 bool use_internal_endpoint,
92 const GURL& embed_origin) const {
93 GURL url = base_url_.Resolve(use_internal_endpoint ?
94 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) :
95 kDriveV2FileUrlPrefix + net::EscapePath(file_id));
96 if (!embed_origin.is_empty()) {
97 // Construct a valid serialized embed origin from an url, according to
98 // WD-html5-20110525. Such string has to be built manually, since
99 // GURL::spec() always adds the trailing slash. Moreover, ports are
100 // currently not supported.
101 DCHECK(!embed_origin.has_port());
102 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
103 const std::string serialized_embed_origin =
104 embed_origin.scheme() + "://" + embed_origin.host();
105 url = net::AppendOrReplaceQueryParameter(
106 url, "embedOrigin", serialized_embed_origin);
108 return url;
111 GURL DriveApiUrlGenerator::GetFilesAuthorizeUrl(
112 const std::string& file_id,
113 const std::string& app_id) const {
114 return base_url_.Resolve(base::StringPrintf(kDriveV2FilesAuthorizeUrlFormat,
115 net::EscapePath(file_id).c_str(),
116 net::EscapePath(app_id).c_str()));
119 GURL DriveApiUrlGenerator::GetFilesInsertUrl(
120 const std::string& visibility) const {
121 GURL url = base_url_.Resolve(kDriveV2FilesUrl);
123 if (!visibility.empty())
124 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility);
126 return url;
129 GURL DriveApiUrlGenerator::GetFilesPatchUrl(const std::string& file_id,
130 bool set_modified_date,
131 bool update_viewed_date) const {
132 GURL url =
133 base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
135 // setModifiedDate is "false" by default.
136 if (set_modified_date)
137 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
139 // updateViewedDate is "true" by default.
140 if (!update_viewed_date)
141 url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");
143 return url;
146 GURL DriveApiUrlGenerator::GetFilesCopyUrl(
147 const std::string& file_id,
148 const std::string& visibility) const {
149 GURL url = base_url_.Resolve(base::StringPrintf(
150 kDriveV2FileCopyUrlFormat, net::EscapePath(file_id).c_str()));
152 if (!visibility.empty())
153 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility);
155 return url;
158 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
159 const std::string& page_token,
160 const std::string& q) const {
161 GURL url = base_url_.Resolve(kDriveV2FilesUrl);
163 // maxResults is 100 by default.
164 if (max_results != 100) {
165 url = net::AppendOrReplaceQueryParameter(
166 url, "maxResults", base::IntToString(max_results));
169 if (!page_token.empty())
170 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
172 if (!q.empty())
173 url = net::AppendOrReplaceQueryParameter(url, "q", q);
175 return url;
178 GURL DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string& file_id) const {
179 return base_url_.Resolve(base::StringPrintf(
180 kDriveV2FileDeleteUrlFormat, net::EscapePath(file_id).c_str()));
183 GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
184 return base_url_.Resolve(base::StringPrintf(
185 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str()));
188 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted,
189 int max_results,
190 const std::string& page_token,
191 int64 start_change_id) const {
192 DCHECK_GE(start_change_id, 0);
194 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
196 // includeDeleted is "true" by default.
197 if (!include_deleted)
198 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
200 // maxResults is "100" by default.
201 if (max_results != 100) {
202 url = net::AppendOrReplaceQueryParameter(
203 url, "maxResults", base::IntToString(max_results));
206 if (!page_token.empty())
207 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
209 if (start_change_id > 0)
210 url = net::AppendOrReplaceQueryParameter(
211 url, "startChangeId", base::Int64ToString(start_change_id));
213 return url;
216 GURL DriveApiUrlGenerator::GetChildrenInsertUrl(
217 const std::string& file_id) const {
218 return base_url_.Resolve(base::StringPrintf(
219 kDriveV2ChildrenUrlFormat, net::EscapePath(file_id).c_str()));
222 GURL DriveApiUrlGenerator::GetChildrenDeleteUrl(
223 const std::string& child_id, const std::string& folder_id) const {
224 return base_url_.Resolve(
225 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat,
226 net::EscapePath(folder_id).c_str(),
227 net::EscapePath(child_id).c_str()));
230 GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
231 bool set_modified_date) const {
232 GURL url = AddResumableUploadParam(
233 base_url_.Resolve(kDriveV2UploadNewFileUrl));
235 // setModifiedDate is "false" by default.
236 if (set_modified_date)
237 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
239 return url;
242 GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
243 const std::string& resource_id,
244 bool set_modified_date) const {
245 GURL url = base_url_.Resolve(
246 kDriveV2UploadExistingFileUrlPrefix +
247 net::EscapePath(resource_id));
248 url = AddResumableUploadParam(url);
250 // setModifiedDate is "false" by default.
251 if (set_modified_date)
252 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
254 return url;
257 GURL DriveApiUrlGenerator::GetMultipartUploadNewFileUrl(
258 bool set_modified_date) const {
259 GURL url = AddMultipartUploadParam(
260 base_url_.Resolve(kDriveV2UploadNewFileUrl));
262 // setModifiedDate is "false" by default.
263 if (set_modified_date)
264 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
266 return url;
269 GURL DriveApiUrlGenerator::GetMultipartUploadExistingFileUrl(
270 const std::string& resource_id,
271 bool set_modified_date) const {
272 GURL url = base_url_.Resolve(
273 kDriveV2UploadExistingFileUrlPrefix +
274 net::EscapePath(resource_id));
275 url = AddMultipartUploadParam(url);
277 // setModifiedDate is "false" by default.
278 if (set_modified_date)
279 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
281 return url;
284 GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
285 const std::string& resource_id) const {
286 return base_download_url_.Resolve(base::StringPrintf(
287 kDriveV2DownloadUrlFormat, net::EscapePath(resource_id).c_str()));
290 GURL DriveApiUrlGenerator::GetPermissionsInsertUrl(
291 const std::string& resource_id) const {
292 return base_url_.Resolve(
293 base::StringPrintf(kDriveV2PermissionsUrlFormat,
294 net::EscapePath(resource_id).c_str()));
297 GURL DriveApiUrlGenerator::GetThumbnailUrl(const std::string& resource_id,
298 int width,
299 int height,
300 bool crop) const {
301 GURL url = base_download_url_.Resolve(
302 base::StringPrintf(kDriveV2ThumbnailUrlFormat,
303 net::EscapePath(resource_id).c_str(), width, height));
305 // crop is "false" by default.
306 if (crop)
307 url = net::AppendOrReplaceQueryParameter(url, "crop", "true");
309 return url;
312 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const {
313 return base_url_.Resolve(kDriveV2BatchUploadUrl);
316 } // namespace google_apis