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

Side by Side Diff: chrome/browser/google_apis/drive_api_util.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: Refine query parsing. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_util.h" 5 #include "chrome/browser/google_apis/drive_api_util.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/string16.h"
8 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
11 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/google_apis/drive_switches.h" 12 #include "chrome/browser/google_apis/drive_switches.h"
10 13
11 namespace google_apis { 14 namespace google_apis {
12 namespace util { 15 namespace util {
13 16
14 bool IsDriveV2ApiEnabled() { 17 bool IsDriveV2ApiEnabled() {
15 return CommandLine::ForCurrentProcess()->HasSwitch( 18 return CommandLine::ForCurrentProcess()->HasSwitch(
16 switches::kEnableDriveV2Api); 19 switches::kEnableDriveV2Api);
17 } 20 }
18 21
19 } // namespace util 22 } // namespace util
20 23
21 namespace drive { 24 namespace drive {
22 namespace util { 25 namespace util {
23 26
24 std::string EscapeQueryStringValue(const std::string& str) { 27 std::string EscapeQueryStringValue(const std::string& str) {
25 std::string result; 28 std::string result;
26 ReplaceChars(str, "'", "\\'", &result); 29 ReplaceChars(str, "'", "\\'", &result);
27 return result; 30 return result;
28 } 31 }
29 32
33 std::string TranslateQuery(const std::string& original_query) {
34 // In order to handle non-ascii white spaces correctly, convert to UTF16.
35 base::string16 query = UTF8ToUTF16(original_query);
36 const base::string16 kDelimiter(
37 kWhitespaceUTF16 + base::string16(1, static_cast<char16>('"')));
38
39 std::string result;
40 for (size_t index = query.find_first_not_of(kWhitespaceUTF16);
41 index != base::string16::npos;
42 index = query.find_first_not_of(kWhitespaceUTF16, index)) {
43 bool is_exclusion = (query[index] == '-');
44 if (is_exclusion)
45 ++index;
46 if (index == query.length()) {
47 // Here, the token is '-' and it should be ignored.
48 continue;
49 }
50
51 size_t begin_token = index;
52 base::string16 token;
53 if (query[begin_token] == '"') {
54 // Quoted query.
55 ++begin_token;
56 size_t end_token = query.find('"', begin_token);
57 if (end_token == base::string16::npos) {
58 // This is kind of syntax error, since quoted string isn't finished.
59 // However, the query is built by user manually, so here we treat
60 // whole remaining string as a token as a fallback, by appending
61 // a missing double-quote character.
62 end_token = query.length();
63 query.push_back('"');
64 }
65
66 token = query.substr(begin_token, end_token - begin_token);
67 index = end_token + 1; // Consume last '"', too.
68 } else {
69 size_t end_token = query.find_first_of(kDelimiter, begin_token);
70 if (end_token == base::string16::npos) {
71 end_token = query.length();
72 }
73
74 token = query.substr(begin_token, end_token - begin_token);
75 index = end_token;
76 }
77
78 if (token.empty()) {
79 // Just ignore an empty token.
80 continue;
81 }
82
83 if (!result.empty()) {
84 // If there are two or more tokens, need to connect with "and".
85 result.append(" and ");
86 }
87
88 // The meaning of "fullText" should include title, description and content.
89 base::StringAppendF(
90 &result,
91 "%sfullText contains \'%s\'",
92 is_exclusion ? "not " : "",
93 EscapeQueryStringValue(UTF16ToUTF8(token)).c_str());
94 }
95
96 return result;
97 }
98
30 } // namespace util 99 } // namespace util
31 } // namespace drive 100 } // namespace drive
32 } // namespace google_apis 101 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/drive_api_util.h ('k') | chrome/browser/google_apis/drive_api_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698