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