OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file contains the keyword autocomplete provider. The keyword provider | 5 // This file contains the keyword autocomplete provider. The keyword provider |
6 // is responsible for remembering/suggesting user "search keyword queries" | 6 // is responsible for remembering/suggesting user "search keyword queries" |
7 // (e.g. "imdb Godzilla") and then fixing them up into valid URLs. An | 7 // (e.g. "imdb Godzilla") and then fixing them up into valid URLs. An |
8 // instance of it gets created and managed by the autocomplete controller. | 8 // instance of it gets created and managed by the autocomplete controller. |
9 // KeywordProvider uses a TemplateURLService to find the set of keywords. | 9 // KeywordProvider uses a TemplateURLService to find the set of keywords. |
10 // | 10 // |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // action "[keyword] %s". If the user has typed a (possibly partial) keyword | 48 // action "[keyword] %s". If the user has typed a (possibly partial) keyword |
49 // but no search terms, the suggested result is shown greyed out, with | 49 // but no search terms, the suggested result is shown greyed out, with |
50 // "<enter term(s)>" as the substituted input, and does nothing when selected. | 50 // "<enter term(s)>" as the substituted input, and does nothing when selected. |
51 class KeywordProvider : public AutocompleteProvider, | 51 class KeywordProvider : public AutocompleteProvider, |
52 public content::NotificationObserver { | 52 public content::NotificationObserver { |
53 public: | 53 public: |
54 KeywordProvider(ACProviderListener* listener, Profile* profile); | 54 KeywordProvider(ACProviderListener* listener, Profile* profile); |
55 // For testing. | 55 // For testing. |
56 KeywordProvider(ACProviderListener* listener, TemplateURLService* model); | 56 KeywordProvider(ACProviderListener* listener, TemplateURLService* model); |
57 | 57 |
| 58 // Extracts the next whitespace-delimited token from input and returns it. |
| 59 // Sets |remaining_input| to everything after the first token (skipping over |
| 60 // the first intervening whitespace). |
| 61 // If |trim_leading_whitespace| is true then leading whitespace in |
| 62 // |*remaining_input| will be trimmed. |
| 63 static string16 SplitKeywordFromInput(const string16& input, |
| 64 bool trim_leading_whitespace, |
| 65 string16* remaining_input); |
| 66 |
58 // Returns the replacement string from the user input. The replacement | 67 // Returns the replacement string from the user input. The replacement |
59 // string is the portion of the input that does not contain the keyword. | 68 // string is the portion of the input that does not contain the keyword. |
60 // For example, the replacement string for "b blah" is blah. | 69 // For example, the replacement string for "b blah" is blah. |
61 // If |trim_leading_whitespace| is true then leading whitespace in | 70 // If |trim_leading_whitespace| is true then leading whitespace in |
62 // replacement string will be trimmed. | 71 // replacement string will be trimmed. |
63 static string16 SplitReplacementStringFromInput( | 72 static string16 SplitReplacementStringFromInput( |
64 const string16& input, | 73 const string16& input, |
65 bool trim_leading_whitespace); | 74 bool trim_leading_whitespace); |
66 | 75 |
67 // Returns the matching substituting keyword for |input|, or NULL if there | 76 // Returns the matching substituting keyword for |input|, or NULL if there |
68 // is no keyword for the specified input. | 77 // is no keyword for the specified input. |
69 static const TemplateURL* GetSubstitutingTemplateURLForInput( | 78 static const TemplateURL* GetSubstitutingTemplateURLForInput( |
70 Profile* profile, | 79 Profile* profile, |
71 const AutocompleteInput& input, | 80 const AutocompleteInput& input, |
72 string16* remaining_input); | 81 string16* remaining_input); |
73 | 82 |
| 83 // If |text| corresponds (in the sense of |
| 84 // TemplateURLModel::CleanUserInputKeyword()) to an enabled, substituting |
| 85 // keyword, returns that keyword; returns the empty string otherwise. |
| 86 string16 GetKeywordForText(const string16& text) const; |
| 87 |
| 88 // Creates a fully marked-up AutocompleteMatch for a specific keyword. |
| 89 AutocompleteMatch CreateAutocompleteMatch( |
| 90 const string16& text, |
| 91 const string16& keyword, |
| 92 const AutocompleteInput& input); |
| 93 |
74 // AutocompleteProvider | 94 // AutocompleteProvider |
75 virtual void Start(const AutocompleteInput& input, | 95 virtual void Start(const AutocompleteInput& input, |
76 bool minimal_changes) OVERRIDE; | 96 bool minimal_changes) OVERRIDE; |
77 virtual void Stop() OVERRIDE; | 97 virtual void Stop() OVERRIDE; |
78 | 98 |
79 private: | 99 private: |
80 class ScopedEndExtensionKeywordMode; | 100 class ScopedEndExtensionKeywordMode; |
81 friend class ScopedEndExtensionKeywordMode; | 101 friend class ScopedEndExtensionKeywordMode; |
82 | 102 |
83 virtual ~KeywordProvider(); | 103 virtual ~KeywordProvider(); |
84 | 104 |
85 // Extracts the keyword from |input| into |keyword|. Any remaining characters | 105 // Extracts the keyword from |input| into |keyword|. Any remaining characters |
86 // after the keyword are placed in |remaining_input|. Returns true if |input| | 106 // after the keyword are placed in |remaining_input|. Returns true if |input| |
87 // is valid and has a keyword. This makes use of SplitKeywordFromInput to | 107 // is valid and has a keyword. This makes use of SplitKeywordFromInput to |
88 // extract the keyword and remaining string, and uses | 108 // extract the keyword and remaining string, and uses |
89 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. | 109 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. |
90 // In general use this instead of SplitKeywordFromInput. | 110 // In general use this instead of SplitKeywordFromInput. |
91 // Leading whitespace in |*remaining_input| will be trimmed. | 111 // Leading whitespace in |*remaining_input| will be trimmed. |
92 static bool ExtractKeywordFromInput(const AutocompleteInput& input, | 112 static bool ExtractKeywordFromInput(const AutocompleteInput& input, |
93 string16* keyword, | 113 string16* keyword, |
94 string16* remaining_input); | 114 string16* remaining_input); |
95 | 115 |
96 // Extracts the next whitespace-delimited token from input and returns it. | |
97 // Sets |remaining_input| to everything after the first token (skipping over | |
98 // the first intervening whitespace). | |
99 // If |trim_leading_whitespace| is true then leading whitespace in | |
100 // |*remaining_input| will be trimmed. | |
101 static string16 SplitKeywordFromInput(const string16& input, | |
102 bool trim_leading_whitespace, | |
103 string16* remaining_input); | |
104 | |
105 // Fills in the "destination_url" and "contents" fields of |match| with the | 116 // Fills in the "destination_url" and "contents" fields of |match| with the |
106 // provided user input and keyword data. | 117 // provided user input and keyword data. |
107 static void FillInURLAndContents( | 118 static void FillInURLAndContents( |
108 Profile* profile, | 119 Profile* profile, |
109 const string16& remaining_input, | 120 const string16& remaining_input, |
110 const TemplateURL* element, | 121 const TemplateURL* element, |
111 AutocompleteMatch* match); | 122 AutocompleteMatch* match); |
112 | 123 |
113 // Determines the relevance for some input, given its type, whether the user | 124 // Determines the relevance for some input, given its type, whether the user |
114 // typed the complete keyword, and whether the user is in "prefer keyword | 125 // typed the complete keyword, and whether the user is in "prefer keyword |
(...skipping 17 matching lines...) Expand all Loading... |
132 int relevance); | 143 int relevance); |
133 | 144 |
134 void EnterExtensionKeywordMode(const std::string& extension_id); | 145 void EnterExtensionKeywordMode(const std::string& extension_id); |
135 void MaybeEndExtensionKeywordMode(); | 146 void MaybeEndExtensionKeywordMode(); |
136 | 147 |
137 // content::NotificationObserver interface. | 148 // content::NotificationObserver interface. |
138 virtual void Observe(int type, | 149 virtual void Observe(int type, |
139 const content::NotificationSource& source, | 150 const content::NotificationSource& source, |
140 const content::NotificationDetails& details) OVERRIDE; | 151 const content::NotificationDetails& details) OVERRIDE; |
141 | 152 |
| 153 TemplateURLService* GetTemplateURLService() const; |
| 154 |
142 // Model for the keywords. This is only non-null when testing, otherwise the | 155 // Model for the keywords. This is only non-null when testing, otherwise the |
143 // TemplateURLService from the Profile is used. | 156 // TemplateURLService from the Profile is used. |
144 TemplateURLService* model_; | 157 TemplateURLService* model_; |
145 | 158 |
146 // Identifies the current input state. This is incremented each time the | 159 // Identifies the current input state. This is incremented each time the |
147 // autocomplete edit's input changes in any way. It is used to tell whether | 160 // autocomplete edit's input changes in any way. It is used to tell whether |
148 // suggest results from the extension are current. | 161 // suggest results from the extension are current. |
149 int current_input_id_; | 162 int current_input_id_; |
150 | 163 |
151 // The input state at the time we last asked the extension for suggest | 164 // The input state at the time we last asked the extension for suggest |
152 // results. | 165 // results. |
153 AutocompleteInput extension_suggest_last_input_; | 166 AutocompleteInput extension_suggest_last_input_; |
154 | 167 |
155 // We remember the last suggestions we've received from the extension in case | 168 // We remember the last suggestions we've received from the extension in case |
156 // we need to reset our matches without asking the extension again. | 169 // we need to reset our matches without asking the extension again. |
157 std::vector<AutocompleteMatch> extension_suggest_matches_; | 170 std::vector<AutocompleteMatch> extension_suggest_matches_; |
158 | 171 |
159 // If non-empty, holds the ID of the extension whose keyword is currently in | 172 // If non-empty, holds the ID of the extension whose keyword is currently in |
160 // the URL bar while the autocomplete popup is open. | 173 // the URL bar while the autocomplete popup is open. |
161 std::string current_keyword_extension_id_; | 174 std::string current_keyword_extension_id_; |
162 | 175 |
163 content::NotificationRegistrar registrar_; | 176 content::NotificationRegistrar registrar_; |
164 | 177 |
165 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); | 178 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); |
166 }; | 179 }; |
167 | 180 |
168 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 181 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
OLD | NEW |