Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / autocomplete / autocomplete_provider.h
blobb59f97abf98dbc1315a967541ccc48401b6264e2
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_PROVIDER_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_PROVIDER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string16.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "chrome/common/metrics/proto/omnibox_event.pb.h"
16 class AutocompleteInput;
17 class AutocompleteProviderListener;
18 class GURL;
19 class Profile;
21 typedef std::vector<metrics::OmniboxEventProto_ProviderInfo> ProvidersInfo;
23 // The AutocompleteProviders each return different kinds of matches,
24 // such as history or search matches. These matches are given
25 // "relevance" scores. Higher scores are better matches than lower
26 // scores. The relevance scores and classes providing the respective
27 // matches are as listed below.
29 // IMPORTANT CAVEAT: The tables below are NOT COMPLETE. Developers
30 // often forget to keep these tables in sync with the code when they
31 // change scoring algorithms or add new providers. For example,
32 // neither the HistoryQuickProvider (which is a provider that appears
33 // often) nor the ShortcutsProvider are listed here. For the best
34 // idea of how scoring works and what providers are affecting which
35 // queries, play with chrome://omnibox/ for a while. While the tables
36 // below may have some utility, nothing compares with first-hand
37 // investigation and experience.
39 // UNKNOWN input type:
40 // --------------------------------------------------------------------|-----
41 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
42 // Extension App (exact match) | 1425
43 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
44 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
45 // Search Primary Provider (past query in history within 2 days) | 1399**
46 // Search Primary Provider (what you typed) | 1300
47 // HistoryURL (what you typed, some inexact matches) | 1200++
48 // Extension App (inexact match) | 1175*~
49 // Keyword (substituting, exact match) | 1100
50 // Search Primary Provider (past query in history older than 2 days) | 1050--
51 // HistoryURL (some inexact matches) | 900++
52 // BookmarkProvider (prefix match in bookmark title) | 900+-
53 // Built-in | 860++
54 // Search Primary Provider (navigational suggestion) | 800++
55 // Search Primary Provider (suggestion) | 600++
56 // Keyword (inexact match) | 450
57 // Search Secondary Provider (what you typed) | 250
58 // Search Secondary Provider (past query in history) | 200--
59 // Search Secondary Provider (navigational suggestion) | 150++
60 // Search Secondary Provider (suggestion) | 100++
62 // URL input type:
63 // --------------------------------------------------------------------|-----
64 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
65 // Extension App (exact match) | 1425
66 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
67 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
68 // HistoryURL (what you typed, some inexact matches) | 1200++
69 // Extension App (inexact match) | 1175*~
70 // Keyword (substituting, exact match) | 1100
71 // HistoryURL (some inexact matches) | 900++
72 // Built-in | 860++
73 // Search Primary Provider (what you typed) | 850
74 // Search Primary Provider (navigational suggestion) | 800++
75 // Search Primary Provider (past query in history) | 750--
76 // Keyword (inexact match) | 700
77 // Search Primary Provider (suggestion) | 300++
78 // Search Secondary Provider (what you typed) | 250
79 // Search Secondary Provider (past query in history) | 200--
80 // Search Secondary Provider (navigational suggestion) | 150++
81 // Search Secondary Provider (suggestion) | 100++
83 // QUERY input type:
84 // --------------------------------------------------------------------|-----
85 // Search Primary or Secondary (past query in history within 2 days) | 1599**
86 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
87 // Keyword (substituting, exact match) | 1450
88 // Extension App (exact match) | 1425
89 // Search Primary Provider (past query in history within 2 days) | 1399**
90 // Search Primary Provider (what you typed) | 1300
91 // Extension App (inexact match) | 1175*~
92 // Search Primary Provider (past query in history older than 2 days) | 1050--
93 // HistoryURL (inexact match) | 900++
94 // BookmarkProvider (prefix match in bookmark title) | 900+-
95 // Search Primary Provider (navigational suggestion) | 800++
96 // Search Primary Provider (suggestion) | 600++
97 // Keyword (inexact match) | 450
98 // Search Secondary Provider (what you typed) | 250
99 // Search Secondary Provider (past query in history) | 200--
100 // Search Secondary Provider (navigational suggestion) | 150++
101 // Search Secondary Provider (suggestion) | 100++
103 // FORCED_QUERY input type:
104 // --------------------------------------------------------------------|-----
105 // Extension App (exact match on title only, not url) | 1425
106 // Search Primary Provider (past query in history within 2 days) | 1399**
107 // Search Primary Provider (what you typed) | 1300
108 // Extension App (inexact match on title only, not url) | 1175*~
109 // Search Primary Provider (past query in history older than 2 days) | 1050--
110 // Search Primary Provider (navigational suggestion) | 800++
111 // Search Primary Provider (suggestion) | 600++
113 // (A search keyword is a keyword with a replacement string; a bookmark keyword
114 // is a keyword with no replacement string, that is, a shortcut for a URL.)
116 // There are two possible providers for search suggestions. If the user has
117 // typed a keyword, then the primary provider is the keyword provider and the
118 // secondary provider is the default provider. If the user has not typed a
119 // keyword, then the primary provider corresponds to the default provider.
121 // Search providers may supply relevance values along with their results to be
122 // used in place of client-side calculated values.
124 // The value column gives the ranking returned from the various providers.
125 // ++: a series of matches with relevance from n up to (n + max_matches).
126 // --: relevance score falls off over time (discounted 50 points @ 15 minutes,
127 // 450 points @ two weeks)
128 // **: relevance score falls off over two days (discounted 99 points after two
129 // days).
130 // *~: Partial matches get a score on a sliding scale from about 575-1125 based
131 // on how many times the URL for the Extension App has been typed and how
132 // many of the letters match.
133 // +-: A base score that the provider will adjust upward or downward based on
134 // provider-specific metrics.
136 // A single result provider for the autocomplete system. Given user input, the
137 // provider decides what (if any) matches to return, their relevance, and their
138 // classifications.
139 class AutocompleteProvider
140 : public base::RefCountedThreadSafe<AutocompleteProvider> {
141 public:
142 // Different AutocompleteProvider implementations.
143 enum Type {
144 TYPE_BOOKMARK = 1 << 0,
145 TYPE_BUILTIN = 1 << 1,
146 TYPE_CONTACT = 1 << 2,
147 TYPE_EXTENSION_APP = 1 << 3,
148 TYPE_HISTORY_QUICK = 1 << 4,
149 TYPE_HISTORY_URL = 1 << 5,
150 TYPE_KEYWORD = 1 << 6,
151 TYPE_SEARCH = 1 << 7,
152 TYPE_SHORTCUTS = 1 << 8,
153 TYPE_ZERO_SUGGEST = 1 << 9,
156 AutocompleteProvider(AutocompleteProviderListener* listener,
157 Profile* profile,
158 Type type);
160 // Returns a string describing a particular AutocompleteProvider type.
161 static const char* TypeToString(Type type);
163 // Called to start an autocomplete query. The provider is responsible for
164 // tracking its matches for this query and whether it is done processing the
165 // query. When new matches are available or the provider finishes, it
166 // calls the controller's OnProviderUpdate() method. The controller can then
167 // get the new matches using the provider's accessors.
168 // Exception: Matches available immediately after starting the query (that
169 // is, synchronously) do not cause any notifications to be sent. The
170 // controller is expected to check for these without prompting (since
171 // otherwise, starting each provider running would result in a flurry of
172 // notifications).
174 // Once Stop() has been called, no more notifications should be sent.
176 // |minimal_changes| is an optimization that lets the provider do less work
177 // when the |input|'s text hasn't changed. See the body of
178 // OmniboxPopupModel::StartAutocomplete().
179 virtual void Start(const AutocompleteInput& input, bool minimal_changes) = 0;
181 // Called when a provider must not make any more callbacks for the current
182 // query. This will be called regardless of whether the provider is already
183 // done. If the provider caches any results, it should clear the cache based
184 // on the value of |clear_cached_results|.
185 virtual void Stop(bool clear_cached_results);
187 // Returns the enum equivalent to the name of this provider.
188 // TODO(derat): Make metrics use AutocompleteProvider::Type directly, or at
189 // least move this method to the metrics directory.
190 metrics::OmniboxEventProto_ProviderType AsOmniboxEventProviderType() const;
192 // Called to delete a match and the backing data that produced it. This
193 // match should not appear again in this or future queries. This can only be
194 // called for matches the provider marks as deletable. This should only be
195 // called when no query is running.
196 // NOTE: Remember to call OnProviderUpdate() if matches_ is updated.
197 virtual void DeleteMatch(const AutocompleteMatch& match);
199 // Called when an omnibox event log entry is generated. This gives
200 // a provider the opportunity to add diagnostic information to the
201 // logs. A provider is expected to append a single entry of whatever
202 // information it wants to |provider_info|.
203 virtual void AddProviderInfo(ProvidersInfo* provider_info) const;
205 // Called when a new omnibox session starts or the current session ends.
206 // This gives the opportunity to reset the internal state, if any, associated
207 // with the previous session.
208 virtual void ResetSession();
210 // A convenience function to call net::FormatUrl() with the current set of
211 // "Accept Languages" when check_accept_lang is true. Otherwise, it's called
212 // with an empty list.
213 base::string16 StringForURLDisplay(const GURL& url,
214 bool check_accept_lang,
215 bool trim_http) const;
217 // Returns the set of matches for the current query.
218 const ACMatches& matches() const { return matches_; }
220 // Returns whether the provider is done processing the query.
221 bool done() const { return done_; }
223 // Returns this provider's type.
224 Type type() const { return type_; }
226 // Returns a string describing this provider's type.
227 const char* GetName() const;
229 #ifdef UNIT_TEST
230 void set_listener(AutocompleteProviderListener* listener) {
231 listener_ = listener;
233 #endif
234 // A suggested upper bound for how many matches a provider should return.
235 // TODO(pkasting): http://b/1111299 , http://b/933133 This should go away once
236 // we have good relevance heuristics; the controller should handle all
237 // culling.
238 static const size_t kMaxMatches;
240 protected:
241 friend class base::RefCountedThreadSafe<AutocompleteProvider>;
243 virtual ~AutocompleteProvider();
245 // Updates the starred state of each of the matches in matches_ from the
246 // profile's bookmark bar model.
247 void UpdateStarredStateOfMatches();
249 // Fixes up user URL input to make it more possible to match against. Among
250 // many other things, this takes care of the following:
251 // * Prepending file:// to file URLs
252 // * Converting drive letters in file URLs to uppercase
253 // * Converting case-insensitive parts of URLs (like the scheme and domain)
254 // to lowercase
255 // * Convert spaces to %20s
256 // Note that we don't do this in AutocompleteInput's constructor, because if
257 // e.g. we convert a Unicode hostname to punycode, other providers will show
258 // output that surprises the user ("Search Google for xn--6ca.com").
259 // Returns false if the fixup attempt resulted in an empty string (which
260 // providers generally can't do anything with).
261 static bool FixupUserInput(AutocompleteInput* input);
263 // Trims "http:" and up to two subsequent slashes from |url|. Returns the
264 // number of characters that were trimmed.
265 // NOTE: For a view-source: URL, this will trim from after "view-source:" and
266 // return 0.
267 static size_t TrimHttpPrefix(base::string16* url);
269 // The profile associated with the AutocompleteProvider. Reference is not
270 // owned by us.
271 Profile* profile_;
273 AutocompleteProviderListener* listener_;
274 ACMatches matches_;
275 bool done_;
277 Type type_;
279 private:
280 DISALLOW_COPY_AND_ASSIGN(AutocompleteProvider);
283 typedef std::vector<AutocompleteProvider*> ACProviders;
285 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_PROVIDER_H_