[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_HISTORY_QUERY_PARSER_H_ |
| 6 | #define CHROME_BROWSER_HISTORY_QUERY_PARSER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame^] | 11 | #include "base/strings/string16.h" |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 12 | #include "chrome/browser/history/snippet.h" |
| 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | class QueryNodeList; |
| 15 | |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 16 | // Used by HasMatchIn. |
| 17 | struct QueryWord { |
| 18 | // The work to match against. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 19 | string16 word; |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 20 | |
| 21 | // The starting position of the word in the original text. |
[email protected] | c29962f2 | 2008-12-03 00:47:58 | [diff] [blame] | 22 | size_t position; |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 23 | }; |
| 24 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 25 | // QueryNode is used by QueryParser to represent the elements that constitute a |
| 26 | // query. While QueryNode is exposed by way of ParseQuery, it really isn't meant |
| 27 | // for external usage. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | class QueryNode { |
| 29 | public: |
| 30 | virtual ~QueryNode() {} |
| 31 | |
| 32 | // Serialize ourselves out to a string that can be passed to SQLite. Returns |
| 33 | // the number of words in this node. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 34 | virtual int AppendToSQLiteQuery(string16* query) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 36 | // Return true if this is a QueryNodeWord, false if it's a QueryNodeList. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | virtual bool IsWord() const = 0; |
| 38 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 39 | // Returns true if this node matches |word|. If |exact| is true, the string |
| 40 | // must exactly match. Otherwise, this uses a starts with comparison. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 41 | virtual bool Matches(const string16& word, bool exact) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | 2532060 | 2012-10-18 22:05:56 | [diff] [blame] | 43 | // Returns true if this node matches at least one of the words in |words|. An |
| 44 | // entry is added to |match_positions| for all matching words giving the |
| 45 | // matching regions. |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 46 | virtual bool HasMatchIn(const std::vector<QueryWord>& words, |
| 47 | Snippet::MatchPositions* match_positions) const = 0; |
[email protected] | 7de9959 | 2008-12-09 19:16:02 | [diff] [blame] | 48 | |
| 49 | // Appends the words that make up this node in |words|. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 50 | virtual void AppendWords(std::vector<string16>* words) const = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | }; |
| 52 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 53 | // This class is used to parse queries entered into the history search into more |
| 54 | // normalized queries that can be passed to the SQLite backend. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | class QueryParser { |
| 56 | public: |
| 57 | QueryParser(); |
| 58 | |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 59 | // For CJK ideographs and Korean Hangul, even a single character |
| 60 | // can be useful in prefix matching, but that may give us too many |
| 61 | // false positives. Moreover, the current ICU word breaker gives us |
| 62 | // back every single Chinese character as a word so that there's no |
| 63 | // point doing anything for them and we only adjust the minimum length |
| 64 | // to 2 for Korean Hangul while using 3 for others. This is a temporary |
| 65 | // hack until we have a segmentation support. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 66 | static bool IsWordLongEnoughForPrefixSearch(const string16& word); |
[email protected] | 85d911c | 2009-05-19 03:59:42 | [diff] [blame] | 67 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // Parse a query into a SQLite query. The resulting query is placed in |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 69 | // |sqlite_query| and the number of words is returned. |
| 70 | int ParseQuery(const string16& query, string16* sqlite_query); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | |
[email protected] | 8c793c8 | 2011-05-19 00:41:33 | [diff] [blame] | 72 | // Parses |query|, returning the words that make up it. Any words in quotes |
| 73 | // are put in |words| without the quotes. For example, the query text |
| 74 | // "foo bar" results in two entries being added to words, one for foo and one |
| 75 | // for bar. |
| 76 | void ParseQueryWords(const string16& query, std::vector<string16>* words); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | |
[email protected] | 8c793c8 | 2011-05-19 00:41:33 | [diff] [blame] | 78 | // Parses |query|, returning the nodes that constitute the valid words in the |
| 79 | // query. This is intended for later usage with DoesQueryMatch. Ownership of |
| 80 | // the nodes passes to the caller. |
| 81 | void ParseQueryNodes(const string16& query, std::vector<QueryNode*>* nodes); |
[email protected] | 7de9959 | 2008-12-09 19:16:02 | [diff] [blame] | 82 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | // Returns true if the string text matches the query nodes created by a call |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 84 | // to ParseQuery. If the query does match, each of the matching positions in |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 85 | // the text is added to |match_positions|. |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 86 | bool DoesQueryMatch(const string16& text, |
[email protected] | 6956cd6 | 2008-08-29 19:48:58 | [diff] [blame] | 87 | const std::vector<QueryNode*>& nodes, |
| 88 | Snippet::MatchPositions* match_positions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | |
| 90 | private: |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 91 | // Does the work of parsing |query|; creates nodes in |root| as appropriate. |
| 92 | // This is invoked from both of the ParseQuery methods. |
| 93 | bool ParseQueryImpl(const string16& query, QueryNodeList* root); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
[email protected] | 13f698d | 2011-05-12 21:55:45 | [diff] [blame] | 95 | // Extracts the words from |text|, placing each word into |words|. |
| 96 | void ExtractQueryWords(const string16& text, std::vector<QueryWord>* words); |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(QueryParser); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | }; |
| 100 | |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 101 | #endif // CHROME_BROWSER_HISTORY_QUERY_PARSER_H_ |