OLD | NEW |
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 Loading... |
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 Loading... |
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 | |
128 virtual void Shutdown() OVERRIDE; | |
129 | |
130 // NotificationObserver | 130 // NotificationObserver |
131 virtual void Observe(int type, | 131 virtual void Observe(int type, |
132 const content::NotificationSource& source, | 132 const content::NotificationSource& source, |
133 const content::NotificationDetails& details) OVERRIDE; | 133 const content::NotificationDetails& details) OVERRIDE; |
134 | 134 |
135 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. | 135 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. |
136 void OnOmniboxOpenedUrl(const AutocompleteLog& log); | 136 void OnOmniboxOpenedUrl(const AutocompleteLog& log); |
137 | 137 |
138 // Deletes any old or invalid entries from the local caches. |url_db| and | 138 // 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. | 139 // |id_list| must not be NULL. Every row id deleted will be added to id_list. |
140 void DeleteOldIdsFromCaches( | 140 void DeleteOldIdsFromCaches( |
141 history::URLDatabase* url_db, | 141 history::URLDatabase* url_db, |
142 std::vector<AutocompleteActionPredictorDatabase::Row::Id>* id_list); | 142 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list); |
143 | 143 |
144 // Called to delete any old or invalid entries from the database. Called after | 144 // 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. | 145 // the local caches are created once the history service is available. |
146 void DeleteOldEntries(history::URLDatabase* url_db); | 146 void DeleteOldEntries(history::URLDatabase* url_db); |
147 | 147 |
148 // Called to populate the local caches. This also calls DeleteOldEntries | 148 // Called to populate the local caches. This also calls DeleteOldEntries |
149 // if the history service is available, or registers for the notification of | 149 // if the history service is available, or registers for the notification of |
150 // it becoming available. | 150 // it becoming available. |
151 void CreateCaches( | 151 void CreateCaches( |
152 std::vector<AutocompleteActionPredictorDatabase::Row>* row_buffer); | 152 std::vector<AutocompleteActionPredictorTable::Row>* row_buffer); |
153 | 153 |
154 // Attempts to call DeleteOldEntries if the in-memory database has been loaded | 154 // Attempts to call DeleteOldEntries if the in-memory database has been loaded |
155 // by |service|. Returns success as a boolean. | 155 // by |service|. Returns success as a boolean. |
156 bool TryDeleteOldEntries(HistoryService* service); | 156 bool TryDeleteOldEntries(HistoryService* service); |
157 | 157 |
158 // Uses local caches to calculate an exact percentage prediction that the user | 158 // 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 | 159 // 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 | 160 // to differentiate trivial zero results resulting from a match not being |
161 // found from actual zero results where the calculation returns 0.0. | 161 // found from actual zero results where the calculation returns 0.0. |
162 double CalculateConfidence(const string16& user_text, | 162 double CalculateConfidence(const string16& user_text, |
163 const AutocompleteMatch& match, | 163 const AutocompleteMatch& match, |
164 bool* is_in_db) const; | 164 bool* is_in_db) const; |
165 | 165 |
166 // Calculates the confidence for an entry in the DBCacheMap. | 166 // Calculates the confidence for an entry in the DBCacheMap. |
167 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const; | 167 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const; |
168 | 168 |
169 // Adds a row to the database and caches. | 169 // Adds and updates rows in the database and caches. |
170 void AddRow(const DBCacheKey& key, | 170 void AddAndUpdateRows( |
171 const AutocompleteActionPredictorDatabase::Row& row); | 171 const AutocompleteActionPredictorTable::Rows& rows_to_add, |
172 | 172 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 | 173 |
177 // Removes all rows from the database and caches. | 174 // Removes all rows from the database and caches. |
178 void DeleteAllRows(); | 175 void DeleteAllRows(); |
179 | 176 |
180 // Removes rows from the database and caches that contain a URL in |rows|. | 177 // Removes rows from the database and caches that contain a URL in |rows|. |
181 void DeleteRowsWithURLs(const history::URLRows& rows); | 178 void DeleteRowsWithURLs(const history::URLRows& rows); |
182 | 179 |
183 // Used to batch operations on the database. | |
184 void BeginTransaction(); | |
185 void CommitTransaction(); | |
186 | |
187 Profile* profile_; | 180 Profile* profile_; |
188 scoped_refptr<AutocompleteActionPredictorDatabase> db_; | 181 scoped_refptr<AutocompleteActionPredictorTable> table_; |
189 content::NotificationRegistrar notification_registrar_; | 182 content::NotificationRegistrar notification_registrar_; |
190 | 183 |
191 // This is cleared after every Omnibox navigation. | 184 // This is cleared after every Omnibox navigation. |
192 std::vector<TransitionalMatch> transitional_matches_; | 185 std::vector<TransitionalMatch> transitional_matches_; |
193 | 186 |
194 // This allows us to predict the effect of confidence threshold changes on | 187 // This allows us to predict the effect of confidence threshold changes on |
195 // accuracy. | 188 // accuracy. |
196 mutable std::vector<std::pair<GURL, double> > tracked_urls_; | 189 mutable std::vector<std::pair<GURL, double> > tracked_urls_; |
197 | 190 |
198 DBCacheMap db_cache_; | 191 DBCacheMap db_cache_; |
199 DBIdCacheMap db_id_cache_; | 192 DBIdCacheMap db_id_cache_; |
200 | 193 |
201 bool initialized_; | 194 bool initialized_; |
202 | 195 |
203 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor); | 196 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor); |
204 }; | 197 }; |
205 | 198 |
| 199 } // namespace predictors |
| 200 |
206 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ | 201 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ |
OLD | NEW |