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/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.
150 // |creator_child_id| is the id of the child process that caused the prerender
151 // to be created, and is needed so that the prerendered URLs can be sent to it
152 // so render-initiated navigations will swap in the prerendered page. |size|
153 // indicates the rectangular dimensions that the prerendered page should be.
154 // |session_storage_namespace| indicates the namespace that the prerendered
155 // page should be part of.
156 virtual void StartPrerendering(
157 int creator_child_id
,
158 const gfx::Size
& size
,
159 content::SessionStorageNamespace
* session_storage_namespace
,
160 net::URLRequestContextGetter
* request_context
);
162 // Verifies that the prerendering is not using too many resources, and kills
164 void DestroyWhenUsingTooManyResources();
166 content::RenderViewHost
* GetRenderViewHostMutable();
167 const content::RenderViewHost
* GetRenderViewHost() const;
169 PrerenderManager
* prerender_manager() { return prerender_manager_
; }
171 base::string16
title() const { return title_
; }
172 int32
page_id() const { return page_id_
; }
173 const GURL
& prerender_url() const { return prerender_url_
; }
174 const content::Referrer
& referrer() const { return referrer_
; }
175 bool has_stopped_loading() const { return has_stopped_loading_
; }
176 bool has_finished_loading() const { return has_finished_loading_
; }
177 bool prerendering_has_started() const { return prerendering_has_started_
; }
178 MatchCompleteStatus
match_complete_status() const {
179 return match_complete_status_
;
181 void set_match_complete_status(MatchCompleteStatus status
) {
182 match_complete_status_
= status
;
185 // Sets the parameter to the value of the associated RenderViewHost's child id
186 // and returns a boolean indicating the validity of that id.
187 virtual bool GetChildId(int* child_id
) const;
189 // Sets the parameter to the value of the associated RenderViewHost's route id
190 // and returns a boolean indicating the validity of that id.
191 virtual bool GetRouteId(int* route_id
) const;
193 FinalStatus
final_status() const { return final_status_
; }
195 Origin
origin() const { return origin_
; }
196 uint8
experiment_id() const { return experiment_id_
; }
197 int child_id() const { return child_id_
; }
199 base::TimeTicks
load_start_time() const { return load_start_time_
; }
201 // Indicates whether this prerendered page can be used for the provided
202 // |url| and |session_storage_namespace|.
205 const content::SessionStorageNamespace
* session_storage_namespace
) const;
207 // content::WebContentsObserver implementation.
208 void RenderFrameCreated(content::RenderFrameHost
* render_frame_host
) override
;
209 void DidStopLoading(content::RenderViewHost
* render_view_host
) override
;
210 void DocumentLoadedInFrame(
211 content::RenderFrameHost
* render_frame_host
) override
;
212 void DidStartProvisionalLoadForFrame(
213 content::RenderFrameHost
* render_frame_host
,
214 const GURL
& validated_url
,
216 bool is_iframe_srcdoc
) override
;
217 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
218 const GURL
& validated_url
) override
;
219 void DidNavigateMainFrame(
220 const content::LoadCommittedDetails
& details
,
221 const content::FrameNavigateParams
& params
) override
;
222 void DidGetRedirectForResourceRequest(
223 content::RenderFrameHost
* render_frame_host
,
224 const content::ResourceRedirectDetails
& details
) override
;
225 bool OnMessageReceived(const IPC::Message
& message
) override
;
227 void RenderProcessGone(base::TerminationStatus status
) override
;
229 // content::NotificationObserver
230 void Observe(int type
,
231 const content::NotificationSource
& source
,
232 const content::NotificationDetails
& details
) override
;
234 // Checks that a URL may be prerendered, for one of the many redirections. If
235 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
236 // be destroyed and false is returned. Otherwise, true is returned.
237 virtual bool CheckURL(const GURL
& url
);
239 // Adds an alias URL. If the URL can not be prerendered, |this| will be
240 // destroyed and false is returned.
241 bool AddAliasURL(const GURL
& url
);
243 // The prerender WebContents (may be NULL).
244 content::WebContents
* prerender_contents() const {
245 return prerender_contents_
.get();
248 content::WebContents
* ReleasePrerenderContents();
250 // Sets the final status, calls OnDestroy and adds |this| to the
251 // PrerenderManager's pending deletes list.
252 void Destroy(FinalStatus reason
);
254 // Called by the history tab helper with the information that it woudl have
255 // added to the history service had this web contents not been used for
257 void DidNavigate(const history::HistoryAddPageArgs
& add_page_args
);
259 // Applies all the URL history encountered during prerendering to the
261 void CommitHistory(content::WebContents
* tab
);
263 base::Value
* GetAsValue() const;
265 // Returns whether a pending cross-site navigation is happening.
266 // This could happen with renderer-issued navigations, such as a
267 // MouseEvent being dispatched by a link to a website installed as an app.
268 bool IsCrossSiteNavigationPending() const;
270 // Marks prerender as used and releases any throttled resource requests.
271 void PrepareForUse();
273 content::SessionStorageNamespace
* GetSessionStorageNamespace() const;
277 COOKIE_EVENT_SEND
= 0,
278 COOKIE_EVENT_CHANGE
= 1,
282 // Record a cookie transaction for this prerender contents.
283 // In the event of cookies being sent, |earliest_create_date| contains
284 // the time that the earliest of the cookies sent was created.
285 void RecordCookieEvent(CookieEvent event
,
286 bool is_main_frame_http_request
,
287 bool is_third_party_cookie
,
288 bool is_for_blocking_resource
,
289 base::Time earliest_create_date
);
291 static const int kNumCookieStatuses
;
292 static const int kNumCookieSendTypes
;
294 // Called when a PrerenderResourceThrottle defers a request. If the prerender
295 // is used it'll be resumed on the IO thread, otherwise they will get
296 // cancelled automatically if prerendering is cancelled.
297 void AddResourceThrottle(
298 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
);
300 // Increments the number of bytes fetched over the network for this prerender.
301 void AddNetworkBytes(int64 bytes
);
304 PrerenderContents(PrerenderManager
* prerender_manager
,
307 const content::Referrer
& referrer
,
309 uint8 experiment_id
);
311 // Set the final status for how the PrerenderContents was used. This
312 // should only be called once, and should be called before the prerender
313 // contents are destroyed.
314 void SetFinalStatus(FinalStatus final_status
);
316 // These call out to methods on our Observers, using our observer_list_. Note
317 // that NotifyPrerenderStop() also clears the observer list.
318 void NotifyPrerenderStart();
319 void NotifyPrerenderStopLoading();
320 void NotifyPrerenderDomContentLoaded();
321 void NotifyPrerenderStop();
322 void NotifyPrerenderCreatedMatchCompleteReplacement(
323 PrerenderContents
* replacement
);
325 // Called whenever a RenderViewHost is created for prerendering. Only called
326 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
327 virtual void OnRenderViewHostCreated(
328 content::RenderViewHost
* new_render_view_host
);
330 content::NotificationRegistrar
& notification_registrar() {
331 return notification_registrar_
;
334 bool prerendering_has_been_cancelled() const {
335 return prerendering_has_been_cancelled_
;
338 content::WebContents
* CreateWebContents(
339 content::SessionStorageNamespace
* session_storage_namespace
);
341 bool prerendering_has_started_
;
343 // Time at which we started to load the URL. This is used to compute
344 // the time elapsed from initiating a prerender until the time the
345 // (potentially only partially) prerendered page is shown to the user.
346 base::TimeTicks load_start_time_
;
348 // The prerendered WebContents; may be null.
349 scoped_ptr
<content::WebContents
> prerender_contents_
;
351 // The session storage namespace id for use in matching. We must save it
352 // rather than get it from the RenderViewHost since in the control group
353 // we won't have a RenderViewHost.
354 int64 session_storage_namespace_id_
;
356 // The time at which we started prerendering, for the purpose of comparing
357 // cookie creation times.
358 base::Time start_time_
;
361 class WebContentsDelegateImpl
;
363 // Needs to be able to call the constructor.
364 friend class PrerenderContentsFactoryImpl
;
366 // Returns the ProcessMetrics for the render process, if it exists.
367 base::ProcessMetrics
* MaybeGetProcessMetrics();
370 void OnCancelPrerenderForPrinting();
372 ObserverList
<Observer
> observer_list_
;
374 // The prerender manager owning this object.
375 PrerenderManager
* prerender_manager_
;
377 // The URL being prerendered.
381 content::Referrer referrer_
;
383 // The profile being used
386 // Information about the title and URL of the page that this class as a
387 // RenderViewHostDelegate has received from the RenderView.
388 // Used to apply to the new RenderViewHost delegate that might eventually
389 // own the contained RenderViewHost when the prerendered page is shown
391 base::string16 title_
;
394 content::NotificationRegistrar notification_registrar_
;
396 // A vector of URLs that this prerendered page matches against.
397 // This array can contain more than element as a result of redirects,
398 // such as HTTP redirects or javascript redirects.
399 std::vector
<GURL
> alias_urls_
;
401 bool has_stopped_loading_
;
403 // True when the main frame has finished loading.
404 bool has_finished_loading_
;
406 // This must be the same value as the PrerenderTracker has recorded for
407 // |this|, when |this| has a RenderView.
408 FinalStatus final_status_
;
410 // The MatchComplete status of the prerender, indicating how it relates
411 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
413 MatchCompleteStatus match_complete_status_
;
415 // Tracks whether or not prerendering has been cancelled by calling Destroy.
416 // Used solely to prevent double deletion.
417 bool prerendering_has_been_cancelled_
;
419 // Process Metrics of the render process associated with the
420 // RenderViewHost for this object.
421 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
423 scoped_ptr
<WebContentsDelegateImpl
> web_contents_delegate_
;
425 // These are -1 before a RenderView is created.
429 // Origin for this prerender.
432 // Experiment during which this prerender is performed.
433 uint8 experiment_id_
;
435 // The process that created the child id.
436 int creator_child_id_
;
438 // The size of the WebView from the launching page.
441 typedef std::vector
<history::HistoryAddPageArgs
> AddPageVector
;
443 // Caches pages to be added to the history.
444 AddPageVector add_page_vector_
;
446 // The alias session storage namespace for this prerender.
447 scoped_refptr
<content::SessionStorageNamespace
>
448 alias_session_storage_namespace
;
450 // Indicates what internal cookie events (see prerender_contents.cc) have
451 // occurred, using 1 bit for each possible InternalCookieEvent.
454 // Indicates whether existing cookies were sent for this prerender, and
455 // whether they were third-party cookies, and whether they were for blocking
456 // resources. See the enum CookieSendType in prerender_contents.cc
457 int cookie_send_type_
;
459 // Resources that are throttled, pending a prerender use. Can only access a
460 // throttle on the IO thread.
461 std::vector
<base::WeakPtr
<PrerenderResourceThrottle
> > resource_throttles_
;
463 // A running tally of the number of bytes this prerender has caused to be
464 // transferred over the network for resources. Updated with AddNetworkBytes.
465 int64 network_bytes_
;
467 DISALLOW_COPY_AND_ASSIGN(PrerenderContents
);
470 } // namespace prerender
472 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_