| 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_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ |
| 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ |
| 7 | 7 |
| 8 #include <map> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 11 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 14 #include "chrome/browser/common/cancelable_request.h" | 15 #include "chrome/browser/common/cancelable_request.h" |
| 15 #include "chrome/browser/history/visit_database.h" | 16 #include "chrome/browser/history/visit_database.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 class HistoryService; | 20 class HistoryService; |
| 19 | 21 |
| 22 namespace base { |
| 23 class DictionaryValue; |
| 24 } |
| 25 |
| 20 namespace content { | 26 namespace content { |
| 21 class SessionStorageNamespace; | 27 class SessionStorageNamespace; |
| 22 class WebContents; | 28 class WebContents; |
| 23 } | 29 } |
| 24 | 30 |
| 25 namespace gfx { | 31 namespace gfx { |
| 26 class Size; | 32 class Size; |
| 27 } | 33 } |
| 28 | 34 |
| 29 namespace prerender { | 35 namespace prerender { |
| 30 | 36 |
| 31 class PrerenderHandle; | 37 class PrerenderHandle; |
| 32 class PrerenderManager; | 38 class PrerenderManager; |
| 33 | 39 |
| 34 // PrerenderLocalPredictor maintains local browsing history to make prerender | 40 // PrerenderLocalPredictor maintains local browsing history to make prerender |
| 35 // predictions. | 41 // predictions. |
| 36 // At this point, the class is not actually creating prerenders, but just | 42 // At this point, the class is not actually creating prerenders, but just |
| 37 // recording timing stats about the effect prerendering would have. | 43 // recording timing stats about the effect prerendering would have. |
| 38 class PrerenderLocalPredictor : public history::VisitDatabaseObserver { | 44 class PrerenderLocalPredictor : public history::VisitDatabaseObserver, |
| 45 public net::URLFetcherDelegate { |
| 39 public: | 46 public: |
| 40 struct LocalPredictorURLInfo; | 47 struct LocalPredictorURLInfo; |
| 41 struct LocalPredictorURLLookupInfo; | 48 struct CandidatePrerenderInfo; |
| 42 enum Event { | 49 enum Event { |
| 43 EVENT_CONSTRUCTED = 0, | 50 EVENT_CONSTRUCTED = 0, |
| 44 EVENT_INIT_SCHEDULED = 1, | 51 EVENT_INIT_SCHEDULED = 1, |
| 45 EVENT_INIT_STARTED = 2, | 52 EVENT_INIT_STARTED = 2, |
| 46 EVENT_INIT_FAILED_NO_HISTORY = 3, | 53 EVENT_INIT_FAILED_NO_HISTORY = 3, |
| 47 EVENT_INIT_SUCCEEDED = 4, | 54 EVENT_INIT_SUCCEEDED = 4, |
| 48 EVENT_ADD_VISIT = 5, | 55 EVENT_ADD_VISIT = 5, |
| 49 EVENT_ADD_VISIT_INITIALIZED = 6, | 56 EVENT_ADD_VISIT_INITIALIZED = 6, |
| 50 EVENT_ADD_VISIT_PRERENDER_IDENTIFIED = 7, | 57 EVENT_ADD_VISIT_PRERENDER_IDENTIFIED = 7, |
| 51 EVENT_ADD_VISIT_RELEVANT_TRANSITION = 8, | 58 EVENT_ADD_VISIT_RELEVANT_TRANSITION = 8, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 EVENT_TAB_HELPER_URL_SEEN = 46, | 96 EVENT_TAB_HELPER_URL_SEEN = 46, |
| 90 EVENT_TAB_HELPER_URL_SEEN_MATCH = 47, | 97 EVENT_TAB_HELPER_URL_SEEN_MATCH = 47, |
| 91 EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MATCH = 48, | 98 EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MATCH = 48, |
| 92 EVENT_PRERENDER_URL_LOOKUP_MULTIPLE_SOURCE_WEBCONTENTS_FOUND = 49, | 99 EVENT_PRERENDER_URL_LOOKUP_MULTIPLE_SOURCE_WEBCONTENTS_FOUND = 49, |
| 93 EVENT_CONTINUE_PRERENDER_CHECK_ON_SIDE_EFFECT_FREE_WHITELIST = 50, | 100 EVENT_CONTINUE_PRERENDER_CHECK_ON_SIDE_EFFECT_FREE_WHITELIST = 50, |
| 94 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL = 51, | 101 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL = 51, |
| 95 EVENT_ISSUE_PRERENDER_ALREADY_PRERENDERING = 52, | 102 EVENT_ISSUE_PRERENDER_ALREADY_PRERENDERING = 52, |
| 96 EVENT_ISSUE_PRERENDER_NEW_PRERENDER = 53, | 103 EVENT_ISSUE_PRERENDER_NEW_PRERENDER = 53, |
| 97 EVENT_ISSUE_PRERENDER_CANCELLED_OLD_PRERENDER = 54, | 104 EVENT_ISSUE_PRERENDER_CANCELLED_OLD_PRERENDER = 54, |
| 98 EVENT_CONTINUE_PRERENDER_CHECK_FALLTHROUGH_PRERENDERING = 55, | 105 EVENT_CONTINUE_PRERENDER_CHECK_FALLTHROUGH_PRERENDERING = 55, |
| 106 EVENT_PRERENDER_URL_LOOKUP_SUCCESS = 56, |
| 107 EVENT_PRERENDER_SERVICE_DISABLED = 57, |
| 108 EVENT_PRERENDER_SERVICE_ISSUED_LOOKUP = 58, |
| 109 EVENT_PRERENDER_SERVICE_LOOKUP_TIMED_OUT = 59, |
| 110 EVENT_PRERENDER_SERVICE_RECEIVED_RESULT = 60, |
| 111 EVENT_PRERENDER_SERVICE_NO_RECORD_FOR_RESULT = 61, |
| 112 EVENT_PRERENDER_SERVICE_PARSED_CORRECTLY = 62, |
| 113 EVENT_PRERENDER_SERVICE_PARSE_ERROR = 63, |
| 114 EVENT_PRERENDER_SERVICE_PARSE_ERROR_INCORRECT_JSON = 64, |
| 115 EVENT_PRERENDER_SERVICE_HINTING_TIMED_OUT = 65, |
| 116 EVENT_PRERENDER_SERVICE_HINTING_URL_LOOKUP_TIMED_OUT = 66, |
| 117 EVENT_PRERENDER_SERVICE_CANDIDATE_URL_LOOKUP_TIMED_OUT = 67, |
| 118 EVENT_CONTINUE_PRERENDER_CHECK_ON_SERVICE_WHITELIST = 68, |
| 99 EVENT_MAX_VALUE | 119 EVENT_MAX_VALUE |
| 100 }; | 120 }; |
| 101 | 121 |
| 102 // A PrerenderLocalPredictor is owned by the PrerenderManager specified | 122 // A PrerenderLocalPredictor is owned by the PrerenderManager specified |
| 103 // in the constructor. It will be destoryed at the time its owning | 123 // in the constructor. It will be destoryed at the time its owning |
| 104 // PrerenderManager is destroyed. | 124 // PrerenderManager is destroyed. |
| 105 explicit PrerenderLocalPredictor(PrerenderManager* prerender_manager); | 125 explicit PrerenderLocalPredictor(PrerenderManager* prerender_manager); |
| 106 virtual ~PrerenderLocalPredictor(); | 126 virtual ~PrerenderLocalPredictor(); |
| 107 | 127 |
| 108 void Shutdown(); | 128 void Shutdown(); |
| 109 | 129 |
| 110 // history::VisitDatabaseObserver implementation | 130 // history::VisitDatabaseObserver implementation |
| 111 virtual void OnAddVisit(const history::BriefVisitInfo& info) OVERRIDE; | 131 virtual void OnAddVisit(const history::BriefVisitInfo& info) OVERRIDE; |
| 112 | 132 |
| 113 void OnGetInitialVisitHistory( | 133 void OnGetInitialVisitHistory( |
| 114 scoped_ptr<std::vector<history::BriefVisitInfo> > visit_history); | 134 scoped_ptr<std::vector<history::BriefVisitInfo> > visit_history); |
| 115 | 135 |
| 116 void OnPLTEventForURL(const GURL& url, base::TimeDelta page_load_time); | 136 void OnPLTEventForURL(const GURL& url, base::TimeDelta page_load_time); |
| 117 | 137 |
| 118 void OnTabHelperURLSeen(const GURL& url, content::WebContents* web_contents); | 138 void OnTabHelperURLSeen(const GURL& url, content::WebContents* web_contents); |
| 119 | 139 |
| 140 // net::URLFetcherDelegate implementation: |
| 141 void virtual OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 142 |
| 120 private: | 143 private: |
| 121 struct PrerenderProperties; | 144 struct PrerenderProperties; |
| 122 HistoryService* GetHistoryIfExists() const; | 145 HistoryService* GetHistoryIfExists() const; |
| 123 void Init(); | 146 void Init(); |
| 124 bool IsPrerenderStillValid(PrerenderProperties* prerender) const; | 147 bool IsPrerenderStillValid(PrerenderProperties* prerender) const; |
| 125 bool DoesPrerenderMatchPLTRecord(PrerenderProperties* prerender, | 148 bool DoesPrerenderMatchPLTRecord(PrerenderProperties* prerender, |
| 126 const GURL& url, | 149 const GURL& url, |
| 127 base::TimeDelta plt) const; | 150 base::TimeDelta plt) const; |
| 128 void RecordEvent(Event event) const; | 151 void RecordEvent(Event event) const; |
| 129 | 152 |
| 130 void OnLookupURL(scoped_ptr<LocalPredictorURLLookupInfo> info); | 153 void OnLookupURL(scoped_ptr<CandidatePrerenderInfo> info); |
| 154 |
| 155 // Lookup the prerender candidate in the Prerender Service (if applicable). |
| 156 void DoPrerenderServiceCheck(scoped_ptr<CandidatePrerenderInfo> info); |
| 157 |
| 158 // Lookup the prerender candidate in the LoggedIn Predictor. |
| 159 void DoLoggedInLookup(scoped_ptr<CandidatePrerenderInfo> info); |
| 131 | 160 |
| 132 // Returns an element of issued_prerenders_, which should be replaced | 161 // Returns an element of issued_prerenders_, which should be replaced |
| 133 // by a new prerender of the priority indicated, or NULL, if the priority | 162 // by a new prerender of the priority indicated, or NULL, if the priority |
| 134 // is too low. | 163 // is too low. |
| 135 PrerenderProperties* GetIssuedPrerenderSlotForPriority(double priority); | 164 PrerenderProperties* GetIssuedPrerenderSlotForPriority(double priority); |
| 136 | 165 |
| 137 void ContinuePrerenderCheck( | 166 void ContinuePrerenderCheck(scoped_ptr<CandidatePrerenderInfo> info); |
| 138 scoped_refptr<content::SessionStorageNamespace> session_storage_namespace, | |
| 139 scoped_ptr<gfx::Size> size, | |
| 140 scoped_ptr<LocalPredictorURLLookupInfo> info); | |
| 141 void LogCandidateURLStats(const GURL& url) const; | 167 void LogCandidateURLStats(const GURL& url) const; |
| 142 void IssuePrerender(scoped_refptr<content::SessionStorageNamespace> | 168 void IssuePrerender(scoped_ptr<CandidatePrerenderInfo> info, |
| 143 session_storage_namespace, | 169 scoped_ptr<LocalPredictorURLInfo> url_info, |
| 144 scoped_ptr<gfx::Size> size, | |
| 145 scoped_ptr<LocalPredictorURLInfo> info, | |
| 146 PrerenderProperties* prerender_properties); | 170 PrerenderProperties* prerender_properties); |
| 171 void MaybeCancelURLFetcher(net::URLFetcher* fetcher); |
| 172 // Returns true if the parsed response is semantically correct and could |
| 173 // be fully applied. |
| 174 bool ApplyParsedPrerenderServiceResponse( |
| 175 base::DictionaryValue* dict, |
| 176 CandidatePrerenderInfo* info, |
| 177 bool* hinting_timed_out, |
| 178 bool* hinting_url_lookup_timed_out, |
| 179 bool* candidate_url_lookup_timed_out); |
| 180 typedef std::map<net::URLFetcher*, CandidatePrerenderInfo*> |
| 181 OutstandingFetchers; |
| 182 OutstandingFetchers outstanding_prerender_service_requests_; |
| 147 PrerenderManager* prerender_manager_; | 183 PrerenderManager* prerender_manager_; |
| 148 base::OneShotTimer<PrerenderLocalPredictor> timer_; | 184 base::OneShotTimer<PrerenderLocalPredictor> timer_; |
| 149 | 185 |
| 150 // Delay after which to initialize, to avoid putting to much load on the | 186 // Delay after which to initialize, to avoid putting to much load on the |
| 151 // database thread early on when Chrome is starting up. | 187 // database thread early on when Chrome is starting up. |
| 152 static const int kInitDelayMs = 5 * 1000; | 188 static const int kInitDelayMs = 5 * 1000; |
| 153 | 189 |
| 154 // Whether we're registered with the history service as a | 190 // Whether we're registered with the history service as a |
| 155 // history::VisitDatabaseObserver. | 191 // history::VisitDatabaseObserver. |
| 156 bool is_visit_database_observer_; | 192 bool is_visit_database_observer_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 167 base::hash_set<int64> url_whitelist_; | 203 base::hash_set<int64> url_whitelist_; |
| 168 | 204 |
| 169 base::WeakPtrFactory<PrerenderLocalPredictor> weak_factory_; | 205 base::WeakPtrFactory<PrerenderLocalPredictor> weak_factory_; |
| 170 | 206 |
| 171 DISALLOW_COPY_AND_ASSIGN(PrerenderLocalPredictor); | 207 DISALLOW_COPY_AND_ASSIGN(PrerenderLocalPredictor); |
| 172 }; | 208 }; |
| 173 | 209 |
| 174 } // namespace prerender | 210 } // namespace prerender |
| 175 | 211 |
| 176 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ | 212 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_ |
| OLD | NEW |