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 "chrome/browser/google_apis/drive_api_service.h" | 5 #include "chrome/browser/google_apis/drive_api_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "base/string16.h" | |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
17 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "chrome/browser/google_apis/auth_service.h" | 19 #include "chrome/browser/google_apis/auth_service.h" |
18 #include "chrome/browser/google_apis/drive_api_operations.h" | 20 #include "chrome/browser/google_apis/drive_api_operations.h" |
19 #include "chrome/browser/google_apis/drive_api_parser.h" | 21 #include "chrome/browser/google_apis/drive_api_parser.h" |
20 #include "chrome/browser/google_apis/gdata_wapi_operations.h" | 22 #include "chrome/browser/google_apis/gdata_wapi_operations.h" |
21 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 23 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
22 #include "chrome/browser/google_apis/operation_runner.h" | 24 #include "chrome/browser/google_apis/operation_runner.h" |
23 #include "chrome/browser/google_apis/time_util.h" | 25 #include "chrome/browser/google_apis/time_util.h" |
24 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
25 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 } | 204 } |
203 | 205 |
204 // It is necessary to escape ' to \' in the query's string value. | 206 // It is necessary to escape ' to \' in the query's string value. |
205 // See also: https://developers.google.com/drive/search-parameters | 207 // See also: https://developers.google.com/drive/search-parameters |
206 std::string EscapeQueryStringValue(const std::string& str) { | 208 std::string EscapeQueryStringValue(const std::string& str) { |
207 std::string result; | 209 std::string result; |
208 ReplaceChars(str, "'", "\\'", &result); | 210 ReplaceChars(str, "'", "\\'", &result); |
209 return result; | 211 return result; |
210 } | 212 } |
211 | 213 |
214 // Parses the query, and builds a search query for Drive API v2. | |
215 // This only supports: | |
216 // Regular query (e.g. dog). | |
217 // Conjunctions (e.g. dog cat). | |
218 // Exclusion query (e.g. -cat). | |
hashimoto
2013/04/18 07:23:50
Please add output examples.
hidehiko
2013/04/18 09:04:32
Done.
| |
219 // Quoted query (e.g. "dog cat"). | |
kinaba
2013/04/18 06:59:48
Could you add the URLs for the search syntax of tw
hidehiko
2013/04/18 09:04:32
Done.
| |
220 std::string TranslateQuery(const std::string& original_query) { | |
hashimoto
2013/04/18 07:23:50
Can't we have a test for this function or DriveAPI
hidehiko
2013/04/18 09:04:32
Done.
| |
221 base::string16 query = UTF8ToUTF16(original_query); | |
hashimoto
2013/04/18 07:23:50
Why do we need to convert the string to UTF16, not
hidehiko
2013/04/18 09:04:32
To handle non-ascii white space correctly. Added c
| |
222 const base::string16 kDelimiter( | |
223 kWhitespaceUTF16 + base::string16(1, static_cast<char16>('"'))); | |
224 | |
225 std::string result; | |
226 for (size_t index = query.find_first_not_of(kWhitespaceUTF16); | |
227 index != base::string16::npos; | |
228 index = query.find_first_not_of(kWhitespaceUTF16, index)) { | |
229 bool is_exclusion = false; | |
230 size_t begin_token = index; | |
231 base::string16 token; | |
232 if (query[begin_token] == '"') { | |
233 // Quoted query. | |
234 ++begin_token; | |
235 size_t end_token = query.find('"', begin_token); | |
236 if (end_token == base::string16::npos) { | |
237 // This is kind of syntax error, since quoted string isn't finished. | |
238 // However, the query is built by user manually, so here we treat | |
239 // whole remaining string as a token as a fallback. | |
kinaba
2013/04/18 06:59:48
How about:
end_token = query.length();
query += '"
hidehiko
2013/04/18 09:04:32
Indeed. Done.
| |
240 token = query.substr(begin_token); | |
241 index = base::string16::npos; | |
242 } else { | |
243 token = query.substr(begin_token, end_token - begin_token); | |
244 index = (end_token + 1); // Consume last '"', too. | |
245 } | |
246 } else { | |
247 size_t end_token = query.find_first_of(kDelimiter, begin_token); | |
248 if (end_token == base::string16::npos) { | |
249 end_token = query.length(); | |
250 } | |
251 | |
252 if (query[begin_token] == '-' && end_token > begin_token + 1) { | |
253 // This is an exclusion query token. | |
254 is_exclusion = true; | |
255 ++begin_token; | |
256 } | |
257 | |
258 token = query.substr(begin_token, end_token - begin_token); | |
259 index = end_token; | |
260 } | |
261 | |
262 if (!result.empty()) { | |
263 // If there are two or more tokens, need to connect with "and". | |
264 result.append(" and "); | |
265 } | |
266 | |
267 base::StringAppendF( | |
kinaba
2013/04/18 06:59:48
Please add comment on what "fullText" includes, li
hidehiko
2013/04/18 09:04:32
Done.
| |
268 &result, | |
269 "%sfullText contains \'%s\'", | |
270 is_exclusion ? "not " : "", | |
271 EscapeQueryStringValue(UTF16ToUTF8(token)).c_str()); | |
272 } | |
273 | |
274 return result; | |
275 } | |
276 | |
212 // The resource ID for the root directory for Drive API is defined in the spec: | 277 // The resource ID for the root directory for Drive API is defined in the spec: |
213 // https://developers.google.com/drive/folder | 278 // https://developers.google.com/drive/folder |
214 const char kDriveApiRootDirectoryResourceId[] = "root"; | 279 const char kDriveApiRootDirectoryResourceId[] = "root"; |
215 | 280 |
216 } // namespace | 281 } // namespace |
217 | 282 |
218 DriveAPIService::DriveAPIService( | 283 DriveAPIService::DriveAPIService( |
219 net::URLRequestContextGetter* url_request_context_getter, | 284 net::URLRequestContextGetter* url_request_context_getter, |
220 const GURL& base_url, | 285 const GURL& base_url, |
221 const GURL& wapi_base_url, | 286 const GURL& wapi_base_url, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 const GetResourceListCallback& callback) { | 400 const GetResourceListCallback& callback) { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
337 DCHECK(!search_query.empty()); | 402 DCHECK(!search_query.empty()); |
338 DCHECK(!callback.is_null()); | 403 DCHECK(!callback.is_null()); |
339 | 404 |
340 runner_->StartOperationWithRetry( | 405 runner_->StartOperationWithRetry( |
341 new GetFilelistOperation( | 406 new GetFilelistOperation( |
342 operation_registry(), | 407 operation_registry(), |
343 url_request_context_getter_, | 408 url_request_context_getter_, |
344 url_generator_, | 409 url_generator_, |
345 search_query, | 410 TranslateQuery(search_query), |
346 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); | 411 base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback))); |
347 } | 412 } |
348 | 413 |
349 void DriveAPIService::SearchInDirectory( | 414 void DriveAPIService::SearchInDirectory( |
350 const std::string& search_query, | 415 const std::string& search_query, |
351 const std::string& directory_resource_id, | 416 const std::string& directory_resource_id, |
352 const GetResourceListCallback& callback) { | 417 const GetResourceListCallback& callback) { |
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
354 DCHECK(!search_query.empty()); | 419 DCHECK(!search_query.empty()); |
355 DCHECK(!directory_resource_id.empty()); | 420 DCHECK(!directory_resource_id.empty()); |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 } | 783 } |
719 | 784 |
720 void DriveAPIService::OnProgressUpdate( | 785 void DriveAPIService::OnProgressUpdate( |
721 const OperationProgressStatusList& list) { | 786 const OperationProgressStatusList& list) { |
722 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
723 FOR_EACH_OBSERVER( | 788 FOR_EACH_OBSERVER( |
724 DriveServiceObserver, observers_, OnProgressUpdate(list)); | 789 DriveServiceObserver, observers_, OnProgressUpdate(list)); |
725 } | 790 } |
726 | 791 |
727 } // namespace google_apis | 792 } // namespace google_apis |
OLD | NEW |