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) | |
hashimoto
2017/02/22 03:52:03
Can't you just use "= default"?
yamaguchi
2017/02/22 06:41:22
Done.
| |
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 = nullptr; |
91 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) : | 109 if (use_internal_endpoint) |
92 kDriveV2FileUrlPrefix + net::EscapePath(file_id)); | 110 url_prefix = kDriveV2InternalFileUrlPrefix; |
111 else if (enable_team_drives_) | |
112 url_prefix = kDriveV2BetaFileUrlPrefix; | |
113 else | |
114 url_prefix = kDriveV2FileUrlPrefix; | |
115 | |
116 GURL url = base_url_.Resolve(url_prefix + net::EscapePath(file_id)); | |
117 | |
118 if (enable_team_drives_) | |
119 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
120 | |
93 if (!embed_origin.is_empty()) { | 121 if (!embed_origin.is_empty()) { |
94 // Construct a valid serialized embed origin from an url, according to | 122 // Construct a valid serialized embed origin from an url, according to |
95 // WD-html5-20110525. Such string has to be built manually, since | 123 // WD-html5-20110525. Such string has to be built manually, since |
96 // GURL::spec() always adds the trailing slash. Moreover, ports are | 124 // GURL::spec() always adds the trailing slash. Moreover, ports are |
97 // currently not supported. | 125 // currently not supported. |
98 DCHECK(!embed_origin.has_port()); | 126 DCHECK(!embed_origin.has_port()); |
99 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); | 127 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); |
100 const std::string serialized_embed_origin = | 128 const std::string serialized_embed_origin = |
101 embed_origin.scheme() + "://" + embed_origin.host(); | 129 embed_origin.scheme() + "://" + embed_origin.host(); |
102 url = net::AppendOrReplaceQueryParameter( | 130 url = net::AppendOrReplaceQueryParameter( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 | 176 |
149 if (!visibility.empty()) | 177 if (!visibility.empty()) |
150 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); | 178 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); |
151 | 179 |
152 return url; | 180 return url; |
153 } | 181 } |
154 | 182 |
155 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, | 183 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, |
156 const std::string& page_token, | 184 const std::string& page_token, |
157 const std::string& q) const { | 185 const std::string& q) const { |
158 GURL url = base_url_.Resolve(kDriveV2FilesUrl); | 186 GURL url; |
159 | 187 if (enable_team_drives_) { |
188 url = base_url_.Resolve(kDriveV2BetaFilesUrl); | |
189 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
190 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, | |
191 "true"); | |
192 } else { | |
193 url = base_url_.Resolve(kDriveV2FilesUrl); | |
194 } | |
160 // maxResults is 100 by default. | 195 // maxResults is 100 by default. |
161 if (max_results != 100) { | 196 if (max_results != 100) { |
162 url = net::AppendOrReplaceQueryParameter( | 197 url = net::AppendOrReplaceQueryParameter( |
163 url, "maxResults", base::IntToString(max_results)); | 198 url, "maxResults", base::IntToString(max_results)); |
164 } | 199 } |
165 | 200 |
166 if (!page_token.empty()) | 201 if (!page_token.empty()) |
167 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); | 202 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); |
168 | 203 |
169 if (!q.empty()) | 204 if (!q.empty()) |
(...skipping 11 matching lines...) Expand all Loading... | |
181 return base_url_.Resolve(base::StringPrintf( | 216 return base_url_.Resolve(base::StringPrintf( |
182 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); | 217 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); |
183 } | 218 } |
184 | 219 |
185 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, | 220 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, |
186 int max_results, | 221 int max_results, |
187 const std::string& page_token, | 222 const std::string& page_token, |
188 int64_t start_change_id) const { | 223 int64_t start_change_id) const { |
189 DCHECK_GE(start_change_id, 0); | 224 DCHECK_GE(start_change_id, 0); |
190 | 225 |
191 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl); | 226 GURL url; |
192 | 227 if (enable_team_drives_) { |
228 url = base_url_.Resolve(kDriveV2BetaChangelistUrl); | |
229 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
230 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, | |
231 "true"); | |
232 } else { | |
233 url = base_url_.Resolve(kDriveV2ChangelistUrl); | |
234 } | |
193 // includeDeleted is "true" by default. | 235 // includeDeleted is "true" by default. |
194 if (!include_deleted) | 236 if (!include_deleted) |
195 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); | 237 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); |
196 | 238 |
197 // maxResults is "100" by default. | 239 // maxResults is "100" by default. |
198 if (max_results != 100) { | 240 if (max_results != 100) { |
199 url = net::AppendOrReplaceQueryParameter( | 241 url = net::AppendOrReplaceQueryParameter( |
200 url, "maxResults", base::IntToString(max_results)); | 242 url, "maxResults", base::IntToString(max_results)); |
201 } | 243 } |
202 | 244 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 base::StringPrintf( | 341 base::StringPrintf( |
300 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, | 342 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, |
301 net::EscapePath(resource_id).c_str(), width, height)); | 343 net::EscapePath(resource_id).c_str(), width, height)); |
302 } | 344 } |
303 | 345 |
304 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { | 346 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { |
305 return base_url_.Resolve(kDriveV2BatchUploadUrl); | 347 return base_url_.Resolve(kDriveV2BatchUploadUrl); |
306 } | 348 } |
307 | 349 |
308 } // namespace google_apis | 350 } // namespace google_apis |
OLD | NEW |