Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1202)

Side by Side Diff: chrome/browser/predictors/autocomplete_action_predictor.h

Issue 10380041: Refactoring AutocompleteActionPredictor Database. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ 5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ 6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 10
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/string16.h" 14 #include "base/string16.h"
15 #include "chrome/browser/history/history_types.h" 15 #include "chrome/browser/history/history_types.h"
16 #include "chrome/browser/predictors/autocomplete_action_predictor_database.h" 16 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
17 #include "chrome/browser/profiles/profile_keyed_service.h" 17 #include "chrome/browser/profiles/profile_keyed_service.h"
18 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
20 #include "googleurl/src/gurl.h" 20 #include "googleurl/src/gurl.h"
21 21
22 struct AutocompleteLog; 22 struct AutocompleteLog;
23 struct AutocompleteMatch; 23 struct AutocompleteMatch;
24 class AutocompleteResult; 24 class AutocompleteResult;
25 class HistoryService; 25 class HistoryService;
26 class PredictorsDOMHandler;
26 class Profile; 27 class Profile;
27 28
28 namespace history { 29 namespace history {
29 class URLDatabase; 30 class URLDatabase;
30 } 31 }
31 32
33 namespace predictors {
34
32 // This class is responsible for determining the correct predictive network 35 // This class is responsible for determining the correct predictive network
33 // action to take given for a given AutocompleteMatch and entered text. it uses 36 // action to take given for a given AutocompleteMatch and entered text. it uses
34 // an AutocompleteActionPredictorDatabase accessed asynchronously on the DB 37 // a AutocompleteActionPredictorTable accessed asynchronously on the DB thread
35 // thread to permanently store the data used to make predictions, and keeps 38 // to permanently store the data used to make predictions, and keeps local
36 // local caches of that data to be able to make predictions synchronously on the 39 // caches of that data to be able to make predictions synchronously on the UI
37 // UI thread where it lives. It can be accessed as a weak pointer so that it can 40 // thread where it lives. It can be accessed as a weak pointer so that it can
38 // safely use PostTaskAndReply without fear of crashes if it is destroyed before 41 // safely use PostTaskAndReply without fear of crashes if it is destroyed before
39 // the reply triggers. This is necessary during initialization. 42 // the reply triggers. This is necessary during initialization.
40 class AutocompleteActionPredictor 43 class AutocompleteActionPredictor
41 : public ProfileKeyedService, 44 : public ProfileKeyedService,
42 public content::NotificationObserver, 45 public content::NotificationObserver,
43 public base::SupportsWeakPtr<AutocompleteActionPredictor> { 46 public base::SupportsWeakPtr<AutocompleteActionPredictor> {
44 public: 47 public:
45 enum Action { 48 enum Action {
46 ACTION_PRERENDER = 0, 49 ACTION_PRERENDER = 0,
47 ACTION_PRECONNECT, 50 ACTION_PRECONNECT,
(...skipping 25 matching lines...) Expand all
73 // is then mapped to an Action. 76 // is then mapped to an Action.
74 Action RecommendAction(const string16& user_text, 77 Action RecommendAction(const string16& user_text,
75 const AutocompleteMatch& match) const; 78 const AutocompleteMatch& match) const;
76 79
77 // Return true if the suggestion type warrants a TCP/IP preconnection. 80 // Return true if the suggestion type warrants a TCP/IP preconnection.
78 // i.e., it is now quite likely that the user will select the related domain. 81 // i.e., it is now quite likely that the user will select the related domain.
79 static bool IsPreconnectable(const AutocompleteMatch& match); 82 static bool IsPreconnectable(const AutocompleteMatch& match);
80 83
81 private: 84 private:
82 friend class AutocompleteActionPredictorTest; 85 friend class AutocompleteActionPredictorTest;
83 friend class AutocompleteActionPredictorDOMHandler; 86 friend class ::PredictorsDOMHandler;
84 87
85 struct TransitionalMatch { 88 struct TransitionalMatch {
86 TransitionalMatch(); 89 TransitionalMatch();
87 ~TransitionalMatch(); 90 ~TransitionalMatch();
88 91
89 string16 user_text; 92 string16 user_text;
90 std::vector<GURL> urls; 93 std::vector<GURL> urls;
91 94
92 bool operator==(const string16& other_user_text) const { 95 bool operator==(const string16& other_user_text) const {
93 return user_text == other_user_text; 96 return user_text == other_user_text;
(...skipping 13 matching lines...) Expand all
107 return (user_text == rhs.user_text) && (url == rhs.url); 110 return (user_text == rhs.user_text) && (url == rhs.url);
108 } 111 }
109 }; 112 };
110 113
111 struct DBCacheValue { 114 struct DBCacheValue {
112 int number_of_hits; 115 int number_of_hits;
113 int number_of_misses; 116 int number_of_misses;
114 }; 117 };
115 118
116 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap; 119 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap;
117 typedef std::map<DBCacheKey, AutocompleteActionPredictorDatabase::Row::Id> 120 typedef std::map<DBCacheKey, AutocompleteActionPredictorTable::Row::Id>
118 DBIdCacheMap; 121 DBIdCacheMap;
119 122
120 static const int kMaximumDaysToKeepEntry; 123 static const int kMaximumDaysToKeepEntry;
121 124
122 // Multiplying factor applied to the |number_of_hits| for a database entry 125 // Multiplying factor applied to the |number_of_hits| for a database entry
123 // when calculating the confidence. It is currently set by a field trial so is 126 // when calculating the confidence. It is currently set by a field trial so is
124 // static. Once the field trial ends, this will be a constant value. 127 // static. Once the field trial ends, this will be a constant value.
125 static double hit_weight_; 128 static double hit_weight_;
126 129
127 // ProfileKeyedService 130 // ProfileKeyedService
128 virtual void Shutdown() OVERRIDE; 131 virtual void Shutdown() OVERRIDE;
129 132
130 // NotificationObserver 133 // NotificationObserver
131 virtual void Observe(int type, 134 virtual void Observe(int type,
132 const content::NotificationSource& source, 135 const content::NotificationSource& source,
133 const content::NotificationDetails& details) OVERRIDE; 136 const content::NotificationDetails& details) OVERRIDE;
134 137
135 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. 138 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
136 void OnOmniboxOpenedUrl(const AutocompleteLog& log); 139 void OnOmniboxOpenedUrl(const AutocompleteLog& log);
137 140
138 // Deletes any old or invalid entries from the local caches. |url_db| and 141 // Deletes any old or invalid entries from the local caches. |url_db| and
139 // |id_list| must not be NULL. Every row id deleted will be added to id_list. 142 // |id_list| must not be NULL. Every row id deleted will be added to id_list.
140 void DeleteOldIdsFromCaches( 143 void DeleteOldIdsFromCaches(
141 history::URLDatabase* url_db, 144 history::URLDatabase* url_db,
142 std::vector<AutocompleteActionPredictorDatabase::Row::Id>* id_list); 145 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list);
143 146
144 // Called to delete any old or invalid entries from the database. Called after 147 // Called to delete any old or invalid entries from the database. Called after
145 // the local caches are created once the history service is available. 148 // the local caches are created once the history service is available.
146 void DeleteOldEntries(history::URLDatabase* url_db); 149 void DeleteOldEntries(history::URLDatabase* url_db);
147 150
148 // Called to populate the local caches. This also calls DeleteOldEntries 151 // Called to populate the local caches. This also calls DeleteOldEntries
149 // if the history service is available, or registers for the notification of 152 // if the history service is available, or registers for the notification of
150 // it becoming available. 153 // it becoming available.
151 void CreateCaches( 154 void CreateCaches(
152 std::vector<AutocompleteActionPredictorDatabase::Row>* row_buffer); 155 std::vector<AutocompleteActionPredictorTable::Row>* row_buffer);
153 156
154 // Attempts to call DeleteOldEntries if the in-memory database has been loaded 157 // Attempts to call DeleteOldEntries if the in-memory database has been loaded
155 // by |service|. Returns success as a boolean. 158 // by |service|. Returns success as a boolean.
156 bool TryDeleteOldEntries(HistoryService* service); 159 bool TryDeleteOldEntries(HistoryService* service);
157 160
158 // Uses local caches to calculate an exact percentage prediction that the user 161 // Uses local caches to calculate an exact percentage prediction that the user
159 // will take a particular match given what they have typed. |is_in_db| is set 162 // will take a particular match given what they have typed. |is_in_db| is set
160 // to differentiate trivial zero results resulting from a match not being 163 // to differentiate trivial zero results resulting from a match not being
161 // found from actual zero results where the calculation returns 0.0. 164 // found from actual zero results where the calculation returns 0.0.
162 double CalculateConfidence(const string16& user_text, 165 double CalculateConfidence(const string16& user_text,
163 const AutocompleteMatch& match, 166 const AutocompleteMatch& match,
164 bool* is_in_db) const; 167 bool* is_in_db) const;
165 168
166 // Calculates the confidence for an entry in the DBCacheMap. 169 // Calculates the confidence for an entry in the DBCacheMap.
167 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const; 170 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const;
168 171
169 // Adds a row to the database and caches. 172 // Adds and updates rows in the database and caches.
170 void AddRow(const DBCacheKey& key, 173 void AddAndUpdateRows(
171 const AutocompleteActionPredictorDatabase::Row& row); 174 const AutocompleteActionPredictorTable::Rows& rows_to_add,
172 175 const AutocompleteActionPredictorTable::Rows& rows_to_update);
173 // Updates a row in the database and the caches.
174 void UpdateRow(DBCacheMap::iterator it,
175 const AutocompleteActionPredictorDatabase::Row& row);
176 176
177 // Removes all rows from the database and caches. 177 // Removes all rows from the database and caches.
178 void DeleteAllRows(); 178 void DeleteAllRows();
179 179
180 // Removes rows from the database and caches that contain a URL in |rows|. 180 // Removes rows from the database and caches that contain a URL in |rows|.
181 void DeleteRowsWithURLs(const history::URLRows& rows); 181 void DeleteRowsWithURLs(const history::URLRows& rows);
182 182
183 // Used to batch operations on the database.
184 void BeginTransaction();
185 void CommitTransaction();
186
187 Profile* profile_; 183 Profile* profile_;
188 scoped_refptr<AutocompleteActionPredictorDatabase> db_; 184 scoped_refptr<AutocompleteActionPredictorTable> db_;
dominich 2012/05/08 20:35:28 db_ -> table_
Shishir 2012/05/08 21:49:03 Done.
189 content::NotificationRegistrar notification_registrar_; 185 content::NotificationRegistrar notification_registrar_;
190 186
191 // This is cleared after every Omnibox navigation. 187 // This is cleared after every Omnibox navigation.
192 std::vector<TransitionalMatch> transitional_matches_; 188 std::vector<TransitionalMatch> transitional_matches_;
193 189
194 // This allows us to predict the effect of confidence threshold changes on 190 // This allows us to predict the effect of confidence threshold changes on
195 // accuracy. 191 // accuracy.
196 mutable std::vector<std::pair<GURL, double> > tracked_urls_; 192 mutable std::vector<std::pair<GURL, double> > tracked_urls_;
197 193
198 DBCacheMap db_cache_; 194 DBCacheMap db_cache_;
199 DBIdCacheMap db_id_cache_; 195 DBIdCacheMap db_id_cache_;
200 196
201 bool initialized_; 197 bool initialized_;
202 198
203 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor); 199 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor);
204 }; 200 };
205 201
202 } // namespace predictors
203
206 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ 204 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698