Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / predictors / autocomplete_action_predictor.h
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.
4
5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
7
8 #include <map>
9
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"
21 #include "url/gurl.h"
22
23 struct AutocompleteMatch;
24 class AutocompleteResult;
25 class HistoryService;
26 struct OmniboxLog;
27 class PredictorsHandler;
28 class Profile;
29
30 namespace content {
31 class SessionStorageNamespace;
32 }
33
34 namespace gfx {
35 class Size;
36 }
37
38 namespace history {
39 class URLDatabase;
40 }
41
42 namespace prerender {
43 class PrerenderHandle;
44 }
45
46 namespace predictors {
47
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.
57 //
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> {
65  public:
66   enum Action {
67     ACTION_PRERENDER = 0,
68     ACTION_PRECONNECT,
69     ACTION_NONE,
70     LAST_PREDICT_ACTION = ACTION_NONE
71   };
72
73   explicit AutocompleteActionPredictor(Profile* profile);
74   virtual ~AutocompleteActionPredictor();
75
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);
81
82   // Clears any transitional matches that have been registered. Called when, for
83   // example, the OmniboxEditModel is reverted.
84   void ClearTransitionalMatches();
85
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;
94
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(
100       const GURL& url,
101       const content::SessionStorageNamespaceMap& session_storage_namespace_map,
102       const gfx::Size& size);
103
104   // Cancels the current prerender, unless it has already been abandoned.
105   void CancelPrerender();
106
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);
110
111   // Returns true if there is an active Omnibox prerender and it has been
112   // abandoned.
113   bool IsPrerenderAbandonedForTesting();
114
115  private:
116   friend class AutocompleteActionPredictorTest;
117   friend class ::PredictorsHandler;
118
119   struct TransitionalMatch {
120     TransitionalMatch();
121     ~TransitionalMatch();
122
123     base::string16 user_text;
124     std::vector<GURL> urls;
125
126     bool operator==(const base::string16& other_user_text) const {
127       return user_text == other_user_text;
128     }
129   };
130
131   struct DBCacheKey {
132     base::string16 user_text;
133     GURL url;
134
135     bool operator<(const DBCacheKey& rhs) const {
136       return (user_text != rhs.user_text) ?
137           (user_text < rhs.user_text) :  (url < rhs.url);
138     }
139
140     bool operator==(const DBCacheKey& rhs) const {
141       return (user_text == rhs.user_text) && (url == rhs.url);
142     }
143   };
144
145   struct DBCacheValue {
146     int number_of_hits;
147     int number_of_misses;
148   };
149
150   typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap;
151   typedef std::map<DBCacheKey, AutocompleteActionPredictorTable::Row::Id>
152       DBIdCacheMap;
153
154   static const int kMaximumDaysToKeepEntry;
155
156   // NotificationObserver
157   virtual void Observe(int type,
158                        const content::NotificationSource& source,
159                        const content::NotificationDetails& details) OVERRIDE;
160
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();
165
166   // Removes all rows from the database and caches.
167   void DeleteAllRows();
168
169   // Removes rows from the database and caches that contain a URL in |rows|.
170   void DeleteRowsWithURLs(const history::URLRows& rows);
171
172   // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
173   void OnOmniboxOpenedUrl(const OmniboxLog& log);
174
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);
179
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.
183   void CreateCaches(
184       std::vector<AutocompleteActionPredictorTable::Row>* row_buffer);
185
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);
189
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);
193
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);
199
200   // Called on an incognito-owned predictor to copy the current caches from the
201   // main profile.
202   void CopyFromMainProfile();
203
204   // Registers for notifications and sets the |initialized_| flag.
205   void FinishInitialization();
206
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;
214
215   // Calculates the confidence for an entry in the DBCacheMap.
216   double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const;
217
218   Profile* profile_;
219
220   // Set when this is a predictor for an incognito profile.
221   AutocompleteActionPredictor* main_profile_predictor_;
222
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_;
227
228   // The backing data store.  This is NULL for incognito-owned predictors.
229   scoped_refptr<AutocompleteActionPredictorTable> table_;
230
231   content::NotificationRegistrar notification_registrar_;
232
233   // This is cleared after every Omnibox navigation.
234   std::vector<TransitionalMatch> transitional_matches_;
235
236   scoped_ptr<prerender::PrerenderHandle> prerender_handle_;
237
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_;
241
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_;
246
247   bool initialized_;
248
249   DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor);
250 };
251
252 }  // namespace predictors
253
254 #endif  // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_