OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_NETWORK_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/autocomplete/network_action_predictor_database.h" |
16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 | 19 |
| 20 struct AutocompleteLog; |
20 struct AutocompleteMatch; | 21 struct AutocompleteMatch; |
| 22 class AutocompleteResult; |
21 class HistoryService; | 23 class HistoryService; |
22 class Profile; | 24 class Profile; |
23 | 25 |
24 namespace history { | 26 namespace history { |
25 class URLDatabase; | 27 class URLDatabase; |
26 } | 28 } |
27 | 29 |
28 // This class is responsible for determining the correct predictive network | 30 // This class is responsible for determining the correct predictive network |
29 // action to take given for a given AutocompleteMatch and entered text. it uses | 31 // action to take given for a given AutocompleteMatch and entered text. it uses |
30 // a NetworkActionPredictorDatabase accessed asynchronously on the DB thread to | 32 // a NetworkActionPredictorDatabase accessed asynchronously on the DB thread to |
31 // permanently store the data used to make predictions, and keeps local caches | 33 // permanently store the data used to make predictions, and keeps local caches |
32 // of that data to be able to make predictions synchronously on the UI thread | 34 // of that data to be able to make predictions synchronously on the UI thread |
33 // where it lives. It can be accessed as a weak pointer so that it can safely | 35 // where it lives. It can be accessed as a weak pointer so that it can safely |
34 // use PostTaskAndReply without fear of crashes if it is destroyed before the | 36 // use PostTaskAndReply without fear of crashes if it is destroyed before the |
35 // reply triggers. This is necessary during initialization. | 37 // reply triggers. This is necessary during initialization. |
36 class NetworkActionPredictor | 38 class NetworkActionPredictor |
37 : public content::NotificationObserver, | 39 : public content::NotificationObserver, |
38 public base::SupportsWeakPtr<NetworkActionPredictor> { | 40 public base::SupportsWeakPtr<NetworkActionPredictor> { |
39 public: | 41 public: |
40 enum Action { | 42 enum Action { |
41 ACTION_PRERENDER = 0, | 43 ACTION_PRERENDER = 0, |
42 ACTION_PRECONNECT, | 44 ACTION_PRECONNECT, |
43 ACTION_NONE, | 45 ACTION_NONE, |
44 LAST_PREDICT_ACTION = ACTION_NONE | 46 LAST_PREDICT_ACTION = ACTION_NONE |
45 }; | 47 }; |
46 | 48 |
47 explicit NetworkActionPredictor(Profile* profile); | 49 explicit NetworkActionPredictor(Profile* profile); |
48 virtual ~NetworkActionPredictor(); | 50 virtual ~NetworkActionPredictor(); |
49 | 51 |
| 52 // Registers an AutocompleteResult for a given |user_text|. This will be used |
| 53 // when the user navigates from the Omnibox to determine early opportunities |
| 54 // to predict their actions. |
| 55 void RegisterTransitionalMatches(const string16& user_text, |
| 56 const AutocompleteResult& result); |
| 57 |
| 58 // Clears any transitional matches that have been registered. Called when, for |
| 59 // example, the AutocompleteEditModel is reverted. |
| 60 void ClearTransitionalMatches(); |
| 61 |
50 // Return the recommended action given |user_text|, the text the user has | 62 // Return the recommended action given |user_text|, the text the user has |
51 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. | 63 // entered in the Omnibox, and |match|, the suggestion from Autocomplete. |
52 // This method uses information from the ShortcutsBackend including how much | 64 // This method uses information from the ShortcutsBackend including how much |
53 // of the matching entry the user typed, and how long it's been since the user | 65 // of the matching entry the user typed, and how long it's been since the user |
54 // visited the matching URL, to calculate a score between 0 and 1. This score | 66 // visited the matching URL, to calculate a score between 0 and 1. This score |
55 // is then mapped to an Action. | 67 // is then mapped to an Action. |
56 Action RecommendAction(const string16& user_text, | 68 Action RecommendAction(const string16& user_text, |
57 const AutocompleteMatch& match) const; | 69 const AutocompleteMatch& match) const; |
58 | 70 |
59 // Return true if the suggestion type warrants a TCP/IP preconnection. | 71 // Return true if the suggestion type warrants a TCP/IP preconnection. |
60 // i.e., it is now quite likely that the user will select the related domain. | 72 // i.e., it is now quite likely that the user will select the related domain. |
61 static bool IsPreconnectable(const AutocompleteMatch& match); | 73 static bool IsPreconnectable(const AutocompleteMatch& match); |
62 | 74 |
63 private: | 75 private: |
64 friend class NetworkActionPredictorTest; | 76 friend class NetworkActionPredictorTest; |
65 | 77 |
| 78 struct TransitionalMatch { |
| 79 TransitionalMatch(); |
| 80 ~TransitionalMatch(); |
| 81 |
| 82 string16 user_text; |
| 83 std::vector<GURL> urls; |
| 84 |
| 85 bool operator==(const string16& other_user_text) const { |
| 86 return user_text == other_user_text; |
| 87 } |
| 88 }; |
| 89 |
66 struct DBCacheKey { | 90 struct DBCacheKey { |
67 string16 user_text; | 91 string16 user_text; |
68 GURL url; | 92 GURL url; |
69 | 93 |
70 bool operator<(const DBCacheKey& rhs) const { | 94 bool operator<(const DBCacheKey& rhs) const { |
71 return (user_text != rhs.user_text) ? | 95 return (user_text != rhs.user_text) ? |
72 (user_text < rhs.user_text) : (url < rhs.url); | 96 (user_text < rhs.user_text) : (url < rhs.url); |
73 } | 97 } |
74 | 98 |
75 bool operator==(const DBCacheKey& rhs) const { | 99 bool operator==(const DBCacheKey& rhs) const { |
(...skipping 10 matching lines...) Expand all Loading... |
86 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id> | 110 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id> |
87 DBIdCacheMap; | 111 DBIdCacheMap; |
88 | 112 |
89 static const int kMaximumDaysToKeepEntry; | 113 static const int kMaximumDaysToKeepEntry; |
90 | 114 |
91 // NotificationObserver | 115 // NotificationObserver |
92 virtual void Observe(int type, | 116 virtual void Observe(int type, |
93 const content::NotificationSource& source, | 117 const content::NotificationSource& source, |
94 const content::NotificationDetails& details) OVERRIDE; | 118 const content::NotificationDetails& details) OVERRIDE; |
95 | 119 |
| 120 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed. |
| 121 void OnOmniboxOpenedUrl(const AutocompleteLog& log); |
| 122 |
96 // Deletes any old or invalid entries from the local caches. |url_db| and | 123 // Deletes any old or invalid entries from the local caches. |url_db| and |
97 // |id_list| must not be NULL. Every row id deleted will be added to id_list. | 124 // |id_list| must not be NULL. Every row id deleted will be added to id_list. |
98 void DeleteOldIdsFromCaches( | 125 void DeleteOldIdsFromCaches( |
99 history::URLDatabase* url_db, | 126 history::URLDatabase* url_db, |
100 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list); | 127 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list); |
101 | 128 |
102 // Called to delete any old or invalid entries from the database. Called after | 129 // Called to delete any old or invalid entries from the database. Called after |
103 // the local caches are created once the history service is available. | 130 // the local caches are created once the history service is available. |
104 void DeleteOldEntries(history::URLDatabase* url_db); | 131 void DeleteOldEntries(history::URLDatabase* url_db); |
105 | 132 |
(...skipping 27 matching lines...) Expand all Loading... |
133 void DeleteRowsWithURLs(const std::set<GURL>& urls); | 160 void DeleteRowsWithURLs(const std::set<GURL>& urls); |
134 | 161 |
135 // Used to batch operations on the database. | 162 // Used to batch operations on the database. |
136 void BeginTransaction(); | 163 void BeginTransaction(); |
137 void CommitTransaction(); | 164 void CommitTransaction(); |
138 | 165 |
139 Profile* profile_; | 166 Profile* profile_; |
140 scoped_refptr<NetworkActionPredictorDatabase> db_; | 167 scoped_refptr<NetworkActionPredictorDatabase> db_; |
141 content::NotificationRegistrar notification_registrar_; | 168 content::NotificationRegistrar notification_registrar_; |
142 | 169 |
| 170 // This is cleared after every Omnibox navigation. |
| 171 std::vector<TransitionalMatch> transitional_matches_; |
| 172 |
143 DBCacheMap db_cache_; | 173 DBCacheMap db_cache_; |
144 DBIdCacheMap db_id_cache_; | 174 DBIdCacheMap db_id_cache_; |
145 | 175 |
146 bool initialized_; | 176 bool initialized_; |
147 | 177 |
148 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor); | 178 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor); |
149 }; | 179 }; |
150 | 180 |
151 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ | 181 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ |
OLD | NEW |