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_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // load may have started prior to navigation due to prerender hints. | 152 // load may have started prior to navigation due to prerender hints. |
153 // This must be called on the UI thread. | 153 // This must be called on the UI thread. |
154 // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value | 154 // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value |
155 // outside that range indicating that it doesn't apply. | 155 // outside that range indicating that it doesn't apply. |
156 static void RecordPerceivedPageLoadTime( | 156 static void RecordPerceivedPageLoadTime( |
157 base::TimeDelta perceived_page_load_time, | 157 base::TimeDelta perceived_page_load_time, |
158 double fraction_plt_elapsed_at_swap_in, | 158 double fraction_plt_elapsed_at_swap_in, |
159 content::WebContents* web_contents, | 159 content::WebContents* web_contents, |
160 const GURL& url); | 160 const GURL& url); |
161 | 161 |
| 162 // Records the percentage of pixels of the final page in place at swap-in. |
| 163 void RecordFractionPixelsFinalAtSwapin( |
| 164 content::WebContents* web_contents, |
| 165 double fraction); |
| 166 |
162 // Set whether prerendering is currently enabled for this manager. | 167 // Set whether prerendering is currently enabled for this manager. |
163 // Must be called on the UI thread. | 168 // Must be called on the UI thread. |
164 // If |enabled| is false, existing prerendered pages will still persist until | 169 // If |enabled| is false, existing prerendered pages will still persist until |
165 // they time out, but new ones will not be generated. | 170 // they time out, but new ones will not be generated. |
166 void set_enabled(bool enabled); | 171 void set_enabled(bool enabled); |
167 | 172 |
168 // Controls if we launch or squash prefetch requests as they arrive from | 173 // Controls if we launch or squash prefetch requests as they arrive from |
169 // renderers. | 174 // renderers. |
170 static bool IsPrefetchEnabled(); | 175 static bool IsPrefetchEnabled(); |
171 static void SetIsPrefetchEnabled(bool enabled); | 176 static void SetIsPrefetchEnabled(bool enabled); |
172 | 177 |
173 static PrerenderManagerMode GetMode(); | 178 static PrerenderManagerMode GetMode(); |
174 static void SetMode(PrerenderManagerMode mode); | 179 static void SetMode(PrerenderManagerMode mode); |
175 static const char* GetModeString(); | 180 static const char* GetModeString(); |
176 static bool IsPrerenderingPossible(); | 181 static bool IsPrerenderingPossible(); |
177 static bool ActuallyPrerendering(); | 182 static bool ActuallyPrerendering(); |
178 static bool IsControlGroup(); | 183 static bool IsControlGroup(); |
179 static bool IsNoUseGroup(); | 184 static bool IsNoUseGroup(); |
180 | 185 |
181 // Query the list of current prerender pages to see if the given web contents | 186 // Query the list of current prerender pages to see if the given web contents |
182 // is prerendering a page. | 187 // is prerendering a page. The optional parameter |origin| is an output |
183 bool IsWebContentsPrerendering(content::WebContents* web_contents) const; | 188 // parameter which, if a prerender is found, is set to the Origin of the |
| 189 // prerender |web_contents|. |
| 190 bool IsWebContentsPrerendering(content::WebContents* web_contents, |
| 191 Origin* origin) const; |
184 | 192 |
185 // Returns the PrerenderContents object for the given web_contents if it's | 193 // Returns the PrerenderContents object for the given web_contents if it's |
186 // used for an active prerender page, otherwise returns NULL. | 194 // used for an active prerender page, otherwise returns NULL. |
187 PrerenderContents* GetPrerenderContents( | 195 PrerenderContents* GetPrerenderContents( |
188 content::WebContents* web_contents) const; | 196 content::WebContents* web_contents) const; |
189 | 197 |
190 // Maintaining and querying the set of WebContents belonging to this | 198 // Maintaining and querying the set of WebContents belonging to this |
191 // PrerenderManager that are currently showing prerendered pages. | 199 // PrerenderManager that are currently showing prerendered pages. |
192 void MarkWebContentsAsPrerendered(content::WebContents* web_contents); | 200 void MarkWebContentsAsPrerendered(content::WebContents* web_contents, |
193 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents); | 201 Origin origin); |
| 202 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents, |
| 203 Origin origin); |
194 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents); | 204 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents); |
195 bool IsWebContentsPrerendered(content::WebContents* web_contents) const; | 205 |
196 bool WouldWebContentsBePrerendered(content::WebContents* web_contents) const; | 206 // Returns true if |web_contents| was originally a prerender that has since |
| 207 // been swapped in. The optional parameter |origin| is an output parameter |
| 208 // which, if a prerender is found, is set to the Origin of the prerender of |
| 209 // |web_contents|. |
| 210 bool IsWebContentsPrerendered(content::WebContents* web_contents, |
| 211 Origin* origin) const; |
| 212 bool WouldWebContentsBePrerendered(content::WebContents* web_contents, |
| 213 Origin* origin) const; |
197 | 214 |
198 // Checks whether |url| has been recently navigated to. | 215 // Checks whether |url| has been recently navigated to. |
199 bool HasRecentlyBeenNavigatedTo(const GURL& url); | 216 bool HasRecentlyBeenNavigatedTo(Origin origin, const GURL& url); |
200 | 217 |
201 // Returns true if the method given is invalid for prerendering. | 218 // Returns true if the method given is invalid for prerendering. |
202 static bool IsValidHttpMethod(const std::string& method); | 219 static bool IsValidHttpMethod(const std::string& method); |
203 | 220 |
204 // Returns a Value object containing the active pages being prerendered, and | 221 // Returns a Value object containing the active pages being prerendered, and |
205 // a history of pages which were prerendered. The caller is responsible for | 222 // a history of pages which were prerendered. The caller is responsible for |
206 // deleting the return value. | 223 // deleting the return value. |
207 base::DictionaryValue* GetAsValue() const; | 224 base::DictionaryValue* GetAsValue() const; |
208 | 225 |
209 // Clears the data indicated by which bits of clear_flags are set. | 226 // Clears the data indicated by which bits of clear_flags are set. |
(...skipping 25 matching lines...) Expand all Loading... |
235 | 252 |
236 // Adds a condition. This is owned by the PrerenderManager. | 253 // Adds a condition. This is owned by the PrerenderManager. |
237 void AddCondition(const PrerenderCondition* condition); | 254 void AddCondition(const PrerenderCondition* condition); |
238 | 255 |
239 // Records that some visible tab navigated (or was redirected) to the | 256 // Records that some visible tab navigated (or was redirected) to the |
240 // provided URL. | 257 // provided URL. |
241 void RecordNavigation(const GURL& url); | 258 void RecordNavigation(const GURL& url); |
242 | 259 |
243 Profile* profile() const { return profile_; } | 260 Profile* profile() const { return profile_; } |
244 | 261 |
245 PrerenderHistograms* histograms() const { return histograms_.get(); } | |
246 | |
247 protected: | 262 protected: |
248 class PrerenderData : public base::SupportsWeakPtr<PrerenderData> { | 263 class PrerenderData : public base::SupportsWeakPtr<PrerenderData> { |
249 public: | 264 public: |
250 // Constructor for a pending prerender, which will get its contents later. | 265 // Constructor for a pending prerender, which will get its contents later. |
251 explicit PrerenderData(PrerenderManager* manager); | 266 explicit PrerenderData(PrerenderManager* manager); |
252 | 267 |
253 // Constructor for an active prerender. | 268 // Constructor for an active prerender. |
254 PrerenderData(PrerenderManager* manager, | 269 PrerenderData(PrerenderManager* manager, |
255 PrerenderContents* contents, | 270 PrerenderContents* contents, |
256 base::TimeTicks expiry_time); | 271 base::TimeTicks expiry_time); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 | 343 |
329 private: | 344 private: |
330 friend class PrerenderBrowserTest; | 345 friend class PrerenderBrowserTest; |
331 friend class PrerenderContents; | 346 friend class PrerenderContents; |
332 friend class PrerenderHandle; | 347 friend class PrerenderHandle; |
333 friend class UnitTestPrerenderManager; | 348 friend class UnitTestPrerenderManager; |
334 | 349 |
335 class OnCloseTabContentsDeleter; | 350 class OnCloseTabContentsDeleter; |
336 struct NavigationRecord; | 351 struct NavigationRecord; |
337 | 352 |
338 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; | 353 // For each WebContents that is swapped in, we store a |
| 354 // PrerenderedWebContentsData so that we can track the origin of the |
| 355 // prerender. |
| 356 struct PrerenderedWebContentsData { |
| 357 explicit PrerenderedWebContentsData(Origin origin); |
| 358 |
| 359 Origin origin; |
| 360 }; |
| 361 |
| 362 // In the control group experimental group for each WebContents "not swapped |
| 363 // in" we create a WouldBePrerenderedWebContentsData to the origin of the |
| 364 // "prerender" we did not launch. We also track a state machine to ensure |
| 365 // the histogram reporting tracks what histograms would have done. |
| 366 struct WouldBePrerenderedWebContentsData { |
| 367 // When the WebContents gets a provisional load, we'd like to remove the |
| 368 // WebContents from the map since the new navigation would not have swapped |
| 369 // in a prerender. But the first provisional load after the control |
| 370 // prerender is not "swapped in" is actually to the prerendered location! So |
| 371 // we don't remove the item from the map on the first provisional load, but |
| 372 // we do for subsequent loads. |
| 373 enum State { |
| 374 WAITING_FOR_PROVISIONAL_LOAD, |
| 375 SEEN_PROVISIONAL_LOAD, |
| 376 }; |
| 377 |
| 378 explicit WouldBePrerenderedWebContentsData(Origin origin); |
| 379 |
| 380 Origin origin; |
| 381 State state; |
| 382 }; |
339 | 383 |
340 // Time interval before a new prerender is allowed. | 384 // Time interval before a new prerender is allowed. |
341 static const int kMinTimeBetweenPrerendersMs = 500; | 385 static const int kMinTimeBetweenPrerendersMs = 500; |
342 | 386 |
343 // Time window for which we record old navigations, in milliseconds. | 387 // Time window for which we record old navigations, in milliseconds. |
344 static const int kNavigationRecordWindowMs = 5000; | 388 static const int kNavigationRecordWindowMs = 5000; |
345 | 389 |
346 void OnCancelPrerenderHandle(PrerenderData* prerender_data); | 390 void OnCancelPrerenderHandle(PrerenderData* prerender_data); |
347 | 391 |
348 // Adds a prerender for |url| from |referrer| initiated from the process | 392 // Adds a prerender for |url| from |referrer| initiated from the process |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // If |child_id| and |route_id| correspond to a RenderView that is an active | 448 // If |child_id| and |route_id| correspond to a RenderView that is an active |
405 // prerender, returns the PrerenderData object for that prerender. Otherwise, | 449 // prerender, returns the PrerenderData object for that prerender. Otherwise, |
406 // returns NULL. | 450 // returns NULL. |
407 PrerenderData* FindPrerenderDataForChildAndRoute(int child_id, int route_id); | 451 PrerenderData* FindPrerenderDataForChildAndRoute(int child_id, int route_id); |
408 | 452 |
409 // Given the |prerender_contents|, find the iterator in active_prerender_list_ | 453 // Given the |prerender_contents|, find the iterator in active_prerender_list_ |
410 // correponding to the given prerender. | 454 // correponding to the given prerender. |
411 std::list<linked_ptr<PrerenderData> >::iterator | 455 std::list<linked_ptr<PrerenderData> >::iterator |
412 FindIteratorForPrerenderContents(PrerenderContents* prerender_contents); | 456 FindIteratorForPrerenderContents(PrerenderContents* prerender_contents); |
413 | 457 |
414 bool DoesRateLimitAllowPrerender() const; | 458 bool DoesRateLimitAllowPrerender(Origin origin) const; |
415 | 459 |
416 // Deletes old WebContents that have been replaced by prerendered ones. This | 460 // Deletes old WebContents that have been replaced by prerendered ones. This |
417 // is needed because they're replaced in a callback from the old WebContents, | 461 // is needed because they're replaced in a callback from the old WebContents, |
418 // so cannot immediately be deleted. | 462 // so cannot immediately be deleted. |
419 void DeleteOldTabContents(); | 463 void DeleteOldTabContents(); |
420 | 464 |
421 // Cleans up old NavigationRecord's. | 465 // Cleans up old NavigationRecord's. |
422 void CleanUpOldNavigations(); | 466 void CleanUpOldNavigations(); |
423 | 467 |
424 // Arrange for the given tab contents to be deleted asap. If deleter is not | 468 // Arrange for the given tab contents to be deleted asap. If deleter is not |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 // List of all pending prerenders. | 521 // List of all pending prerenders. |
478 std::list<linked_ptr<PrerenderData> > pending_prerender_list_; | 522 std::list<linked_ptr<PrerenderData> > pending_prerender_list_; |
479 | 523 |
480 // List of recent navigations in this profile, sorted by ascending | 524 // List of recent navigations in this profile, sorted by ascending |
481 // navigate_time_. | 525 // navigate_time_. |
482 std::list<NavigationRecord> navigations_; | 526 std::list<NavigationRecord> navigations_; |
483 | 527 |
484 // List of prerender elements to be deleted | 528 // List of prerender elements to be deleted |
485 std::list<PrerenderContents*> pending_delete_list_; | 529 std::list<PrerenderContents*> pending_delete_list_; |
486 | 530 |
487 // Set of WebContents which are currently displaying a prerendered page. | 531 // This map is from all WebContents which are currently displaying a |
488 base::hash_set<content::WebContents*> prerendered_tab_contents_set_; | 532 // prerendered page which has already been swapped in to a |
| 533 // PrerenderedWebContentsData for tracking full lifetime information |
| 534 // on prerenders. |
| 535 base::hash_map<content::WebContents*, PrerenderedWebContentsData> |
| 536 prerendered_web_contents_data_; |
489 | 537 |
490 // WebContents that would have been swapped out for a prerendered WebContents | 538 // WebContents that would have been swapped out for a prerendered WebContents |
491 // if the user was not part of the control group for measurement. When the | 539 // if the user was not part of the control group for measurement. When the |
492 // WebContents gets a provisional load, the WebContents is removed from | 540 // WebContents gets a provisional load, the WebContents is removed from |
493 // the map since the new navigation would not have swapped in a prerender. | 541 // the map since the new navigation would not have swapped in a prerender. |
494 // However, one complication exists because the first provisional load after | 542 // However, one complication exists because the first provisional load after |
495 // the WebContents is marked as "Would Have Been Prerendered" is actually to | 543 // the WebContents is marked as "Would Have Been Prerendered" is actually to |
496 // the prerendered location. So, we need to keep a boolean around that does | 544 // the prerendered location. So, we need to keep a state around that does |
497 // not clear the item from the map on the first provisional load, but does | 545 // not clear the item from the map on the first provisional load, but does |
498 // for subsequent loads. | 546 // for subsequent loads. |
499 WouldBePrerenderedMap would_be_prerendered_map_; | 547 base::hash_map<content::WebContents*, WouldBePrerenderedWebContentsData> |
| 548 would_be_prerendered_map_; |
500 | 549 |
501 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_; | 550 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_; |
502 | 551 |
503 static PrerenderManagerMode mode_; | 552 static PrerenderManagerMode mode_; |
504 | 553 |
505 // A count of how many prerenders we do per session. Initialized to 0 then | 554 // A count of how many prerenders we do per session. Initialized to 0 then |
506 // incremented and emitted to a histogram on each successful prerender. | 555 // incremented and emitted to a histogram on each successful prerender. |
507 static int prerenders_per_session_count_; | 556 static int prerenders_per_session_count_; |
508 | 557 |
509 // RepeatingTimer to perform periodic cleanups of pending prerendered | 558 // RepeatingTimer to perform periodic cleanups of pending prerendered |
(...skipping 20 matching lines...) Expand all Loading... |
530 | 579 |
531 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); | 580 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); |
532 }; | 581 }; |
533 | 582 |
534 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( | 583 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( |
535 int render_process_id); | 584 int render_process_id); |
536 | 585 |
537 } // namespace prerender | 586 } // namespace prerender |
538 | 587 |
539 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ | 588 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ |
OLD | NEW |