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.
4
5#include "google_apis/drive/drive_api_url_generator.h"
6
7#include "base/logging.h"
8#include "base/strings/string_number_conversions.h"
9#include "base/strings/stringprintf.h"
10#include "net/base/escape.h"
11#include "net/base/url_util.h"
12
13namespace google_apis {
14
15namespace {
16
17// Hard coded URLs for communication with a google drive server.
18const char kDriveV2AboutUrl[] = "/drive/v2/about";
19const char kDriveV2AppsUrl[] = "/drive/v2/apps";
20const char kDriveV2ChangelistUrl[] = "/drive/v2/changes";
21const char kDriveV2FilesUrl[] = "/drive/v2/files";
22const char kDriveV2FileUrlPrefix[] = "/drive/v2/files/";
23const char kDriveV2ChildrenUrlFormat[] = "/drive/v2/files/%s/children";
24const char kDriveV2ChildrenUrlForRemovalFormat[] =
25    "/drive/v2/files/%s/children/%s";
26const char kDriveV2FileCopyUrlFormat[] = "/drive/v2/files/%s/copy";
27const char kDriveV2FileDeleteUrlFormat[] = "/drive/v2/files/%s";
28const char kDriveV2FileTrashUrlFormat[] = "/drive/v2/files/%s/trash";
29const char kDriveV2InitiateUploadNewFileUrl[] = "/upload/drive/v2/files";
30const char kDriveV2InitiateUploadExistingFileUrlPrefix[] =
31    "/upload/drive/v2/files/";
32
33GURL AddResumableUploadParam(const GURL& url) {
34  return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
35}
36
37}  // namespace
38
39DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url,
40                                           const GURL& base_download_url)
41    : base_url_(base_url),
42      base_download_url_(base_download_url) {
43  // Do nothing.
44}
45
46DriveApiUrlGenerator::~DriveApiUrlGenerator() {
47  // Do nothing.
48}
49
50const char DriveApiUrlGenerator::kBaseUrlForProduction[] =
51    "https://www.googleapis.com";
52const char DriveApiUrlGenerator::kBaseDownloadUrlForProduction[] =
53    "https://www.googledrive.com/host/";
54
55GURL DriveApiUrlGenerator::GetAboutGetUrl() const {
56  return base_url_.Resolve(kDriveV2AboutUrl);
57}
58
59GURL DriveApiUrlGenerator::GetAppsListUrl() const {
60  return base_url_.Resolve(kDriveV2AppsUrl);
61}
62
63GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id) const {
64  return base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
65}
66
67GURL DriveApiUrlGenerator::GetFilesInsertUrl() const {
68  return base_url_.Resolve(kDriveV2FilesUrl);
69}
70
71GURL DriveApiUrlGenerator::GetFilesPatchUrl(const std::string& file_id,
72                                            bool set_modified_date,
73                                            bool update_viewed_date) const {
74  GURL url =
75      base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
76
77  // setModifiedDate is "false" by default.
78  if (set_modified_date)
79    url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
80
81  // updateViewedDate is "true" by default.
82  if (!update_viewed_date)
83    url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");
84
85  return url;
86}
87
88GURL DriveApiUrlGenerator::GetFilesCopyUrl(const std::string& file_id) const {
89  return base_url_.Resolve(base::StringPrintf(
90      kDriveV2FileCopyUrlFormat, net::EscapePath(file_id).c_str()));
91}
92
93GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
94                                           const std::string& page_token,
95                                           const std::string& q) const {
96  GURL url = base_url_.Resolve(kDriveV2FilesUrl);
97
98  // maxResults is 100 by default.
99  if (max_results != 100) {
100    url = net::AppendOrReplaceQueryParameter(
101        url, "maxResults", base::IntToString(max_results));
102  }
103
104  if (!page_token.empty())
105    url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
106
107  if (!q.empty())
108    url = net::AppendOrReplaceQueryParameter(url, "q", q);
109
110  return url;
111}
112
113GURL DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string& file_id) const {
114  return base_url_.Resolve(base::StringPrintf(
115      kDriveV2FileDeleteUrlFormat, net::EscapePath(file_id).c_str()));
116}
117
118GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
119  return base_url_.Resolve(base::StringPrintf(
120      kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str()));
121}
122
123GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted,
124                                             int max_results,
125                                             const std::string& page_token,
126                                             int64 start_change_id) const {
127  DCHECK_GE(start_change_id, 0);
128
129  GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
130
131  // includeDeleted is "true" by default.
132  if (!include_deleted)
133    url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
134
135  // maxResults is "100" by default.
136  if (max_results != 100) {
137    url = net::AppendOrReplaceQueryParameter(
138        url, "maxResults", base::IntToString(max_results));
139  }
140
141  if (!page_token.empty())
142    url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
143
144  if (start_change_id > 0)
145    url = net::AppendOrReplaceQueryParameter(
146        url, "startChangeId", base::Int64ToString(start_change_id));
147
148  return url;
149}
150
151GURL DriveApiUrlGenerator::GetChildrenInsertUrl(
152    const std::string& file_id) const {
153  return base_url_.Resolve(base::StringPrintf(
154      kDriveV2ChildrenUrlFormat, net::EscapePath(file_id).c_str()));
155}
156
157GURL DriveApiUrlGenerator::GetChildrenDeleteUrl(
158    const std::string& child_id, const std::string& folder_id) const {
159  return base_url_.Resolve(
160      base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat,
161                         net::EscapePath(folder_id).c_str(),
162                         net::EscapePath(child_id).c_str()));
163}
164
165GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl() const {
166  return AddResumableUploadParam(
167      base_url_.Resolve(kDriveV2InitiateUploadNewFileUrl));
168}
169
170GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
171    const std::string& resource_id) const {
172  const GURL& url = base_url_.Resolve(
173      kDriveV2InitiateUploadExistingFileUrlPrefix +
174      net::EscapePath(resource_id));
175  return AddResumableUploadParam(url);
176}
177
178GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
179    const std::string& resource_id) const {
180  return base_download_url_.Resolve(net::EscapePath(resource_id));
181}
182
183}  // namespace google_apis
184