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" |
10 #include "google_apis/google_api_keys.h" | 11 #include "google_apis/google_api_keys.h" |
11 #include "net/base/escape.h" | 12 #include "net/base/escape.h" |
12 #include "net/base/url_util.h" | 13 #include "net/base/url_util.h" |
13 | 14 |
14 namespace google_apis { | 15 namespace google_apis { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Hard coded URLs for communication with a google drive server. | 19 // Hard coded URLs for communication with a google drive server. |
20 // TODO(yamaguchi): Make a utility function to compose some of these URLs by a | |
21 // version and a resource name. | |
19 const char kDriveV2AboutUrl[] = "drive/v2/about"; | 22 const char kDriveV2AboutUrl[] = "drive/v2/about"; |
20 const char kDriveV2AppsUrl[] = "drive/v2/apps"; | 23 const char kDriveV2AppsUrl[] = "drive/v2/apps"; |
21 const char kDriveV2ChangelistUrl[] = "drive/v2/changes"; | 24 const char kDriveV2ChangelistUrl[] = "drive/v2/changes"; |
25 const char kDriveV2BetaChangelistUrl[] = "drive/v2beta/changes"; | |
22 const char kDriveV2FilesUrl[] = "drive/v2/files"; | 26 const char kDriveV2FilesUrl[] = "drive/v2/files"; |
27 const char kDriveV2BetaFilesUrl[] = "drive/v2beta/files"; | |
23 const char kDriveV2FileUrlPrefix[] = "drive/v2/files/"; | 28 const char kDriveV2FileUrlPrefix[] = "drive/v2/files/"; |
29 const char kDriveV2BetaFileUrlPrefix[] = "drive/v2beta/files/"; | |
24 const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children"; | 30 const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children"; |
25 const char kDriveV2ChildrenUrlForRemovalFormat[] = | 31 const char kDriveV2ChildrenUrlForRemovalFormat[] = |
26 "drive/v2/files/%s/children/%s"; | 32 "drive/v2/files/%s/children/%s"; |
27 const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy"; | 33 const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy"; |
28 const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s"; | 34 const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s"; |
29 const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash"; | 35 const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash"; |
30 const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files"; | 36 const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files"; |
31 const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/"; | 37 const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/"; |
32 const char kDriveV2BatchUploadUrl[] = "upload/drive"; | 38 const char kDriveV2BatchUploadUrl[] = "upload/drive"; |
33 const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions"; | 39 const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions"; |
34 const char kDriveV2DownloadUrlFormat[] = "drive/v2/files/%s?alt=media"; | 40 const char kDriveV2DownloadUrlFormat[] = "drive/v2/files/%s?alt=media"; |
35 const char kDriveV2ThumbnailUrlFormat[] = "d/%s=w%d-h%d"; | 41 const char kDriveV2ThumbnailUrlFormat[] = "d/%s=w%d-h%d"; |
36 const char kDriveV2ThumbnailUrlWithCropFormat[] = "d/%s=w%d-h%d-c"; | 42 const char kDriveV2ThumbnailUrlWithCropFormat[] = "d/%s=w%d-h%d-c"; |
37 | 43 |
44 const char kIncludeTeamDriveItems[] = "includeTeamDriveItems"; | |
45 const char kSupportsTeamDrives[] = "supportsTeamDrives"; | |
46 | |
38 // apps.delete and file.authorize API is exposed through a special endpoint | 47 // 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. | 48 // v2internal that is accessible only by the official API key for Chrome. |
40 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps"; | 49 const char kDriveV2InternalAppsUrl[] = "drive/v2internal/apps"; |
41 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s"; | 50 const char kDriveV2AppsDeleteUrlFormat[] = "drive/v2internal/apps/%s"; |
42 const char kDriveV2FilesAuthorizeUrlFormat[] = | 51 const char kDriveV2FilesAuthorizeUrlFormat[] = |
43 "drive/v2internal/files/%s/authorize?appId=%s"; | 52 "drive/v2internal/files/%s/authorize?appId=%s"; |
44 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/"; | 53 const char kDriveV2InternalFileUrlPrefix[] = "drive/v2internal/files/"; |
45 | 54 |
55 namespace switches { | |
56 | |
57 // Enables or disables Team Drives integration. | |
58 const char kEnableTeamDrives[] = "team-drives"; | |
hashimoto
2017/02/20 03:26:30
Please put switch constants in a separate file.
Us
yamaguchi
2017/02/20 08:21:29
Done.
| |
59 | |
60 } // namespace switches | |
61 | |
62 bool IsTeamDrivesEnabled() { | |
63 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
64 switches::kEnableTeamDrives); | |
65 } | |
66 | |
46 GURL AddResumableUploadParam(const GURL& url) { | 67 GURL AddResumableUploadParam(const GURL& url) { |
47 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable"); | 68 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable"); |
48 } | 69 } |
49 | 70 |
50 GURL AddMultipartUploadParam(const GURL& url) { | 71 GURL AddMultipartUploadParam(const GURL& url) { |
51 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart"); | 72 return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart"); |
52 } | 73 } |
53 | 74 |
54 } // namespace | 75 } // namespace |
55 | 76 |
77 DriveApiUrlGenerator::DriveApiUrlGenerator(const DriveApiUrlGenerator& src) | |
78 : base_url_(src.base_url_), | |
79 base_thumbnail_url_(src.base_thumbnail_url_), | |
80 enable_team_drives_(src.enable_team_drives_) { | |
81 // Do nothing. | |
82 } | |
83 | |
56 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url, | 84 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url, |
57 const GURL& base_thumbnail_url) | 85 const GURL& base_thumbnail_url) |
58 : base_url_(base_url), | 86 : base_url_(base_url), |
59 base_thumbnail_url_(base_thumbnail_url) { | 87 base_thumbnail_url_(base_thumbnail_url), |
88 enable_team_drives_(IsTeamDrivesEnabled()) { | |
60 // Do nothing. | 89 // Do nothing. |
61 } | 90 } |
62 | 91 |
63 DriveApiUrlGenerator::~DriveApiUrlGenerator() { | 92 DriveApiUrlGenerator::~DriveApiUrlGenerator() { |
64 // Do nothing. | 93 // Do nothing. |
65 } | 94 } |
66 | 95 |
67 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = | 96 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = |
68 "https://www.googleapis.com"; | 97 "https://www.googleapis.com"; |
69 | 98 |
(...skipping 10 matching lines...) Expand all Loading... | |
80 } | 109 } |
81 | 110 |
82 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { | 111 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { |
83 return base_url_.Resolve(base::StringPrintf( | 112 return base_url_.Resolve(base::StringPrintf( |
84 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); | 113 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); |
85 } | 114 } |
86 | 115 |
87 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, | 116 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, |
88 bool use_internal_endpoint, | 117 bool use_internal_endpoint, |
89 const GURL& embed_origin) const { | 118 const GURL& embed_origin) const { |
90 GURL url = base_url_.Resolve(use_internal_endpoint ? | 119 const char* url_prefix; |
hashimoto
2017/02/20 03:26:29
Please don't leave this value uninitialized.
yamaguchi
2017/02/20 08:21:29
Is this what you meant? (or did you mean make this
hashimoto
2017/02/21 10:19:10
I meant just initializing with nullptr.
yamaguchi
2017/02/22 02:07:52
Done.
| |
91 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) : | 120 if (use_internal_endpoint) |
92 kDriveV2FileUrlPrefix + net::EscapePath(file_id)); | 121 url_prefix = kDriveV2InternalFileUrlPrefix; |
122 else if (enable_team_drives_) | |
123 url_prefix = kDriveV2BetaFileUrlPrefix; | |
124 else | |
125 url_prefix = kDriveV2FileUrlPrefix; | |
126 | |
127 GURL url = base_url_.Resolve(url_prefix + net::EscapePath(file_id)); | |
128 | |
129 if (enable_team_drives_) | |
130 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
131 | |
93 if (!embed_origin.is_empty()) { | 132 if (!embed_origin.is_empty()) { |
94 // Construct a valid serialized embed origin from an url, according to | 133 // Construct a valid serialized embed origin from an url, according to |
95 // WD-html5-20110525. Such string has to be built manually, since | 134 // WD-html5-20110525. Such string has to be built manually, since |
96 // GURL::spec() always adds the trailing slash. Moreover, ports are | 135 // GURL::spec() always adds the trailing slash. Moreover, ports are |
97 // currently not supported. | 136 // currently not supported. |
98 DCHECK(!embed_origin.has_port()); | 137 DCHECK(!embed_origin.has_port()); |
99 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); | 138 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); |
100 const std::string serialized_embed_origin = | 139 const std::string serialized_embed_origin = |
101 embed_origin.scheme() + "://" + embed_origin.host(); | 140 embed_origin.scheme() + "://" + embed_origin.host(); |
102 url = net::AppendOrReplaceQueryParameter( | 141 url = net::AppendOrReplaceQueryParameter( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 | 187 |
149 if (!visibility.empty()) | 188 if (!visibility.empty()) |
150 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); | 189 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); |
151 | 190 |
152 return url; | 191 return url; |
153 } | 192 } |
154 | 193 |
155 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, | 194 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, |
156 const std::string& page_token, | 195 const std::string& page_token, |
157 const std::string& q) const { | 196 const std::string& q) const { |
158 GURL url = base_url_.Resolve(kDriveV2FilesUrl); | 197 GURL url = base_url_.Resolve(kDriveV2FilesUrl); |
hashimoto
2017/02/20 03:26:29
Please be consistent with the code below which put
yamaguchi
2017/02/20 08:21:29
Done.
| |
159 | 198 |
199 if (enable_team_drives_) { | |
200 url = base_url_.Resolve(kDriveV2BetaFilesUrl); | |
201 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
202 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, | |
203 "true"); | |
204 } | |
160 // maxResults is 100 by default. | 205 // maxResults is 100 by default. |
161 if (max_results != 100) { | 206 if (max_results != 100) { |
162 url = net::AppendOrReplaceQueryParameter( | 207 url = net::AppendOrReplaceQueryParameter( |
163 url, "maxResults", base::IntToString(max_results)); | 208 url, "maxResults", base::IntToString(max_results)); |
164 } | 209 } |
165 | 210 |
166 if (!page_token.empty()) | 211 if (!page_token.empty()) |
167 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); | 212 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); |
168 | 213 |
169 if (!q.empty()) | 214 if (!q.empty()) |
(...skipping 11 matching lines...) Expand all Loading... | |
181 return base_url_.Resolve(base::StringPrintf( | 226 return base_url_.Resolve(base::StringPrintf( |
182 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); | 227 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); |
183 } | 228 } |
184 | 229 |
185 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, | 230 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, |
186 int max_results, | 231 int max_results, |
187 const std::string& page_token, | 232 const std::string& page_token, |
188 int64_t start_change_id) const { | 233 int64_t start_change_id) const { |
189 DCHECK_GE(start_change_id, 0); | 234 DCHECK_GE(start_change_id, 0); |
190 | 235 |
191 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl); | 236 GURL url; |
192 | 237 if (enable_team_drives_) { |
238 url = base_url_.Resolve(kDriveV2BetaChangelistUrl); | |
239 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); | |
240 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, | |
241 "true"); | |
242 } else { | |
243 url = base_url_.Resolve(kDriveV2ChangelistUrl); | |
244 } | |
193 // includeDeleted is "true" by default. | 245 // includeDeleted is "true" by default. |
194 if (!include_deleted) | 246 if (!include_deleted) |
195 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); | 247 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); |
196 | 248 |
197 // maxResults is "100" by default. | 249 // maxResults is "100" by default. |
198 if (max_results != 100) { | 250 if (max_results != 100) { |
199 url = net::AppendOrReplaceQueryParameter( | 251 url = net::AppendOrReplaceQueryParameter( |
200 url, "maxResults", base::IntToString(max_results)); | 252 url, "maxResults", base::IntToString(max_results)); |
201 } | 253 } |
202 | 254 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 base::StringPrintf( | 351 base::StringPrintf( |
300 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, | 352 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, |
301 net::EscapePath(resource_id).c_str(), width, height)); | 353 net::EscapePath(resource_id).c_str(), width, height)); |
302 } | 354 } |
303 | 355 |
304 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { | 356 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { |
305 return base_url_.Resolve(kDriveV2BatchUploadUrl); | 357 return base_url_.Resolve(kDriveV2BatchUploadUrl); |
306 } | 358 } |
307 | 359 |
308 } // namespace google_apis | 360 } // namespace google_apis |
OLD | NEW |