OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/drive/drive_api_url_generator.h" | 5 #include "google_apis/drive/drive_api_url_generator.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "google_apis/drive/drive_switches.h" |
10 #include "google_apis/google_api_keys.h" | 12 #include "google_apis/google_api_keys.h" |
11 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
12 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
13 | 15 |
14 namespace google_apis { | 16 namespace google_apis { |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 // Hard coded URLs for communication with a google drive server. | 20 // Hard coded URLs for communication with a google drive server. |
| 21 // TODO(yamaguchi): Make a utility function to compose some of these URLs by a |
| 22 // version and a resource name. |
19 const char kDriveV2AboutUrl[] = "drive/v2/about"; | 23 const char kDriveV2AboutUrl[] = "drive/v2/about"; |
20 const char kDriveV2AppsUrl[] = "drive/v2/apps"; | 24 const char kDriveV2AppsUrl[] = "drive/v2/apps"; |
21 const char kDriveV2ChangelistUrl[] = "drive/v2/changes"; | 25 const char kDriveV2ChangelistUrl[] = "drive/v2/changes"; |
| 26 const char kDriveV2BetaChangelistUrl[] = "drive/v2beta/changes"; |
22 const char kDriveV2FilesUrl[] = "drive/v2/files"; | 27 const char kDriveV2FilesUrl[] = "drive/v2/files"; |
| 28 const char kDriveV2BetaFilesUrl[] = "drive/v2beta/files"; |
23 const char kDriveV2FileUrlPrefix[] = "drive/v2/files/"; | 29 const char kDriveV2FileUrlPrefix[] = "drive/v2/files/"; |
| 30 const char kDriveV2BetaFileUrlPrefix[] = "drive/v2beta/files/"; |
24 const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children"; | 31 const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children"; |
25 const char kDriveV2ChildrenUrlForRemovalFormat[] = | 32 const char kDriveV2ChildrenUrlForRemovalFormat[] = |
26 "drive/v2/files/%s/children/%s"; | 33 "drive/v2/files/%s/children/%s"; |
27 const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy"; | 34 const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy"; |
28 const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s"; | 35 const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s"; |
29 const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash"; | 36 const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash"; |
30 const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files"; | 37 const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files"; |
31 const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/"; | 38 const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/"; |
32 const char kDriveV2BatchUploadUrl[] = "upload/drive"; | 39 const char kDriveV2BatchUploadUrl[] = "upload/drive"; |
33 const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions"; | 40 const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions"; |
34 const char kDriveV2DownloadUrlFormat[] = "drive/v2/files/%s?alt=media"; | 41 const char kDriveV2DownloadUrlFormat[] = "drive/v2/files/%s?alt=media"; |
35 const char kDriveV2ThumbnailUrlFormat[] = "d/%s=w%d-h%d"; | 42 const char kDriveV2ThumbnailUrlFormat[] = "d/%s=w%d-h%d"; |
36 const char kDriveV2ThumbnailUrlWithCropFormat[] = "d/%s=w%d-h%d-c"; | 43 const char kDriveV2ThumbnailUrlWithCropFormat[] = "d/%s=w%d-h%d-c"; |
37 | 44 |
| 45 const char kIncludeTeamDriveItems[] = "includeTeamDriveItems"; |
| 46 const char kSupportsTeamDrives[] = "supportsTeamDrives"; |
| 47 |
38 // apps.delete and file.authorize API is exposed through a special endpoint | 48 // apps.delete and file.authorize API is exposed through a special endpoint |
39 // v2internal that is accessible only by the official API key for Chrome. | 49 // v2internal that is accessible only by the official API key for Chrome. |
40 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps"; | 50 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps"; |
41 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s"; | 51 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s"; |
42 const char kDriveV2FilesAuthorizeUrlFormat[] = | 52 const char kDriveV2FilesAuthorizeUrlFormat[] = |
43 "drive/v2internal/files/%s/authorize?appId=%s"; | 53 "drive/v2internal/files/%s/authorize?appId=%s"; |
44 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/"; | 54 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/"; |
45 | 55 |
46 GURL AddResumableUploadParam(const GURL& url) { | 56 GURL AddResumableUploadParam(const GURL& url) { |
47 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable"); | 57 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable"); |
48 } | 58 } |
49 | 59 |
50 GURL AddMultipartUploadParam(const GURL& url) { | 60 GURL AddMultipartUploadParam(const GURL& url) { |
51 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart"); | 61 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart"); |
52 } | 62 } |
53 | 63 |
54 } // namespace | 64 } // namespace |
55 | 65 |
| 66 DriveApiUrlGenerator::DriveApiUrlGenerator(const DriveApiUrlGenerator& src) |
| 67 : base_url_(src.base_url_), |
| 68 base_thumbnail_url_(src.base_thumbnail_url_), |
| 69 enable_team_drives_(src.enable_team_drives_) { |
| 70 // Do nothing. |
| 71 } |
| 72 |
56 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url, | 73 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url, |
57 const GURL& base_thumbnail_url) | 74 const GURL& base_thumbnail_url) |
58 : base_url_(base_url), | 75 : base_url_(base_url), |
59 base_thumbnail_url_(base_thumbnail_url) { | 76 base_thumbnail_url_(base_thumbnail_url), |
| 77 enable_team_drives_(IsTeamDrivesEnabled()) { |
60 // Do nothing. | 78 // Do nothing. |
61 } | 79 } |
62 | 80 |
63 DriveApiUrlGenerator::~DriveApiUrlGenerator() { | 81 DriveApiUrlGenerator::~DriveApiUrlGenerator() { |
64 // Do nothing. | 82 // Do nothing. |
65 } | 83 } |
66 | 84 |
67 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = | 85 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = |
68 "https://www.googleapis.com"; | 86 "https://www.googleapis.com"; |
69 | 87 |
(...skipping 10 matching lines...) Expand all Loading... |
80 } | 98 } |
81 | 99 |
82 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { | 100 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { |
83 return base_url_.Resolve(base::StringPrintf( | 101 return base_url_.Resolve(base::StringPrintf( |
84 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); | 102 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); |
85 } | 103 } |
86 | 104 |
87 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, | 105 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, |
88 bool use_internal_endpoint, | 106 bool use_internal_endpoint, |
89 const GURL& embed_origin) const { | 107 const GURL& embed_origin) const { |
90 GURL url = base_url_.Resolve(use_internal_endpoint ? | 108 const char* url_prefix = |
91 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) : | 109 use_internal_endpoint ? kDriveV2InternalFileUrlPrefix : |
92 kDriveV2FileUrlPrefix + net::EscapePath(file_id)); | 110 enable_team_drives_ ? kDriveV2BetaFileUrlPrefix : |
| 111 kDriveV2FileUrlPrefix; |
| 112 |
| 113 GURL url = base_url_.Resolve(url_prefix + net::EscapePath(file_id)); |
| 114 |
| 115 if (enable_team_drives_) |
| 116 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 117 |
93 if (!embed_origin.is_empty()) { | 118 if (!embed_origin.is_empty()) { |
94 // Construct a valid serialized embed origin from an url, according to | 119 // Construct a valid serialized embed origin from an url, according to |
95 // WD-html5-20110525. Such string has to be built manually, since | 120 // WD-html5-20110525. Such string has to be built manually, since |
96 // GURL::spec() always adds the trailing slash. Moreover, ports are | 121 // GURL::spec() always adds the trailing slash. Moreover, ports are |
97 // currently not supported. | 122 // currently not supported. |
98 DCHECK(!embed_origin.has_port()); | 123 DCHECK(!embed_origin.has_port()); |
99 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); | 124 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); |
100 const std::string serialized_embed_origin = | 125 const std::string serialized_embed_origin = |
101 embed_origin.scheme() + "://" + embed_origin.host(); | 126 embed_origin.scheme() + "://" + embed_origin.host(); |
102 url = net::AppendOrReplaceQueryParameter( | 127 url = net::AppendOrReplaceQueryParameter( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 173 |
149 if (!visibility.empty()) | 174 if (!visibility.empty()) |
150 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); | 175 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); |
151 | 176 |
152 return url; | 177 return url; |
153 } | 178 } |
154 | 179 |
155 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, | 180 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, |
156 const std::string& page_token, | 181 const std::string& page_token, |
157 const std::string& q) const { | 182 const std::string& q) const { |
158 GURL url = base_url_.Resolve(kDriveV2FilesUrl); | 183 GURL url; |
159 | 184 if (enable_team_drives_) { |
| 185 url = base_url_.Resolve(kDriveV2BetaFilesUrl); |
| 186 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 187 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, |
| 188 "true"); |
| 189 } else { |
| 190 url = base_url_.Resolve(kDriveV2FilesUrl); |
| 191 } |
160 // maxResults is 100 by default. | 192 // maxResults is 100 by default. |
161 if (max_results != 100) { | 193 if (max_results != 100) { |
162 url = net::AppendOrReplaceQueryParameter( | 194 url = net::AppendOrReplaceQueryParameter( |
163 url, "maxResults", base::IntToString(max_results)); | 195 url, "maxResults", base::IntToString(max_results)); |
164 } | 196 } |
165 | 197 |
166 if (!page_token.empty()) | 198 if (!page_token.empty()) |
167 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); | 199 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); |
168 | 200 |
169 if (!q.empty()) | 201 if (!q.empty()) |
(...skipping 11 matching lines...) Expand all Loading... |
181 return base_url_.Resolve(base::StringPrintf( | 213 return base_url_.Resolve(base::StringPrintf( |
182 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); | 214 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); |
183 } | 215 } |
184 | 216 |
185 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, | 217 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, |
186 int max_results, | 218 int max_results, |
187 const std::string& page_token, | 219 const std::string& page_token, |
188 int64_t start_change_id) const { | 220 int64_t start_change_id) const { |
189 DCHECK_GE(start_change_id, 0); | 221 DCHECK_GE(start_change_id, 0); |
190 | 222 |
191 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl); | 223 GURL url; |
192 | 224 if (enable_team_drives_) { |
| 225 url = base_url_.Resolve(kDriveV2BetaChangelistUrl); |
| 226 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 227 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, |
| 228 "true"); |
| 229 } else { |
| 230 url = base_url_.Resolve(kDriveV2ChangelistUrl); |
| 231 } |
193 // includeDeleted is "true" by default. | 232 // includeDeleted is "true" by default. |
194 if (!include_deleted) | 233 if (!include_deleted) |
195 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); | 234 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); |
196 | 235 |
197 // maxResults is "100" by default. | 236 // maxResults is "100" by default. |
198 if (max_results != 100) { | 237 if (max_results != 100) { |
199 url = net::AppendOrReplaceQueryParameter( | 238 url = net::AppendOrReplaceQueryParameter( |
200 url, "maxResults", base::IntToString(max_results)); | 239 url, "maxResults", base::IntToString(max_results)); |
201 } | 240 } |
202 | 241 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 base::StringPrintf( | 338 base::StringPrintf( |
300 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, | 339 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, |
301 net::EscapePath(resource_id).c_str(), width, height)); | 340 net::EscapePath(resource_id).c_str(), width, height)); |
302 } | 341 } |
303 | 342 |
304 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { | 343 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { |
305 return base_url_.Resolve(kDriveV2BatchUploadUrl); | 344 return base_url_.Resolve(kDriveV2BatchUploadUrl); |
306 } | 345 } |
307 | 346 |
308 } // namespace google_apis | 347 } // namespace google_apis |
OLD | NEW |