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 // 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 |
11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
12 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 12 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
19 #include "components/metrics/proto/omnibox_input_type.pb.h" | 19 #include "components/metrics/proto/omnibox_input_type.pb.h" |
20 #include "components/omnibox/autocomplete_input.h" | 20 #include "components/omnibox/autocomplete_input.h" |
21 #include "components/omnibox/autocomplete_provider.h" | 21 #include "components/omnibox/autocomplete_provider.h" |
22 | 22 |
23 class AutocompleteProviderListener; | 23 class AutocompleteProviderListener; |
24 class KeywordExtensionsDelegate; | 24 class KeywordExtensionsDelegate; |
25 class Profile; | |
26 class TemplateURL; | 25 class TemplateURL; |
27 class TemplateURLService; | 26 class TemplateURLService; |
28 | 27 |
29 // Autocomplete provider for keyword input. | 28 // Autocomplete provider for keyword input. |
30 // | 29 // |
31 // After construction, the autocomplete controller repeatedly calls Start() | 30 // After construction, the autocomplete controller repeatedly calls Start() |
32 // with some user input, each time expecting to receive a small set of the best | 31 // with some user input, each time expecting to receive a small set of the best |
33 // matches (either synchronously or asynchronously). | 32 // matches (either synchronously or asynchronously). |
34 // | 33 // |
35 // To construct these matches, the provider treats user input as a series of | 34 // To construct these matches, the provider treats user input as a series of |
36 // whitespace-delimited tokens and tries to match the first token as the prefix | 35 // whitespace-delimited tokens and tries to match the first token as the prefix |
37 // of a known "keyword". A keyword is some string that maps to a search query | 36 // of a known "keyword". A keyword is some string that maps to a search query |
38 // URL; the rest of the user's input is taken as the input to the query. For | 37 // URL; the rest of the user's input is taken as the input to the query. For |
39 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so | 38 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so |
40 // input like "bug 123" would become "http://b/issue?id=123". | 39 // input like "bug 123" would become "http://b/issue?id=123". |
41 // | 40 // |
42 // Because we do prefix matching, user input could match more than one keyword | 41 // Because we do prefix matching, user input could match more than one keyword |
43 // at once. (Example: the input "f jazz" matches all keywords starting with | 42 // at once. (Example: the input "f jazz" matches all keywords starting with |
44 // "f".) We return the best matches, up to three. | 43 // "f".) We return the best matches, up to three. |
45 // | 44 // |
46 // The resulting matches are shown with content specified by the keyword | 45 // The resulting matches are shown with content specified by the keyword |
47 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and | 46 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and |
48 // action "[keyword] %s". If the user has typed a (possibly partial) keyword | 47 // 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 | 48 // 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. | 49 // "<enter term(s)>" as the substituted input, and does nothing when selected. |
51 class KeywordProvider : public AutocompleteProvider { | 50 class KeywordProvider : public AutocompleteProvider { |
52 public: | 51 public: |
53 KeywordProvider(AutocompleteProviderListener* listener, Profile* profile); | |
54 // For testing. | |
55 KeywordProvider(AutocompleteProviderListener* listener, | 52 KeywordProvider(AutocompleteProviderListener* listener, |
56 TemplateURLService* model); | 53 TemplateURLService* model); |
57 | 54 |
| 55 void SetExtensionsDelegate( |
| 56 scoped_ptr<KeywordExtensionsDelegate> extensions_delegate); |
| 57 |
58 // Extracts the next whitespace-delimited token from input and returns it. | 58 // Extracts the next whitespace-delimited token from input and returns it. |
59 // Sets |remaining_input| to everything after the first token (skipping over | 59 // Sets |remaining_input| to everything after the first token (skipping over |
60 // the first intervening whitespace). | 60 // the first intervening whitespace). |
61 // If |trim_leading_whitespace| is true then leading whitespace in | 61 // If |trim_leading_whitespace| is true then leading whitespace in |
62 // |*remaining_input| will be trimmed. | 62 // |*remaining_input| will be trimmed. |
63 static base::string16 SplitKeywordFromInput(const base::string16& input, | 63 static base::string16 SplitKeywordFromInput(const base::string16& input, |
64 bool trim_leading_whitespace, | 64 bool trim_leading_whitespace, |
65 base::string16* remaining_input); | 65 base::string16* remaining_input); |
66 | 66 |
67 // Returns the replacement string from the user input. The replacement | 67 // Returns the replacement string from the user input. The replacement |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 134 |
135 // Fills in the "destination_url" and "contents" fields of |match| with the | 135 // Fills in the "destination_url" and "contents" fields of |match| with the |
136 // provided user input and keyword data. | 136 // provided user input and keyword data. |
137 void FillInURLAndContents(const base::string16& remaining_input, | 137 void FillInURLAndContents(const base::string16& remaining_input, |
138 const TemplateURL* element, | 138 const TemplateURL* element, |
139 AutocompleteMatch* match) const; | 139 AutocompleteMatch* match) const; |
140 | 140 |
141 TemplateURLService* GetTemplateURLService() const; | 141 TemplateURLService* GetTemplateURLService() const; |
142 | 142 |
143 AutocompleteProviderListener* listener_; | 143 AutocompleteProviderListener* listener_; |
144 Profile* profile_; | |
145 | 144 |
146 // Model for the keywords. This is only non-null when testing, otherwise the | 145 // Model for the keywords. |
147 // TemplateURLService from the Profile is used. | |
148 TemplateURLService* model_; | 146 TemplateURLService* model_; |
149 | 147 |
150 // Delegate to handle the extensions-only logic for KeywordProvider. | 148 // Delegate to handle the extensions-only logic for KeywordProvider. |
151 // NULL when extensions are not enabled. May be NULL for tests. | 149 // NULL when extensions are not enabled. May be NULL for tests. |
152 scoped_ptr<KeywordExtensionsDelegate> extensions_delegate_; | 150 scoped_ptr<KeywordExtensionsDelegate> extensions_delegate_; |
153 | 151 |
154 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); | 152 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); |
155 }; | 153 }; |
156 | 154 |
157 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 155 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
OLD | NEW |