| 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" | |
| 16 #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" |
| 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 Profile; | 26 class Profile; |
| 27 | 27 |
| 28 namespace history { | 28 namespace history { |
| 29 class URLDatabase; | 29 class URLDatabase; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // This class is responsible for determining the correct predictive network | 32 // This class is responsible for determining the correct predictive network |
| 33 // action to take given for a given AutocompleteMatch and entered text. it uses | 33 // action to take given for a given AutocompleteMatch and entered text. it uses |
| 34 // a NetworkActionPredictorDatabase accessed asynchronously on the DB thread to | 34 // an AutocompleteActionPredictorDatabase accessed asynchronously on the DB |
| 35 // permanently store the data used to make predictions, and keeps local caches | 35 // thread to permanently store the data used to make predictions, and keeps |
| 36 // of that data to be able to make predictions synchronously on the UI thread | 36 // local caches of that data to be able to make predictions synchronously on the |
| 37 // where it lives. It can be accessed as a weak pointer so that it can safely | 37 // UI thread where it lives. It can be accessed as a weak pointer so that it can |
| 38 // use PostTaskAndReply without fear of crashes if it is destroyed before the | 38 // safely use PostTaskAndReply without fear of crashes if it is destroyed before |
| 39 // reply triggers. This is necessary during initialization. | 39 // the reply triggers. This is necessary during initialization. |
| 40 class NetworkActionPredictor | 40 class AutocompleteActionPredictor |
| 41 : public ProfileKeyedService, | 41 : public ProfileKeyedService, |
| 42 public content::NotificationObserver, | 42 public content::NotificationObserver, |
| 43 public base::SupportsWeakPtr<NetworkActionPredictor> { | 43 public base::SupportsWeakPtr<AutocompleteActionPredictor> { |
| 44 public: | 44 public: |
| 45 enum Action { | 45 enum Action { |
| 46 ACTION_PRERENDER = 0, | 46 ACTION_PRERENDER = 0, |
| 47 ACTION_PRECONNECT, | 47 ACTION_PRECONNECT, |
| 48 ACTION_NONE, | 48 ACTION_NONE, |
| 49 LAST_PREDICT_ACTION = ACTION_NONE | 49 LAST_PREDICT_ACTION = ACTION_NONE |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 explicit NetworkActionPredictor(Profile* profile); | 52 explicit AutocompleteActionPredictor(Profile* profile); |
| 53 virtual ~NetworkActionPredictor(); | 53 virtual ~AutocompleteActionPredictor(); |
| 54 | 54 |
| 55 static void set_hit_weight(double weight) { hit_weight_ = weight; } | 55 static void set_hit_weight(double weight) { hit_weight_ = weight; } |
| 56 static double get_hit_weight() { return hit_weight_; } | 56 static double get_hit_weight() { return hit_weight_; } |
| 57 | 57 |
| 58 // Registers an AutocompleteResult for a given |user_text|. This will be used | 58 // Registers an AutocompleteResult for a given |user_text|. This will be used |
| 59 // when the user navigates from the Omnibox to determine early opportunities | 59 // when the user navigates from the Omnibox to determine early opportunities |
| 60 // to predict their actions. | 60 // to predict their actions. |
| 61 void RegisterTransitionalMatches(const string16& user_text, | 61 void RegisterTransitionalMatches(const string16& user_text, |
| 62 const AutocompleteResult& result); | 62 const AutocompleteResult& result); |
| 63 | 63 |
| 64 // Clears any transitional matches that have been registered. Called when, for | 64 // Clears any transitional matches that have been registered. Called when, for |
| 65 // example, the AutocompleteEditModel is reverted. | 65 // example, the AutocompleteEditModel is reverted. |
| 66 void ClearTransitionalMatches(); | 66 void ClearTransitionalMatches(); |
| 67 | 67 |
| 68 // Return the recommended action given |user_text|, the text the user has | 68 // Return the recommended action given |user_text|, the text the user has |
| 69 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. | 69 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. |
| 70 // This method uses information from the ShortcutsBackend including how much | 70 // This method uses information from the ShortcutsBackend including how much |
| 71 // of the matching entry the user typed, and how long it's been since the user | 71 // of the matching entry the user typed, and how long it's been since the user |
| 72 // visited the matching URL, to calculate a score between 0 and 1. This score | 72 // visited the matching URL, to calculate a score between 0 and 1. This score |
| 73 // is then mapped to an Action. | 73 // is then mapped to an Action. |
| 74 Action RecommendAction(const string16& user_text, | 74 Action RecommendAction(const string16& user_text, |
| 75 const AutocompleteMatch& match) const; | 75 const AutocompleteMatch& match) const; |
| 76 | 76 |
| 77 // Return true if the suggestion type warrants a TCP/IP preconnection. | 77 // 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. | 78 // i.e., it is now quite likely that the user will select the related domain. |
| 79 static bool IsPreconnectable(const AutocompleteMatch& match); | 79 static bool IsPreconnectable(const AutocompleteMatch& match); |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 friend class NetworkActionPredictorTest; | 82 friend class AutocompleteActionPredictorTest; |
| 83 friend class NetworkActionPredictorDOMHandler; | 83 friend class AutocompleteActionPredictorDOMHandler; |
| 84 | 84 |
| 85 struct TransitionalMatch { | 85 struct TransitionalMatch { |
| 86 TransitionalMatch(); | 86 TransitionalMatch(); |
| 87 ~TransitionalMatch(); | 87 ~TransitionalMatch(); |
| 88 | 88 |
| 89 string16 user_text; | 89 string16 user_text; |
| 90 std::vector<GURL> urls; | 90 std::vector<GURL> urls; |
| 91 | 91 |
| 92 bool operator==(const string16& other_user_text) const { | 92 bool operator==(const string16& other_user_text) const { |
| 93 return user_text == other_user_text; | 93 return user_text == other_user_text; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 107 return (user_text == rhs.user_text) && (url == rhs.url); | 107 return (user_text == rhs.user_text) && (url == rhs.url); |
| 108 } | 108 } |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 struct DBCacheValue { | 111 struct DBCacheValue { |
| 112 int number_of_hits; | 112 int number_of_hits; |
| 113 int number_of_misses; | 113 int number_of_misses; |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap; | 116 typedef std::map<DBCacheKey, DBCacheValue> DBCacheMap; |
| 117 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id> | 117 typedef std::map<DBCacheKey, AutocompleteActionPredictorDatabase::Row::Id> |
| 118 DBIdCacheMap; | 118 DBIdCacheMap; |
| 119 | 119 |
| 120 static const int kMaximumDaysToKeepEntry; | 120 static const int kMaximumDaysToKeepEntry; |
| 121 | 121 |
| 122 // Multiplying factor applied to the |number_of_hits| for a database entry | 122 // 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 | 123 // 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. | 124 // static. Once the field trial ends, this will be a constant value. |
| 125 static double hit_weight_; | 125 static double hit_weight_; |
| 126 | 126 |
| 127 // ProfileKeyedService | 127 // ProfileKeyedService |
| 128 virtual void Shutdown() OVERRIDE; | 128 virtual void Shutdown() OVERRIDE; |
| 129 | 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<NetworkActionPredictorDatabase::Row::Id>* id_list); | 142 std::vector<AutocompleteActionPredictorDatabase::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<NetworkActionPredictorDatabase::Row>* row_buffer); | 152 std::vector<AutocompleteActionPredictorDatabase::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 a row to the database and caches. |
| 170 void AddRow(const DBCacheKey& key, | 170 void AddRow(const DBCacheKey& key, |
| 171 const NetworkActionPredictorDatabase::Row& row); | 171 const AutocompleteActionPredictorDatabase::Row& row); |
| 172 | 172 |
| 173 // Updates a row in the database and the caches. | 173 // Updates a row in the database and the caches. |
| 174 void UpdateRow(DBCacheMap::iterator it, | 174 void UpdateRow(DBCacheMap::iterator it, |
| 175 const NetworkActionPredictorDatabase::Row& row); | 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. | 183 // Used to batch operations on the database. |
| 184 void BeginTransaction(); | 184 void BeginTransaction(); |
| 185 void CommitTransaction(); | 185 void CommitTransaction(); |
| 186 | 186 |
| 187 Profile* profile_; | 187 Profile* profile_; |
| 188 scoped_refptr<NetworkActionPredictorDatabase> db_; | 188 scoped_refptr<AutocompleteActionPredictorDatabase> db_; |
| 189 content::NotificationRegistrar notification_registrar_; | 189 content::NotificationRegistrar notification_registrar_; |
| 190 | 190 |
| 191 // This is cleared after every Omnibox navigation. | 191 // This is cleared after every Omnibox navigation. |
| 192 std::vector<TransitionalMatch> transitional_matches_; | 192 std::vector<TransitionalMatch> transitional_matches_; |
| 193 | 193 |
| 194 // This allows us to predict the effect of confidence threshold changes on | 194 // This allows us to predict the effect of confidence threshold changes on |
| 195 // accuracy. | 195 // accuracy. |
| 196 mutable std::vector<std::pair<GURL, double> > tracked_urls_; | 196 mutable std::vector<std::pair<GURL, double> > tracked_urls_; |
| 197 | 197 |
| 198 DBCacheMap db_cache_; | 198 DBCacheMap db_cache_; |
| 199 DBIdCacheMap db_id_cache_; | 199 DBIdCacheMap db_id_cache_; |
| 200 | 200 |
| 201 bool initialized_; | 201 bool initialized_; |
| 202 | 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor); | 203 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictor); |
| 204 }; | 204 }; |
| 205 | 205 |
| 206 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ | 206 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_H_ |
| OLD | NEW |