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 "chrome/browser/google_apis/drive_api_url_generator.h"
7 #include "base/logging.h"
8 #include "base/stringprintf.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/base/escape.h"
11 #include "net/base/url_util.h"
13 namespace google_apis
{
17 // Hard coded URLs for communication with a google drive server.
18 const char kDriveV2AboutUrl
[] = "/drive/v2/about";
19 const char kDriveV2ApplistUrl
[] = "/drive/v2/apps";
20 const char kDriveV2ChangelistUrl
[] = "/drive/v2/changes";
21 const char kDriveV2FilelistUrl
[] = "/drive/v2/files";
22 const char kDriveV2FileUrlPrefix
[] = "/drive/v2/files/";
23 const char kDriveV2ChildrenUrlFormat
[] = "/drive/v2/files/%s/children";
24 const char kDriveV2ChildrenUrlForRemovalFormat
[] =
25 "/drive/v2/files/%s/children/%s";
26 const char kDriveV2FileCopyUrlFormat
[] = "/drive/v2/files/%s/copy";
27 const char kDriveV2FileTrashUrlFormat
[] = "/drive/v2/files/%s/trash";
28 const char kDriveV2InitiateUploadNewFileUrl
[] = "/upload/drive/v2/files";
29 const char kDriveV2InitiateUploadExistingFileUrlPrefix
[] =
30 "/upload/drive/v2/files/";
32 GURL
AddResumableUploadParam(const GURL
& url
) {
33 return net::AppendOrReplaceQueryParameter(url
, "uploadType", "resumable");
38 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL
& base_url
)
39 : base_url_(base_url
) {
43 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
47 const char DriveApiUrlGenerator::kBaseUrlForProduction
[] =
48 "https://www.googleapis.com";
50 GURL
DriveApiUrlGenerator::GetAboutUrl() const {
51 return base_url_
.Resolve(kDriveV2AboutUrl
);
54 GURL
DriveApiUrlGenerator::GetApplistUrl() const {
55 return base_url_
.Resolve(kDriveV2ApplistUrl
);
58 GURL
DriveApiUrlGenerator::GetChangelistUrl(
59 bool include_deleted
, int64 start_changestamp
) const {
60 DCHECK_GE(start_changestamp
, 0);
62 GURL url
= base_url_
.Resolve(kDriveV2ChangelistUrl
);
63 if (!include_deleted
) {
64 // If include_deleted is set to "false", set the query parameter,
65 // because its default parameter is "true".
66 url
= net::AppendOrReplaceQueryParameter(url
, "includeDeleted", "false");
69 if (start_changestamp
> 0) {
70 url
= net::AppendOrReplaceQueryParameter(
71 url
, "startChangeId", base::Int64ToString(start_changestamp
));
76 GURL
DriveApiUrlGenerator::GetFilelistUrl(
77 const std::string
& search_string
) const {
78 const GURL
& url
= base_url_
.Resolve(kDriveV2FilelistUrl
);
79 return search_string
.empty() ?
81 net::AppendOrReplaceQueryParameter(url
, "q", search_string
);
84 GURL
DriveApiUrlGenerator::GetFileUrl(const std::string
& file_id
) const {
85 return base_url_
.Resolve(kDriveV2FileUrlPrefix
+ net::EscapePath(file_id
));
88 GURL
DriveApiUrlGenerator::GetFileCopyUrl(
89 const std::string
& resource_id
) const {
90 return base_url_
.Resolve(
91 base::StringPrintf(kDriveV2FileCopyUrlFormat
,
92 net::EscapePath(resource_id
).c_str()));
95 GURL
DriveApiUrlGenerator::GetFileTrashUrl(const std::string
& file_id
) const {
96 return base_url_
.Resolve(
97 base::StringPrintf(kDriveV2FileTrashUrlFormat
,
98 net::EscapePath(file_id
).c_str()));
101 GURL
DriveApiUrlGenerator::GetChildrenUrl(
102 const std::string
& resource_id
) const {
103 return base_url_
.Resolve(
104 base::StringPrintf(kDriveV2ChildrenUrlFormat
,
105 net::EscapePath(resource_id
).c_str()));
108 GURL
DriveApiUrlGenerator::GetChildrenUrlForRemoval(
109 const std::string
& folder_id
, const std::string
& child_id
) const {
110 return base_url_
.Resolve(
111 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat
,
112 net::EscapePath(folder_id
).c_str(),
113 net::EscapePath(child_id
).c_str()));
116 GURL
DriveApiUrlGenerator::GetInitiateUploadNewFileUrl() const {
117 return AddResumableUploadParam(
118 base_url_
.Resolve(kDriveV2InitiateUploadNewFileUrl
));
121 GURL
DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
122 const std::string
& resource_id
) const {
123 const GURL
& url
= base_url_
.Resolve(
124 kDriveV2InitiateUploadExistingFileUrlPrefix
+
125 net::EscapePath(resource_id
));
126 return AddResumableUploadParam(url
);
129 } // namespace google_apis