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

Side by Side Diff: chrome/browser/autocomplete/network_action_predictor.h

Issue 8600004: Adding a fourth omnibox prerender experiment. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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 string16 user_text;
80 std::vector<GURL> urls;
81
82 bool operator==(const string16& other_user_text) const {
83 return user_text.compare(other_user_text) == 0;
Peter Kasting 2011/11/18 21:17:13 Nit: Just return user_text == other_user_text. Ju
dominich 2011/11/18 23:05:51 Done.
84 }
85 };
86
66 struct DBCacheKey { 87 struct DBCacheKey {
67 string16 user_text; 88 string16 user_text;
68 GURL url; 89 GURL url;
69 90
70 bool operator<(const DBCacheKey& rhs) const { 91 bool operator<(const DBCacheKey& rhs) const {
71 return (user_text != rhs.user_text) ? 92 return (user_text != rhs.user_text) ?
72 (user_text < rhs.user_text) : (url < rhs.url); 93 (user_text < rhs.user_text) : (url < rhs.url);
73 } 94 }
74 95
75 bool operator==(const DBCacheKey& rhs) const { 96 bool operator==(const DBCacheKey& rhs) const {
(...skipping 10 matching lines...) Expand all
86 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id> 107 typedef std::map<DBCacheKey, NetworkActionPredictorDatabase::Row::Id>
87 DBIdCacheMap; 108 DBIdCacheMap;
88 109
89 static const int kMaximumDaysToKeepEntry; 110 static const int kMaximumDaysToKeepEntry;
90 111
91 // NotificationObserver 112 // NotificationObserver
92 virtual void Observe(int type, 113 virtual void Observe(int type,
93 const content::NotificationSource& source, 114 const content::NotificationSource& source,
94 const content::NotificationDetails& details) OVERRIDE; 115 const content::NotificationDetails& details) OVERRIDE;
95 116
117 // Called when NOTIFICATION_OMNIBOX_OPENED_URL is observed.
118 void OnOmniboxOpenedUrl(const AutocompleteLog& log);
119
96 // Deletes any old or invalid entries from the local caches. |url_db| and 120 // 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. 121 // |id_list| must not be NULL. Every row id deleted will be added to id_list.
98 void DeleteOldIdsFromCaches( 122 void DeleteOldIdsFromCaches(
99 history::URLDatabase* url_db, 123 history::URLDatabase* url_db,
100 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list); 124 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list);
101 125
102 // Called to delete any old or invalid entries from the database. Called after 126 // 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. 127 // the local caches are created once the history service is available.
104 void DeleteOldEntries(history::URLDatabase* url_db); 128 void DeleteOldEntries(history::URLDatabase* url_db);
105 129
(...skipping 27 matching lines...) Expand all
133 void DeleteRowsWithURLs(const std::set<GURL>& urls); 157 void DeleteRowsWithURLs(const std::set<GURL>& urls);
134 158
135 // Used to batch operations on the database. 159 // Used to batch operations on the database.
136 void BeginTransaction(); 160 void BeginTransaction();
137 void CommitTransaction(); 161 void CommitTransaction();
138 162
139 Profile* profile_; 163 Profile* profile_;
140 scoped_refptr<NetworkActionPredictorDatabase> db_; 164 scoped_refptr<NetworkActionPredictorDatabase> db_;
141 content::NotificationRegistrar notification_registrar_; 165 content::NotificationRegistrar notification_registrar_;
142 166
167 // This is cleared after every Omnibox navigation.
168 std::vector<TransitionalMatch> transitional_matches_;
169
143 DBCacheMap db_cache_; 170 DBCacheMap db_cache_;
144 DBIdCacheMap db_id_cache_; 171 DBIdCacheMap db_id_cache_;
145 172
146 bool initialized_; 173 bool initialized_;
147 174
148 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor); 175 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictor);
149 }; 176 };
150 177
151 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_ 178 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698