OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | |
10 #include <string> | 9 #include <string> |
11 #include <utility> | 10 #include <utility> |
12 #include <vector> | 11 #include <vector> |
13 | 12 |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/prerender/prerender_final_status.h" | 17 #include "chrome/browser/prerender/prerender_final_status.h" |
18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
20 #include "content/public/browser/web_contents_observer.h" | 20 #include "content/public/browser/web_contents_observer.h" |
21 #include "content/public/common/referrer.h" | 21 #include "content/public/common/referrer.h" |
22 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
23 | 23 |
24 class Profile; | 24 class Profile; |
25 class TabContents; | 25 class TabContents; |
26 struct FaviconURL; | 26 struct FaviconURL; |
27 | 27 |
28 namespace base { | 28 namespace base { |
29 class ProcessMetrics; | 29 class ProcessMetrics; |
30 } | 30 } |
31 | 31 |
32 namespace content { | 32 namespace content { |
33 class RenderViewHost; | 33 class RenderViewHost; |
34 class SessionStorageNamespace; | 34 class SessionStorageNamespace; |
35 class WebContents; | 35 class WebContents; |
36 } | 36 } |
37 | 37 |
38 namespace prerender { | 38 namespace prerender { |
39 | 39 |
40 class PrerenderHandle; | |
40 class PrerenderManager; | 41 class PrerenderManager; |
41 class PrerenderRenderViewHostObserver; | 42 class PrerenderRenderViewHostObserver; |
42 class PrerenderTracker; | 43 class PrerenderTracker; |
43 | 44 |
44 class PrerenderContents : public content::NotificationObserver, | 45 class PrerenderContents : public content::NotificationObserver, |
45 public content::WebContentsObserver { | 46 public content::WebContentsObserver { |
46 public: | 47 public: |
47 // PrerenderContents::Create uses the currently registered Factory to create | 48 // PrerenderContents::Create uses the currently registered Factory to create |
48 // the PrerenderContents. Factory is intended for testing. | 49 // the PrerenderContents. Factory is intended for testing. |
49 class Factory { | 50 class Factory { |
50 public: | 51 public: |
51 Factory() {} | 52 Factory() {} |
52 virtual ~Factory() {} | 53 virtual ~Factory() {} |
53 | 54 |
54 // Ownership is not transfered through this interface as prerender_manager, | 55 // Ownership is not transfered through this interface as prerender_manager, |
55 // prerender_tracker, and profile are stored as weak pointers. | 56 // prerender_tracker, and profile are stored as weak pointers. |
56 virtual PrerenderContents* CreatePrerenderContents( | 57 virtual PrerenderContents* CreatePrerenderContents( |
57 PrerenderManager* prerender_manager, | 58 PrerenderManager* prerender_manager, |
58 PrerenderTracker* prerender_tracker, | 59 PrerenderTracker* prerender_tracker, |
59 Profile* profile, | 60 Profile* profile, |
60 const GURL& url, | 61 const GURL& url, |
61 const content::Referrer& referrer, | 62 const content::Referrer& referrer, |
62 Origin origin, | 63 Origin origin, |
63 uint8 experiment_id) = 0; | 64 uint8 experiment_id) = 0; |
64 | 65 |
65 private: | 66 private: |
66 DISALLOW_COPY_AND_ASSIGN(Factory); | 67 DISALLOW_COPY_AND_ASSIGN(Factory); |
67 }; | 68 }; |
68 | 69 |
69 // Information on pages that the prerendered page has tried to prerender. | |
70 struct PendingPrerenderInfo; | |
71 typedef std::list<PendingPrerenderInfo> PendingPrerenderList; | |
72 | |
73 // Indicates how this PrerenderContents relates to MatchComplete. This is to | 70 // Indicates how this PrerenderContents relates to MatchComplete. This is to |
74 // figure out which histograms to use to record the FinalStatus, Match (record | 71 // figure out which histograms to use to record the FinalStatus, Match (record |
75 // all prerenders and control group prerenders) or MatchComplete (record | 72 // all prerenders and control group prerenders) or MatchComplete (record |
76 // running prerenders only in the way they would have been recorded in the | 73 // running prerenders only in the way they would have been recorded in the |
77 // control group). | 74 // control group). |
78 enum MatchCompleteStatus { | 75 enum MatchCompleteStatus { |
79 // A regular prerender which will be recorded both in Match and | 76 // A regular prerender which will be recorded both in Match and |
80 // MatchComplete. | 77 // MatchComplete. |
81 MATCH_COMPLETE_DEFAULT, | 78 MATCH_COMPLETE_DEFAULT, |
82 // A prerender that used to be a regular prerender, but has since been | 79 // A prerender that used to be a regular prerender, but has since been |
83 // replaced by a MatchComplete dummy. Therefore, we will record this only | 80 // replaced by a MatchComplete dummy. Therefore, we will record this only |
84 // for Match, but not for MatchComplete. | 81 // for Match, but not for MatchComplete. |
85 MATCH_COMPLETE_REPLACED, | 82 MATCH_COMPLETE_REPLACED, |
86 // A prerender that is a MatchComplete dummy replacing a regular prerender. | 83 // A prerender that is a MatchComplete dummy replacing a regular prerender. |
87 // In the control group, our prerender never would have been canceled, so | 84 // In the control group, our prerender never would have been canceled, so |
88 // we record in MatchComplete but not Match. | 85 // we record in MatchComplete but not Match. |
89 MATCH_COMPLETE_REPLACEMENT, | 86 MATCH_COMPLETE_REPLACEMENT, |
90 // A prerender that is a MatchComplete dummy, early in the process of being | 87 // A prerender that is a MatchComplete dummy, early in the process of being |
91 // created. This prerender should not fail. Record for MatchComplete, but | 88 // created. This prerender should not fail. Record for MatchComplete, but |
92 // not Match. | 89 // not Match. |
93 MATCH_COMPLETE_REPLACEMENT_PENDING, | 90 MATCH_COMPLETE_REPLACEMENT_PENDING, |
94 }; | 91 }; |
95 | 92 |
96 virtual ~PrerenderContents(); | 93 virtual ~PrerenderContents(); |
97 | 94 |
95 // For MatchComplete correctness, create a dummy replacement prerender | |
96 // contents to stand in for this prerender contents that (which we are about | |
97 // to destroy). | |
98 void MakeIntoDummyReplacementOf( | |
99 const PrerenderContents* original_prerender_contents); | |
100 | |
98 bool Init(); | 101 bool Init(); |
99 | 102 |
100 static Factory* CreateFactory(); | 103 static Factory* CreateFactory(); |
101 | 104 |
102 // Start rendering the contents in the prerendered state. If | 105 // Start rendering the contents in the prerendered state. If |
103 // |is_control_group| is true, this will go through some of the mechanics of | 106 // |is_control_group| is true, this will go through some of the mechanics of |
104 // starting a prerender, without actually creating the RenderView. | 107 // starting a prerender, without actually creating the RenderView. |
105 // |creator_child_id| is the id of the child process that caused the prerender | 108 // |creator_child_id| is the id of the child process that caused the prerender |
106 // to be created, and is needed so that the prerendered URLs can be sent to it | 109 // to be created, and is needed so that the prerendered URLs can be sent to it |
107 // so render-initiated navigations will swap in the prerendered page. |size| | 110 // so render-initiated navigations will swap in the prerendered page. |size| |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 // contents are destroyed. | 152 // contents are destroyed. |
150 void set_final_status(FinalStatus final_status); | 153 void set_final_status(FinalStatus final_status); |
151 FinalStatus final_status() const { return final_status_; } | 154 FinalStatus final_status() const { return final_status_; } |
152 | 155 |
153 Origin origin() const { return origin_; } | 156 Origin origin() const { return origin_; } |
154 uint8 experiment_id() const { return experiment_id_; } | 157 uint8 experiment_id() const { return experiment_id_; } |
155 | 158 |
156 base::TimeTicks load_start_time() const { return load_start_time_; } | 159 base::TimeTicks load_start_time() const { return load_start_time_; } |
157 | 160 |
158 // Indicates whether this prerendered page can be used for the provided | 161 // Indicates whether this prerendered page can be used for the provided |
159 // URL, i.e. whether there is a match. |matching_url| is optional and will be | 162 // |url| and |session_storage_namespace|. |
160 // set to the URL that is found as a match if it is provided. | 163 bool Matches( |
161 // TODO(gavinp,mmenke): Rework matching to be based on both the URL | 164 const GURL& url, |
162 // and the session WebStorage. | 165 const content::SessionStorageNamespace* session_storage_namespace); |
dominich
2012/07/12 16:16:48
why is this method no longer const?
gavinp
2012/07/13 12:02:22
No good reason. Fixed.
On 2012/07/12 16:16:48, do
| |
163 bool MatchesURL(const GURL& url, GURL* matching_url) const; | |
164 | 166 |
165 // content::WebContentsObserver implementation. | 167 // content::WebContentsObserver implementation. |
166 virtual void DidStopLoading() OVERRIDE; | 168 virtual void DidStopLoading() OVERRIDE; |
167 virtual void DidStartProvisionalLoadForFrame( | 169 virtual void DidStartProvisionalLoadForFrame( |
168 int64 frame_id, | 170 int64 frame_id, |
169 bool is_main_frame, | 171 bool is_main_frame, |
170 const GURL& validated_url, | 172 const GURL& validated_url, |
171 bool is_error_page, | 173 bool is_error_page, |
172 content::RenderViewHost* render_view_host) OVERRIDE; | 174 content::RenderViewHost* render_view_host) OVERRIDE; |
173 virtual void DidFinishLoad(int64 frame_id, | 175 virtual void DidFinishLoad(int64 frame_id, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 // new tab. | 207 // new tab. |
206 void CommitHistory(TabContents* tab); | 208 void CommitHistory(TabContents* tab); |
207 | 209 |
208 base::Value* GetAsValue() const; | 210 base::Value* GetAsValue() const; |
209 | 211 |
210 // Returns whether a pending cross-site navigation is happening. | 212 // Returns whether a pending cross-site navigation is happening. |
211 // This could happen with renderer-issued navigations, such as a | 213 // This could happen with renderer-issued navigations, such as a |
212 // MouseEvent being dispatched by a link to a website installed as an app. | 214 // MouseEvent being dispatched by a link to a website installed as an app. |
213 bool IsCrossSiteNavigationPending() const; | 215 bool IsCrossSiteNavigationPending() const; |
214 | 216 |
215 // Adds a pending prerender to the list. | 217 // Adds a pending prerender to the list. If |weak_prerender_handle| still |
216 virtual void AddPendingPrerender(const GURL& url, | 218 // exists when this page is made visible, it will be launched. |
217 const content::Referrer& referrer, | 219 virtual void AddPendingPrerender( |
218 const gfx::Size& size); | 220 base::WeakPtr<PrerenderHandle> weak_prerender_handle, |
221 const GURL& url, | |
222 const content::Referrer& referrer, | |
223 const gfx::Size& size); | |
219 | 224 |
220 // Returns true if |url| corresponds to a pending prerender. | 225 // Returns true if |url| corresponds to a pending prerender. |
221 bool IsPendingEntry(const GURL& url) const; | 226 bool IsPendingEntry(const PrerenderHandle& prerender_handle) const; |
222 | 227 |
223 // Reissues any pending prerender requests from the prerendered page. Also | 228 // Reissues any pending prerender requests from the prerendered page. Also |
224 // clears the list of pending requests. | 229 // clears the list of pending requests. |
225 void StartPendingPrerenders(); | 230 void StartPendingPrerenders(); |
226 | 231 |
227 protected: | 232 protected: |
233 // Information on pages that the prerendered page has tried to prerender. | |
234 struct PendingPrerenderInfo { | |
235 PendingPrerenderInfo( | |
236 base::WeakPtr<PrerenderHandle> weak_prerender_handle, | |
237 const GURL& url, | |
238 const content::Referrer& referrer, | |
239 const gfx::Size& size); | |
240 ~PendingPrerenderInfo(); | |
241 | |
242 base::WeakPtr<PrerenderHandle> weak_prerender_handle; | |
243 GURL url; | |
244 content::Referrer referrer; | |
245 gfx::Size size; | |
246 }; | |
247 | |
228 PrerenderContents(PrerenderManager* prerender_manager, | 248 PrerenderContents(PrerenderManager* prerender_manager, |
229 PrerenderTracker* prerender_tracker, | 249 PrerenderTracker* prerender_tracker, |
230 Profile* profile, | 250 Profile* profile, |
231 const GURL& url, | 251 const GURL& url, |
232 const content::Referrer& referrer, | 252 const content::Referrer& referrer, |
233 Origin origin, | 253 Origin origin, |
234 uint8 experiment_id); | 254 uint8 experiment_id); |
235 | 255 |
236 // Called whenever a RenderViewHost is created for prerendering. Only called | 256 // Called whenever a RenderViewHost is created for prerendering. Only called |
237 // once the RenderViewHost has a RenderView and RenderWidgetHostView. | 257 // once the RenderViewHost has a RenderView and RenderWidgetHostView. |
238 virtual void OnRenderViewHostCreated( | 258 virtual void OnRenderViewHostCreated( |
239 content::RenderViewHost* new_render_view_host); | 259 content::RenderViewHost* new_render_view_host); |
240 | 260 |
241 content::NotificationRegistrar& notification_registrar() { | 261 content::NotificationRegistrar& notification_registrar() { |
242 return notification_registrar_; | 262 return notification_registrar_; |
243 } | 263 } |
244 | 264 |
245 const PendingPrerenderList* pending_prerender_list() const { | 265 const std::vector<PendingPrerenderInfo>* pending_prerenders() const { |
dominich
2012/07/12 16:16:48
we could return a const reference here instead, ri
gavinp
2012/07/13 12:02:22
Done.
| |
246 return &pending_prerender_list_; | 266 return &pending_prerenders_; |
247 } | 267 } |
248 | 268 |
249 bool prerendering_has_been_cancelled() const { | 269 bool prerendering_has_been_cancelled() const { |
250 return prerendering_has_been_cancelled_; | 270 return prerendering_has_been_cancelled_; |
251 } | 271 } |
252 | 272 |
253 virtual content::WebContents* CreateWebContents( | 273 virtual content::WebContents* CreateWebContents( |
254 content::SessionStorageNamespace* session_storage_namespace); | 274 content::SessionStorageNamespace* session_storage_namespace); |
255 | 275 |
256 bool prerendering_has_started_; | 276 bool prerendering_has_started_; |
257 | 277 |
278 // Time at which we started to load the URL. This is used to compute | |
279 // the time elapsed from initiating a prerender until the time the | |
280 // (potentially only partially) prerendered page is shown to the user. | |
281 base::TimeTicks load_start_time_; | |
282 | |
258 private: | 283 private: |
259 class TabContentsDelegateImpl; | 284 class TabContentsDelegateImpl; |
260 | 285 |
261 // Needs to be able to call the constructor. | 286 // Needs to be able to call the constructor. |
262 friend class PrerenderContentsFactoryImpl; | 287 friend class PrerenderContentsFactoryImpl; |
263 | 288 |
264 friend class PrerenderRenderViewHostObserver; | 289 friend class PrerenderRenderViewHostObserver; |
265 | 290 |
266 // Message handlers. | 291 // Message handlers. |
267 void OnUpdateFaviconURL(int32 page_id, const std::vector<FaviconURL>& urls); | 292 void OnUpdateFaviconURL(int32 page_id, const std::vector<FaviconURL>& urls); |
(...skipping 28 matching lines...) Expand all Loading... | |
296 int32 page_id_; | 321 int32 page_id_; |
297 GURL url_; | 322 GURL url_; |
298 GURL icon_url_; | 323 GURL icon_url_; |
299 content::NotificationRegistrar notification_registrar_; | 324 content::NotificationRegistrar notification_registrar_; |
300 | 325 |
301 // A vector of URLs that this prerendered page matches against. | 326 // A vector of URLs that this prerendered page matches against. |
302 // This array can contain more than element as a result of redirects, | 327 // This array can contain more than element as a result of redirects, |
303 // such as HTTP redirects or javascript redirects. | 328 // such as HTTP redirects or javascript redirects. |
304 std::vector<GURL> alias_urls_; | 329 std::vector<GURL> alias_urls_; |
305 | 330 |
331 // The session storage namespace id for use in Matching. We must save it | |
mmenke
2012/07/12 16:26:51
nit: matching
gavinp
2012/07/13 12:02:22
Done.
| |
332 // rather than get it from the RenderViewHost since in the control group | |
333 // we won't have a RenderViewHost. | |
334 int64 session_storage_namespace_id_; | |
335 | |
306 bool has_stopped_loading_; | 336 bool has_stopped_loading_; |
307 | 337 |
308 // True when the main frame has finished loading. | 338 // True when the main frame has finished loading. |
309 bool has_finished_loading_; | 339 bool has_finished_loading_; |
310 | 340 |
311 // This must be the same value as the PrerenderTracker has recorded for | 341 // This must be the same value as the PrerenderTracker has recorded for |
312 // |this|, when |this| has a RenderView. | 342 // |this|, when |this| has a RenderView. |
313 FinalStatus final_status_; | 343 FinalStatus final_status_; |
314 | 344 |
315 // The MatchComplete status of the prerender, indicating how it relates | 345 // The MatchComplete status of the prerender, indicating how it relates |
316 // to being a MatchComplete dummy (see definition of MatchCompleteStatus | 346 // to being a MatchComplete dummy (see definition of MatchCompleteStatus |
317 // above). | 347 // above). |
318 MatchCompleteStatus match_complete_status_; | 348 MatchCompleteStatus match_complete_status_; |
319 | 349 |
320 // Tracks whether or not prerendering has been cancelled by calling Destroy. | 350 // Tracks whether or not prerendering has been cancelled by calling Destroy. |
321 // Used solely to prevent double deletion. | 351 // Used solely to prevent double deletion. |
322 bool prerendering_has_been_cancelled_; | 352 bool prerendering_has_been_cancelled_; |
323 | 353 |
324 // Time at which we started to load the URL. This is used to compute | |
325 // the time elapsed from initiating a prerender until the time the | |
326 // (potentially only partially) prerendered page is shown to the user. | |
327 base::TimeTicks load_start_time_; | |
328 | |
329 // Process Metrics of the render process associated with the | 354 // Process Metrics of the render process associated with the |
330 // RenderViewHost for this object. | 355 // RenderViewHost for this object. |
331 scoped_ptr<base::ProcessMetrics> process_metrics_; | 356 scoped_ptr<base::ProcessMetrics> process_metrics_; |
332 | 357 |
333 // The prerendered TabContents; may be null. | 358 // The prerendered TabContents; may be null. |
334 scoped_ptr<TabContents> prerender_contents_; | 359 scoped_ptr<TabContents> prerender_contents_; |
335 | 360 |
336 scoped_ptr<PrerenderRenderViewHostObserver> render_view_host_observer_; | 361 scoped_ptr<PrerenderRenderViewHostObserver> render_view_host_observer_; |
337 | 362 |
338 scoped_ptr<TabContentsDelegateImpl> tab_contents_delegate_; | 363 scoped_ptr<TabContentsDelegateImpl> tab_contents_delegate_; |
339 | 364 |
340 // These are -1 before a RenderView is created. | 365 // These are -1 before a RenderView is created. |
341 int child_id_; | 366 int child_id_; |
342 int route_id_; | 367 int route_id_; |
343 | 368 |
344 // Origin for this prerender. | 369 // Origin for this prerender. |
345 Origin origin_; | 370 Origin origin_; |
346 | 371 |
347 // Experiment during which this prerender is performed. | 372 // Experiment during which this prerender is performed. |
348 uint8 experiment_id_; | 373 uint8 experiment_id_; |
349 | 374 |
350 // List of all pages the prerendered page has tried to prerender. | 375 // List of all pages the prerendered page has tried to prerender. |
351 PendingPrerenderList pending_prerender_list_; | 376 std::vector<PendingPrerenderInfo> pending_prerenders_; |
352 | 377 |
353 // The process that created the child id. | 378 // The process that created the child id. |
354 int creator_child_id_; | 379 int creator_child_id_; |
355 | 380 |
356 // The size of the WebView from the launching page. | 381 // The size of the WebView from the launching page. |
357 gfx::Size size_; | 382 gfx::Size size_; |
358 | 383 |
359 DISALLOW_COPY_AND_ASSIGN(PrerenderContents); | 384 DISALLOW_COPY_AND_ASSIGN(PrerenderContents); |
360 }; | 385 }; |
361 | 386 |
362 } // namespace prerender | 387 } // namespace prerender |
363 | 388 |
364 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ | 389 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_CONTENTS_H_ |
OLD | NEW |