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 #ifndef COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_PROVIDER_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_PROVIDER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string16.h"
11 #include "components/metrics/proto/omnibox_event.pb.h"
12 #include "components/omnibox/browser/autocomplete_match.h"
14 class AutocompleteInput
;
16 typedef std::vector
<metrics::OmniboxEventProto_ProviderInfo
> ProvidersInfo
;
18 // The AutocompleteProviders each return different kinds of matches,
19 // such as history or search matches. These matches are given
20 // "relevance" scores. Higher scores are better matches than lower
21 // scores. The relevance scores and classes providing the respective
22 // matches are as listed below.
24 // IMPORTANT CAVEAT: The tables below are NOT COMPLETE. Developers
25 // often forget to keep these tables in sync with the code when they
26 // change scoring algorithms or add new providers. For example,
27 // neither the HistoryQuickProvider (which is a provider that appears
28 // often) nor the ShortcutsProvider are listed here. For the best
29 // idea of how scoring works and what providers are affecting which
30 // queries, play with chrome://omnibox/ for a while. While the tables
31 // below may have some utility, nothing compares with first-hand
32 // investigation and experience.
34 // UNKNOWN input type:
35 // --------------------------------------------------------------------|-----
36 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
37 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
38 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
39 // Search Primary Provider (past query in history within 2 days) | 1399**
40 // Search Primary Provider (what you typed) | 1300
41 // HistoryURL (what you typed, some inexact matches) | 1200++
42 // Keyword (substituting, exact match) | 1100
43 // Search Primary Provider (past query in history older than 2 days) | 1050--
44 // HistoryURL (some inexact matches) | 900++
45 // BookmarkProvider (prefix match in bookmark title) | 900+-
47 // Search Primary Provider (navigational suggestion) | 800++
48 // Search Primary Provider (suggestion) | 600++
49 // Keyword (inexact match) | 450
50 // Search Secondary Provider (what you typed) | 250
51 // Search Secondary Provider (past query in history) | 200--
52 // Search Secondary Provider (navigational suggestion) | 150++
53 // Search Secondary Provider (suggestion) | 100++
56 // --------------------------------------------------------------------|-----
57 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
58 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
59 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
60 // HistoryURL (what you typed, some inexact matches) | 1200++
61 // Keyword (substituting, exact match) | 1100
62 // HistoryURL (some inexact matches) | 900++
64 // Search Primary Provider (what you typed) | 850
65 // Search Primary Provider (navigational suggestion) | 800++
66 // Search Primary Provider (past query in history) | 750--
67 // Keyword (inexact match) | 700
68 // Search Primary Provider (suggestion) | 300++
69 // Search Secondary Provider (what you typed) | 250
70 // Search Secondary Provider (past query in history) | 200--
71 // Search Secondary Provider (navigational suggestion) | 150++
72 // Search Secondary Provider (suggestion) | 100++
75 // --------------------------------------------------------------------|-----
76 // Search Primary or Secondary (past query in history within 2 days) | 1599**
77 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
78 // Keyword (substituting, exact match) | 1450
79 // Search Primary Provider (past query in history within 2 days) | 1399**
80 // Search Primary Provider (what you typed) | 1300
81 // Search Primary Provider (past query in history older than 2 days) | 1050--
82 // HistoryURL (inexact match) | 900++
83 // BookmarkProvider (prefix match in bookmark title) | 900+-
84 // Search Primary Provider (navigational suggestion) | 800++
85 // Search Primary Provider (suggestion) | 600++
86 // Keyword (inexact match) | 450
87 // Search Secondary Provider (what you typed) | 250
88 // Search Secondary Provider (past query in history) | 200--
89 // Search Secondary Provider (navigational suggestion) | 150++
90 // Search Secondary Provider (suggestion) | 100++
92 // FORCED_QUERY input type:
93 // --------------------------------------------------------------------|-----
94 // Search Primary Provider (past query in history within 2 days) | 1399**
95 // Search Primary Provider (what you typed) | 1300
96 // Search Primary Provider (past query in history older than 2 days) | 1050--
97 // Search Primary Provider (navigational suggestion) | 800++
98 // Search Primary Provider (suggestion) | 600++
100 // (A search keyword is a keyword with a replacement string; a bookmark keyword
101 // is a keyword with no replacement string, that is, a shortcut for a URL.)
103 // There are two possible providers for search suggestions. If the user has
104 // typed a keyword, then the primary provider is the keyword provider and the
105 // secondary provider is the default provider. If the user has not typed a
106 // keyword, then the primary provider corresponds to the default provider.
108 // Search providers may supply relevance values along with their results to be
109 // used in place of client-side calculated values.
111 // The value column gives the ranking returned from the various providers.
112 // ++: a series of matches with relevance from n up to (n + max_matches).
113 // --: relevance score falls off over time (discounted 50 points @ 15 minutes,
114 // 450 points @ two weeks)
115 // **: relevance score falls off over two days (discounted 99 points after two
117 // +-: A base score that the provider will adjust upward or downward based on
118 // provider-specific metrics.
120 // A single result provider for the autocomplete system. Given user input, the
121 // provider decides what (if any) matches to return, their relevance, and their
123 class AutocompleteProvider
124 : public base::RefCountedThreadSafe
<AutocompleteProvider
> {
126 // Different AutocompleteProvider implementations.
128 TYPE_BOOKMARK
= 1 << 0,
129 TYPE_BUILTIN
= 1 << 1,
130 TYPE_HISTORY_QUICK
= 1 << 2,
131 TYPE_HISTORY_URL
= 1 << 3,
132 TYPE_KEYWORD
= 1 << 4,
133 TYPE_SEARCH
= 1 << 5,
134 TYPE_SHORTCUTS
= 1 << 6,
135 TYPE_ZERO_SUGGEST
= 1 << 7,
136 TYPE_CLIPBOARD_URL
= 1 << 8,
139 explicit AutocompleteProvider(Type type
);
141 // Returns a string describing a particular AutocompleteProvider type.
142 static const char* TypeToString(Type type
);
144 // Called to start an autocomplete query. The provider is responsible for
145 // tracking its matches for this query and whether it is done processing the
146 // query. When new matches are available or the provider finishes, it
147 // calls the controller's OnProviderUpdate() method. The controller can then
148 // get the new matches using the provider's accessors.
149 // Exception: Matches available immediately after starting the query (that
150 // is, synchronously) do not cause any notifications to be sent. The
151 // controller is expected to check for these without prompting (since
152 // otherwise, starting each provider running would result in a flurry of
155 // Once Stop() has been called, usually no more notifications should be sent.
156 // (See comments on Stop() below.)
158 // |minimal_changes| is an optimization that lets the provider do less work
159 // when the |input|'s text hasn't changed. See the body of
160 // OmniboxPopupModel::StartAutocomplete().
161 virtual void Start(const AutocompleteInput
& input
, bool minimal_changes
) = 0;
163 // Advises the provider to stop processing. This may be called even if the
164 // provider is already done. If the provider caches any results, it should
165 // clear the cache based on the value of |clear_cached_results|. Normally,
166 // once this is called, the provider should not send more notifications to
169 // If |user_inactivity_timer| is true, Stop() is being called because it's
170 // been a long time since the user started the current query, and returning
171 // further asynchronous results would normally just be disruptive. Most
172 // providers should still stop processing in this case, but continuing is
173 // legal if there's a good reason the user is likely to want even long-
174 // delayed asynchronous results, e.g. the user has explicitly invoked a
175 // keyword extension and the extension is still processing the request.
176 virtual void Stop(bool clear_cached_results
,
177 bool due_to_user_inactivity
);
179 // Returns the enum equivalent to the name of this provider.
180 // TODO(derat): Make metrics use AutocompleteProvider::Type directly, or at
181 // least move this method to the metrics directory.
182 metrics::OmniboxEventProto_ProviderType
AsOmniboxEventProviderType() const;
184 // Called to delete a match and the backing data that produced it. This
185 // match should not appear again in this or future queries. This can only be
186 // called for matches the provider marks as deletable. This should only be
187 // called when no query is running.
188 // NOTE: Do NOT call OnProviderUpdate() in this method, it is the
189 // responsibility of the caller to do so after calling us.
190 virtual void DeleteMatch(const AutocompleteMatch
& match
);
192 // Called when an omnibox event log entry is generated. This gives
193 // a provider the opportunity to add diagnostic information to the
194 // logs. A provider is expected to append a single entry of whatever
195 // information it wants to |provider_info|.
196 virtual void AddProviderInfo(ProvidersInfo
* provider_info
) const;
198 // Called when a new omnibox session starts or the current session ends.
199 // This gives the opportunity to reset the internal state, if any, associated
200 // with the previous session.
201 virtual void ResetSession();
203 // Returns the set of matches for the current query.
204 const ACMatches
& matches() const { return matches_
; }
206 // Returns whether the provider is done processing the query.
207 bool done() const { return done_
; }
209 // Returns this provider's type.
210 Type
type() const { return type_
; }
212 // Returns a string describing this provider's type.
213 const char* GetName() const;
215 // A suggested upper bound for how many matches a provider should return.
216 // TODO(pkasting): http://b/1111299 , http://b/933133 This should go away once
217 // we have good relevance heuristics; the controller should handle all
219 static const size_t kMaxMatches
;
222 friend class base::RefCountedThreadSafe
<AutocompleteProvider
>;
223 FRIEND_TEST_ALL_PREFIXES(BookmarkProviderTest
, InlineAutocompletion
);
225 typedef std::pair
<bool, base::string16
> FixupReturn
;
227 virtual ~AutocompleteProvider();
229 // Fixes up user URL input to make it more possible to match against. Among
230 // many other things, this takes care of the following:
231 // * Prepending file:// to file URLs
232 // * Converting drive letters in file URLs to uppercase
233 // * Converting case-insensitive parts of URLs (like the scheme and domain)
235 // * Convert spaces to %20s
236 // Note that we don't do this in AutocompleteInput's constructor, because if
237 // e.g. we convert a Unicode hostname to punycode, other providers will show
238 // output that surprises the user ("Search Google for xn--6ca.com").
239 // Returns a bool indicating whether fixup succeeded, as well as the fixed-up
240 // input text. The returned string will be the same as the input string if
241 // fixup failed; this lets callers who don't care about failure simply use the
242 // string unconditionally.
243 static FixupReturn
FixupUserInput(const AutocompleteInput
& input
);
245 // Trims "http:" and up to two subsequent slashes from |url|. Returns the
246 // number of characters that were trimmed.
247 // NOTE: For a view-source: URL, this will trim from after "view-source:" and
249 static size_t TrimHttpPrefix(base::string16
* url
);
257 DISALLOW_COPY_AND_ASSIGN(AutocompleteProvider
);
260 #endif // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_PROVIDER_H_