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_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 = false; |
| 44 size_t begin_token = index; |
| 45 base::string16 token; |
| 46 if (query[begin_token] == '"') { |
| 47 // Quoted query. |
| 48 ++begin_token; |
| 49 size_t end_token = query.find('"', begin_token); |
| 50 if (end_token == base::string16::npos) { |
| 51 // This is kind of syntax error, since quoted string isn't finished. |
| 52 // However, the query is built by user manually, so here we treat |
| 53 // whole remaining string as a token as a fallback, by appending |
| 54 // a missing double-quote character. |
| 55 end_token = query.length(); |
| 56 query.push_back('"'); |
| 57 } |
| 58 |
| 59 token = query.substr(begin_token, end_token - begin_token); |
| 60 index = end_token + 1; // Consume last '"', too. |
| 61 } else { |
| 62 size_t end_token = query.find_first_of(kDelimiter, begin_token); |
| 63 if (end_token == base::string16::npos) { |
| 64 end_token = query.length(); |
| 65 } |
| 66 |
| 67 if (query[begin_token] == '-' && end_token > begin_token + 1) { |
| 68 // This is an exclusion query token. |
| 69 is_exclusion = true; |
| 70 ++begin_token; |
| 71 } |
| 72 |
| 73 token = query.substr(begin_token, end_token - begin_token); |
| 74 index = end_token; |
| 75 } |
| 76 |
| 77 if (!result.empty()) { |
| 78 // If there are two or more tokens, need to connect with "and". |
| 79 result.append(" and "); |
| 80 } |
| 81 |
| 82 // The meaning of "fullText" should include title, description and content. |
| 83 base::StringAppendF( |
| 84 &result, |
| 85 "%sfullText contains \'%s\'", |
| 86 is_exclusion ? "not " : "", |
| 87 EscapeQueryStringValue(UTF16ToUTF8(token)).c_str()); |
| 88 } |
| 89 |
| 90 return result; |
| 91 } |
| 92 |
30 } // namespace util | 93 } // namespace util |
31 } // namespace drive | 94 } // namespace drive |
32 } // namespace google_apis | 95 } // namespace google_apis |
OLD | NEW |