Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4959)

Unified Diff: chrome/browser/google_apis/drive_api_service.cc

Issue 14341002: Implement query translation from GData WAPI to Drive API v2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/google_apis/drive_api_service.cc
diff --git a/chrome/browser/google_apis/drive_api_service.cc b/chrome/browser/google_apis/drive_api_service.cc
index 20b48049e8a71ce79ea910f82aeb02506beeaca9..5931e3b8055895538e534e4eb98c6221762bf549 100644
--- a/chrome/browser/google_apis/drive_api_service.cc
+++ b/chrome/browser/google_apis/drive_api_service.cc
@@ -9,10 +9,12 @@
#include "base/bind.h"
#include "base/message_loop_proxy.h"
+#include "base/string16.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/task_runner_util.h"
#include "base/threading/sequenced_worker_pool.h"
+#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/google_apis/auth_service.h"
#include "chrome/browser/google_apis/drive_api_operations.h"
@@ -209,6 +211,69 @@ std::string EscapeQueryStringValue(const std::string& str) {
return result;
}
+// Parses the query, and builds a search query for Drive API v2.
+// This only supports:
+// Regular query (e.g. dog).
+// Conjunctions (e.g. dog cat).
+// Exclusion query (e.g. -cat).
hashimoto 2013/04/18 07:23:50 Please add output examples.
hidehiko 2013/04/18 09:04:32 Done.
+// 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.
+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.
+ 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
+ const base::string16 kDelimiter(
+ kWhitespaceUTF16 + base::string16(1, static_cast<char16>('"')));
+
+ std::string result;
+ for (size_t index = query.find_first_not_of(kWhitespaceUTF16);
+ index != base::string16::npos;
+ index = query.find_first_not_of(kWhitespaceUTF16, index)) {
+ bool is_exclusion = false;
+ size_t begin_token = index;
+ base::string16 token;
+ if (query[begin_token] == '"') {
+ // Quoted query.
+ ++begin_token;
+ size_t end_token = query.find('"', begin_token);
+ if (end_token == base::string16::npos) {
+ // This is kind of syntax error, since quoted string isn't finished.
+ // However, the query is built by user manually, so here we treat
+ // 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.
+ token = query.substr(begin_token);
+ index = base::string16::npos;
+ } else {
+ token = query.substr(begin_token, end_token - begin_token);
+ index = (end_token + 1); // Consume last '"', too.
+ }
+ } else {
+ size_t end_token = query.find_first_of(kDelimiter, begin_token);
+ if (end_token == base::string16::npos) {
+ end_token = query.length();
+ }
+
+ if (query[begin_token] == '-' && end_token > begin_token + 1) {
+ // This is an exclusion query token.
+ is_exclusion = true;
+ ++begin_token;
+ }
+
+ token = query.substr(begin_token, end_token - begin_token);
+ index = end_token;
+ }
+
+ if (!result.empty()) {
+ // If there are two or more tokens, need to connect with "and".
+ result.append(" and ");
+ }
+
+ 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.
+ &result,
+ "%sfullText contains \'%s\'",
+ is_exclusion ? "not " : "",
+ EscapeQueryStringValue(UTF16ToUTF8(token)).c_str());
+ }
+
+ return result;
+}
+
// The resource ID for the root directory for Drive API is defined in the spec:
// https://developers.google.com/drive/folder
const char kDriveApiRootDirectoryResourceId[] = "root";
@@ -342,7 +407,7 @@ void DriveAPIService::Search(const std::string& search_query,
operation_registry(),
url_request_context_getter_,
url_generator_,
- search_query,
+ TranslateQuery(search_query),
base::Bind(&ParseResourceListOnBlockingPoolAndRun, callback)));
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698