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/browser_context_keyed_service/browser_context_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 BrowserContextKeyedService
,
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 // Return true if the suggestion type warrants a TCP/IP preconnection.
105 // i.e., it is now quite likely that the user will select the related domain.
106 static bool IsPreconnectable(const AutocompleteMatch
& match
);
109 friend class AutocompleteActionPredictorTest
;
110 friend class ::PredictorsHandler
;
112 struct TransitionalMatch
{
114 ~TransitionalMatch();
116 base::string16 user_text
;
117 std::vector
<GURL
> urls
;
119 bool operator==(const base::string16
& other_user_text
) const {
120 return user_text
== other_user_text
;
125 base::string16 user_text
;
128 bool operator<(const DBCacheKey
& rhs
) const {
129 return (user_text
!= rhs
.user_text
) ?
130 (user_text
< rhs
.user_text
) : (url
< rhs
.url
);
133 bool operator==(const DBCacheKey
& rhs
) const {
134 return (user_text
== rhs
.user_text
) && (url
== rhs
.url
);
138 struct DBCacheValue
{
140 int number_of_misses
;
143 typedef std::map
<DBCacheKey
, DBCacheValue
> DBCacheMap
;
144 typedef std::map
<DBCacheKey
, AutocompleteActionPredictorTable::Row::Id
>
147 static const int kMaximumDaysToKeepEntry
;
149 // NotificationObserver
150 virtual void Observe(int type
,
151 const content::NotificationSource
& source
,
152 const content::NotificationDetails
& details
) OVERRIDE
;
154 // The first step in initializing the predictor is accessing the database and
155 // building the local cache. This should be delayed until after critical DB
156 // and IO processes have completed.
157 void CreateLocalCachesFromDatabase();
159 // Removes all rows from the database and caches.
160 void DeleteAllRows();
162 // Removes rows from the database and caches that contain a URL in |rows|.
163 void DeleteRowsWithURLs(const history::URLRows
& rows
);
165 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
166 void OnOmniboxOpenedUrl(const OmniboxLog
& log
);
168 // Adds and updates rows in the database and caches.
169 void AddAndUpdateRows(
170 const AutocompleteActionPredictorTable::Rows
& rows_to_add
,
171 const AutocompleteActionPredictorTable::Rows
& rows_to_update
);
173 // Called to populate the local caches. This also calls DeleteOldEntries
174 // if the history service is available, or registers for the notification of
175 // it becoming available.
177 std::vector
<AutocompleteActionPredictorTable::Row
>* row_buffer
);
179 // Attempts to call DeleteOldEntries if the in-memory database has been loaded
180 // by |service|. Returns success as a boolean.
181 bool TryDeleteOldEntries(HistoryService
* service
);
183 // Called to delete any old or invalid entries from the database. Called after
184 // the local caches are created once the history service is available.
185 void DeleteOldEntries(history::URLDatabase
* url_db
);
187 // Deletes any old or invalid entries from the local caches. |url_db| and
188 // |id_list| must not be NULL. Every row id deleted will be added to id_list.
189 void DeleteOldIdsFromCaches(
190 history::URLDatabase
* url_db
,
191 std::vector
<AutocompleteActionPredictorTable::Row::Id
>* id_list
);
193 // Called on an incognito-owned predictor to copy the current caches from the
195 void CopyFromMainProfile();
197 // Registers for notifications and sets the |initialized_| flag.
198 void FinishInitialization();
200 // Uses local caches to calculate an exact percentage prediction that the user
201 // will take a particular match given what they have typed. |is_in_db| is set
202 // to differentiate trivial zero results resulting from a match not being
203 // found from actual zero results where the calculation returns 0.0.
204 double CalculateConfidence(const base::string16
& user_text
,
205 const AutocompleteMatch
& match
,
206 bool* is_in_db
) const;
208 // Calculates the confidence for an entry in the DBCacheMap.
209 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter
) const;
213 // Set when this is a predictor for an incognito profile.
214 AutocompleteActionPredictor
* main_profile_predictor_
;
216 // Set when this is a predictor for a non-incognito profile, and the incognito
217 // profile creates a predictor. If this is non-NULL when we finish
218 // initialization, we should call CopyFromMainProfile() on it.
219 AutocompleteActionPredictor
* incognito_predictor_
;
221 // The backing data store. This is NULL for incognito-owned predictors.
222 scoped_refptr
<AutocompleteActionPredictorTable
> table_
;
224 content::NotificationRegistrar notification_registrar_
;
226 // This is cleared after every Omnibox navigation.
227 std::vector
<TransitionalMatch
> transitional_matches_
;
229 scoped_ptr
<prerender::PrerenderHandle
> prerender_handle_
;
231 // This allows us to predict the effect of confidence threshold changes on
232 // accuracy. This is cleared after every omnibox navigation.
233 mutable std::vector
<std::pair
<GURL
, double> > tracked_urls_
;
235 // Local caches of the data store. For incognito-owned predictors this is the
236 // only copy of the data.
237 DBCacheMap db_cache_
;
238 DBIdCacheMap db_id_cache_
;
242 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor
);
245 } // namespace predictors
247 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_