Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_contents.h
blobbcb52072b00962e388d1f4b0d9a7dfbe772bb7ea
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_
8 #include <string>
9 #include <utility>
10 #include <vector>
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"
26 class Profile;
28 namespace base {
29 class ProcessMetrics;
32 namespace content {
33 class RenderViewHost;
34 class SessionStorageNamespace;
35 class WebContents;
38 namespace history {
39 struct HistoryAddPageArgs;
42 namespace prerender {
44 class PrerenderHandle;
45 class PrerenderManager;
46 class PrerenderResourceThrottle;
48 class PrerenderContents : public content::NotificationObserver,
49 public content::WebContentsObserver {
50 public:
51 // PrerenderContents::Create uses the currently registered Factory to create
52 // the PrerenderContents. Factory is intended for testing.
53 class Factory {
54 public:
55 Factory() {}
56 virtual ~Factory() {}
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,
62 Profile* profile,
63 const GURL& url,
64 const content::Referrer& referrer,
65 Origin origin) = 0;
67 private:
68 DISALLOW_COPY_AND_ASSIGN(Factory);
71 class Observer {
72 public:
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
84 // destroyed.
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);
91 protected:
92 Observer();
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
100 // control group).
101 enum MatchCompleteStatus {
102 // A regular prerender which will be recorded both in Match and
103 // MatchComplete.
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
115 // not Match.
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
123 // use case.
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
129 // to destroy).
130 PrerenderContents* CreateMatchCompleteReplacement();
132 bool Init();
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
138 // convenience.
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
152 // it if not.
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 const GURL& prerender_url() const { return prerender_url_; }
162 const content::Referrer& referrer() const { return referrer_; }
163 bool has_stopped_loading() const { return has_stopped_loading_; }
164 bool has_finished_loading() const { return has_finished_loading_; }
165 bool prerendering_has_started() const { return prerendering_has_started_; }
166 MatchCompleteStatus match_complete_status() const {
167 return match_complete_status_;
169 void set_match_complete_status(MatchCompleteStatus status) {
170 match_complete_status_ = status;
173 // Sets the parameter to the value of the associated RenderViewHost's child id
174 // and returns a boolean indicating the validity of that id.
175 virtual bool GetChildId(int* child_id) const;
177 // Sets the parameter to the value of the associated RenderViewHost's route id
178 // and returns a boolean indicating the validity of that id.
179 virtual bool GetRouteId(int* route_id) const;
181 FinalStatus final_status() const { return final_status_; }
183 Origin origin() const { return origin_; }
185 base::TimeTicks load_start_time() const { return load_start_time_; }
187 // Indicates whether this prerendered page can be used for the provided
188 // |url| and |session_storage_namespace|.
189 bool Matches(
190 const GURL& url,
191 const content::SessionStorageNamespace* session_storage_namespace) const;
193 // content::WebContentsObserver implementation.
194 void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
195 void DidStopLoading() override;
196 void DocumentLoadedInFrame(
197 content::RenderFrameHost* render_frame_host) override;
198 void DidStartProvisionalLoadForFrame(
199 content::RenderFrameHost* render_frame_host,
200 const GURL& validated_url,
201 bool is_error_page,
202 bool is_iframe_srcdoc) override;
203 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
204 const GURL& validated_url) override;
205 void DidNavigateMainFrame(
206 const content::LoadCommittedDetails& details,
207 const content::FrameNavigateParams& params) override;
208 void DidGetRedirectForResourceRequest(
209 content::RenderFrameHost* render_frame_host,
210 const content::ResourceRedirectDetails& details) override;
211 bool OnMessageReceived(const IPC::Message& message) override;
213 void RenderProcessGone(base::TerminationStatus status) override;
215 // content::NotificationObserver
216 void Observe(int type,
217 const content::NotificationSource& source,
218 const content::NotificationDetails& details) override;
220 // Checks that a URL may be prerendered, for one of the many redirections. If
221 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
222 // be destroyed and false is returned. Otherwise, true is returned.
223 virtual bool CheckURL(const GURL& url);
225 // Adds an alias URL. If the URL can not be prerendered, |this| will be
226 // destroyed and false is returned.
227 bool AddAliasURL(const GURL& url);
229 // The prerender WebContents (may be NULL).
230 content::WebContents* prerender_contents() const {
231 return prerender_contents_.get();
234 content::WebContents* ReleasePrerenderContents();
236 // Sets the final status, calls OnDestroy and adds |this| to the
237 // PrerenderManager's pending deletes list.
238 void Destroy(FinalStatus reason);
240 // Called by the history tab helper with the information that it woudl have
241 // added to the history service had this web contents not been used for
242 // prerendering.
243 void DidNavigate(const history::HistoryAddPageArgs& add_page_args);
245 // Applies all the URL history encountered during prerendering to the
246 // new tab.
247 void CommitHistory(content::WebContents* tab);
249 base::Value* GetAsValue() const;
251 // Returns whether a pending cross-site navigation is happening.
252 // This could happen with renderer-issued navigations, such as a
253 // MouseEvent being dispatched by a link to a website installed as an app.
254 bool IsCrossSiteNavigationPending() const;
256 // Marks prerender as used and releases any throttled resource requests.
257 void PrepareForUse();
259 // Called when a PrerenderResourceThrottle defers a request. If the prerender
260 // is used it'll be resumed on the IO thread, otherwise they will get
261 // cancelled automatically if prerendering is cancelled.
262 void AddResourceThrottle(
263 const base::WeakPtr<PrerenderResourceThrottle>& throttle);
265 // Increments the number of bytes fetched over the network for this prerender.
266 void AddNetworkBytes(int64 bytes);
268 protected:
269 PrerenderContents(PrerenderManager* prerender_manager,
270 Profile* profile,
271 const GURL& url,
272 const content::Referrer& referrer,
273 Origin origin);
275 // Set the final status for how the PrerenderContents was used. This
276 // should only be called once, and should be called before the prerender
277 // contents are destroyed.
278 void SetFinalStatus(FinalStatus final_status);
280 // These call out to methods on our Observers, using our observer_list_. Note
281 // that NotifyPrerenderStop() also clears the observer list.
282 void NotifyPrerenderStart();
283 void NotifyPrerenderStopLoading();
284 void NotifyPrerenderDomContentLoaded();
285 void NotifyPrerenderStop();
286 void NotifyPrerenderCreatedMatchCompleteReplacement(
287 PrerenderContents* replacement);
289 // Called whenever a RenderViewHost is created for prerendering. Only called
290 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
291 virtual void OnRenderViewHostCreated(
292 content::RenderViewHost* new_render_view_host);
294 content::NotificationRegistrar& notification_registrar() {
295 return notification_registrar_;
298 bool prerendering_has_been_cancelled() const {
299 return prerendering_has_been_cancelled_;
302 content::WebContents* CreateWebContents(
303 content::SessionStorageNamespace* session_storage_namespace);
305 bool prerendering_has_started_;
307 // Time at which we started to load the URL. This is used to compute
308 // the time elapsed from initiating a prerender until the time the
309 // (potentially only partially) prerendered page is shown to the user.
310 base::TimeTicks load_start_time_;
312 // The prerendered WebContents; may be null.
313 scoped_ptr<content::WebContents> prerender_contents_;
315 // The session storage namespace id for use in matching. We must save it
316 // rather than get it from the RenderViewHost since in the control group
317 // we won't have a RenderViewHost.
318 int64 session_storage_namespace_id_;
320 private:
321 class WebContentsDelegateImpl;
323 // Needs to be able to call the constructor.
324 friend class PrerenderContentsFactoryImpl;
326 // Returns the ProcessMetrics for the render process, if it exists.
327 base::ProcessMetrics* MaybeGetProcessMetrics();
329 // Message handlers.
330 void OnCancelPrerenderForPrinting();
332 base::ObserverList<Observer> observer_list_;
334 // The prerender manager owning this object.
335 PrerenderManager* prerender_manager_;
337 // The URL being prerendered.
338 GURL prerender_url_;
340 // The referrer.
341 content::Referrer referrer_;
343 // The profile being used
344 Profile* profile_;
346 // Information about the title and URL of the page that this class as a
347 // RenderViewHostDelegate has received from the RenderView.
348 // Used to apply to the new RenderViewHost delegate that might eventually
349 // own the contained RenderViewHost when the prerendered page is shown
350 // in a WebContents.
351 base::string16 title_;
352 GURL url_;
353 content::NotificationRegistrar notification_registrar_;
355 // A vector of URLs that this prerendered page matches against.
356 // This array can contain more than element as a result of redirects,
357 // such as HTTP redirects or javascript redirects.
358 std::vector<GURL> alias_urls_;
360 bool has_stopped_loading_;
362 // True when the main frame has finished loading.
363 bool has_finished_loading_;
365 FinalStatus final_status_;
367 // The MatchComplete status of the prerender, indicating how it relates
368 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
369 // above).
370 MatchCompleteStatus match_complete_status_;
372 // Tracks whether or not prerendering has been cancelled by calling Destroy.
373 // Used solely to prevent double deletion.
374 bool prerendering_has_been_cancelled_;
376 // Process Metrics of the render process associated with the
377 // RenderViewHost for this object.
378 scoped_ptr<base::ProcessMetrics> process_metrics_;
380 scoped_ptr<WebContentsDelegateImpl> web_contents_delegate_;
382 // These are -1 before a RenderView is created.
383 int child_id_;
384 int route_id_;
386 // Origin for this prerender.
387 Origin origin_;
389 // The size of the WebView from the launching page.
390 gfx::Size size_;
392 typedef std::vector<history::HistoryAddPageArgs> AddPageVector;
394 // Caches pages to be added to the history.
395 AddPageVector add_page_vector_;
397 // Resources that are throttled, pending a prerender use. Can only access a
398 // throttle on the IO thread.
399 std::vector<base::WeakPtr<PrerenderResourceThrottle> > resource_throttles_;
401 // A running tally of the number of bytes this prerender has caused to be
402 // transferred over the network for resources. Updated with AddNetworkBytes.
403 int64 network_bytes_;
405 DISALLOW_COPY_AND_ASSIGN(PrerenderContents);
408 } // namespace prerender
410 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_