| 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 |
| 56 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url, | 66 DriveApiUrlGenerator::DriveApiUrlGenerator( |
| 57 const GURL& base_thumbnail_url) | 67 const GURL& base_url, const GURL& base_thumbnail_url, |
| 68 TeamDrivesIntegrationStatus team_drives_integration) |
| 58 : base_url_(base_url), | 69 : base_url_(base_url), |
| 59 base_thumbnail_url_(base_thumbnail_url) { | 70 base_thumbnail_url_(base_thumbnail_url), |
| 71 enable_team_drives_( |
| 72 team_drives_integration == TEAM_DRIVES_INTEGRATION_ENABLED) { |
| 60 // Do nothing. | 73 // Do nothing. |
| 61 } | 74 } |
| 62 | 75 |
| 76 DriveApiUrlGenerator::DriveApiUrlGenerator(const DriveApiUrlGenerator& src) = |
| 77 default; |
| 78 |
| 63 DriveApiUrlGenerator::~DriveApiUrlGenerator() { | 79 DriveApiUrlGenerator::~DriveApiUrlGenerator() { |
| 64 // Do nothing. | 80 // Do nothing. |
| 65 } | 81 } |
| 66 | 82 |
| 67 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = | 83 const char DriveApiUrlGenerator::kBaseUrlForProduction[] = |
| 68 "https://www.googleapis.com"; | 84 "https://www.googleapis.com"; |
| 69 | 85 |
| 70 const char DriveApiUrlGenerator::kBaseThumbnailUrlForProduction[] = | 86 const char DriveApiUrlGenerator::kBaseThumbnailUrlForProduction[] = |
| 71 "https://lh3.googleusercontent.com"; | 87 "https://lh3.googleusercontent.com"; |
| 72 | 88 |
| 73 GURL DriveApiUrlGenerator::GetAboutGetUrl() const { | 89 GURL DriveApiUrlGenerator::GetAboutGetUrl() const { |
| 74 return base_url_.Resolve(kDriveV2AboutUrl); | 90 return base_url_.Resolve(kDriveV2AboutUrl); |
| 75 } | 91 } |
| 76 | 92 |
| 77 GURL DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint) const { | 93 GURL DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint) const { |
| 78 return base_url_.Resolve(use_internal_endpoint ? | 94 return base_url_.Resolve(use_internal_endpoint ? |
| 79 kDriveV2InternalAppsUrl : kDriveV2AppsUrl); | 95 kDriveV2InternalAppsUrl : kDriveV2AppsUrl); |
| 80 } | 96 } |
| 81 | 97 |
| 82 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { | 98 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const { |
| 83 return base_url_.Resolve(base::StringPrintf( | 99 return base_url_.Resolve(base::StringPrintf( |
| 84 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); | 100 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str())); |
| 85 } | 101 } |
| 86 | 102 |
| 87 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, | 103 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id, |
| 88 bool use_internal_endpoint, | 104 bool use_internal_endpoint, |
| 89 const GURL& embed_origin) const { | 105 const GURL& embed_origin) const { |
| 90 GURL url = base_url_.Resolve(use_internal_endpoint ? | 106 const char* url_prefix = nullptr; |
| 91 kDriveV2InternalFileUrlPrefix + net::EscapePath(file_id) : | 107 if (use_internal_endpoint) |
| 92 kDriveV2FileUrlPrefix + net::EscapePath(file_id)); | 108 url_prefix = kDriveV2InternalFileUrlPrefix; |
| 109 else if (enable_team_drives_) |
| 110 url_prefix = kDriveV2BetaFileUrlPrefix; |
| 111 else |
| 112 url_prefix = kDriveV2FileUrlPrefix; |
| 113 |
| 114 GURL url = base_url_.Resolve(url_prefix + net::EscapePath(file_id)); |
| 115 |
| 116 if (enable_team_drives_) |
| 117 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 118 |
| 93 if (!embed_origin.is_empty()) { | 119 if (!embed_origin.is_empty()) { |
| 94 // Construct a valid serialized embed origin from an url, according to | 120 // Construct a valid serialized embed origin from an url, according to |
| 95 // WD-html5-20110525. Such string has to be built manually, since | 121 // WD-html5-20110525. Such string has to be built manually, since |
| 96 // GURL::spec() always adds the trailing slash. Moreover, ports are | 122 // GURL::spec() always adds the trailing slash. Moreover, ports are |
| 97 // currently not supported. | 123 // currently not supported. |
| 98 DCHECK(!embed_origin.has_port()); | 124 DCHECK(!embed_origin.has_port()); |
| 99 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); | 125 DCHECK(!embed_origin.has_path() || embed_origin.path() == "/"); |
| 100 const std::string serialized_embed_origin = | 126 const std::string serialized_embed_origin = |
| 101 embed_origin.scheme() + "://" + embed_origin.host(); | 127 embed_origin.scheme() + "://" + embed_origin.host(); |
| 102 url = net::AppendOrReplaceQueryParameter( | 128 url = net::AppendOrReplaceQueryParameter( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 174 |
| 149 if (!visibility.empty()) | 175 if (!visibility.empty()) |
| 150 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); | 176 url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility); |
| 151 | 177 |
| 152 return url; | 178 return url; |
| 153 } | 179 } |
| 154 | 180 |
| 155 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, | 181 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results, |
| 156 const std::string& page_token, | 182 const std::string& page_token, |
| 157 const std::string& q) const { | 183 const std::string& q) const { |
| 158 GURL url = base_url_.Resolve(kDriveV2FilesUrl); | 184 GURL url; |
| 159 | 185 if (enable_team_drives_) { |
| 186 url = base_url_.Resolve(kDriveV2BetaFilesUrl); |
| 187 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 188 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, |
| 189 "true"); |
| 190 } else { |
| 191 url = base_url_.Resolve(kDriveV2FilesUrl); |
| 192 } |
| 160 // maxResults is 100 by default. | 193 // maxResults is 100 by default. |
| 161 if (max_results != 100) { | 194 if (max_results != 100) { |
| 162 url = net::AppendOrReplaceQueryParameter( | 195 url = net::AppendOrReplaceQueryParameter( |
| 163 url, "maxResults", base::IntToString(max_results)); | 196 url, "maxResults", base::IntToString(max_results)); |
| 164 } | 197 } |
| 165 | 198 |
| 166 if (!page_token.empty()) | 199 if (!page_token.empty()) |
| 167 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); | 200 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token); |
| 168 | 201 |
| 169 if (!q.empty()) | 202 if (!q.empty()) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 181 return base_url_.Resolve(base::StringPrintf( | 214 return base_url_.Resolve(base::StringPrintf( |
| 182 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); | 215 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str())); |
| 183 } | 216 } |
| 184 | 217 |
| 185 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, | 218 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted, |
| 186 int max_results, | 219 int max_results, |
| 187 const std::string& page_token, | 220 const std::string& page_token, |
| 188 int64_t start_change_id) const { | 221 int64_t start_change_id) const { |
| 189 DCHECK_GE(start_change_id, 0); | 222 DCHECK_GE(start_change_id, 0); |
| 190 | 223 |
| 191 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl); | 224 GURL url; |
| 192 | 225 if (enable_team_drives_) { |
| 226 url = base_url_.Resolve(kDriveV2BetaChangelistUrl); |
| 227 url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true"); |
| 228 url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, |
| 229 "true"); |
| 230 } else { |
| 231 url = base_url_.Resolve(kDriveV2ChangelistUrl); |
| 232 } |
| 193 // includeDeleted is "true" by default. | 233 // includeDeleted is "true" by default. |
| 194 if (!include_deleted) | 234 if (!include_deleted) |
| 195 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); | 235 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false"); |
| 196 | 236 |
| 197 // maxResults is "100" by default. | 237 // maxResults is "100" by default. |
| 198 if (max_results != 100) { | 238 if (max_results != 100) { |
| 199 url = net::AppendOrReplaceQueryParameter( | 239 url = net::AppendOrReplaceQueryParameter( |
| 200 url, "maxResults", base::IntToString(max_results)); | 240 url, "maxResults", base::IntToString(max_results)); |
| 201 } | 241 } |
| 202 | 242 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 base::StringPrintf( | 339 base::StringPrintf( |
| 300 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, | 340 crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat, |
| 301 net::EscapePath(resource_id).c_str(), width, height)); | 341 net::EscapePath(resource_id).c_str(), width, height)); |
| 302 } | 342 } |
| 303 | 343 |
| 304 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { | 344 GURL DriveApiUrlGenerator::GetBatchUploadUrl() const { |
| 305 return base_url_.Resolve(kDriveV2BatchUploadUrl); | 345 return base_url_.Resolve(kDriveV2BatchUploadUrl); |
| 306 } | 346 } |
| 307 | 347 |
| 308 } // namespace google_apis | 348 } // namespace google_apis |
| OLD | NEW |