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
;
44 class PrerenderHandle
;
45 class PrerenderManager
;
46 class PrerenderResourceThrottle
;
48 class PrerenderContents
: public content::NotificationObserver
,
49 public content::WebContentsObserver
{
51 // PrerenderContents::Create uses the currently registered Factory to create
52 // the PrerenderContents. Factory is intended for testing.
58 // Ownership is not transfered through this interface as prerender_manager
59 // and profile are stored as weak pointers.
60 virtual PrerenderContents
* CreatePrerenderContents(
61 PrerenderManager
* prerender_manager
,
64 const content::Referrer
& referrer
,
68 DISALLOW_COPY_AND_ASSIGN(Factory
);
73 // Signals that the prerender has started running.
74 virtual void OnPrerenderStart(PrerenderContents
* contents
) = 0;
76 // Signals that the prerender has had its load event.
77 virtual void OnPrerenderStopLoading(PrerenderContents
* contents
);
79 // Signals that the prerender has had its 'DOMContentLoaded' event.
80 virtual void OnPrerenderDomContentLoaded(PrerenderContents
* contents
);
82 // Signals that the prerender has stopped running. A PrerenderContents with
83 // an unset final status will always call OnPrerenderStop before being
85 virtual void OnPrerenderStop(PrerenderContents
* contents
) = 0;
87 // Signals that this prerender has just become a MatchComplete replacement.
88 virtual void OnPrerenderCreatedMatchCompleteReplacement(
89 PrerenderContents
* contents
, PrerenderContents
* replacement
);
93 virtual ~Observer() = 0;
96 // Indicates how this PrerenderContents relates to MatchComplete. This is to
97 // figure out which histograms to use to record the FinalStatus, Match (record
98 // all prerenders and control group prerenders) or MatchComplete (record
99 // running prerenders only in the way they would have been recorded in the
101 enum MatchCompleteStatus
{
102 // A regular prerender which will be recorded both in Match and
104 MATCH_COMPLETE_DEFAULT
,
105 // A prerender that used to be a regular prerender, but has since been
106 // replaced by a MatchComplete dummy. Therefore, we will record this only
107 // for Match, but not for MatchComplete.
108 MATCH_COMPLETE_REPLACED
,
109 // A prerender that is a MatchComplete dummy replacing a regular prerender.
110 // In the control group, our prerender never would have been canceled, so
111 // we record in MatchComplete but not Match.
112 MATCH_COMPLETE_REPLACEMENT
,
113 // A prerender that is a MatchComplete dummy, early in the process of being
114 // created. This prerender should not fail. Record for MatchComplete, but
116 MATCH_COMPLETE_REPLACEMENT_PENDING
,
119 ~PrerenderContents() override
;
121 // All observers of a PrerenderContents are removed after the OnPrerenderStop
122 // event is sent, so there is no need to call RemoveObserver() in the normal
124 void AddObserver(Observer
* observer
);
125 void RemoveObserver(Observer
* observer
);
127 // For MatchComplete correctness, create a dummy replacement prerender
128 // contents to stand in for this prerender contents that (which we are about
130 PrerenderContents
* CreateMatchCompleteReplacement();
134 static Factory
* CreateFactory();
136 // Returns a PrerenderContents from the given web_contents, if it's used for
137 // prerendering. Otherwise returns NULL. Handles a NULL input for
139 static PrerenderContents
* FromWebContents(content::WebContents
* web_contents
);
141 // Start rendering the contents in the prerendered state. If
142 // |is_control_group| is true, this will go through some of the mechanics of
143 // starting a prerender, without actually creating the RenderView. |size|
144 // indicates the rectangular dimensions that the prerendered page should be.
145 // |session_storage_namespace| indicates the namespace that the prerendered
146 // page should be part of.
147 virtual void StartPrerendering(
148 const gfx::Size
& size
,
149 content::SessionStorageNamespace
* session_storage_namespace
);
151 // Verifies that the prerendering is not using too many resources, and kills
153 void DestroyWhenUsingTooManyResources();
155 content::RenderViewHost
* GetRenderViewHostMutable();
156 const content::RenderViewHost
* GetRenderViewHost() const;
158 PrerenderManager
* prerender_manager() { return prerender_manager_
; }
160 base::string16
title() const { return title_
; }
161 int32
page_id() const { return page_id_
; }
162 const GURL
& prerender_url() const { return prerender_url_
; }
163 const content::Referrer
& referrer() const { return referrer_
; }
164 bool has_stopped_loading() const { return has_stopped_loading_
; }
165 bool has_finished_loading() const { return has_finished_loading_
; }
166 bool prerendering_has_started() const { return prerendering_has_started_
; }
167 MatchCompleteStatus
match_complete_status() const {
168 return match_complete_status_
;
170 void set_match_complete_status(MatchCompleteStatus status
) {
171 match_complete_status_
= status
;
174 // Sets the parameter to the value of the associated RenderViewHost's child id
175 // and returns a boolean indicating the validity of that id.
176 virtual bool GetChildId(int* child_id
) const;
178 // Sets the parameter to the value of the associated RenderViewHost's route id
179 // and returns a boolean indicating the validity of that id.
180 virtual bool GetRouteId(int* route_id
) const;
182 FinalStatus
final_status() const { return final_status_
; }
184 Origin
origin() const { return origin_
; }
186 base::TimeTicks
load_start_time() const { return load_start_time_
; }
188 // Indicates whether this prerendered page can be used for the provided
189 // |url| and |session_storage_namespace|.
192 const content::SessionStorageNamespace
* session_storage_namespace
) const;
194 // content::WebContentsObserver implementation.
195 void RenderFrameCreated(content::RenderFrameHost
* render_frame_host
) override
;
196 void DidStopLoading() override
;
197 void DocumentLoadedInFrame(
198 content::RenderFrameHost
* render_frame_host
) override
;
199 void DidStartProvisionalLoadForFrame(
200 content::RenderFrameHost
* render_frame_host
,
201 const GURL
& validated_url
,
203 bool is_iframe_srcdoc
) override
;
204 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
205 const GURL
& validated_url
) override
;
206 void DidNavigateMainFrame(
207 const content::LoadCommittedDetails
& details
,
208 const content::FrameNavigateParams
& params
) override
;
209 void DidGetRedirectForResourceRequest(
210 content::RenderFrameHost
* render_frame_host
,
211 const content::ResourceRedirectDetails
& details
) override
;
212 bool OnMessageReceived(const IPC::Message
& message
) override
;
214 void RenderProcessGone(base::TerminationStatus status
) override
;
216 // content::NotificationObserver
217 void Observe(int type
,
218 const content::NotificationSource
& source
,
219 const content::NotificationDetails
& details
) override
;
221 // Checks that a URL may be prerendered, for one of the many redirections. If
222 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
223 // be destroyed and false is returned. Otherwise, true is returned.
224 virtual bool CheckURL(const GURL
& url
);
226 // Adds an alias URL. If the URL can not be prerendered, |this| will be
227 // destroyed and false is returned.
228 bool AddAliasURL(const GURL
& url
);
230 // The prerender WebContents (may be NULL).
231 content::WebContents
* prerender_contents() const {
232 return prerender_contents_
.get();
235 content::WebContents
* ReleasePrerenderContents();
237 // Sets the final status, calls OnDestroy and adds |this| to the
238 // PrerenderManager's pending deletes list.
239 void Destroy(FinalStatus reason
);
241 // Called by the history tab helper with the information that it woudl have
242 // added to the history service had this web contents not been used for
244 void DidNavigate(const history::HistoryAddPageArgs
& add_page_args
);
246 // Applies all the URL history encountered during prerendering to the
248 void CommitHistory(content::WebContents
* tab
);
250 base::Value
* GetAsValue() const;
252 // Returns whether a pending cross-site navigation is happening.
253 // This could happen with renderer-issued navigations, such as a
254 // MouseEvent being dispatched by a link to a website installed as an app.
255 bool IsCrossSiteNavigationPending() const;
257 // Marks prerender as used and releases any throttled resource requests.
258 void PrepareForUse();
260 // Called when a PrerenderResourceThrottle defers a request. If the prerender
261 // is used it'll be resumed on the IO thread, otherwise they will get
262 // cancelled automatically if prerendering is cancelled.
263 void AddResourceThrottle(
264 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
);
266 // Increments the number of bytes fetched over the network for this prerender.
267 void AddNetworkBytes(int64 bytes
);
270 PrerenderContents(PrerenderManager
* prerender_manager
,
273 const content::Referrer
& referrer
,
276 // Set the final status for how the PrerenderContents was used. This
277 // should only be called once, and should be called before the prerender
278 // contents are destroyed.
279 void SetFinalStatus(FinalStatus final_status
);
281 // These call out to methods on our Observers, using our observer_list_. Note
282 // that NotifyPrerenderStop() also clears the observer list.
283 void NotifyPrerenderStart();
284 void NotifyPrerenderStopLoading();
285 void NotifyPrerenderDomContentLoaded();
286 void NotifyPrerenderStop();
287 void NotifyPrerenderCreatedMatchCompleteReplacement(
288 PrerenderContents
* replacement
);
290 // Called whenever a RenderViewHost is created for prerendering. Only called
291 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
292 virtual void OnRenderViewHostCreated(
293 content::RenderViewHost
* new_render_view_host
);
295 content::NotificationRegistrar
& notification_registrar() {
296 return notification_registrar_
;
299 bool prerendering_has_been_cancelled() const {
300 return prerendering_has_been_cancelled_
;
303 content::WebContents
* CreateWebContents(
304 content::SessionStorageNamespace
* session_storage_namespace
);
306 bool prerendering_has_started_
;
308 // Time at which we started to load the URL. This is used to compute
309 // the time elapsed from initiating a prerender until the time the
310 // (potentially only partially) prerendered page is shown to the user.
311 base::TimeTicks load_start_time_
;
313 // The prerendered WebContents; may be null.
314 scoped_ptr
<content::WebContents
> prerender_contents_
;
316 // The session storage namespace id for use in matching. We must save it
317 // rather than get it from the RenderViewHost since in the control group
318 // we won't have a RenderViewHost.
319 int64 session_storage_namespace_id_
;
322 class WebContentsDelegateImpl
;
324 // Needs to be able to call the constructor.
325 friend class PrerenderContentsFactoryImpl
;
327 // Returns the ProcessMetrics for the render process, if it exists.
328 base::ProcessMetrics
* MaybeGetProcessMetrics();
331 void OnCancelPrerenderForPrinting();
333 base::ObserverList
<Observer
> observer_list_
;
335 // The prerender manager owning this object.
336 PrerenderManager
* prerender_manager_
;
338 // The URL being prerendered.
342 content::Referrer referrer_
;
344 // The profile being used
347 // Information about the title and URL of the page that this class as a
348 // RenderViewHostDelegate has received from the RenderView.
349 // Used to apply to the new RenderViewHost delegate that might eventually
350 // own the contained RenderViewHost when the prerendered page is shown
352 base::string16 title_
;
355 content::NotificationRegistrar notification_registrar_
;
357 // A vector of URLs that this prerendered page matches against.
358 // This array can contain more than element as a result of redirects,
359 // such as HTTP redirects or javascript redirects.
360 std::vector
<GURL
> alias_urls_
;
362 bool has_stopped_loading_
;
364 // True when the main frame has finished loading.
365 bool has_finished_loading_
;
367 FinalStatus final_status_
;
369 // The MatchComplete status of the prerender, indicating how it relates
370 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
372 MatchCompleteStatus match_complete_status_
;
374 // Tracks whether or not prerendering has been cancelled by calling Destroy.
375 // Used solely to prevent double deletion.
376 bool prerendering_has_been_cancelled_
;
378 // Process Metrics of the render process associated with the
379 // RenderViewHost for this object.
380 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
382 scoped_ptr
<WebContentsDelegateImpl
> web_contents_delegate_
;
384 // These are -1 before a RenderView is created.
388 // Origin for this prerender.
391 // The size of the WebView from the launching page.
394 typedef std::vector
<history::HistoryAddPageArgs
> AddPageVector
;
396 // Caches pages to be added to the history.
397 AddPageVector add_page_vector_
;
399 // Resources that are throttled, pending a prerender use. Can only access a
400 // throttle on the IO thread.
401 std::vector
<base::WeakPtr
<PrerenderResourceThrottle
> > resource_throttles_
;
403 // A running tally of the number of bytes this prerender has caused to be
404 // transferred over the network for resources. Updated with AddNetworkBytes.
405 int64 network_bytes_
;
407 DISALLOW_COPY_AND_ASSIGN(PrerenderContents
);
410 } // namespace prerender
412 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_