Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_contents.h
blobb31f7e763d2ac381f2a6676bda4fe1beea79e0f7
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,
66 uint8 experiment_id) = 0;
68 private:
69 DISALLOW_COPY_AND_ASSIGN(Factory);
72 class Observer {
73 public:
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 had its 'DOMContentLoaded' event.
81 virtual void OnPrerenderDomContentLoaded(PrerenderContents* contents);
83 // Signals that the prerender has stopped running. A PrerenderContents with
84 // an unset final status will always call OnPrerenderStop before being
85 // destroyed.
86 virtual void OnPrerenderStop(PrerenderContents* contents) = 0;
88 // Signals that this prerender has just become a MatchComplete replacement.
89 virtual void OnPrerenderCreatedMatchCompleteReplacement(
90 PrerenderContents* contents, PrerenderContents* replacement);
92 protected:
93 Observer();
94 virtual ~Observer() = 0;
97 // Indicates how this PrerenderContents relates to MatchComplete. This is to
98 // figure out which histograms to use to record the FinalStatus, Match (record
99 // all prerenders and control group prerenders) or MatchComplete (record
100 // running prerenders only in the way they would have been recorded in the
101 // control group).
102 enum MatchCompleteStatus {
103 // A regular prerender which will be recorded both in Match and
104 // MatchComplete.
105 MATCH_COMPLETE_DEFAULT,
106 // A prerender that used to be a regular prerender, but has since been
107 // replaced by a MatchComplete dummy. Therefore, we will record this only
108 // for Match, but not for MatchComplete.
109 MATCH_COMPLETE_REPLACED,
110 // A prerender that is a MatchComplete dummy replacing a regular prerender.
111 // In the control group, our prerender never would have been canceled, so
112 // we record in MatchComplete but not Match.
113 MATCH_COMPLETE_REPLACEMENT,
114 // A prerender that is a MatchComplete dummy, early in the process of being
115 // created. This prerender should not fail. Record for MatchComplete, but
116 // not Match.
117 MATCH_COMPLETE_REPLACEMENT_PENDING,
120 ~PrerenderContents() override;
122 // All observers of a PrerenderContents are removed after the OnPrerenderStop
123 // event is sent, so there is no need to call RemoveObserver() in the normal
124 // use case.
125 void AddObserver(Observer* observer);
126 void RemoveObserver(Observer* observer);
128 // For MatchComplete correctness, create a dummy replacement prerender
129 // contents to stand in for this prerender contents that (which we are about
130 // to destroy).
131 PrerenderContents* CreateMatchCompleteReplacement();
133 bool Init();
135 static Factory* CreateFactory();
137 // Returns a PrerenderContents from the given web_contents, if it's used for
138 // prerendering. Otherwise returns NULL. Handles a NULL input for
139 // convenience.
140 static PrerenderContents* FromWebContents(content::WebContents* web_contents);
142 // Start rendering the contents in the prerendered state. If
143 // |is_control_group| is true, this will go through some of the mechanics of
144 // starting a prerender, without actually creating the RenderView. |size|
145 // indicates the rectangular dimensions that the prerendered page should be.
146 // |session_storage_namespace| indicates the namespace that the prerendered
147 // page should be part of.
148 virtual void StartPrerendering(
149 const gfx::Size& size,
150 content::SessionStorageNamespace* session_storage_namespace);
152 // Verifies that the prerendering is not using too many resources, and kills
153 // it if not.
154 void DestroyWhenUsingTooManyResources();
156 content::RenderViewHost* GetRenderViewHostMutable();
157 const content::RenderViewHost* GetRenderViewHost() const;
159 PrerenderManager* prerender_manager() { return prerender_manager_; }
161 base::string16 title() const { return title_; }
162 int32 page_id() const { return page_id_; }
163 const GURL& prerender_url() const { return prerender_url_; }
164 const content::Referrer& referrer() const { return referrer_; }
165 bool has_stopped_loading() const { return has_stopped_loading_; }
166 bool has_finished_loading() const { return has_finished_loading_; }
167 bool prerendering_has_started() const { return prerendering_has_started_; }
168 MatchCompleteStatus match_complete_status() const {
169 return match_complete_status_;
171 void set_match_complete_status(MatchCompleteStatus status) {
172 match_complete_status_ = status;
175 // Sets the parameter to the value of the associated RenderViewHost's child id
176 // and returns a boolean indicating the validity of that id.
177 virtual bool GetChildId(int* child_id) const;
179 // Sets the parameter to the value of the associated RenderViewHost's route id
180 // and returns a boolean indicating the validity of that id.
181 virtual bool GetRouteId(int* route_id) const;
183 FinalStatus final_status() const { return final_status_; }
185 Origin origin() const { return origin_; }
186 uint8 experiment_id() const { return experiment_id_; }
188 base::TimeTicks load_start_time() const { return load_start_time_; }
190 // Indicates whether this prerendered page can be used for the provided
191 // |url| and |session_storage_namespace|.
192 bool Matches(
193 const GURL& url,
194 const content::SessionStorageNamespace* session_storage_namespace) const;
196 // content::WebContentsObserver implementation.
197 void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
198 void DidStopLoading() override;
199 void DocumentLoadedInFrame(
200 content::RenderFrameHost* render_frame_host) override;
201 void DidStartProvisionalLoadForFrame(
202 content::RenderFrameHost* render_frame_host,
203 const GURL& validated_url,
204 bool is_error_page,
205 bool is_iframe_srcdoc) override;
206 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
207 const GURL& validated_url) override;
208 void DidNavigateMainFrame(
209 const content::LoadCommittedDetails& details,
210 const content::FrameNavigateParams& params) override;
211 void DidGetRedirectForResourceRequest(
212 content::RenderFrameHost* render_frame_host,
213 const content::ResourceRedirectDetails& details) override;
214 bool OnMessageReceived(const IPC::Message& message) override;
216 void RenderProcessGone(base::TerminationStatus status) override;
218 // content::NotificationObserver
219 void Observe(int type,
220 const content::NotificationSource& source,
221 const content::NotificationDetails& details) override;
223 // Checks that a URL may be prerendered, for one of the many redirections. If
224 // the URL can not be prerendered - for example, it's an ftp URL - |this| will
225 // be destroyed and false is returned. Otherwise, true is returned.
226 virtual bool CheckURL(const GURL& url);
228 // Adds an alias URL. If the URL can not be prerendered, |this| will be
229 // destroyed and false is returned.
230 bool AddAliasURL(const GURL& url);
232 // The prerender WebContents (may be NULL).
233 content::WebContents* prerender_contents() const {
234 return prerender_contents_.get();
237 content::WebContents* ReleasePrerenderContents();
239 // Sets the final status, calls OnDestroy and adds |this| to the
240 // PrerenderManager's pending deletes list.
241 void Destroy(FinalStatus reason);
243 // Called by the history tab helper with the information that it woudl have
244 // added to the history service had this web contents not been used for
245 // prerendering.
246 void DidNavigate(const history::HistoryAddPageArgs& add_page_args);
248 // Applies all the URL history encountered during prerendering to the
249 // new tab.
250 void CommitHistory(content::WebContents* tab);
252 base::Value* GetAsValue() const;
254 // Returns whether a pending cross-site navigation is happening.
255 // This could happen with renderer-issued navigations, such as a
256 // MouseEvent being dispatched by a link to a website installed as an app.
257 bool IsCrossSiteNavigationPending() const;
259 // Marks prerender as used and releases any throttled resource requests.
260 void PrepareForUse();
262 // Called when a PrerenderResourceThrottle defers a request. If the prerender
263 // is used it'll be resumed on the IO thread, otherwise they will get
264 // cancelled automatically if prerendering is cancelled.
265 void AddResourceThrottle(
266 const base::WeakPtr<PrerenderResourceThrottle>& throttle);
268 // Increments the number of bytes fetched over the network for this prerender.
269 void AddNetworkBytes(int64 bytes);
271 protected:
272 PrerenderContents(PrerenderManager* prerender_manager,
273 Profile* profile,
274 const GURL& url,
275 const content::Referrer& referrer,
276 Origin origin,
277 uint8 experiment_id);
279 // Set the final status for how the PrerenderContents was used. This
280 // should only be called once, and should be called before the prerender
281 // contents are destroyed.
282 void SetFinalStatus(FinalStatus final_status);
284 // These call out to methods on our Observers, using our observer_list_. Note
285 // that NotifyPrerenderStop() also clears the observer list.
286 void NotifyPrerenderStart();
287 void NotifyPrerenderStopLoading();
288 void NotifyPrerenderDomContentLoaded();
289 void NotifyPrerenderStop();
290 void NotifyPrerenderCreatedMatchCompleteReplacement(
291 PrerenderContents* replacement);
293 // Called whenever a RenderViewHost is created for prerendering. Only called
294 // once the RenderViewHost has a RenderView and RenderWidgetHostView.
295 virtual void OnRenderViewHostCreated(
296 content::RenderViewHost* new_render_view_host);
298 content::NotificationRegistrar& notification_registrar() {
299 return notification_registrar_;
302 bool prerendering_has_been_cancelled() const {
303 return prerendering_has_been_cancelled_;
306 content::WebContents* CreateWebContents(
307 content::SessionStorageNamespace* session_storage_namespace);
309 bool prerendering_has_started_;
311 // Time at which we started to load the URL. This is used to compute
312 // the time elapsed from initiating a prerender until the time the
313 // (potentially only partially) prerendered page is shown to the user.
314 base::TimeTicks load_start_time_;
316 // The prerendered WebContents; may be null.
317 scoped_ptr<content::WebContents> prerender_contents_;
319 // The session storage namespace id for use in matching. We must save it
320 // rather than get it from the RenderViewHost since in the control group
321 // we won't have a RenderViewHost.
322 int64 session_storage_namespace_id_;
324 private:
325 class WebContentsDelegateImpl;
327 // Needs to be able to call the constructor.
328 friend class PrerenderContentsFactoryImpl;
330 // Returns the ProcessMetrics for the render process, if it exists.
331 base::ProcessMetrics* MaybeGetProcessMetrics();
333 // Message handlers.
334 void OnCancelPrerenderForPrinting();
336 ObserverList<Observer> observer_list_;
338 // The prerender manager owning this object.
339 PrerenderManager* prerender_manager_;
341 // The URL being prerendered.
342 GURL prerender_url_;
344 // The referrer.
345 content::Referrer referrer_;
347 // The profile being used
348 Profile* profile_;
350 // Information about the title and URL of the page that this class as a
351 // RenderViewHostDelegate has received from the RenderView.
352 // Used to apply to the new RenderViewHost delegate that might eventually
353 // own the contained RenderViewHost when the prerendered page is shown
354 // in a WebContents.
355 base::string16 title_;
356 int32 page_id_;
357 GURL url_;
358 content::NotificationRegistrar notification_registrar_;
360 // A vector of URLs that this prerendered page matches against.
361 // This array can contain more than element as a result of redirects,
362 // such as HTTP redirects or javascript redirects.
363 std::vector<GURL> alias_urls_;
365 bool has_stopped_loading_;
367 // True when the main frame has finished loading.
368 bool has_finished_loading_;
370 FinalStatus final_status_;
372 // The MatchComplete status of the prerender, indicating how it relates
373 // to being a MatchComplete dummy (see definition of MatchCompleteStatus
374 // above).
375 MatchCompleteStatus match_complete_status_;
377 // Tracks whether or not prerendering has been cancelled by calling Destroy.
378 // Used solely to prevent double deletion.
379 bool prerendering_has_been_cancelled_;
381 // Process Metrics of the render process associated with the
382 // RenderViewHost for this object.
383 scoped_ptr<base::ProcessMetrics> process_metrics_;
385 scoped_ptr<WebContentsDelegateImpl> web_contents_delegate_;
387 // These are -1 before a RenderView is created.
388 int child_id_;
389 int route_id_;
391 // Origin for this prerender.
392 Origin origin_;
394 // Experiment during which this prerender is performed.
395 uint8 experiment_id_;
397 // The size of the WebView from the launching page.
398 gfx::Size size_;
400 typedef std::vector<history::HistoryAddPageArgs> AddPageVector;
402 // Caches pages to be added to the history.
403 AddPageVector add_page_vector_;
405 // Resources that are throttled, pending a prerender use. Can only access a
406 // throttle on the IO thread.
407 std::vector<base::WeakPtr<PrerenderResourceThrottle> > resource_throttles_;
409 // A running tally of the number of bytes this prerender has caused to be
410 // transferred over the network for resources. Updated with AddNetworkBytes.
411 int64 network_bytes_;
413 DISALLOW_COPY_AND_ASSIGN(PrerenderContents);
416 } // namespace prerender
418 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_