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_CONTENTS_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "chrome/browser/prerender/prerender_final_status.h"
19 #include "chrome/browser/prerender/prerender_origin.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/common/referrer.h"
24 #include "ui/gfx/geometry/size.h"
34 class SessionStorageNamespace
;
39 struct HistoryAddPageArgs
;
43 class URLRequestContextGetter
;
48 class PrerenderHandle
;
49 class PrerenderManager
;
50 class PrerenderResourceThrottle
;
52 class PrerenderContents
: public content::NotificationObserver
,
53 public content::WebContentsObserver
,
54 public base::SupportsWeakPtr
<PrerenderContents
> {
56 // PrerenderContents::Create uses the currently registered Factory to create
57 // the PrerenderContents. Factory is intended for testing.
63 // Ownership is not transfered through this interface as prerender_manager,
64 // prerender_tracker, and profile are stored as weak pointers.
65 virtual PrerenderContents
* CreatePrerenderContents(
66 PrerenderManager
* prerender_manager
,
69 const content::Referrer
& referrer
,
71 uint8 experiment_id
) = 0;
74 DISALLOW_COPY_AND_ASSIGN(Factory
);
79 // Signals that the prerender has started running.
80 virtual void OnPrerenderStart(PrerenderContents
* contents
) = 0;
82 // Signals that the prerender has had its load event.
83 virtual void OnPrerenderStopLoading(PrerenderContents
* contents
);
85 // Signals that the prerender has had its 'DOMContentLoaded' event.
86 virtual void OnPrerenderDomContentLoaded(PrerenderContents
* contents
);
88 // Signals that the prerender has stopped running. A PrerenderContents with
89 // an unset final status will always call OnPrerenderStop before being
91 virtual void OnPrerenderStop(PrerenderContents
* contents
) = 0;
93 // Signals that this prerender has just become a MatchComplete replacement.
94 virtual void OnPrerenderCreatedMatchCompleteReplacement(
95 PrerenderContents
* contents
, PrerenderContents
* replacement
);
99 virtual ~Observer() = 0;
102 // Indicates how this PrerenderContents relates to MatchComplete. This is to
103 // figure out which histograms to use to record the FinalStatus, Match (record
104 // all prerenders and control group prerenders) or MatchComplete (record
105 // running prerenders only in the way they would have been recorded in the
107 enum MatchCompleteStatus
{
108 // A regular prerender which will be recorded both in Match and
110 MATCH_COMPLETE_DEFAULT
,
111 // A prerender that used to be a regular prerender, but has since been
112 // replaced by a MatchComplete dummy. Therefore, we will record this only
113 // for Match, but not for MatchComplete.
114 MATCH_COMPLETE_REPLACED
,
115 // A prerender that is a MatchComplete dummy replacing a regular prerender.
116 // In the control group, our prerender never would have been canceled, so
117 // we record in MatchComplete but not Match.
118 MATCH_COMPLETE_REPLACEMENT
,
119 // A prerender that is a MatchComplete dummy, early in the process of being
120 // created. This prerender should not fail. Record for MatchComplete, but
122 MATCH_COMPLETE_REPLACEMENT_PENDING
,
125 ~PrerenderContents() override
;
127 // All observers of a PrerenderContents are removed after the OnPrerenderStop
128 // event is sent, so there is no need to call RemoveObserver() in the normal
130 void AddObserver(Observer
* observer
);
131 void RemoveObserver(Observer
* observer
);
133 // For MatchComplete correctness, create a dummy replacement prerender
134 // contents to stand in for this prerender contents that (which we are about
136 PrerenderContents
* CreateMatchCompleteReplacement();
140 static Factory
* CreateFactory();
142 // Returns a PrerenderContents from the given web_contents, if it's used for
143 // prerendering. Otherwise returns NULL. Handles a NULL input for
145 static PrerenderContents
* FromWebContents(content::WebContents
* web_contents
);
147 // Start rendering the contents in the prerendered state. If
148 // |is_control_group| is true, this will go through some of the mechanics of
149 // starting a prerender, without actually creating the RenderView. |size|
150 // indicates the rectangular dimensions that the prerendered page should be.
151 // |session_storage_namespace| indicates the namespace that the prerendered
152 // page should be part of.
153 virtual void StartPrerendering(
154 const gfx::Size
& size
,
155 content::SessionStorageNamespace
* session_storage_namespace
,
156 net::URLRequestContextGetter
* request_context
);
158 // Verifies that the prerendering is not using too many resources, and kills
160 void DestroyWhenUsingTooManyResources();
162 content::RenderViewHost
* GetRenderViewHostMutable();
163 const content::RenderViewHost
* GetRenderViewHost() const;
165 PrerenderManager
* prerender_manager() { return prerender_manager_
; }
167 base::string16
title() const { return title_
; }
168 int32
page_id() const { return page_id_
; }
169 const GURL
& prerender_url() const { return prerender_url_
; }
170 const content::Referrer
& referrer() const { return referrer_
; }
171 bool has_stopped_loading() const { return has_stopped_loading_
; }
172 bool has_finished_loading() const { return has_finished_loading_
; }
173 bool prerendering_has_started() const { return prerendering_has_started_
; }
174 MatchCompleteStatus
match_complete_status() const {
175 return match_complete_status_
;
177 void set_match_complete_status(MatchCompleteStatus status
) {
178 match_complete_status_
= status
;
181 // Sets the parameter to the value of the associated RenderViewHost's child id
182 // and returns a boolean indicating the validity of that id.
183 virtual bool GetChildId(int* child_id
) const;
185 // Sets the parameter to the value of the associated RenderViewHost's route id
186 // and returns a boolean indicating the validity of that id.
187 virtual bool GetRouteId(int* route_id
) const;
189 FinalStatus
final_status() const { return final_status_
; }
191 Origin
origin() const { return origin_
; }
192 uint8
experiment_id() const { return experiment_id_
; }
194 base::TimeTicks
load_start_time() const { return load_start_time_
; }
196 // Indicates whether this prerendered page can be used for the provided
197 // |url| and |session_storage_namespace|.
200 const content::SessionStorageNamespace
* session_storage_namespace
) const;
202 // content::WebContentsObserver implementation.
203 void RenderFrameCreated(content::RenderFrameHost
* render_frame_host
) override
;
204 void DidStopLoading(content::RenderViewHost
* render_view_host
) override
;
205 void DocumentLoadedInFrame(
206 content::RenderFrameHost
* render_frame_host
) override
;
207 void DidStartProvisionalLoadForFrame(
208 content::RenderFrameHost
* render_frame_host
,
209 const GURL
& validated_url
,
211 bool is_iframe_srcdoc
) override
;
212 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
213 const GURL
& validated_url
) override
;
214 void DidNavigateMainFrame(
215 const content::LoadCommittedDetails
& details
,
216 const content::FrameNavigateParams
& params
) override
;
217 void DidGetRedirectForResourceRequest(
218 content::RenderFrameHost
* render_frame_host
,
219 const content::ResourceRedirectDetails
& details
) override
;
220 bool OnMessageReceived(const IPC::Message
& message
) override
;
222 void RenderProcessGone(base::TerminationStatus status
) override
;
224 // content::NotificationObserver
225 void Observe(int type
,
226 const content::NotificationSource
& source
,
227 const content::NotificationDetails
& details
) override
;
229 // Checks that a URL may be prerendered, for one of the many redirections. If
230 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
231 // be destroyed and false is returned. Otherwise, true is returned.
232 virtual bool CheckURL(const GURL
& url
);
234 // Adds an alias URL. If the URL can not be prerendered, |this| will be
235 // destroyed and false is returned.
236 bool AddAliasURL(const GURL
& url
);
238 // The prerender WebContents (may be NULL).
239 content::WebContents
* prerender_contents() const {
240 return prerender_contents_
.get();
243 content::WebContents
* ReleasePrerenderContents();
245 // Sets the final status, calls OnDestroy and adds |this| to the
246 // PrerenderManager's pending deletes list.
247 void Destroy(FinalStatus reason
);
249 // Called by the history tab helper with the information that it woudl have
250 // added to the history service had this web contents not been used for
252 void DidNavigate(const history::HistoryAddPageArgs
& add_page_args
);
254 // Applies all the URL history encountered during prerendering to the
256 void CommitHistory(content::WebContents
* tab
);
258 base::Value
* GetAsValue() const;
260 // Returns whether a pending cross-site navigation is happening.
261 // This could happen with renderer-issued navigations, such as a
262 // MouseEvent being dispatched by a link to a website installed as an app.
263 bool IsCrossSiteNavigationPending() const;
265 // Marks prerender as used and releases any throttled resource requests.
266 void PrepareForUse();
270 COOKIE_EVENT_SEND
= 0,
271 COOKIE_EVENT_CHANGE
= 1,
275 // Record a cookie transaction for this prerender contents.
276 // In the event of cookies being sent, |earliest_create_date| contains
277 // the time that the earliest of the cookies sent was created.
278 void RecordCookieEvent(CookieEvent event
,
279 bool is_main_frame_http_request
,
280 bool is_third_party_cookie
,
281 bool is_for_blocking_resource
,
282 base::Time earliest_create_date
);
284 static const int kNumCookieStatuses
;
285 static const int kNumCookieSendTypes
;
287 // Called when a PrerenderResourceThrottle defers a request. If the prerender
288 // is used it'll be resumed on the IO thread, otherwise they will get
289 // cancelled automatically if prerendering is cancelled.
290 void AddResourceThrottle(
291 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
);
293 // Increments the number of bytes fetched over the network for this prerender.
294 void AddNetworkBytes(int64 bytes
);
297 PrerenderContents(PrerenderManager
* prerender_manager
,
300 const content::Referrer
& referrer
,
302 uint8 experiment_id
);
304 // Set the final status for how the PrerenderContents was used. This
305 // should only be called once, and should be called before the prerender
306 // contents are destroyed.
307 void SetFinalStatus(FinalStatus final_status
);
309 // These call out to methods on our Observers, using our observer_list_. Note
310 // that NotifyPrerenderStop() also clears the observer list.
311 void NotifyPrerenderStart();
312 void NotifyPrerenderStopLoading();
313 void NotifyPrerenderDomContentLoaded();
314 void NotifyPrerenderStop();
315 void NotifyPrerenderCreatedMatchCompleteReplacement(
316 PrerenderContents
* replacement
);
318 // Called whenever a RenderViewHost is created for prerendering. Only called
319 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
320 virtual void OnRenderViewHostCreated(
321 content::RenderViewHost
* new_render_view_host
);
323 content::NotificationRegistrar
& notification_registrar() {
324 return notification_registrar_
;
327 bool prerendering_has_been_cancelled() const {
328 return prerendering_has_been_cancelled_
;
331 content::WebContents
* CreateWebContents(
332 content::SessionStorageNamespace
* session_storage_namespace
);
334 bool prerendering_has_started_
;
336 // Time at which we started to load the URL. This is used to compute
337 // the time elapsed from initiating a prerender until the time the
338 // (potentially only partially) prerendered page is shown to the user.
339 base::TimeTicks load_start_time_
;
341 // The prerendered WebContents; may be null.
342 scoped_ptr
<content::WebContents
> prerender_contents_
;
344 // The session storage namespace id for use in matching. We must save it
345 // rather than get it from the RenderViewHost since in the control group
346 // we won't have a RenderViewHost.
347 int64 session_storage_namespace_id_
;
349 // The time at which we started prerendering, for the purpose of comparing
350 // cookie creation times.
351 base::Time start_time_
;
354 class WebContentsDelegateImpl
;
356 // Needs to be able to call the constructor.
357 friend class PrerenderContentsFactoryImpl
;
359 // Returns the ProcessMetrics for the render process, if it exists.
360 base::ProcessMetrics
* MaybeGetProcessMetrics();
363 void OnCancelPrerenderForPrinting();
365 ObserverList
<Observer
> observer_list_
;
367 // The prerender manager owning this object.
368 PrerenderManager
* prerender_manager_
;
370 // The URL being prerendered.
374 content::Referrer referrer_
;
376 // The profile being used
379 // Information about the title and URL of the page that this class as a
380 // RenderViewHostDelegate has received from the RenderView.
381 // Used to apply to the new RenderViewHost delegate that might eventually
382 // own the contained RenderViewHost when the prerendered page is shown
384 base::string16 title_
;
387 content::NotificationRegistrar notification_registrar_
;
389 // A vector of URLs that this prerendered page matches against.
390 // This array can contain more than element as a result of redirects,
391 // such as HTTP redirects or javascript redirects.
392 std::vector
<GURL
> alias_urls_
;
394 bool has_stopped_loading_
;
396 // True when the main frame has finished loading.
397 bool has_finished_loading_
;
399 // This must be the same value as the PrerenderTracker has recorded for
400 // |this|, when |this| has a RenderView.
401 FinalStatus final_status_
;
403 // The MatchComplete status of the prerender, indicating how it relates
404 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
406 MatchCompleteStatus match_complete_status_
;
408 // Tracks whether or not prerendering has been cancelled by calling Destroy.
409 // Used solely to prevent double deletion.
410 bool prerendering_has_been_cancelled_
;
412 // Process Metrics of the render process associated with the
413 // RenderViewHost for this object.
414 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
416 scoped_ptr
<WebContentsDelegateImpl
> web_contents_delegate_
;
418 // These are -1 before a RenderView is created.
422 // Origin for this prerender.
425 // Experiment during which this prerender is performed.
426 uint8 experiment_id_
;
428 // The size of the WebView from the launching page.
431 typedef std::vector
<history::HistoryAddPageArgs
> AddPageVector
;
433 // Caches pages to be added to the history.
434 AddPageVector add_page_vector_
;
436 // Indicates what internal cookie events (see prerender_contents.cc) have
437 // occurred, using 1 bit for each possible InternalCookieEvent.
440 // Indicates whether existing cookies were sent for this prerender, and
441 // whether they were third-party cookies, and whether they were for blocking
442 // resources. See the enum CookieSendType in prerender_contents.cc
443 int cookie_send_type_
;
445 // Resources that are throttled, pending a prerender use. Can only access a
446 // throttle on the IO thread.
447 std::vector
<base::WeakPtr
<PrerenderResourceThrottle
> > resource_throttles_
;
449 // A running tally of the number of bytes this prerender has caused to be
450 // transferred over the network for resources. Updated with AddNetworkBytes.
451 int64 network_bytes_
;
453 DISALLOW_COPY_AND_ASSIGN(PrerenderContents
);
456 } // namespace prerender
458 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_