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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <set> | 9 #include <set> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
12 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
13 #include "chrome/browser/history/shortcuts_backend.h" | 14 #include "chrome/browser/history/shortcuts_backend.h" |
14 | 15 |
15 class Profile; | 16 class Profile; |
16 | 17 |
17 // Provider of recently autocompleted links. Provides autocomplete suggestions | 18 // Provider of recently autocompleted links. Provides autocomplete suggestions |
18 // from previously selected suggestions. The more often a user selects a | 19 // from previously selected suggestions. The more often a user selects a |
19 // suggestion for a given search term the higher will be that suggestion's | 20 // suggestion for a given search term the higher will be that suggestion's |
20 // ranking for future uses of that search term. | 21 // ranking for future uses of that search term. |
21 class ShortcutsProvider | 22 class ShortcutsProvider |
22 : public AutocompleteProvider, | 23 : public AutocompleteProvider, |
23 public history::ShortcutsBackend::ShortcutsBackendObserver { | 24 public history::ShortcutsBackend::ShortcutsBackendObserver { |
24 public: | 25 public: |
25 ShortcutsProvider(AutocompleteProviderListener* listener, Profile* profile); | 26 ShortcutsProvider(AutocompleteProviderListener* listener, Profile* profile); |
26 | 27 |
27 // Performs the autocompletion synchronously. Since no asynch completion is | 28 // Performs the autocompletion synchronously. Since no asynch completion is |
28 // performed |minimal_changes| is ignored. | 29 // performed |minimal_changes| is ignored. |
29 virtual void Start(const AutocompleteInput& input, | 30 virtual void Start(const AutocompleteInput& input, |
30 bool minimal_changes) OVERRIDE; | 31 bool minimal_changes) OVERRIDE; |
31 | 32 |
32 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; | 33 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; |
33 | 34 |
34 private: | 35 private: |
| 36 friend class ClassifyTest; |
35 friend class ShortcutsProviderTest; | 37 friend class ShortcutsProviderTest; |
36 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, ClassifyAllMatchesInString); | |
37 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); | 38 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); |
38 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, DeleteMatch); | 39 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, DeleteMatch); |
39 | 40 |
| 41 typedef std::multimap<char16, string16> WordMap; |
| 42 |
40 virtual ~ShortcutsProvider(); | 43 virtual ~ShortcutsProvider(); |
41 | 44 |
42 // ShortcutsBackendObserver: | 45 // ShortcutsBackendObserver: |
43 virtual void OnShortcutsLoaded() OVERRIDE; | 46 virtual void OnShortcutsLoaded() OVERRIDE; |
44 | 47 |
45 void DeleteMatchesWithURLs(const std::set<GURL>& urls); | 48 void DeleteMatchesWithURLs(const std::set<GURL>& urls); |
46 void DeleteShortcutsWithURLs(const std::set<GURL>& urls); | 49 void DeleteShortcutsWithURLs(const std::set<GURL>& urls); |
47 | 50 |
48 // Performs the autocomplete matching and scoring. | 51 // Performs the autocomplete matching and scoring. |
49 void GetMatches(const AutocompleteInput& input); | 52 void GetMatches(const AutocompleteInput& input); |
50 | 53 |
51 AutocompleteMatch ShortcutToACMatch( | 54 AutocompleteMatch ShortcutToACMatch( |
52 const AutocompleteInput& input, | 55 int relevance, |
53 const string16& terms, | 56 const string16& terms, |
54 const history::ShortcutsBackend::Shortcut& shortcut); | 57 const history::ShortcutsBackend::Shortcut& shortcut); |
55 | 58 |
| 59 // Returns a map mapping characters to groups of words from |text| that start |
| 60 // with those characters, ordered lexicographically descending so that longer |
| 61 // words appear before their prefixes (if any) within a particular |
| 62 // equal_range(). |
| 63 static WordMap CreateWordMapForString(const string16& text); |
| 64 |
56 // Given |text| and a corresponding base set of classifications | 65 // Given |text| and a corresponding base set of classifications |
57 // |original_class|, adds ACMatchClassification::MATCH markers for all | 66 // |original_class|, adds ACMatchClassification::MATCH markers for all |
58 // instances of the words from |find_text| within |text| and returns the | 67 // instances of the words from |find_words| within |text| and returns the |
59 // resulting classifications. | 68 // resulting classifications. (|find_text| is provided as the original string |
| 69 // used to create |find_words|. This is supplied because it's common for this |
| 70 // to be a prefix of |text|, so we can quickly check for that and mark that |
| 71 // entire substring as a match before proceeding with the more generic |
| 72 // algorithm.) |
60 // | 73 // |
61 // For example, given the |text| | 74 // For example, given the |text| |
62 // "Sports and News at sports.somesite.com - visit us!" and |original_class| | 75 // "Sports and News at sports.somesite.com - visit us!" and |original_class| |
63 // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a | 76 // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a |
64 // URL), calling with |find_text| set to "sp ew" would return | 77 // URL), calling with |find_text| set to "sp ew" would return |
65 // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH}, | 78 // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH}, |
66 // {20, URL}, {37, NONE}} | 79 // {20, URL}, {37, NONE}}. |
| 80 // |
| 81 // |find_words| should be as constructed by CreateWordMapForString(find_text). |
| 82 // |
| 83 // |find_text| (and thus |find_words|) are expected to be lowercase. |text| |
| 84 // will be lowercased in this function. |
67 static ACMatchClassifications ClassifyAllMatchesInString( | 85 static ACMatchClassifications ClassifyAllMatchesInString( |
68 const string16& find_text, | 86 const string16& find_text, |
| 87 const WordMap& find_words, |
69 const string16& text, | 88 const string16& text, |
70 const ACMatchClassifications& original_class); | 89 const ACMatchClassifications& original_class); |
71 | 90 |
72 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. | 91 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. |
73 // Returns shortcuts_map_.end() if there are no matches. | 92 // Returns shortcuts_map_.end() if there are no matches. |
74 history::ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( | 93 history::ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( |
75 const string16& keyword, | 94 const string16& keyword, |
76 history::ShortcutsBackend* backend); | 95 history::ShortcutsBackend* backend); |
77 | 96 |
78 static int CalculateScore( | 97 static int CalculateScore( |
79 const string16& terms, | 98 const string16& terms, |
80 const history::ShortcutsBackend::Shortcut& shortcut); | 99 const history::ShortcutsBackend::Shortcut& shortcut); |
81 | 100 |
82 std::string languages_; | 101 std::string languages_; |
83 bool initialized_; | 102 bool initialized_; |
84 }; | 103 }; |
85 | 104 |
86 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ | 105 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ |
OLD | NEW |