Port Android relocation packer to chromium build
[chromium-blink-merge.git] / google_apis / drive / drive_api_url_generator.cc
blob285e7540cb889c3974b54ed071156a13df38ee3b
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 kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions";
33 const char kDriveV2DownloadUrlFormat[] = "host/%s";
34 const char kDriveV2ThumbnailUrlFormat[] = "thumb/%s?width=%d&height=%d";
36 // apps.delete and file.authorize API is exposed through a special endpoint
37 // v2internal that is accessible only by the official API key for Chrome.
38 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps";
39 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s";
40 const char kDriveV2FilesAuthorizeUrlFormat[] =
41 "drive/v2internal/files/%s/authorize?appId=%s";
42 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/";
44 GURL AddResumableUploadParam(const GURL& url) {
45 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
48 GURL AddMultipartUploadParam(const GURL& url) {
49 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart");
52 } // namespace
54 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url,
55 const GURL& base_download_url)
56 : base_url_(base_url),
57 base_download_url_(base_download_url) {
58 // Do nothing.
61 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
62 // Do nothing.
65 const char DriveApiUrlGenerator::kBaseUrlForProduction[] =
66 "https://www.googleapis.com";
68 const char DriveApiUrlGenerator::kBaseDownloadUrlForProduction[] =
69 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
70 "https://www.googledrive.com/p/";
71 #else
72 "https://www.googledrive.com";
73 #endif
75 GURL DriveApiUrlGenerator::GetAboutGetUrl() const {
76 return base_url_.Resolve(kDriveV2AboutUrl);
79 GURL DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint) const {
80 return base_url_.Resolve(use_internal_endpoint ?
81 kDriveV2InternalAppsUrl : kDriveV2AppsUrl);
84 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const {
85 return base_url_.Resolve(base::StringPrintf(
86 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str()));
89 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id,
90 bool use_internal_endpoint,
91 const GURL& embed_origin) const {
92 GURL url = base_url_.Resolve(use_internal_endpoint ?
93 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) :
94 kDriveV2FileUrlPrefix + net::EscapePath(file_id));
95 if (!embed_origin.is_empty()) {
96 // Construct a valid serialized embed origin from an url, according to
97 // WD-html5-20110525. Such string has to be built manually, since
98 // GURL::spec() always adds the trailing slash. Moreover, ports are
99 // currently not supported.
100 DCHECK(!embed_origin.has_port());
101 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
102 const std::string serialized_embed_origin =
103 embed_origin.scheme() + "://" + embed_origin.host();
104 url = net::AppendOrReplaceQueryParameter(
105 url, "embedOrigin", serialized_embed_origin);
107 return url;
110 GURL DriveApiUrlGenerator::GetFilesAuthorizeUrl(
111 const std::string& file_id,
112 const std::string& app_id) const {
113 return base_url_.Resolve(base::StringPrintf(kDriveV2FilesAuthorizeUrlFormat,
114 net::EscapePath(file_id).c_str(),
115 net::EscapePath(app_id).c_str()));
118 GURL DriveApiUrlGenerator::GetFilesInsertUrl() const {
119 return base_url_.Resolve(kDriveV2FilesUrl);
122 GURL DriveApiUrlGenerator::GetFilesPatchUrl(const std::string& file_id,
123 bool set_modified_date,
124 bool update_viewed_date) const {
125 GURL url =
126 base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
128 // setModifiedDate is "false" by default.
129 if (set_modified_date)
130 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
132 // updateViewedDate is "true" by default.
133 if (!update_viewed_date)
134 url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");
136 return url;
139 GURL DriveApiUrlGenerator::GetFilesCopyUrl(const std::string& file_id) const {
140 return base_url_.Resolve(base::StringPrintf(
141 kDriveV2FileCopyUrlFormat, net::EscapePath(file_id).c_str()));
144 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
145 const std::string& page_token,
146 const std::string& q) const {
147 GURL url = base_url_.Resolve(kDriveV2FilesUrl);
149 // maxResults is 100 by default.
150 if (max_results != 100) {
151 url = net::AppendOrReplaceQueryParameter(
152 url, "maxResults", base::IntToString(max_results));
155 if (!page_token.empty())
156 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
158 if (!q.empty())
159 url = net::AppendOrReplaceQueryParameter(url, "q", q);
161 return url;
164 GURL DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string& file_id) const {
165 return base_url_.Resolve(base::StringPrintf(
166 kDriveV2FileDeleteUrlFormat, net::EscapePath(file_id).c_str()));
169 GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
170 return base_url_.Resolve(base::StringPrintf(
171 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str()));
174 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted,
175 int max_results,
176 const std::string& page_token,
177 int64 start_change_id) const {
178 DCHECK_GE(start_change_id, 0);
180 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
182 // includeDeleted is "true" by default.
183 if (!include_deleted)
184 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
186 // maxResults is "100" by default.
187 if (max_results != 100) {
188 url = net::AppendOrReplaceQueryParameter(
189 url, "maxResults", base::IntToString(max_results));
192 if (!page_token.empty())
193 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
195 if (start_change_id > 0)
196 url = net::AppendOrReplaceQueryParameter(
197 url, "startChangeId", base::Int64ToString(start_change_id));
199 return url;
202 GURL DriveApiUrlGenerator::GetChildrenInsertUrl(
203 const std::string& file_id) const {
204 return base_url_.Resolve(base::StringPrintf(
205 kDriveV2ChildrenUrlFormat, net::EscapePath(file_id).c_str()));
208 GURL DriveApiUrlGenerator::GetChildrenDeleteUrl(
209 const std::string& child_id, const std::string& folder_id) const {
210 return base_url_.Resolve(
211 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat,
212 net::EscapePath(folder_id).c_str(),
213 net::EscapePath(child_id).c_str()));
216 GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
217 bool set_modified_date) const {
218 GURL url = AddResumableUploadParam(
219 base_url_.Resolve(kDriveV2UploadNewFileUrl));
221 // setModifiedDate is "false" by default.
222 if (set_modified_date)
223 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
225 return url;
228 GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
229 const std::string& resource_id,
230 bool set_modified_date) const {
231 GURL url = base_url_.Resolve(
232 kDriveV2UploadExistingFileUrlPrefix +
233 net::EscapePath(resource_id));
234 url = AddResumableUploadParam(url);
236 // setModifiedDate is "false" by default.
237 if (set_modified_date)
238 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
240 return url;
243 GURL DriveApiUrlGenerator::GetMultipartUploadNewFileUrl(
244 bool set_modified_date) const {
245 GURL url = AddMultipartUploadParam(
246 base_url_.Resolve(kDriveV2UploadNewFileUrl));
248 // setModifiedDate is "false" by default.
249 if (set_modified_date)
250 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
252 return url;
255 GURL DriveApiUrlGenerator::GetMultipartUploadExistingFileUrl(
256 const std::string& resource_id,
257 bool set_modified_date) const {
258 GURL url = base_url_.Resolve(
259 kDriveV2UploadExistingFileUrlPrefix +
260 net::EscapePath(resource_id));
261 url = AddMultipartUploadParam(url);
263 // setModifiedDate is "false" by default.
264 if (set_modified_date)
265 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
267 return url;
270 GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
271 const std::string& resource_id) const {
272 return base_download_url_.Resolve(base::StringPrintf(
273 kDriveV2DownloadUrlFormat, net::EscapePath(resource_id).c_str()));
276 GURL DriveApiUrlGenerator::GetPermissionsInsertUrl(
277 const std::string& resource_id) const {
278 return base_url_.Resolve(
279 base::StringPrintf(kDriveV2PermissionsUrlFormat,
280 net::EscapePath(resource_id).c_str()));
283 GURL DriveApiUrlGenerator::GetThumbnailUrl(const std::string& resource_id,
284 int width,
285 int height) const {
286 return base_download_url_.Resolve(
287 base::StringPrintf(kDriveV2ThumbnailUrlFormat,
288 net::EscapePath(resource_id).c_str(), width, height));
291 } // namespace google_apis