Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_contents.h
blob6d6fdd01b2cc192110d35482ed2a78c720b8fa39
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/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "chrome/browser/prerender/prerender_final_status.h"
18 #include "chrome/browser/prerender/prerender_origin.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/common/referrer.h"
23 #include "ui/gfx/geometry/size.h"
25 class Profile;
27 namespace base {
28 class ProcessMetrics;
31 namespace content {
32 class RenderViewHost;
33 class SessionStorageNamespace;
34 class WebContents;
37 namespace history {
38 struct HistoryAddPageArgs;
41 namespace prerender {
43 class PrerenderHandle;
44 class PrerenderManager;
45 class PrerenderResourceThrottle;
47 class PrerenderContents : public content::NotificationObserver,
48 public content::WebContentsObserver {
49 public:
50 // PrerenderContents::Create uses the currently registered Factory to create
51 // the PrerenderContents. Factory is intended for testing.
52 class Factory {
53 public:
54 Factory() {}
55 virtual ~Factory() {}
57 // Ownership is not transfered through this interface as prerender_manager
58 // and profile are stored as weak pointers.
59 virtual PrerenderContents* CreatePrerenderContents(
60 PrerenderManager* prerender_manager,
61 Profile* profile,
62 const GURL& url,
63 const content::Referrer& referrer,
64 Origin origin) = 0;
66 private:
67 DISALLOW_COPY_AND_ASSIGN(Factory);
70 class Observer {
71 public:
72 // Signals that the prerender has started running.
73 virtual void OnPrerenderStart(PrerenderContents* contents) = 0;
75 // Signals that the prerender has had its load event.
76 virtual void OnPrerenderStopLoading(PrerenderContents* contents);
78 // Signals that the prerender has had its 'DOMContentLoaded' event.
79 virtual void OnPrerenderDomContentLoaded(PrerenderContents* contents);
81 // Signals that the prerender has stopped running. A PrerenderContents with
82 // an unset final status will always call OnPrerenderStop before being
83 // destroyed.
84 virtual void OnPrerenderStop(PrerenderContents* contents) = 0;
86 // Signals that this prerender has just become a MatchComplete replacement.
87 virtual void OnPrerenderCreatedMatchCompleteReplacement(
88 PrerenderContents* contents, PrerenderContents* replacement);
90 protected:
91 Observer();
92 virtual ~Observer() = 0;
95 // Indicates how this PrerenderContents relates to MatchComplete. This is to
96 // figure out which histograms to use to record the FinalStatus, Match (record
97 // all prerenders and control group prerenders) or MatchComplete (record
98 // running prerenders only in the way they would have been recorded in the
99 // control group).
100 enum MatchCompleteStatus {
101 // A regular prerender which will be recorded both in Match and
102 // MatchComplete.
103 MATCH_COMPLETE_DEFAULT,
104 // A prerender that used to be a regular prerender, but has since been
105 // replaced by a MatchComplete dummy. Therefore, we will record this only
106 // for Match, but not for MatchComplete.
107 MATCH_COMPLETE_REPLACED,
108 // A prerender that is a MatchComplete dummy replacing a regular prerender.
109 // In the control group, our prerender never would have been canceled, so
110 // we record in MatchComplete but not Match.
111 MATCH_COMPLETE_REPLACEMENT,
112 // A prerender that is a MatchComplete dummy, early in the process of being
113 // created. This prerender should not fail. Record for MatchComplete, but
114 // not Match.
115 MATCH_COMPLETE_REPLACEMENT_PENDING,
118 ~PrerenderContents() override;
120 // All observers of a PrerenderContents are removed after the OnPrerenderStop
121 // event is sent, so there is no need to call RemoveObserver() in the normal
122 // use case.
123 void AddObserver(Observer* observer);
124 void RemoveObserver(Observer* observer);
126 // For MatchComplete correctness, create a dummy replacement prerender
127 // contents to stand in for this prerender contents that (which we are about
128 // to destroy).
129 PrerenderContents* CreateMatchCompleteReplacement();
131 bool Init();
133 static Factory* CreateFactory();
135 // Returns a PrerenderContents from the given web_contents, if it's used for
136 // prerendering. Otherwise returns NULL. Handles a NULL input for
137 // convenience.
138 static PrerenderContents* FromWebContents(content::WebContents* web_contents);
140 // Start rendering the contents in the prerendered state. If
141 // |is_control_group| is true, this will go through some of the mechanics of
142 // starting a prerender, without actually creating the RenderView. |size|
143 // indicates the rectangular dimensions that the prerendered page should be.
144 // |session_storage_namespace| indicates the namespace that the prerendered
145 // page should be part of.
146 virtual void StartPrerendering(
147 const gfx::Size& size,
148 content::SessionStorageNamespace* session_storage_namespace);
150 // Verifies that the prerendering is not using too many resources, and kills
151 // it if not.
152 void DestroyWhenUsingTooManyResources();
154 content::RenderViewHost* GetRenderViewHostMutable();
155 const content::RenderViewHost* GetRenderViewHost() const;
157 PrerenderManager* prerender_manager() { return prerender_manager_; }
159 base::string16 title() const { return title_; }
160 const GURL& prerender_url() const { return prerender_url_; }
161 const content::Referrer& referrer() const { return referrer_; }
162 bool has_stopped_loading() const { return has_stopped_loading_; }
163 bool has_finished_loading() const { return has_finished_loading_; }
164 bool prerendering_has_started() const { return prerendering_has_started_; }
165 MatchCompleteStatus match_complete_status() const {
166 return match_complete_status_;
168 void set_match_complete_status(MatchCompleteStatus status) {
169 match_complete_status_ = status;
172 // Sets the parameter to the value of the associated RenderViewHost's child id
173 // and returns a boolean indicating the validity of that id.
174 virtual bool GetChildId(int* child_id) const;
176 // Sets the parameter to the value of the associated RenderViewHost's route id
177 // and returns a boolean indicating the validity of that id.
178 virtual bool GetRouteId(int* route_id) const;
180 FinalStatus final_status() const { return final_status_; }
182 Origin origin() const { return origin_; }
184 base::TimeTicks load_start_time() const { return load_start_time_; }
186 // Indicates whether this prerendered page can be used for the provided
187 // |url| and |session_storage_namespace|.
188 bool Matches(
189 const GURL& url,
190 const content::SessionStorageNamespace* session_storage_namespace) const;
192 // content::WebContentsObserver implementation.
193 void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
194 void DidStopLoading() override;
195 void DocumentLoadedInFrame(
196 content::RenderFrameHost* render_frame_host) override;
197 void DidStartProvisionalLoadForFrame(
198 content::RenderFrameHost* render_frame_host,
199 const GURL& validated_url,
200 bool is_error_page,
201 bool is_iframe_srcdoc) override;
202 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
203 const GURL& validated_url) override;
204 void DidNavigateMainFrame(
205 const content::LoadCommittedDetails& details,
206 const content::FrameNavigateParams& params) override;
207 void DidGetRedirectForResourceRequest(
208 content::RenderFrameHost* render_frame_host,
209 const content::ResourceRedirectDetails& details) override;
210 bool OnMessageReceived(const IPC::Message& message) override;
212 void RenderProcessGone(base::TerminationStatus status) override;
214 // content::NotificationObserver
215 void Observe(int type,
216 const content::NotificationSource& source,
217 const content::NotificationDetails& details) override;
219 // Checks that a URL may be prerendered, for one of the many redirections. If
220 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
221 // be destroyed and false is returned. Otherwise, true is returned.
222 virtual bool CheckURL(const GURL& url);
224 // Adds an alias URL. If the URL can not be prerendered, |this| will be
225 // destroyed and false is returned.
226 bool AddAliasURL(const GURL& url);
228 // The prerender WebContents (may be NULL).
229 content::WebContents* prerender_contents() const {
230 return prerender_contents_.get();
233 content::WebContents* ReleasePrerenderContents();
235 // Sets the final status, calls OnDestroy and adds |this| to the
236 // PrerenderManager's pending deletes list.
237 void Destroy(FinalStatus reason);
239 // Called by the history tab helper with the information that it woudl have
240 // added to the history service had this web contents not been used for
241 // prerendering.
242 void DidNavigate(const history::HistoryAddPageArgs& add_page_args);
244 // Applies all the URL history encountered during prerendering to the
245 // new tab.
246 void CommitHistory(content::WebContents* tab);
248 base::Value* GetAsValue() const;
250 // Returns whether a pending cross-site navigation is happening.
251 // This could happen with renderer-issued navigations, such as a
252 // MouseEvent being dispatched by a link to a website installed as an app.
253 bool IsCrossSiteNavigationPending() const;
255 // Marks prerender as used and releases any throttled resource requests.
256 void PrepareForUse();
258 // Called when a PrerenderResourceThrottle defers a request. If the prerender
259 // is used it'll be resumed on the IO thread, otherwise they will get
260 // cancelled automatically if prerendering is cancelled.
261 void AddResourceThrottle(
262 const base::WeakPtr<PrerenderResourceThrottle>& throttle);
264 // Increments the number of bytes fetched over the network for this prerender.
265 void AddNetworkBytes(int64 bytes);
267 protected:
268 PrerenderContents(PrerenderManager* prerender_manager,
269 Profile* profile,
270 const GURL& url,
271 const content::Referrer& referrer,
272 Origin origin);
274 // Set the final status for how the PrerenderContents was used. This
275 // should only be called once, and should be called before the prerender
276 // contents are destroyed.
277 void SetFinalStatus(FinalStatus final_status);
279 // These call out to methods on our Observers, using our observer_list_. Note
280 // that NotifyPrerenderStop() also clears the observer list.
281 void NotifyPrerenderStart();
282 void NotifyPrerenderStopLoading();
283 void NotifyPrerenderDomContentLoaded();
284 void NotifyPrerenderStop();
285 void NotifyPrerenderCreatedMatchCompleteReplacement(
286 PrerenderContents* replacement);
288 // Called whenever a RenderViewHost is created for prerendering. Only called
289 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
290 virtual void OnRenderViewHostCreated(
291 content::RenderViewHost* new_render_view_host);
293 content::NotificationRegistrar& notification_registrar() {
294 return notification_registrar_;
297 bool prerendering_has_been_cancelled() const {
298 return prerendering_has_been_cancelled_;
301 content::WebContents* CreateWebContents(
302 content::SessionStorageNamespace* session_storage_namespace);
304 bool prerendering_has_started_;
306 // Time at which we started to load the URL. This is used to compute
307 // the time elapsed from initiating a prerender until the time the
308 // (potentially only partially) prerendered page is shown to the user.
309 base::TimeTicks load_start_time_;
311 // The prerendered WebContents; may be null.
312 scoped_ptr<content::WebContents> prerender_contents_;
314 // The session storage namespace id for use in matching. We must save it
315 // rather than get it from the RenderViewHost since in the control group
316 // we won't have a RenderViewHost.
317 int64 session_storage_namespace_id_;
319 private:
320 class WebContentsDelegateImpl;
322 // Needs to be able to call the constructor.
323 friend class PrerenderContentsFactoryImpl;
325 // Returns the ProcessMetrics for the render process, if it exists.
326 base::ProcessMetrics* MaybeGetProcessMetrics();
328 // Message handlers.
329 void OnCancelPrerenderForPrinting();
331 base::ObserverList<Observer> observer_list_;
333 // The prerender manager owning this object.
334 PrerenderManager* prerender_manager_;
336 // The URL being prerendered.
337 GURL prerender_url_;
339 // The referrer.
340 content::Referrer referrer_;
342 // The profile being used
343 Profile* profile_;
345 // Information about the title and URL of the page that this class as a
346 // RenderViewHostDelegate has received from the RenderView.
347 // Used to apply to the new RenderViewHost delegate that might eventually
348 // own the contained RenderViewHost when the prerendered page is shown
349 // in a WebContents.
350 base::string16 title_;
351 GURL url_;
352 content::NotificationRegistrar notification_registrar_;
354 // A vector of URLs that this prerendered page matches against.
355 // This array can contain more than element as a result of redirects,
356 // such as HTTP redirects or javascript redirects.
357 std::vector<GURL> alias_urls_;
359 bool has_stopped_loading_;
361 // True when the main frame has finished loading.
362 bool has_finished_loading_;
364 FinalStatus final_status_;
366 // The MatchComplete status of the prerender, indicating how it relates
367 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
368 // above).
369 MatchCompleteStatus match_complete_status_;
371 // Tracks whether or not prerendering has been cancelled by calling Destroy.
372 // Used solely to prevent double deletion.
373 bool prerendering_has_been_cancelled_;
375 // Process Metrics of the render process associated with the
376 // RenderViewHost for this object.
377 scoped_ptr<base::ProcessMetrics> process_metrics_;
379 scoped_ptr<WebContentsDelegateImpl> web_contents_delegate_;
381 // These are -1 before a RenderView is created.
382 int child_id_;
383 int route_id_;
385 // Origin for this prerender.
386 Origin origin_;
388 // The size of the WebView from the launching page.
389 gfx::Size size_;
391 typedef std::vector<history::HistoryAddPageArgs> AddPageVector;
393 // Caches pages to be added to the history.
394 AddPageVector add_page_vector_;
396 // Resources that are throttled, pending a prerender use. Can only access a
397 // throttle on the IO thread.
398 std::vector<base::WeakPtr<PrerenderResourceThrottle> > resource_throttles_;
400 // A running tally of the number of bytes this prerender has caused to be
401 // transferred over the network for resources. Updated with AddNetworkBytes.
402 int64 network_bytes_;
404 DISALLOW_COPY_AND_ASSIGN(PrerenderContents);
407 } // namespace prerender
409 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_