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"
35 class SessionStorageNamespace
;
40 struct HistoryAddPageArgs
;
45 class PrerenderHandle
;
46 class PrerenderManager
;
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 // prerender_tracker, and profile are stored as weak pointers.
60 virtual PrerenderContents
* CreatePrerenderContents(
61 PrerenderManager
* prerender_manager
,
64 const content::Referrer
& referrer
,
66 uint8 experiment_id
) = 0;
69 DISALLOW_COPY_AND_ASSIGN(Factory
);
74 // Signals that the prerender has started running.
75 virtual void OnPrerenderStart(PrerenderContents
* contents
) = 0;
77 // Signals that the prerender has had its load event.
78 virtual void OnPrerenderStopLoading(PrerenderContents
* contents
);
80 // Signals that the prerender has stopped running.
81 virtual void OnPrerenderStop(PrerenderContents
* contents
) = 0;
83 // Signals that this prerender has just become a MatchComplete replacement.
84 virtual void OnPrerenderCreatedMatchCompleteReplacement(
85 PrerenderContents
* contents
, PrerenderContents
* replacement
);
89 virtual ~Observer() = 0;
92 // A container for extra data on pending prerenders.
93 struct PendingPrerenderInfo
{
96 base::WeakPtr
<PrerenderHandle
> weak_prerender_handle
,
99 const content::Referrer
& referrer
,
100 const gfx::Size
& size
);
102 ~PendingPrerenderInfo();
104 base::WeakPtr
<PrerenderHandle
> weak_prerender_handle
;
107 content::Referrer referrer
;
111 // Indicates how this PrerenderContents relates to MatchComplete. This is to
112 // figure out which histograms to use to record the FinalStatus, Match (record
113 // all prerenders and control group prerenders) or MatchComplete (record
114 // running prerenders only in the way they would have been recorded in the
116 enum MatchCompleteStatus
{
117 // A regular prerender which will be recorded both in Match and
119 MATCH_COMPLETE_DEFAULT
,
120 // A prerender that used to be a regular prerender, but has since been
121 // replaced by a MatchComplete dummy. Therefore, we will record this only
122 // for Match, but not for MatchComplete.
123 MATCH_COMPLETE_REPLACED
,
124 // A prerender that is a MatchComplete dummy replacing a regular prerender.
125 // In the control group, our prerender never would have been canceled, so
126 // we record in MatchComplete but not Match.
127 MATCH_COMPLETE_REPLACEMENT
,
128 // A prerender that is a MatchComplete dummy, early in the process of being
129 // created. This prerender should not fail. Record for MatchComplete, but
131 MATCH_COMPLETE_REPLACEMENT_PENDING
,
134 virtual ~PrerenderContents();
136 // All observers of a PrerenderContents are removed after the OnPrerenderStop
137 // event is sent, so there is no need to call RemoveObserver() in the normal
139 void AddObserver(Observer
* observer
);
140 void RemoveObserver(Observer
* observer
);
142 // For MatchComplete correctness, create a dummy replacement prerender
143 // contents to stand in for this prerender contents that (which we are about
145 PrerenderContents
* CreateMatchCompleteReplacement();
149 static Factory
* CreateFactory();
151 // Returns a PrerenderContents from the given web_contents, if it's used for
152 // prerendering. Otherwise returns NULL. Handles a NULL input for
154 static PrerenderContents
* FromWebContents(content::WebContents
* web_contents
);
156 // Start rendering the contents in the prerendered state. If
157 // |is_control_group| is true, this will go through some of the mechanics of
158 // starting a prerender, without actually creating the RenderView.
159 // |creator_child_id| is the id of the child process that caused the prerender
160 // to be created, and is needed so that the prerendered URLs can be sent to it
161 // so render-initiated navigations will swap in the prerendered page. |size|
162 // indicates the rectangular dimensions that the prerendered page should be.
163 // |session_storage_namespace| indicates the namespace that the prerendered
164 // page should be part of.
165 virtual void StartPrerendering(
166 int creator_child_id
,
167 const gfx::Size
& size
,
168 content::SessionStorageNamespace
* session_storage_namespace
);
170 // Verifies that the prerendering is not using too many resources, and kills
172 void DestroyWhenUsingTooManyResources();
174 content::RenderViewHost
* GetRenderViewHostMutable();
175 const content::RenderViewHost
* GetRenderViewHost() const;
177 PrerenderManager
* prerender_manager() { return prerender_manager_
; }
179 base::string16
title() const { return title_
; }
180 int32
page_id() const { return page_id_
; }
181 GURL
icon_url() const { return icon_url_
; }
182 const GURL
& prerender_url() const { return prerender_url_
; }
183 const content::Referrer
& referrer() const { return referrer_
; }
184 bool has_stopped_loading() const { return has_stopped_loading_
; }
185 bool has_finished_loading() const { return has_finished_loading_
; }
186 bool prerendering_has_started() const { return prerendering_has_started_
; }
187 MatchCompleteStatus
match_complete_status() const {
188 return match_complete_status_
;
190 void set_match_complete_status(MatchCompleteStatus status
) {
191 match_complete_status_
= status
;
194 // Sets the parameter to the value of the associated RenderViewHost's child id
195 // and returns a boolean indicating the validity of that id.
196 virtual bool GetChildId(int* child_id
) const;
198 // Sets the parameter to the value of the associated RenderViewHost's route id
199 // and returns a boolean indicating the validity of that id.
200 virtual bool GetRouteId(int* route_id
) const;
202 // Set the final status for how the PrerenderContents was used. This
203 // should only be called once, and should be called before the prerender
204 // contents are destroyed.
205 void SetFinalStatus(FinalStatus final_status
);
206 FinalStatus
final_status() const { return final_status_
; }
208 Origin
origin() const { return origin_
; }
209 uint8
experiment_id() const { return experiment_id_
; }
210 int child_id() const { return child_id_
; }
212 base::TimeTicks
load_start_time() const { return load_start_time_
; }
214 // Indicates whether this prerendered page can be used for the provided
215 // |url| and |session_storage_namespace|.
218 const content::SessionStorageNamespace
* session_storage_namespace
) const;
220 // content::WebContentsObserver implementation.
221 virtual void RenderFrameCreated(
222 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
223 virtual void DidStopLoading(
224 content::RenderViewHost
* render_view_host
) OVERRIDE
;
225 virtual void DidStartProvisionalLoadForFrame(
227 int64 parent_frame_id
,
229 const GURL
& validated_url
,
231 bool is_iframe_srcdoc
,
232 content::RenderViewHost
* render_view_host
) OVERRIDE
;
233 virtual void DidFinishLoad(
235 const GURL
& validated_url
,
237 content::RenderViewHost
* render_view_host
) OVERRIDE
;
238 virtual void DidNavigateMainFrame(
239 const content::LoadCommittedDetails
& details
,
240 const content::FrameNavigateParams
& params
) OVERRIDE
;
241 virtual void DidGetRedirectForResourceRequest(
242 content::RenderViewHost
* render_view_host
,
243 const content::ResourceRedirectDetails
& details
) OVERRIDE
;
244 virtual void DidUpdateFaviconURL(int32 page_id
,
245 const std::vector
<content::FaviconURL
>& urls
) OVERRIDE
;
246 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
248 virtual void RenderProcessGone(base::TerminationStatus status
) OVERRIDE
;
250 // content::NotificationObserver
251 virtual void Observe(int type
,
252 const content::NotificationSource
& source
,
253 const content::NotificationDetails
& details
) OVERRIDE
;
255 // Checks that a URL may be prerendered, for one of the many redirections. If
256 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
257 // be destroyed and false is returned. Otherwise, true is returned.
258 virtual bool CheckURL(const GURL
& url
);
260 // Adds an alias URL. If the URL can not be prerendered, |this| will be
261 // destroyed and false is returned.
262 bool AddAliasURL(const GURL
& url
);
264 // The prerender WebContents (may be NULL).
265 content::WebContents
* prerender_contents() const {
266 return prerender_contents_
.get();
269 content::WebContents
* ReleasePrerenderContents();
271 // Sets the final status, calls OnDestroy and adds |this| to the
272 // PrerenderManager's pending deletes list.
273 void Destroy(FinalStatus reason
);
275 // Called by the history tab helper with the information that it woudl have
276 // added to the history service had this web contents not been used for
278 void DidNavigate(const history::HistoryAddPageArgs
& add_page_args
);
280 // Applies all the URL history encountered during prerendering to the
282 void CommitHistory(content::WebContents
* tab
);
284 base::Value
* GetAsValue() const;
286 // Returns whether a pending cross-site navigation is happening.
287 // This could happen with renderer-issued navigations, such as a
288 // MouseEvent being dispatched by a link to a website installed as an app.
289 bool IsCrossSiteNavigationPending() const;
291 // Adds a pending prerender to the list. If |weak_prerender_handle| still
292 // exists when this page is made visible, it will be launched.
293 virtual void AddPendingPrerender(
294 scoped_ptr
<PendingPrerenderInfo
> pending_prerender_info
);
296 // Reissues any pending prerender requests from the prerendered page. Also
297 // clears the list of pending requests. Sends notifications.
298 void PrepareForUse();
300 content::SessionStorageNamespace
* GetSessionStorageNamespace() const;
304 COOKIE_EVENT_SEND
= 0,
305 COOKIE_EVENT_CHANGE
= 1,
309 // Record a cookie transaction for this prerender contents.
310 // In the event of cookies being sent, |earliest_create_date| contains
311 // the time that the earliest of the cookies sent was created.
312 void RecordCookieEvent(CookieEvent event
,
313 bool main_frame_http_request
,
314 base::Time earliest_create_date
);
316 static const int kNumCookieStatuses
;
319 PrerenderContents(PrerenderManager
* prerender_manager
,
322 const content::Referrer
& referrer
,
324 uint8 experiment_id
);
326 // These call out to methods on our Observers, using our observer_list_. Note
327 // that NotifyPrerenderStop() also clears the observer list.
328 void NotifyPrerenderStart();
329 void NotifyPrerenderStopLoading();
330 void NotifyPrerenderStop();
331 void NotifyPrerenderCreatedMatchCompleteReplacement(
332 PrerenderContents
* replacement
);
334 // Called whenever a RenderViewHost is created for prerendering. Only called
335 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
336 virtual void OnRenderViewHostCreated(
337 content::RenderViewHost
* new_render_view_host
);
339 content::NotificationRegistrar
& notification_registrar() {
340 return notification_registrar_
;
343 size_t pending_prerender_count() const;
345 bool prerendering_has_been_cancelled() const {
346 return prerendering_has_been_cancelled_
;
349 virtual content::WebContents
* CreateWebContents(
350 content::SessionStorageNamespace
* session_storage_namespace
);
352 bool prerendering_has_started_
;
354 // Time at which we started to load the URL. This is used to compute
355 // the time elapsed from initiating a prerender until the time the
356 // (potentially only partially) prerendered page is shown to the user.
357 base::TimeTicks load_start_time_
;
359 // The prerendered WebContents; may be null.
360 scoped_ptr
<content::WebContents
> prerender_contents_
;
362 // The session storage namespace id for use in matching. We must save it
363 // rather than get it from the RenderViewHost since in the control group
364 // we won't have a RenderViewHost.
365 int64 session_storage_namespace_id_
;
367 // The time at which we started prerendering, for the purpose of comparing
368 // cookie creation times.
369 base::Time start_time_
;
372 class WebContentsDelegateImpl
;
374 // Needs to be able to call the constructor.
375 friend class PrerenderContentsFactoryImpl
;
377 // Returns the ProcessMetrics for the render process, if it exists.
378 base::ProcessMetrics
* MaybeGetProcessMetrics();
381 void OnCancelPrerenderForPrinting();
383 ObserverList
<Observer
> observer_list_
;
385 // The prerender manager owning this object.
386 PrerenderManager
* prerender_manager_
;
388 // The URL being prerendered.
392 content::Referrer referrer_
;
394 // The profile being used
397 // Information about the title and URL of the page that this class as a
398 // RenderViewHostDelegate has received from the RenderView.
399 // Used to apply to the new RenderViewHost delegate that might eventually
400 // own the contained RenderViewHost when the prerendered page is shown
402 base::string16 title_
;
406 content::NotificationRegistrar notification_registrar_
;
408 // A vector of URLs that this prerendered page matches against.
409 // This array can contain more than element as a result of redirects,
410 // such as HTTP redirects or javascript redirects.
411 std::vector
<GURL
> alias_urls_
;
413 bool has_stopped_loading_
;
415 // True when the main frame has finished loading.
416 bool has_finished_loading_
;
418 // This must be the same value as the PrerenderTracker has recorded for
419 // |this|, when |this| has a RenderView.
420 FinalStatus final_status_
;
422 // The MatchComplete status of the prerender, indicating how it relates
423 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
425 MatchCompleteStatus match_complete_status_
;
427 // Tracks whether or not prerendering has been cancelled by calling Destroy.
428 // Used solely to prevent double deletion.
429 bool prerendering_has_been_cancelled_
;
431 // Process Metrics of the render process associated with the
432 // RenderViewHost for this object.
433 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
435 scoped_ptr
<WebContentsDelegateImpl
> web_contents_delegate_
;
437 // These are -1 before a RenderView is created.
441 // Origin for this prerender.
444 // Experiment during which this prerender is performed.
445 uint8 experiment_id_
;
447 // Prerenders that the prerendered page has tried to prerender. They remain
448 // pending until this page is displayed.
449 ScopedVector
<PendingPrerenderInfo
> pending_prerenders_
;
451 // The process that created the child id.
452 int creator_child_id_
;
454 // The size of the WebView from the launching page.
457 typedef std::vector
<history::HistoryAddPageArgs
> AddPageVector
;
459 // Caches pages to be added to the history.
460 AddPageVector add_page_vector_
;
462 // The alias session storage namespace for this prerender.
463 scoped_refptr
<content::SessionStorageNamespace
>
464 alias_session_storage_namespace
;
466 // Indicates what internal cookie events (see prerender_contents.cc) have
467 // occurred, using 1 bit for each possible InternalCookieEvent.
470 DISALLOW_COPY_AND_ASSIGN(PrerenderContents
);
473 } // namespace prerender
475 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_