Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / predictors / autocomplete_action_predictor.h
blob550237c738379d52837bc6faf48fdbd504a669fb
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_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
8 #include <map>
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "base/strings/string16.h"
16 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
17 #include "components/history/core/browser/history_service_observer.h"
18 #include "components/history/core/browser/history_types.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "url/gurl.h"
24 struct AutocompleteMatch;
25 class AutocompleteResult;
26 struct OmniboxLog;
27 class PredictorsHandler;
28 class Profile;
30 namespace content {
31 class SessionStorageNamespace;
34 namespace gfx {
35 class Size;
38 namespace history {
39 class HistoryService;
40 class URLDatabase;
43 namespace prerender {
44 class PrerenderHandle;
47 namespace predictors {
49 // This class is responsible for determining the correct predictive network
50 // action to take given for a given AutocompleteMatch and entered text. It can
51 // be instantiated for both normal and incognito profiles. For normal profiles,
52 // it uses an AutocompleteActionPredictorTable accessed asynchronously on the DB
53 // thread to permanently store the data used to make predictions, and keeps
54 // local caches of that data to be able to make predictions synchronously on the
55 // UI thread where it lives. For incognito profiles, there is no table; the
56 // local caches are copied from the main profile at creation and from there on
57 // are the only thing used.
59 // This class can be accessed as a weak pointer so that it can safely use
60 // PostTaskAndReply without fear of crashes if it is destroyed before the reply
61 // triggers. This is necessary during initialization.
62 class AutocompleteActionPredictor
63 : public KeyedService,
64 public content::NotificationObserver,
65 public history::HistoryServiceObserver,
66 public base::SupportsWeakPtr<AutocompleteActionPredictor> {
67 public:
68 enum Action {
69 ACTION_PRERENDER = 0,
70 ACTION_PRECONNECT,
71 ACTION_NONE,
72 LAST_PREDICT_ACTION = ACTION_NONE
75 explicit AutocompleteActionPredictor(Profile* profile);
76 ~AutocompleteActionPredictor() override;
78 // Registers an AutocompleteResult for a given |user_text|. This will be used
79 // when the user navigates from the Omnibox to determine early opportunities
80 // to predict their actions.
81 void RegisterTransitionalMatches(const base::string16& user_text,
82 const AutocompleteResult& result);
84 // Clears any transitional matches that have been registered. Called when, for
85 // example, the OmniboxEditModel is reverted.
86 void ClearTransitionalMatches();
88 // Return the recommended action given |user_text|, the text the user has
89 // entered in the Omnibox, and |match|, the suggestion from Autocomplete.
90 // This method uses information from the ShortcutsBackend including how much
91 // of the matching entry the user typed, and how long it's been since the user
92 // visited the matching URL, to calculate a score between 0 and 1. This score
93 // is then mapped to an Action.
94 Action RecommendAction(const base::string16& user_text,
95 const AutocompleteMatch& match) const;
97 // Begin prerendering |url| with |session_storage_namespace|. The |size| gives
98 // the initial size for the target prerender. The predictor will run at most
99 // one prerender at a time, so launching a prerender will cancel our previous
100 // prerenders (if any).
101 void StartPrerendering(
102 const GURL& url,
103 content::SessionStorageNamespace* session_storage_namespace,
104 const gfx::Size& size);
106 // Cancels the current prerender, unless it has already been abandoned.
107 void CancelPrerender();
109 // Return true if the suggestion type warrants a TCP/IP preconnection.
110 // i.e., it is now quite likely that the user will select the related domain.
111 static bool IsPreconnectable(const AutocompleteMatch& match);
113 // Returns true if there is an active Omnibox prerender and it has been
114 // abandoned.
115 bool IsPrerenderAbandonedForTesting();
117 // Should be called when a URL is opened from the omnibox.
118 void OnOmniboxOpenedUrl(const OmniboxLog& log);
120 private:
121 friend class AutocompleteActionPredictorTest;
122 friend class ::PredictorsHandler;
124 struct TransitionalMatch {
125 TransitionalMatch();
126 ~TransitionalMatch();
128 base::string16 user_text;
129 std::vector<GURL> urls;
131 bool operator==(const base::string16& other_user_text) const {
132 return user_text == other_user_text;
136 struct DBCacheKey {
137 base::string16 user_text;
138 GURL url;
140 bool operator<(const DBCacheKey& rhs) const {
141 return (user_text != rhs.user_text) ?
142 (user_text < rhs.user_text) : (url < rhs.url);
145 bool operator==(const DBCacheKey& rhs) const {
146 return (user_text == rhs.user_text) && (url == rhs.url);
150 struct DBCacheValue {
151 int number_of_hits;
152 int number_of_misses;
155 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap;
156 typedef std::map<DBCacheKey, AutocompleteActionPredictorTable::Row::Id>
157 DBIdCacheMap;
159 static const int kMaximumDaysToKeepEntry;
161 // NotificationObserver
162 void Observe(int type,
163 const content::NotificationSource& source,
164 const content::NotificationDetails& details) override;
166 // The first step in initializing the predictor is accessing the database and
167 // building the local cache. This should be delayed until after critical DB
168 // and IO processes have completed.
169 void CreateLocalCachesFromDatabase();
171 // Removes all rows from the database and caches.
172 void DeleteAllRows();
174 // Removes rows from the database and caches that contain a URL in |rows|.
175 void DeleteRowsWithURLs(const history::URLRows& rows);
177 // Adds and updates rows in the database and caches.
178 void AddAndUpdateRows(
179 const AutocompleteActionPredictorTable::Rows& rows_to_add,
180 const AutocompleteActionPredictorTable::Rows& rows_to_update);
182 // Called to populate the local caches. This also calls DeleteOldEntries
183 // if the history service is available, or registers for the notification of
184 // it becoming available.
185 void CreateCaches(
186 std::vector<AutocompleteActionPredictorTable::Row>* row_buffer);
188 // Attempts to call DeleteOldEntries if the in-memory database has been loaded
189 // by |service|. Returns success as a boolean.
190 bool TryDeleteOldEntries(history::HistoryService* service);
192 // Called to delete any old or invalid entries from the database. Called after
193 // the local caches are created once the history service is available.
194 void DeleteOldEntries(history::URLDatabase* url_db);
196 // Deletes any old or invalid entries from the local caches. |url_db| and
197 // |id_list| must not be NULL. Every row id deleted will be added to id_list.
198 void DeleteOldIdsFromCaches(
199 history::URLDatabase* url_db,
200 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list);
202 // Called on an incognito-owned predictor to copy the current caches from the
203 // main profile.
204 void CopyFromMainProfile();
206 // Registers for notifications and sets the |initialized_| flag.
207 void FinishInitialization();
209 // Uses local caches to calculate an exact percentage prediction that the user
210 // will take a particular match given what they have typed. |is_in_db| is set
211 // to differentiate trivial zero results resulting from a match not being
212 // found from actual zero results where the calculation returns 0.0.
213 double CalculateConfidence(const base::string16& user_text,
214 const AutocompleteMatch& match,
215 bool* is_in_db) const;
217 // Calculates the confidence for an entry in the DBCacheMap.
218 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const;
220 // KeyedService:
221 void Shutdown() override;
223 // history::HistoryServiceObserver:
224 void OnURLsDeleted(history::HistoryService* history_service,
225 bool all_history,
226 bool expired,
227 const history::URLRows& deleted_rows,
228 const std::set<GURL>& favicon_urls) override;
229 void OnHistoryServiceLoaded(
230 history::HistoryService* history_service) override;
232 Profile* profile_;
234 // Set when this is a predictor for an incognito profile.
235 AutocompleteActionPredictor* main_profile_predictor_;
237 // Set when this is a predictor for a non-incognito profile, and the incognito
238 // profile creates a predictor. If this is non-NULL when we finish
239 // initialization, we should call CopyFromMainProfile() on it.
240 AutocompleteActionPredictor* incognito_predictor_;
242 // The backing data store. This is NULL for incognito-owned predictors.
243 scoped_refptr<AutocompleteActionPredictorTable> table_;
245 content::NotificationRegistrar notification_registrar_;
247 // This is cleared after every Omnibox navigation.
248 std::vector<TransitionalMatch> transitional_matches_;
250 scoped_ptr<prerender::PrerenderHandle> prerender_handle_;
252 // This allows us to predict the effect of confidence threshold changes on
253 // accuracy. This is cleared after every omnibox navigation.
254 mutable std::vector<std::pair<GURL, double> > tracked_urls_;
256 // Local caches of the data store. For incognito-owned predictors this is the
257 // only copy of the data.
258 DBCacheMap db_cache_;
259 DBIdCacheMap db_id_cache_;
261 bool initialized_;
263 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
264 history_service_observer_;
266 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor);
269 } // namespace predictors
271 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_