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(); } | 262 PrerenderHistograms* histograms() { return histograms_.get(); } |
246 | 263 |
247 protected: | 264 protected: |
248 class PrerenderData : public base::SupportsWeakPtr<PrerenderData> { | 265 class PrerenderData : public base::SupportsWeakPtr<PrerenderData> { |
249 public: | 266 public: |
250 // Constructor for a pending prerender, which will get its contents later. | 267 // Constructor for a pending prerender, which will get its contents later. |
251 explicit PrerenderData(PrerenderManager* manager); | 268 explicit PrerenderData(PrerenderManager* manager); |
252 | 269 |
253 // Constructor for an active prerender. | 270 // Constructor for an active prerender. |
254 PrerenderData(PrerenderManager* manager, | 271 PrerenderData(PrerenderManager* manager, |
255 PrerenderContents* contents, | 272 PrerenderContents* contents, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 void DoShutdown(); | 344 void DoShutdown(); |
328 | 345 |
329 private: | 346 private: |
330 friend class PrerenderBrowserTest; | 347 friend class PrerenderBrowserTest; |
331 friend class PrerenderContents; | 348 friend class PrerenderContents; |
332 friend class PrerenderHandle; | 349 friend class PrerenderHandle; |
333 friend class UnitTestPrerenderManager; | 350 friend class UnitTestPrerenderManager; |
334 | 351 |
335 class OnCloseTabContentsDeleter; | 352 class OnCloseTabContentsDeleter; |
336 struct NavigationRecord; | 353 struct NavigationRecord; |
| 354 struct PrerenderedWebContentsData { |
| 355 Origin origin; |
337 | 356 |
338 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; | 357 explicit PrerenderedWebContentsData(Origin origin); |
| 358 }; |
| 359 |
| 360 struct WouldBePrerenderedWebContentsData { |
| 361 enum State { |
| 362 WAITING_FOR_PROVISIONAL_LOAD, |
| 363 SEEN_PROVISIONAL_LOAD, |
| 364 }; |
| 365 |
| 366 static const State INITIAL_STATE = WAITING_FOR_PROVISIONAL_LOAD; |
| 367 |
| 368 Origin origin; |
| 369 State state; |
| 370 |
| 371 explicit WouldBePrerenderedWebContentsData(Origin origin); |
| 372 }; |
339 | 373 |
340 // Time interval before a new prerender is allowed. | 374 // Time interval before a new prerender is allowed. |
341 static const int kMinTimeBetweenPrerendersMs = 500; | 375 static const int kMinTimeBetweenPrerendersMs = 500; |
342 | 376 |
343 // Time window for which we record old navigations, in milliseconds. | 377 // Time window for which we record old navigations, in milliseconds. |
344 static const int kNavigationRecordWindowMs = 5000; | 378 static const int kNavigationRecordWindowMs = 5000; |
345 | 379 |
346 void OnCancelPrerenderHandle(PrerenderData* prerender_data); | 380 void OnCancelPrerenderHandle(PrerenderData* prerender_data); |
347 | 381 |
348 // Adds a prerender for |url| from |referrer| initiated from the process | 382 // 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 | 438 // If |child_id| and |route_id| correspond to a RenderView that is an active |
405 // prerender, returns the PrerenderData object for that prerender. Otherwise, | 439 // prerender, returns the PrerenderData object for that prerender. Otherwise, |
406 // returns NULL. | 440 // returns NULL. |
407 PrerenderData* FindPrerenderDataForChildAndRoute(int child_id, int route_id); | 441 PrerenderData* FindPrerenderDataForChildAndRoute(int child_id, int route_id); |
408 | 442 |
409 // Given the |prerender_contents|, find the iterator in active_prerender_list_ | 443 // Given the |prerender_contents|, find the iterator in active_prerender_list_ |
410 // correponding to the given prerender. | 444 // correponding to the given prerender. |
411 std::list<linked_ptr<PrerenderData> >::iterator | 445 std::list<linked_ptr<PrerenderData> >::iterator |
412 FindIteratorForPrerenderContents(PrerenderContents* prerender_contents); | 446 FindIteratorForPrerenderContents(PrerenderContents* prerender_contents); |
413 | 447 |
414 bool DoesRateLimitAllowPrerender() const; | 448 bool DoesRateLimitAllowPrerender(Origin origin) const; |
415 | 449 |
416 // Deletes old WebContents that have been replaced by prerendered ones. This | 450 // 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, | 451 // is needed because they're replaced in a callback from the old WebContents, |
418 // so cannot immediately be deleted. | 452 // so cannot immediately be deleted. |
419 void DeleteOldTabContents(); | 453 void DeleteOldTabContents(); |
420 | 454 |
421 // Cleans up old NavigationRecord's. | 455 // Cleans up old NavigationRecord's. |
422 void CleanUpOldNavigations(); | 456 void CleanUpOldNavigations(); |
423 | 457 |
424 // Arrange for the given tab contents to be deleted asap. If deleter is not | 458 // 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. | 511 // List of all pending prerenders. |
478 std::list<linked_ptr<PrerenderData> > pending_prerender_list_; | 512 std::list<linked_ptr<PrerenderData> > pending_prerender_list_; |
479 | 513 |
480 // List of recent navigations in this profile, sorted by ascending | 514 // List of recent navigations in this profile, sorted by ascending |
481 // navigate_time_. | 515 // navigate_time_. |
482 std::list<NavigationRecord> navigations_; | 516 std::list<NavigationRecord> navigations_; |
483 | 517 |
484 // List of prerender elements to be deleted | 518 // List of prerender elements to be deleted |
485 std::list<PrerenderContents*> pending_delete_list_; | 519 std::list<PrerenderContents*> pending_delete_list_; |
486 | 520 |
487 // Set of WebContents which are currently displaying a prerendered page. | 521 // This map is from all WebContents which are currently displaying a |
488 base::hash_set<content::WebContents*> prerendered_tab_contents_set_; | 522 // prerendered page which has already been swapped in to a |
| 523 // PrerenderedWebContentsData for tracking full lifetime information |
| 524 // on prerenders. |
| 525 base::hash_map<content::WebContents*, PrerenderedWebContentsData> |
| 526 prerendered_web_contents_data_; |
489 | 527 |
490 // WebContents that would have been swapped out for a prerendered WebContents | 528 // 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 | 529 // 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 | 530 // WebContents gets a provisional load, the WebContents is removed from |
493 // the map since the new navigation would not have swapped in a prerender. | 531 // the map since the new navigation would not have swapped in a prerender. |
494 // However, one complication exists because the first provisional load after | 532 // However, one complication exists because the first provisional load after |
495 // the WebContents is marked as "Would Have Been Prerendered" is actually to | 533 // 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 | 534 // 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 | 535 // not clear the item from the map on the first provisional load, but does |
498 // for subsequent loads. | 536 // for subsequent loads. |
499 WouldBePrerenderedMap would_be_prerendered_map_; | 537 base::hash_map<content::WebContents*, WouldBePrerenderedWebContentsData> |
| 538 would_be_prerendered_map_; |
500 | 539 |
501 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_; | 540 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_; |
502 | 541 |
503 static PrerenderManagerMode mode_; | 542 static PrerenderManagerMode mode_; |
504 | 543 |
505 // A count of how many prerenders we do per session. Initialized to 0 then | 544 // 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. | 545 // incremented and emitted to a histogram on each successful prerender. |
507 static int prerenders_per_session_count_; | 546 static int prerenders_per_session_count_; |
508 | 547 |
509 // RepeatingTimer to perform periodic cleanups of pending prerendered | 548 // RepeatingTimer to perform periodic cleanups of pending prerendered |
(...skipping 20 matching lines...) Expand all Loading... |
530 | 569 |
531 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); | 570 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); |
532 }; | 571 }; |
533 | 572 |
534 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( | 573 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( |
535 int render_process_id); | 574 int render_process_id); |
536 | 575 |
537 } // namespace prerender | 576 } // namespace prerender |
538 | 577 |
539 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ | 578 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ |
OLD | NEW |