1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains the keyword autocomplete provider. The keyword provider
6 // is responsible for remembering/suggesting user "search keyword queries"
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.
9 // KeywordProvider uses a TemplateURLService to find the set of keywords.
11 #ifndef COMPONENTS_OMNIBOX_BROWSER_KEYWORD_PROVIDER_H_
12 #define COMPONENTS_OMNIBOX_BROWSER_KEYWORD_PROVIDER_H_
16 #include "base/basictypes.h"
17 #include "base/compiler_specific.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "components/metrics/proto/omnibox_input_type.pb.h"
20 #include "components/omnibox/browser/autocomplete_input.h"
21 #include "components/omnibox/browser/autocomplete_provider.h"
22 #include "components/omnibox/browser/keyword_extensions_delegate.h"
24 class AutocompleteProviderClient
;
25 class AutocompleteProviderListener
;
26 class KeywordExtensionsDelegate
;
28 class TemplateURLService
;
30 // Autocomplete provider for keyword input.
32 // After construction, the autocomplete controller repeatedly calls Start()
33 // with some user input, each time expecting to receive a small set of the best
34 // matches (either synchronously or asynchronously).
36 // To construct these matches, the provider treats user input as a series of
37 // whitespace-delimited tokens and tries to match the first token as the prefix
38 // of a known "keyword". A keyword is some string that maps to a search query
39 // URL; the rest of the user's input is taken as the input to the query. For
40 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so
41 // input like "bug 123" would become "http://b/issue?id=123".
43 // Because we do prefix matching, user input could match more than one keyword
44 // at once. (Example: the input "f jazz" matches all keywords starting with
45 // "f".) We return the best matches, up to three.
47 // The resulting matches are shown with content specified by the keyword
48 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and
49 // action "[keyword] %s". If the user has typed a (possibly partial) keyword
50 // but no search terms, the suggested result is shown greyed out, with
51 // "<enter term(s)>" as the substituted input, and does nothing when selected.
52 class KeywordProvider
: public AutocompleteProvider
{
54 KeywordProvider(AutocompleteProviderClient
* client
,
55 AutocompleteProviderListener
* listener
);
57 // Extracts the next whitespace-delimited token from input and returns it.
58 // Sets |remaining_input| to everything after the first token (skipping over
59 // the first intervening whitespace).
60 // If |trim_leading_whitespace| is true then leading whitespace in
61 // |*remaining_input| will be trimmed.
62 static base::string16
SplitKeywordFromInput(const base::string16
& input
,
63 bool trim_leading_whitespace
,
64 base::string16
* remaining_input
);
66 // Returns the replacement string from the user input. The replacement
67 // string is the portion of the input that does not contain the keyword.
68 // For example, the replacement string for "b blah" is blah.
69 // If |trim_leading_whitespace| is true then leading whitespace in
70 // replacement string will be trimmed.
71 static base::string16
SplitReplacementStringFromInput(
72 const base::string16
& input
,
73 bool trim_leading_whitespace
);
75 // Returns the matching substituting keyword for |input|, or NULL if there
76 // is no keyword for the specified input. If the matching keyword was found,
77 // updates |input|'s text and cursor position.
78 static const TemplateURL
* GetSubstitutingTemplateURLForInput(
79 TemplateURLService
* model
,
80 AutocompleteInput
* input
);
82 // If |text| corresponds (in the sense of
83 // TemplateURLModel::CleanUserInputKeyword()) to an enabled, substituting
84 // keyword, returns that keyword; returns the empty string otherwise.
85 base::string16
GetKeywordForText(const base::string16
& text
) const;
87 // Creates a fully marked-up AutocompleteMatch for a specific keyword.
88 AutocompleteMatch
CreateVerbatimMatch(const base::string16
& text
,
89 const base::string16
& keyword
,
90 const AutocompleteInput
& input
);
92 // AutocompleteProvider:
93 void Start(const AutocompleteInput
& input
, bool minimal_changes
) override
;
94 void Stop(bool clear_cached_results
,
95 bool due_to_user_inactivity
) override
;
98 friend class KeywordExtensionsDelegateImpl
;
100 ~KeywordProvider() override
;
102 // Extracts the keyword from |input| into |keyword|. Any remaining characters
103 // after the keyword are placed in |remaining_input|. Returns true if |input|
104 // is valid and has a keyword. This makes use of SplitKeywordFromInput to
105 // extract the keyword and remaining string, and uses
106 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters.
107 // In general use this instead of SplitKeywordFromInput.
108 // Leading whitespace in |*remaining_input| will be trimmed.
109 static bool ExtractKeywordFromInput(const AutocompleteInput
& input
,
110 base::string16
* keyword
,
111 base::string16
* remaining_input
);
113 // 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
115 // matches" mode, and whether the keyword supports replacement.
116 // If |allow_exact_keyword_match| is false, the relevance for complete
117 // keywords that support replacements is degraded.
118 static int CalculateRelevance(metrics::OmniboxInputType::Type type
,
120 bool support_replacement
,
122 bool allow_exact_keyword_match
);
124 // Creates a fully marked-up AutocompleteMatch from the user's input.
125 // If |relevance| is negative, calculate a relevance based on heuristics.
126 AutocompleteMatch
CreateAutocompleteMatch(
127 const TemplateURL
* template_url
,
128 const AutocompleteInput
& input
,
129 size_t prefix_length
,
130 const base::string16
& remaining_input
,
131 bool allowed_to_be_default_match
,
134 // Fills in the "destination_url" and "contents" fields of |match| with the
135 // provided user input and keyword data.
136 void FillInURLAndContents(const base::string16
& remaining_input
,
137 const TemplateURL
* element
,
138 AutocompleteMatch
* match
) const;
140 TemplateURLService
* GetTemplateURLService() const;
142 AutocompleteProviderListener
* listener_
;
144 // Model for the keywords.
145 TemplateURLService
* model_
;
147 // Delegate to handle the extensions-only logic for KeywordProvider.
148 // NULL when extensions are not enabled. May be NULL for tests.
149 scoped_ptr
<KeywordExtensionsDelegate
> extensions_delegate_
;
151 DISALLOW_COPY_AND_ASSIGN(KeywordProvider
);
154 #endif // COMPONENTS_OMNIBOX_BROWSER_KEYWORD_PROVIDER_H_