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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "base/task/cancelable_task_tracker.h"
16 #include "base/timer/timer.h"
17 #include "components/history/core/browser/history_service_observer.h"
18 #include "components/history/core/browser/visit_database.h"
19 #include "net/url_request/url_fetcher_delegate.h"
23 class DictionaryValue
;
40 class PrerenderHandle
;
41 class PrerenderManager
;
43 // PrerenderLocalPredictor maintains local browsing history to make prerender
45 // At this point, the class is not actually creating prerenders, but just
46 // recording timing stats about the effect prerendering would have.
48 // TODO(davidben): Prune this class down to only what it's still being used for.
49 class PrerenderLocalPredictor
: public history::HistoryServiceObserver
,
50 public net::URLFetcherDelegate
{
52 struct LocalPredictorURLInfo
;
53 struct CandidatePrerenderInfo
;
54 // A class simulating a set of URLs prefetched, for statistical purposes.
57 EVENT_CONSTRUCTED
= 0,
58 EVENT_INIT_SCHEDULED
= 1,
59 EVENT_INIT_STARTED
= 2,
60 EVENT_INIT_FAILED_NO_HISTORY
= 3,
61 EVENT_INIT_SUCCEEDED
= 4,
63 EVENT_ADD_VISIT_INITIALIZED
= 6,
64 EVENT_ADD_VISIT_PRERENDER_IDENTIFIED
= 7,
65 EVENT_ADD_VISIT_RELEVANT_TRANSITION
= 8,
66 EVENT_ADD_VISIT_IDENTIFIED_PRERENDER_CANDIDATE
= 9,
67 EVENT_ADD_VISIT_PRERENDERING
= 10,
68 // Obsolete: EVENT_GOT_PRERENDER_URL = 11,
69 EVENT_ERROR_NO_PRERENDER_URL_FOR_PLT
= 12,
70 EVENT_ADD_VISIT_PRERENDERING_EXTENDED
= 13,
71 EVENT_PRERENDER_URL_LOOKUP_RESULT
= 14,
72 EVENT_PRERENDER_URL_LOOKUP_RESULT_ROOT_PAGE
= 15,
73 EVENT_PRERENDER_URL_LOOKUP_RESULT_IS_HTTP
= 16,
74 EVENT_PRERENDER_URL_LOOKUP_RESULT_HAS_QUERY_STRING
= 17,
75 EVENT_PRERENDER_URL_LOOKUP_RESULT_CONTAINS_LOGOUT
= 18,
76 EVENT_PRERENDER_URL_LOOKUP_RESULT_CONTAINS_LOGIN
= 19,
77 EVENT_START_URL_LOOKUP
= 20,
78 // Obsolete: EVENT_ADD_VISIT_NOT_ROOTPAGE = 21,
79 EVENT_URL_WHITELIST_ERROR
= 22,
80 EVENT_URL_WHITELIST_OK
= 23,
81 EVENT_PRERENDER_URL_LOOKUP_RESULT_ON_WHITELIST
= 24,
82 EVENT_PRERENDER_URL_LOOKUP_RESULT_ON_WHITELIST_ROOT_PAGE
= 25,
83 EVENT_PRERENDER_URL_LOOKUP_RESULT_EXTENDED_ROOT_PAGE
= 26,
84 EVENT_PRERENDER_URL_LOOKUP_RESULT_ROOT_PAGE_HTTP
= 27,
85 EVENT_PRERENDER_URL_LOOKUP_FAILED
= 28,
86 EVENT_PRERENDER_URL_LOOKUP_NO_SOURCE_WEBCONTENTS_FOUND
= 29,
87 EVENT_PRERENDER_URL_LOOKUP_NO_LOGGED_IN_TABLE_FOUND
= 30,
88 EVENT_PRERENDER_URL_LOOKUP_ISSUING_LOGGED_IN_LOOKUP
= 31,
89 EVENT_CONTINUE_PRERENDER_CHECK_STARTED
= 32,
90 EVENT_CONTINUE_PRERENDER_CHECK_NO_URL
= 33,
91 EVENT_CONTINUE_PRERENDER_CHECK_PRIORITY_TOO_LOW
= 34,
92 EVENT_CONTINUE_PRERENDER_CHECK_URLS_IDENTICAL_BUT_FRAGMENT
= 35,
93 EVENT_CONTINUE_PRERENDER_CHECK_HTTPS
= 36,
94 EVENT_CONTINUE_PRERENDER_CHECK_ROOT_PAGE
= 37,
95 EVENT_CONTINUE_PRERENDER_CHECK_LOGOUT_URL
= 38,
96 EVENT_CONTINUE_PRERENDER_CHECK_LOGIN_URL
= 39,
97 EVENT_CONTINUE_PRERENDER_CHECK_NOT_LOGGED_IN
= 40,
98 EVENT_CONTINUE_PRERENDER_CHECK_FALLTHROUGH_NOT_PRERENDERING
= 41,
99 EVENT_CONTINUE_PRERENDER_CHECK_ISSUING_PRERENDER
= 42,
100 EVENT_ISSUING_PRERENDER
= 43,
101 EVENT_NO_PRERENDER_CANDIDATES
= 44,
102 EVENT_GOT_HISTORY_ISSUING_LOOKUP
= 45,
103 EVENT_TAB_HELPER_URL_SEEN
= 46,
104 EVENT_TAB_HELPER_URL_SEEN_MATCH
= 47,
105 EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MATCH
= 48,
106 EVENT_PRERENDER_URL_LOOKUP_MULTIPLE_SOURCE_WEBCONTENTS_FOUND
= 49,
107 EVENT_CONTINUE_PRERENDER_CHECK_ON_SIDE_EFFECT_FREE_WHITELIST
= 50,
108 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL
= 51,
109 EVENT_ISSUE_PRERENDER_ALREADY_PRERENDERING
= 52,
110 EVENT_ISSUE_PRERENDER_NEW_PRERENDER
= 53,
111 EVENT_ISSUE_PRERENDER_CANCELLED_OLD_PRERENDER
= 54,
112 EVENT_CONTINUE_PRERENDER_CHECK_FALLTHROUGH_PRERENDERING
= 55,
113 EVENT_PRERENDER_URL_LOOKUP_SUCCESS
= 56,
114 EVENT_PRERENDER_SERVICE_DISABLED
= 57,
115 EVENT_PRERENDER_SERVICE_ISSUED_LOOKUP
= 58,
116 EVENT_PRERENDER_SERVICE_LOOKUP_TIMED_OUT
= 59,
117 EVENT_PRERENDER_SERVICE_RECEIVED_RESULT
= 60,
118 EVENT_PRERENDER_SERVICE_NO_RECORD_FOR_RESULT
= 61,
119 EVENT_PRERENDER_SERVICE_PARSED_CORRECTLY
= 62,
120 EVENT_PRERENDER_SERVICE_PARSE_ERROR
= 63,
121 EVENT_PRERENDER_SERVICE_PARSE_ERROR_INCORRECT_JSON
= 64,
122 EVENT_PRERENDER_SERVICE_HINTING_TIMED_OUT
= 65,
123 EVENT_PRERENDER_SERVICE_HINTING_URL_LOOKUP_TIMED_OUT
= 66,
124 EVENT_PRERENDER_SERVICE_CANDIDATE_URL_LOOKUP_TIMED_OUT
= 67,
125 EVENT_CONTINUE_PRERENDER_CHECK_ON_SERVICE_WHITELIST
= 68,
126 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL_LOCAL
= 69,
127 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL_SERVICE
= 70,
128 EVENT_ADD_VISIT_RELEVANT_TRANSITION_REPEAT_URL
= 71,
129 EVENT_ADD_VISIT_RELEVANT_TRANSITION_NEW_URL
= 72,
130 // Obsolete: EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MISMATCH_NO_NAMESPACE = 73,
131 // Obsolete: EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MISMATCH_MERGE_ISSUED = 74,
132 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_RECEIVED = 75,
133 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_NAMESPACE_NOT_FOUND = 76,
134 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_NOT_LOGGING = 77,
135 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_NO_TRANSACTIONS = 78,
136 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_TOO_MANY_TRANSACTIONS = 79,
137 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_NOT_MERGEABLE = 80,
138 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_MERGEABLE = 81,
139 EVENT_INIT_FAILED_UNENCRYPTED_SYNC_NOT_ENABLED
= 82,
140 EVENT_CONTINUE_PRERENDER_CHECK_EXAMINE_NEXT_URL_NOT_SKIPPED
= 83,
141 EVENT_PRERENDER_SERVICE_RETURNED_HINTING_CANDIDATES
= 84,
142 // Obsolete: EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_NAMESPACE_NOT_ALIAS = 85,
143 EVENT_TAB_HELPER_URL_SEEN_MATCH_ENTRY
= 86,
144 EVENT_TAB_HELPER_URL_SEEN_MATCH_BROWSER_NAVIGATE
= 87,
145 EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MATCH_ENTRY
= 88,
146 EVENT_TAB_HELPER_URL_SEEN_NAMESPACE_MATCH_BROWSER_NAVIGATE
= 89,
147 EVENT_PREFETCH_LIST_ADDED
= 90,
148 EVENT_PREFETCH_LIST_SEEN_TABCONTENTS
= 91,
149 EVENT_PREFETCH_LIST_SEEN_HISTORY
= 92,
150 EVENT_ISSUE_PRERENDER_CALLED
= 93,
151 EVENT_ISSUE_PRERENDER_PREFETCH_ENABLED
= 94,
152 EVENT_ISSUE_PRERENDER_PREFETCH_ISSUED
= 95,
156 // A PrerenderLocalPredictor is owned by the PrerenderManager specified
157 // in the constructor. It will be destoryed at the time its owning
158 // PrerenderManager is destroyed.
159 explicit PrerenderLocalPredictor(PrerenderManager
* prerender_manager
);
160 ~PrerenderLocalPredictor() override
;
164 // history::HistoryServiceObserver:
165 void OnAddVisit(history::HistoryService
* history_service
,
166 const history::BriefVisitInfo
& info
) override
;
168 void OnGetInitialVisitHistory(
169 scoped_ptr
<std::vector
<history::BriefVisitInfo
> > visit_history
);
171 void OnPLTEventForURL(const GURL
& url
, base::TimeDelta page_load_time
);
173 void OnTabHelperURLSeen(const GURL
& url
, content::WebContents
* web_contents
);
175 // net::URLFetcherDelegate implementation:
176 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
179 struct PrerenderProperties
;
180 history::HistoryService
* GetHistoryIfExists() const;
182 bool IsPrerenderStillValid(PrerenderProperties
* prerender
) const;
183 bool DoesPrerenderMatchPLTRecord(PrerenderProperties
* prerender
,
185 base::TimeDelta plt
) const;
186 void RecordEvent(Event event
) const;
188 void OnLookupURL(scoped_ptr
<CandidatePrerenderInfo
> info
);
190 // Lookup the prerender candidate in the Prerender Service (if applicable).
191 void DoPrerenderServiceCheck(scoped_ptr
<CandidatePrerenderInfo
> info
);
193 // Lookup the prerender candidate in the LoggedIn Predictor.
194 void DoLoggedInLookup(scoped_ptr
<CandidatePrerenderInfo
> info
);
196 // Returns an element of issued_prerenders_, which should be replaced
197 // by a new prerender of the priority indicated, or NULL, if the priority
198 // is too low (or if the URL requested is already prerendering).
199 PrerenderProperties
* GetIssuedPrerenderSlotForPriority(const GURL
& url
,
202 void ContinuePrerenderCheck(scoped_ptr
<CandidatePrerenderInfo
> info
);
203 void LogCandidateURLStats(const GURL
& url
) const;
204 void IssuePrerender(CandidatePrerenderInfo
* info
,
205 LocalPredictorURLInfo
* url_info
);
206 void MaybeCancelURLFetcher(net::URLFetcher
* fetcher
);
207 // Returns true if the parsed response is semantically correct and could
209 bool ApplyParsedPrerenderServiceResponse(
210 base::DictionaryValue
* dict
,
211 CandidatePrerenderInfo
* info
,
212 bool* hinting_timed_out
,
213 bool* hinting_url_lookup_timed_out
,
214 bool* candidate_url_lookup_timed_out
);
215 typedef std::map
<net::URLFetcher
*, CandidatePrerenderInfo
*>
217 OutstandingFetchers outstanding_prerender_service_requests_
;
218 PrerenderManager
* prerender_manager_
;
219 base::OneShotTimer
<PrerenderLocalPredictor
> timer_
;
221 // Delay after which to initialize, to avoid putting to much load on the
222 // database thread early on when Chrome is starting up.
223 static const int kInitDelayMs
= 5 * 1000;
225 base::CancelableTaskTracker history_db_tracker_
;
227 scoped_ptr
<std::vector
<history::BriefVisitInfo
> > visit_history_
;
229 scoped_ptr
<PrerenderProperties
> current_prerender_
;
230 scoped_ptr
<PrerenderProperties
> last_swapped_in_prerender_
;
232 ScopedVector
<PrerenderProperties
> issued_prerenders_
;
234 base::hash_set
<int64
> url_whitelist_
;
236 scoped_ptr
<PrefetchList
> prefetch_list_
;
238 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
239 history_service_observer_
;
241 base::WeakPtrFactory
<PrerenderLocalPredictor
> weak_factory_
;
243 DISALLOW_COPY_AND_ASSIGN(PrerenderLocalPredictor
);
246 } // namespace prerender
248 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_LOCAL_PREDICTOR_H_