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
{
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");
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
) {
62 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
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/";
73 "https://www.googledrive.com";
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
);
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() const {
120 return base_url_
.Resolve(kDriveV2FilesUrl
);
123 GURL
DriveApiUrlGenerator::GetFilesPatchUrl(const std::string
& file_id
,
124 bool set_modified_date
,
125 bool update_viewed_date
) const {
127 base_url_
.Resolve(kDriveV2FileUrlPrefix
+ net::EscapePath(file_id
));
129 // setModifiedDate is "false" by default.
130 if (set_modified_date
)
131 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
133 // updateViewedDate is "true" by default.
134 if (!update_viewed_date
)
135 url
= net::AppendOrReplaceQueryParameter(url
, "updateViewedDate", "false");
140 GURL
DriveApiUrlGenerator::GetFilesCopyUrl(const std::string
& file_id
) const {
141 return base_url_
.Resolve(base::StringPrintf(
142 kDriveV2FileCopyUrlFormat
, net::EscapePath(file_id
).c_str()));
145 GURL
DriveApiUrlGenerator::GetFilesListUrl(int max_results
,
146 const std::string
& page_token
,
147 const std::string
& q
) const {
148 GURL url
= base_url_
.Resolve(kDriveV2FilesUrl
);
150 // maxResults is 100 by default.
151 if (max_results
!= 100) {
152 url
= net::AppendOrReplaceQueryParameter(
153 url
, "maxResults", base::IntToString(max_results
));
156 if (!page_token
.empty())
157 url
= net::AppendOrReplaceQueryParameter(url
, "pageToken", page_token
);
160 url
= net::AppendOrReplaceQueryParameter(url
, "q", q
);
165 GURL
DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string
& file_id
) const {
166 return base_url_
.Resolve(base::StringPrintf(
167 kDriveV2FileDeleteUrlFormat
, net::EscapePath(file_id
).c_str()));
170 GURL
DriveApiUrlGenerator::GetFilesTrashUrl(const std::string
& file_id
) const {
171 return base_url_
.Resolve(base::StringPrintf(
172 kDriveV2FileTrashUrlFormat
, net::EscapePath(file_id
).c_str()));
175 GURL
DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted
,
177 const std::string
& page_token
,
178 int64 start_change_id
) const {
179 DCHECK_GE(start_change_id
, 0);
181 GURL url
= base_url_
.Resolve(kDriveV2ChangelistUrl
);
183 // includeDeleted is "true" by default.
184 if (!include_deleted
)
185 url
= net::AppendOrReplaceQueryParameter(url
, "includeDeleted", "false");
187 // maxResults is "100" by default.
188 if (max_results
!= 100) {
189 url
= net::AppendOrReplaceQueryParameter(
190 url
, "maxResults", base::IntToString(max_results
));
193 if (!page_token
.empty())
194 url
= net::AppendOrReplaceQueryParameter(url
, "pageToken", page_token
);
196 if (start_change_id
> 0)
197 url
= net::AppendOrReplaceQueryParameter(
198 url
, "startChangeId", base::Int64ToString(start_change_id
));
203 GURL
DriveApiUrlGenerator::GetChildrenInsertUrl(
204 const std::string
& file_id
) const {
205 return base_url_
.Resolve(base::StringPrintf(
206 kDriveV2ChildrenUrlFormat
, net::EscapePath(file_id
).c_str()));
209 GURL
DriveApiUrlGenerator::GetChildrenDeleteUrl(
210 const std::string
& child_id
, const std::string
& folder_id
) const {
211 return base_url_
.Resolve(
212 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat
,
213 net::EscapePath(folder_id
).c_str(),
214 net::EscapePath(child_id
).c_str()));
217 GURL
DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
218 bool set_modified_date
) const {
219 GURL url
= AddResumableUploadParam(
220 base_url_
.Resolve(kDriveV2UploadNewFileUrl
));
222 // setModifiedDate is "false" by default.
223 if (set_modified_date
)
224 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
229 GURL
DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
230 const std::string
& resource_id
,
231 bool set_modified_date
) const {
232 GURL url
= base_url_
.Resolve(
233 kDriveV2UploadExistingFileUrlPrefix
+
234 net::EscapePath(resource_id
));
235 url
= AddResumableUploadParam(url
);
237 // setModifiedDate is "false" by default.
238 if (set_modified_date
)
239 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
244 GURL
DriveApiUrlGenerator::GetMultipartUploadNewFileUrl(
245 bool set_modified_date
) const {
246 GURL url
= AddMultipartUploadParam(
247 base_url_
.Resolve(kDriveV2UploadNewFileUrl
));
249 // setModifiedDate is "false" by default.
250 if (set_modified_date
)
251 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
256 GURL
DriveApiUrlGenerator::GetMultipartUploadExistingFileUrl(
257 const std::string
& resource_id
,
258 bool set_modified_date
) const {
259 GURL url
= base_url_
.Resolve(
260 kDriveV2UploadExistingFileUrlPrefix
+
261 net::EscapePath(resource_id
));
262 url
= AddMultipartUploadParam(url
);
264 // setModifiedDate is "false" by default.
265 if (set_modified_date
)
266 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
271 GURL
DriveApiUrlGenerator::GenerateDownloadFileUrl(
272 const std::string
& resource_id
) const {
273 return base_download_url_
.Resolve(base::StringPrintf(
274 kDriveV2DownloadUrlFormat
, net::EscapePath(resource_id
).c_str()));
277 GURL
DriveApiUrlGenerator::GetPermissionsInsertUrl(
278 const std::string
& resource_id
) const {
279 return base_url_
.Resolve(
280 base::StringPrintf(kDriveV2PermissionsUrlFormat
,
281 net::EscapePath(resource_id
).c_str()));
284 GURL
DriveApiUrlGenerator::GetThumbnailUrl(const std::string
& resource_id
,
287 return base_download_url_
.Resolve(
288 base::StringPrintf(kDriveV2ThumbnailUrlFormat
,
289 net::EscapePath(resource_id
).c_str(), width
, height
));
292 GURL
DriveApiUrlGenerator::GetBatchUploadUrl() const {
293 return base_url_
.Resolve(kDriveV2BatchUploadUrl
);
296 } // namespace google_apis