Chromium Code Reviews| 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_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ |
| 6 #define CHROME_BROWSER_AUTOCOMPLETE_NETWORK_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/autocomplete/network_action_predictor_database.h" | 15 #include "chrome/browser/predictors/autocomplete_action_predictor_database.h" |
| 16 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 17 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 | 20 |
| 21 struct AutocompleteLog; | 21 struct AutocompleteLog; |
| 22 struct AutocompleteMatch; | 22 struct AutocompleteMatch; |
| 23 class AutocompleteResult; | 23 class AutocompleteResult; |
| 24 class HistoryService; | 24 class HistoryService; |
| 25 class Profile; | 25 class Profile; |
| 26 | 26 |
| 27 namespace history { | 27 namespace history { |
| 28 class URLDatabase; | 28 class URLDatabase; |
| 29 } | 29 } |
| 30 | 30 |
| 31 // This class is responsible for determining the correct predictive network | 31 // This class is responsible for determining the correct predictive network |
| 32 // action to take given for a given AutocompleteMatch and entered text. it uses | 32 // action to take given for a given AutocompleteMatch and entered text. it uses |
| 33 // a NetworkActionPredictorDatabase accessed asynchronously on the DB thread to | 33 // a AutocompleteActionPredictorDatabase accessed asynchronously on the DB |
|
cbentzel
2012/03/29 00:34:28
Nit: an Autocomplete
Shishir
2012/03/29 00:43:55
Done.
| |
| 34 // permanently store the data used to make predictions, and keeps local caches | 34 // thread to permanently store the data used to make predictions, and keeps |
| 35 // of that data to be able to make predictions synchronously on the UI thread | 35 // local caches of that data to be able to make predictions synchronously on the |
| 36 // where it lives. It can be accessed as a weak pointer so that it can safely | 36 // UI thread where it lives. It can be accessed as a weak pointer so that it can |
| 37 // use PostTaskAndReply without fear of crashes if it is destroyed before the | 37 // safely use PostTaskAndReply without fear of crashes if it is destroyed before |
| 38 // reply triggers. This is necessary during initialization. | 38 // the reply triggers. This is necessary during initialization. |
| 39 class NetworkActionPredictor | 39 class AutocompleteActionPredictor |
| 40 : public ProfileKeyedService, | 40 : public ProfileKeyedService, |
| 41 public content::NotificationObserver, | 41 public content::NotificationObserver, |
| 42 public base::SupportsWeakPtr<NetworkActionPredictor> { | 42 public base::SupportsWeakPtr<AutocompleteActionPredictor> { |
| 43 public: | 43 public: |
| 44 enum Action { | 44 enum Action { |
| 45 ACTION_PRERENDER = 0, | 45 ACTION_PRERENDER = 0, |
| 46 ACTION_PRECONNECT, | 46 ACTION_PRECONNECT, |
| 47 ACTION_NONE, | 47 ACTION_NONE, |
| 48 LAST_PREDICT_ACTION = ACTION_NONE | 48 LAST_PREDICT_ACTION = ACTION_NONE |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 explicit NetworkActionPredictor(Profile* profile); | 51 explicit AutocompleteActionPredictor(Profile* profile); |
| 52 virtual ~NetworkActionPredictor(); | 52 virtual ~AutocompleteActionPredictor(); |
| 53 | 53 |
| 54 static void set_hit_weight(double weight) { hit_weight_ = weight; } | 54 static void set_hit_weight(double weight) { hit_weight_ = weight; } |
| 55 static double get_hit_weight() { return hit_weight_; } | 55 static double get_hit_weight() { return hit_weight_; } |
| 56 | 56 |
| 57 // Registers an AutocompleteResult for a given |user_text|. This will be used | 57 // Registers an AutocompleteResult for a given |user_text|. This will be used |
| 58 // when the user navigates from the Omnibox to determine early opportunities | 58 // when the user navigates from the Omnibox to determine early opportunities |
| 59 // to predict their actions. | 59 // to predict their actions. |
| 60 void RegisterTransitionalMatches(const string16& user_text, | 60 void RegisterTransitionalMatches(const string16& user_text, |
| 61 const AutocompleteResult& result); | 61 const AutocompleteResult& result); |
| 62 | 62 |
| 63 // Clears any transitional matches that have been registered. Called when, for | 63 // Clears any transitional matches that have been registered. Called when, for |
| 64 // example, the AutocompleteEditModel is reverted. | 64 // example, the AutocompleteEditModel is reverted. |
| 65 void ClearTransitionalMatches(); | 65 void ClearTransitionalMatches(); |
| 66 | 66 |
| 67 // Return the recommended action given |user_text|, the text the user has | 67 // Return the recommended action given |user_text|, the text the user has |
| 68 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. | 68 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. |
| 69 // This method uses information from the ShortcutsBackend including how much | 69 // This method uses information from the ShortcutsBackend including how much |
| 70 // of the matching entry the user typed, and how long it's been since the user | 70 // of the matching entry the user typed, and how long it's been since the user |
| 71 // visited the matching URL, to calculate a score between 0 and 1. This score | 71 // visited the matching URL, to calculate a score between 0 and 1. This score |
| 72 // is then mapped to an Action. | 72 // is then mapped to an Action. |
| 73 Action RecommendAction(const string16& user_text, | 73 Action RecommendAction(const string16& user_text, |
| 74 const AutocompleteMatch& match) const; | 74 const AutocompleteMatch& match) const; |
| 75 | 75 |
| 76 // Return true if the suggestion type warrants a TCP/IP preconnection. | 76 // Return true if the suggestion type warrants a TCP/IP preconnection. |
| 77 // i.e., it is now quite likely that the user will select the related domain. | 77 // i.e., it is now quite likely that the user will select the related domain. |
| 78 static bool IsPreconnectable(const AutocompleteMatch& match); | 78 static bool IsPreconnectable(const AutocompleteMatch& match); |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 friend class NetworkActionPredictorTest; | 81 friend class AutocompleteActionPredictorTest; |
| 82 friend class NetworkActionPredictorDOMHandler; | 82 friend class AutocompleteActionPredictorDOMHandler; |
| 83 | 83 |
| 84 struct TransitionalMatch { | 84 struct TransitionalMatch { |
| 85 TransitionalMatch(); | 85 TransitionalMatch(); |
| 86 ~TransitionalMatch(); | 86 ~TransitionalMatch(); |
| 87 | 87 |
| 88 string16 user_text; | 88 string16 user_text; |
| 89 std::vector<GURL> urls; | 89 std::vector<GURL> urls; |
| 90 | 90 |
| 91 bool operator==(const string16& other_user_text) const { | 91 bool operator==(const string16& other_user_text) const { |
| 92 return user_text == other_user_text; | 92 return user_text == other_user_text; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 106 return (user_text == rhs.user_text) && (url == rhs.url); | 106 return (user_text == rhs.user_text) && (url == rhs.url); |
| 107 } | 107 } |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 struct DBCacheValue { | 110 struct DBCacheValue { |
| 111 int number_of_hits; | 111 int number_of_hits; |
| 112 int number_of_misses; | 112 int number_of_misses; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap; | 115 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap; |
| 116 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id> | 116 typedef std::map<DBCacheKey, AutocompleteActionPredictorDatabase::Row::Id> |
| 117 DBIdCacheMap; | 117 DBIdCacheMap; |
| 118 | 118 |
| 119 static const int kMaximumDaysToKeepEntry; | 119 static const int kMaximumDaysToKeepEntry; |
| 120 | 120 |
| 121 // Multiplying factor applied to the |number_of_hits| for a database entry | 121 // Multiplying factor applied to the |number_of_hits| for a database entry |
| 122 // when calculating the confidence. It is currently set by a field trial so is | 122 // when calculating the confidence. It is currently set by a field trial so is |
| 123 // static. Once the field trial ends, this will be a constant value. | 123 // static. Once the field trial ends, this will be a constant value. |
| 124 static double hit_weight_; | 124 static double hit_weight_; |
| 125 | 125 |
| 126 // ProfileKeyedService | 126 // ProfileKeyedService |
| 127 virtual void Shutdown() OVERRIDE; | 127 virtual void Shutdown() OVERRIDE; |
| 128 | 128 |
| 129 // NotificationObserver | 129 // NotificationObserver |
| 130 virtual void Observe(int type, | 130 virtual void Observe(int type, |
| 131 const content::NotificationSource& source, | 131 const content::NotificationSource& source, |
| 132 const content::NotificationDetails& details) OVERRIDE; | 132 const content::NotificationDetails& details) OVERRIDE; |
| 133 | 133 |
| 134 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. | 134 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. |
| 135 void OnOmniboxOpenedUrl(const AutocompleteLog& log); | 135 void OnOmniboxOpenedUrl(const AutocompleteLog& log); |
| 136 | 136 |
| 137 // Deletes any old or invalid entries from the local caches. |url_db| and | 137 // Deletes any old or invalid entries from the local caches. |url_db| and |
| 138 // |id_list| must not be NULL. Every row id deleted will be added to id_list. | 138 // |id_list| must not be NULL. Every row id deleted will be added to id_list. |
| 139 void DeleteOldIdsFromCaches( | 139 void DeleteOldIdsFromCaches( |
| 140 history::URLDatabase* url_db, | 140 history::URLDatabase* url_db, |
| 141 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list); | 141 std::vector<AutocompleteActionPredictorDatabase::Row::Id>* id_list); |
| 142 | 142 |
| 143 // Called to delete any old or invalid entries from the database. Called after | 143 // Called to delete any old or invalid entries from the database. Called after |
| 144 // the local caches are created once the history service is available. | 144 // the local caches are created once the history service is available. |
| 145 void DeleteOldEntries(history::URLDatabase* url_db); | 145 void DeleteOldEntries(history::URLDatabase* url_db); |
| 146 | 146 |
| 147 // Called to populate the local caches. This also calls DeleteOldEntries | 147 // Called to populate the local caches. This also calls DeleteOldEntries |
| 148 // if the history service is available, or registers for the notification of | 148 // if the history service is available, or registers for the notification of |
| 149 // it becoming available. | 149 // it becoming available. |
| 150 void CreateCaches( | 150 void CreateCaches( |
| 151 std::vector<NetworkActionPredictorDatabase::Row>* row_buffer); | 151 std::vector<AutocompleteActionPredictorDatabase::Row>* row_buffer); |
| 152 | 152 |
| 153 // Attempts to call DeleteOldEntries if the in-memory database has been loaded | 153 // Attempts to call DeleteOldEntries if the in-memory database has been loaded |
| 154 // by |service|. Returns success as a boolean. | 154 // by |service|. Returns success as a boolean. |
| 155 bool TryDeleteOldEntries(HistoryService* service); | 155 bool TryDeleteOldEntries(HistoryService* service); |
| 156 | 156 |
| 157 // Uses local caches to calculate an exact percentage prediction that the user | 157 // Uses local caches to calculate an exact percentage prediction that the user |
| 158 // will take a particular match given what they have typed. |is_in_db| is set | 158 // will take a particular match given what they have typed. |is_in_db| is set |
| 159 // to differentiate trivial zero results resulting from a match not being | 159 // to differentiate trivial zero results resulting from a match not being |
| 160 // found from actual zero results where the calculation returns 0.0. | 160 // found from actual zero results where the calculation returns 0.0. |
| 161 double CalculateConfidence(const string16& user_text, | 161 double CalculateConfidence(const string16& user_text, |
| 162 const AutocompleteMatch& match, | 162 const AutocompleteMatch& match, |
| 163 bool* is_in_db) const; | 163 bool* is_in_db) const; |
| 164 | 164 |
| 165 // Calculates the confidence for an entry in the DBCacheMap. | 165 // Calculates the confidence for an entry in the DBCacheMap. |
| 166 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const; | 166 double CalculateConfidenceForDbEntry(DBCacheMap::const_iterator iter) const; |
| 167 | 167 |
| 168 // Adds a row to the database and caches. | 168 // Adds a row to the database and caches. |
| 169 void AddRow(const DBCacheKey& key, | 169 void AddRow(const DBCacheKey& key, |
| 170 const NetworkActionPredictorDatabase::Row& row); | 170 const AutocompleteActionPredictorDatabase::Row& row); |
| 171 | 171 |
| 172 // Updates a row in the database and the caches. | 172 // Updates a row in the database and the caches. |
| 173 void UpdateRow(DBCacheMap::iterator it, | 173 void UpdateRow(DBCacheMap::iterator it, |
| 174 const NetworkActionPredictorDatabase::Row& row); | 174 const AutocompleteActionPredictorDatabase::Row& row); |
| 175 | 175 |
| 176 // Removes all rows from the database and caches. | 176 // Removes all rows from the database and caches. |
| 177 void DeleteAllRows(); | 177 void DeleteAllRows(); |
| 178 | 178 |
| 179 // Removes rows from the database and caches that contain a URL in |urls|. | 179 // Removes rows from the database and caches that contain a URL in |urls|. |
| 180 void DeleteRowsWithURLs(const std::set<GURL>& urls); | 180 void DeleteRowsWithURLs(const std::set<GURL>& urls); |
| 181 | 181 |
| 182 // Used to batch operations on the database. | 182 // Used to batch operations on the database. |
| 183 void BeginTransaction(); | 183 void BeginTransaction(); |
| 184 void CommitTransaction(); | 184 void CommitTransaction(); |
| 185 | 185 |
| 186 Profile* profile_; | 186 Profile* profile_; |
| 187 scoped_refptr<NetworkActionPredictorDatabase> db_; | 187 scoped_refptr<AutocompleteActionPredictorDatabase> db_; |
| 188 content::NotificationRegistrar notification_registrar_; | 188 content::NotificationRegistrar notification_registrar_; |
| 189 | 189 |
| 190 // This is cleared after every Omnibox navigation. | 190 // This is cleared after every Omnibox navigation. |
| 191 std::vector<TransitionalMatch> transitional_matches_; | 191 std::vector<TransitionalMatch> transitional_matches_; |
| 192 | 192 |
| 193 // This allows us to predict the effect of confidence threshold changes on | 193 // This allows us to predict the effect of confidence threshold changes on |
| 194 // accuracy. | 194 // accuracy. |
| 195 mutable std::vector<std::pair<GURL, double> > tracked_urls_; | 195 mutable std::vector<std::pair<GURL, double> > tracked_urls_; |
| 196 | 196 |
| 197 DBCacheMap db_cache_; | 197 DBCacheMap db_cache_; |
| 198 DBIdCacheMap db_id_cache_; | 198 DBIdCacheMap db_id_cache_; |
| 199 | 199 |
| 200 bool initialized_; | 200 bool initialized_; |
| 201 | 201 |
| 202 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor); | 202 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor); |
| 203 }; | 203 }; |
| 204 | 204 |
| 205 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ | 205 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ |
| OLD | NEW |