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