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_
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/strings/string16.h"
15 #include "chrome/browser/history/history_types.h"
16 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
23 struct AutocompleteMatch
;
24 class AutocompleteResult
;
27 class PredictorsHandler
;
31 class SessionStorageNamespace
;
43 class PrerenderHandle
;
46 namespace predictors
{
48 // This class is responsible for determining the correct predictive network
49 // action to take given for a given AutocompleteMatch and entered text. It can
50 // be instantiated for both normal and incognito profiles. For normal profiles,
51 // it uses an AutocompleteActionPredictorTable accessed asynchronously on the DB
52 // thread to permanently store the data used to make predictions, and keeps
53 // local caches of that data to be able to make predictions synchronously on the
54 // UI thread where it lives. For incognito profiles, there is no table; the
55 // local caches are copied from the main profile at creation and from there on
56 // are the only thing used.
58 // This class can be accessed as a weak pointer so that it can safely use
59 // PostTaskAndReply without fear of crashes if it is destroyed before the reply
60 // triggers. This is necessary during initialization.
61 class AutocompleteActionPredictor
62 : public KeyedService
,
63 public content::NotificationObserver
,
64 public base::SupportsWeakPtr
<AutocompleteActionPredictor
> {
70 LAST_PREDICT_ACTION
= ACTION_NONE
73 explicit AutocompleteActionPredictor(Profile
* profile
);
74 virtual ~AutocompleteActionPredictor();
76 // Registers an AutocompleteResult for a given |user_text|. This will be used
77 // when the user navigates from the Omnibox to determine early opportunities
78 // to predict their actions.
79 void RegisterTransitionalMatches(const base::string16
& user_text
,
80 const AutocompleteResult
& result
);
82 // Clears any transitional matches that have been registered. Called when, for
83 // example, the OmniboxEditModel is reverted.
84 void ClearTransitionalMatches();
86 // Return the recommended action given |user_text|, the text the user has
87 // entered in the Omnibox, and |match|, the suggestion from Autocomplete.
88 // This method uses information from the ShortcutsBackend including how much
89 // of the matching entry the user typed, and how long it's been since the user
90 // visited the matching URL, to calculate a score between 0 and 1. This score
91 // is then mapped to an Action.
92 Action
RecommendAction(const base::string16
& user_text
,
93 const AutocompleteMatch
& match
) const;
95 // Begin prerendering |url| with |session_storage_namespace|. The |size| gives
96 // the initial size for the target prerender. The predictor will run at most
97 // one prerender at a time, so launching a prerender will cancel our previous
98 // prerenders (if any).
99 void StartPrerendering(
101 const content::SessionStorageNamespaceMap
& session_storage_namespace_map
,
102 const gfx::Size
& size
);
104 // Cancels the current prerender, unless it has already been abandoned.
105 void CancelPrerender();
107 // Return true if the suggestion type warrants a TCP/IP preconnection.
108 // i.e., it is now quite likely that the user will select the related domain.
109 static bool IsPreconnectable(const AutocompleteMatch
& match
);
111 // Returns true if there is an active Omnibox prerender and it has been
113 bool IsPrerenderAbandonedForTesting();
116 friend class AutocompleteActionPredictorTest
;
117 friend class ::PredictorsHandler
;
119 struct TransitionalMatch
{
121 ~TransitionalMatch();
123 base::string16 user_text
;
124 std::vector
<GURL
> urls
;
126 bool operator==(const base::string16
& other_user_text
) const {
127 return user_text
== other_user_text
;
132 base::string16 user_text
;
135 bool operator<(const DBCacheKey
& rhs
) const {
136 return (user_text
!= rhs
.user_text
) ?
137 (user_text
< rhs
.user_text
) : (url
< rhs
.url
);
140 bool operator==(const DBCacheKey
& rhs
) const {
141 return (user_text
== rhs
.user_text
) && (url
== rhs
.url
);
145 struct DBCacheValue
{
147 int number_of_misses
;
150 typedef std::map
<DBCacheKey
, DBCacheValue
> DBCacheMap
;
151 typedef std::map
<DBCacheKey
, AutocompleteActionPredictorTable::Row::Id
>
154 static const int kMaximumDaysToKeepEntry
;
156 // NotificationObserver
157 virtual void Observe(int type
,
158 const content::NotificationSource
& source
,
159 const content::NotificationDetails
& details
) OVERRIDE
;
161 // The first step in initializing the predictor is accessing the database and
162 // building the local cache. This should be delayed until after critical DB
163 // and IO processes have completed.
164 void CreateLocalCachesFromDatabase();
166 // Removes all rows from the database and caches.
167 void DeleteAllRows();
169 // Removes rows from the database and caches that contain a URL in |rows|.
170 void DeleteRowsWithURLs(const history::URLRows
& rows
);
172 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
173 void OnOmniboxOpenedUrl(const OmniboxLog
& log
);
175 // Adds and updates rows in the database and caches.
176 void AddAndUpdateRows(
177 const AutocompleteActionPredictorTable::Rows
& rows_to_add
,
178 const AutocompleteActionPredictorTable::Rows
& rows_to_update
);
180 // Called to populate the local caches. This also calls DeleteOldEntries
181 // if the history service is available, or registers for the notification of
182 // it becoming available.
184 std::vector
<AutocompleteActionPredictorTable::Row
>* row_buffer
);
186 // Attempts to call DeleteOldEntries if the in-memory database has been loaded
187 // by |service|. Returns success as a boolean.
188 bool TryDeleteOldEntries(HistoryService
* service
);
190 // Called to delete any old or invalid entries from the database. Called after
191 // the local caches are created once the history service is available.
192 void DeleteOldEntries(history::URLDatabase
* url_db
);
194 // Deletes any old or invalid entries from the local caches. |url_db| and
195 // |id_list| must not be NULL. Every row id deleted will be added to id_list.
196 void DeleteOldIdsFromCaches(
197 history::URLDatabase
* url_db
,
198 std::vector
<AutocompleteActionPredictorTable::Row::Id
>* id_list
);
200 // Called on an incognito-owned predictor to copy the current caches from the
202 void CopyFromMainProfile();
204 // Registers for notifications and sets the |initialized_| flag.
205 void FinishInitialization();
207 // Uses local caches to calculate an exact percentage prediction that the user
208 // will take a particular match given what they have typed. |is_in_db| is set
209 // to differentiate trivial zero results resulting from a match not being
210 // found from actual zero results where the calculation returns 0.0.
211 double CalculateConfidence(const base::string16
& user_text
,
212 const AutocompleteMatch
& match
,
213 bool* is_in_db
) const;
215 // Calculates the confidence for an entry in the DBCacheMap.
216 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter
) const;
220 // Set when this is a predictor for an incognito profile.
221 AutocompleteActionPredictor
* main_profile_predictor_
;
223 // Set when this is a predictor for a non-incognito profile, and the incognito
224 // profile creates a predictor. If this is non-NULL when we finish
225 // initialization, we should call CopyFromMainProfile() on it.
226 AutocompleteActionPredictor
* incognito_predictor_
;
228 // The backing data store. This is NULL for incognito-owned predictors.
229 scoped_refptr
<AutocompleteActionPredictorTable
> table_
;
231 content::NotificationRegistrar notification_registrar_
;
233 // This is cleared after every Omnibox navigation.
234 std::vector
<TransitionalMatch
> transitional_matches_
;
236 scoped_ptr
<prerender::PrerenderHandle
> prerender_handle_
;
238 // This allows us to predict the effect of confidence threshold changes on
239 // accuracy. This is cleared after every omnibox navigation.
240 mutable std::vector
<std::pair
<GURL
, double> > tracked_urls_
;
242 // Local caches of the data store. For incognito-owned predictors this is the
243 // only copy of the data.
244 DBCacheMap db_cache_
;
245 DBIdCacheMap db_id_cache_
;
249 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor
);
252 } // namespace predictors
254 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_