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
[] =
32 "/upload/drive/v2/files/";
33 const char kDriveV2PermissionsUrlFormat
[] = "/drive/v2/files/%s/permissions";
35 // apps.delete and file.authorize API is exposed through a special endpoint
36 // v2internal that is accessible only by the official API key for Chrome.
37 const char kDriveV2InternalAppsUrl
[] = "/drive/v2internal/apps";
38 const char kDriveV2AppsDeleteUrlFormat
[] = "/drive/v2internal/apps/%s";
39 const char kDriveV2FilesAuthorizeUrlFormat
[] =
40 "/drive/v2internal/files/%s/authorize?appId=%s";
41 const char kDriveV2InternalFileUrlPrefix
[] = "/drive/v2internal/files/";
43 GURL
AddResumableUploadParam(const GURL
& url
) {
44 return net::AppendOrReplaceQueryParameter(url
, "uploadType", "resumable");
47 GURL
AddMultipartUploadParam(const GURL
& url
) {
48 return net::AppendOrReplaceQueryParameter(url
, "uploadType", "multipart");
53 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL
& base_url
,
54 const GURL
& base_download_url
)
55 : base_url_(base_url
),
56 base_download_url_(base_download_url
) {
60 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
64 const char DriveApiUrlGenerator::kBaseUrlForProduction
[] =
65 "https://www.googleapis.com";
66 const char DriveApiUrlGenerator::kBaseDownloadUrlForProduction
[] =
67 "https://www.googledrive.com/host/";
69 GURL
DriveApiUrlGenerator::GetAboutGetUrl() const {
70 return base_url_
.Resolve(kDriveV2AboutUrl
);
73 GURL
DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint
) const {
74 return base_url_
.Resolve(use_internal_endpoint
?
75 kDriveV2InternalAppsUrl
: kDriveV2AppsUrl
);
78 GURL
DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string
& app_id
) const {
79 return base_url_
.Resolve(base::StringPrintf(
80 kDriveV2AppsDeleteUrlFormat
, net::EscapePath(app_id
).c_str()));
83 GURL
DriveApiUrlGenerator::GetFilesGetUrl(const std::string
& file_id
,
84 bool use_internal_endpoint
,
85 const GURL
& embed_origin
) const {
86 GURL url
= base_url_
.Resolve(use_internal_endpoint
?
87 kDriveV2InternalFileUrlPrefix
+ net::EscapePath(file_id
) :
88 kDriveV2FileUrlPrefix
+ net::EscapePath(file_id
));
89 if (!embed_origin
.is_empty()) {
90 // Construct a valid serialized embed origin from an url, according to
91 // WD-html5-20110525. Such string has to be built manually, since
92 // GURL::spec() always adds the trailing slash. Moreover, ports are
93 // currently not supported.
94 DCHECK(!embed_origin
.has_port());
95 DCHECK(!embed_origin
.has_path() || embed_origin
.path() == "/");
96 const std::string serialized_embed_origin
=
97 embed_origin
.scheme() + "://" + embed_origin
.host();
98 url
= net::AppendOrReplaceQueryParameter(
99 url
, "embedOrigin", serialized_embed_origin
);
104 GURL
DriveApiUrlGenerator::GetFilesAuthorizeUrl(
105 const std::string
& file_id
,
106 const std::string
& app_id
) const {
107 return base_url_
.Resolve(base::StringPrintf(kDriveV2FilesAuthorizeUrlFormat
,
108 net::EscapePath(file_id
).c_str(),
109 net::EscapePath(app_id
).c_str()));
112 GURL
DriveApiUrlGenerator::GetFilesInsertUrl() const {
113 return base_url_
.Resolve(kDriveV2FilesUrl
);
116 GURL
DriveApiUrlGenerator::GetFilesPatchUrl(const std::string
& file_id
,
117 bool set_modified_date
,
118 bool update_viewed_date
) const {
120 base_url_
.Resolve(kDriveV2FileUrlPrefix
+ net::EscapePath(file_id
));
122 // setModifiedDate is "false" by default.
123 if (set_modified_date
)
124 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
126 // updateViewedDate is "true" by default.
127 if (!update_viewed_date
)
128 url
= net::AppendOrReplaceQueryParameter(url
, "updateViewedDate", "false");
133 GURL
DriveApiUrlGenerator::GetFilesCopyUrl(const std::string
& file_id
) const {
134 return base_url_
.Resolve(base::StringPrintf(
135 kDriveV2FileCopyUrlFormat
, net::EscapePath(file_id
).c_str()));
138 GURL
DriveApiUrlGenerator::GetFilesListUrl(int max_results
,
139 const std::string
& page_token
,
140 const std::string
& q
) const {
141 GURL url
= base_url_
.Resolve(kDriveV2FilesUrl
);
143 // maxResults is 100 by default.
144 if (max_results
!= 100) {
145 url
= net::AppendOrReplaceQueryParameter(
146 url
, "maxResults", base::IntToString(max_results
));
149 if (!page_token
.empty())
150 url
= net::AppendOrReplaceQueryParameter(url
, "pageToken", page_token
);
153 url
= net::AppendOrReplaceQueryParameter(url
, "q", q
);
158 GURL
DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string
& file_id
) const {
159 return base_url_
.Resolve(base::StringPrintf(
160 kDriveV2FileDeleteUrlFormat
, net::EscapePath(file_id
).c_str()));
163 GURL
DriveApiUrlGenerator::GetFilesTrashUrl(const std::string
& file_id
) const {
164 return base_url_
.Resolve(base::StringPrintf(
165 kDriveV2FileTrashUrlFormat
, net::EscapePath(file_id
).c_str()));
168 GURL
DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted
,
170 const std::string
& page_token
,
171 int64 start_change_id
) const {
172 DCHECK_GE(start_change_id
, 0);
174 GURL url
= base_url_
.Resolve(kDriveV2ChangelistUrl
);
176 // includeDeleted is "true" by default.
177 if (!include_deleted
)
178 url
= net::AppendOrReplaceQueryParameter(url
, "includeDeleted", "false");
180 // maxResults is "100" by default.
181 if (max_results
!= 100) {
182 url
= net::AppendOrReplaceQueryParameter(
183 url
, "maxResults", base::IntToString(max_results
));
186 if (!page_token
.empty())
187 url
= net::AppendOrReplaceQueryParameter(url
, "pageToken", page_token
);
189 if (start_change_id
> 0)
190 url
= net::AppendOrReplaceQueryParameter(
191 url
, "startChangeId", base::Int64ToString(start_change_id
));
196 GURL
DriveApiUrlGenerator::GetChildrenInsertUrl(
197 const std::string
& file_id
) const {
198 return base_url_
.Resolve(base::StringPrintf(
199 kDriveV2ChildrenUrlFormat
, net::EscapePath(file_id
).c_str()));
202 GURL
DriveApiUrlGenerator::GetChildrenDeleteUrl(
203 const std::string
& child_id
, const std::string
& folder_id
) const {
204 return base_url_
.Resolve(
205 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat
,
206 net::EscapePath(folder_id
).c_str(),
207 net::EscapePath(child_id
).c_str()));
210 GURL
DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
211 bool set_modified_date
) const {
212 GURL url
= AddResumableUploadParam(
213 base_url_
.Resolve(kDriveV2UploadNewFileUrl
));
215 // setModifiedDate is "false" by default.
216 if (set_modified_date
)
217 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
222 GURL
DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
223 const std::string
& resource_id
,
224 bool set_modified_date
) const {
225 GURL url
= base_url_
.Resolve(
226 kDriveV2UploadExistingFileUrlPrefix
+
227 net::EscapePath(resource_id
));
228 url
= AddResumableUploadParam(url
);
230 // setModifiedDate is "false" by default.
231 if (set_modified_date
)
232 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
237 GURL
DriveApiUrlGenerator::GetMultipartUploadNewFileUrl(
238 bool set_modified_date
) const {
239 GURL url
= AddMultipartUploadParam(
240 base_url_
.Resolve(kDriveV2UploadNewFileUrl
));
242 // setModifiedDate is "false" by default.
243 if (set_modified_date
)
244 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
249 GURL
DriveApiUrlGenerator::GetMultipartUploadExistingFileUrl(
250 const std::string
& resource_id
,
251 bool set_modified_date
) const {
252 GURL url
= base_url_
.Resolve(
253 kDriveV2UploadExistingFileUrlPrefix
+
254 net::EscapePath(resource_id
));
255 url
= AddMultipartUploadParam(url
);
257 // setModifiedDate is "false" by default.
258 if (set_modified_date
)
259 url
= net::AppendOrReplaceQueryParameter(url
, "setModifiedDate", "true");
264 GURL
DriveApiUrlGenerator::GenerateDownloadFileUrl(
265 const std::string
& resource_id
) const {
266 return base_download_url_
.Resolve(net::EscapePath(resource_id
));
269 GURL
DriveApiUrlGenerator::GetPermissionsInsertUrl(
270 const std::string
& resource_id
) const {
271 return base_url_
.Resolve(
272 base::StringPrintf(kDriveV2PermissionsUrlFormat
,
273 net::EscapePath(resource_id
).c_str()));
276 } // namespace google_apis