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