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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/hash_tables.h" | 14 #include "base/hash_tables.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
19 #include "base/time.h" | 19 #include "base/time.h" |
20 #include "base/timer.h" | 20 #include "base/timer.h" |
21 #include "chrome/browser/prerender/prerender_config.h" | 21 #include "chrome/browser/prerender/prerender_config.h" |
22 #include "chrome/browser/prerender/prerender_contents.h" | 22 #include "chrome/browser/prerender/prerender_contents.h" |
23 #include "chrome/browser/prerender/prerender_final_status.h" | 23 #include "chrome/browser/prerender/prerender_final_status.h" |
24 #include "chrome/browser/prerender/prerender_handle.h" | |
24 #include "chrome/browser/prerender/prerender_origin.h" | 25 #include "chrome/browser/prerender/prerender_origin.h" |
25 #include "chrome/browser/profiles/profile_keyed_service.h" | 26 #include "chrome/browser/profiles/profile_keyed_service.h" |
26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
27 | 28 |
28 class Profile; | 29 class Profile; |
29 | 30 |
30 namespace base { | 31 namespace base { |
31 class DictionaryValue; | 32 class DictionaryValue; |
32 } | 33 } |
33 | 34 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 | 101 |
101 // Entry points for adding prerenders. | 102 // Entry points for adding prerenders. |
102 | 103 |
103 // Adds a prerender for |url| if valid. |process_id| and |route_id| identify | 104 // Adds a prerender for |url| if valid. |process_id| and |route_id| identify |
104 // the RenderView that the prerender request came from. The |size| may be | 105 // the RenderView that the prerender request came from. The |size| may be |
105 // empty, and the current tab size will be used if it is. If the current | 106 // empty, and the current tab size will be used if it is. If the current |
106 // active tab size cannot be found, we use a default from PrerenderConfig. | 107 // active tab size cannot be found, we use a default from PrerenderConfig. |
107 // Returns true if the URL was added, false if it was not. | 108 // Returns true if the URL was added, false if it was not. |
108 // If the launching RenderView is itself prerendering, the prerender is added | 109 // If the launching RenderView is itself prerendering, the prerender is added |
109 // as a pending prerender. | 110 // as a pending prerender. |
110 bool AddPrerenderFromLinkRelPrerender( | 111 PrerenderHandle AddPrerenderFromLinkRelPrerender( |
111 int process_id, | 112 int process_id, |
112 int route_id, | 113 int route_id, |
113 const GURL& url, | 114 const GURL& url, |
114 const content::Referrer& referrer, | 115 const content::Referrer& referrer, |
115 const gfx::Size& size); | 116 const gfx::Size& size); |
116 | 117 |
117 // Adds a prerender for |url| if valid. As the prerender request is coming | 118 // Adds a prerender for |url| if valid. As the prerender request is coming |
118 // from a source without a RenderViewHost (i.e., the omnibox) we don't have a | 119 // from a source without a RenderViewHost (i.e., the omnibox) we don't have a |
119 // child or route id, or a referrer. This method uses sensible values for | 120 // child or route id, or a referrer. This method uses sensible values for |
120 // those. The |session_storage_namespace| matches the namespace of the active | 121 // those. The |session_storage_namespace| matches the namespace of the active |
121 // tab at the time the prerender is generated from the omnibox. | 122 // tab at the time the prerender is generated from the omnibox. |
122 bool AddPrerenderFromOmnibox( | 123 PrerenderHandle AddPrerenderFromOmnibox( |
123 const GURL& url, | 124 const GURL& url, |
124 content::SessionStorageNamespace* session_storage_namespace); | 125 content::SessionStorageNamespace* session_storage_namespace); |
125 | 126 |
126 // Request cancelation of a previously added prerender. If the |active_count_| | |
127 // of the prerender is one, it will be canceled. Otherwise, |active_count_| | |
128 // will be decremented by one. | |
129 void MaybeCancelPrerender(const GURL& url); | |
dominich
2012/06/18 15:32:44
explicit cancellation is the bomb. Why would you r
| |
130 | |
131 // Destroy all prerenders for the given child route id pair and assign a final | 127 // Destroy all prerenders for the given child route id pair and assign a final |
132 // status to them. | 128 // status to them. |
133 virtual void DestroyPrerenderForRenderView(int process_id, | 129 virtual void DestroyPrerenderForRenderView(int process_id, |
134 int view_id, | 130 int view_id, |
135 FinalStatus final_status); | 131 FinalStatus final_status); |
136 | 132 |
137 // Cancels all active prerenders. | 133 // Cancels all active prerenders. |
138 void CancelAllPrerenders(); | 134 void CancelAllPrerenders(); |
139 | 135 |
140 // Cancels all active prerenders with the ORIGIN_OMNIBOX origin. | |
141 void CancelOmniboxPrerenders(); | |
142 | |
143 // If |url| matches a valid prerendered page, try to swap it into | 136 // If |url| matches a valid prerendered page, try to swap it into |
144 // |web_contents| and merge browsing histories. Returns |true| if a | 137 // |web_contents| and merge browsing histories. Returns |true| if a |
145 // prerendered page is swapped in, |false| otherwise. | 138 // prerendered page is swapped in, |false| otherwise. |
146 bool MaybeUsePrerenderedPage(content::WebContents* web_contents, | 139 bool MaybeUsePrerenderedPage(content::WebContents* web_contents, |
147 const GURL& url); | 140 const GURL& url); |
148 | 141 |
149 // Moves a PrerenderContents to the pending delete list from the list of | 142 // Moves a PrerenderContents to the pending delete list from the list of |
150 // active prerenders when prerendering should be cancelled. | 143 // active prerenders when prerendering should be cancelled. |
151 void MoveEntryToPendingDelete(PrerenderContents* entry, | 144 void MoveEntryToPendingDelete(WeakPrerenderHandle prerender, |
152 FinalStatus final_status); | 145 FinalStatus final_status); |
153 | 146 |
154 // Records the perceived page load time for a page - effectively the time from | 147 // Records the perceived page load time for a page - effectively the time from |
155 // when the user navigates to a page to when it finishes loading. The actual | 148 // when the user navigates to a page to when it finishes loading. The actual |
156 // load may have started prior to navigation due to prerender hints. | 149 // load may have started prior to navigation due to prerender hints. |
157 // This must be called on the UI thread. | 150 // This must be called on the UI thread. |
158 // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value | 151 // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value |
159 // outside that range indicating that it doesn't apply. | 152 // outside that range indicating that it doesn't apply. |
160 static void RecordPerceivedPageLoadTime( | 153 static void RecordPerceivedPageLoadTime( |
161 base::TimeDelta perceived_page_load_time, | 154 base::TimeDelta perceived_page_load_time, |
(...skipping 17 matching lines...) Expand all Loading... | |
179 static const char* GetModeString(); | 172 static const char* GetModeString(); |
180 static bool IsPrerenderingPossible(); | 173 static bool IsPrerenderingPossible(); |
181 static bool ActuallyPrerendering(); | 174 static bool ActuallyPrerendering(); |
182 static bool IsControlGroup(); | 175 static bool IsControlGroup(); |
183 static bool IsNoUseGroup(); | 176 static bool IsNoUseGroup(); |
184 | 177 |
185 // Query the list of current prerender pages to see if the given web contents | 178 // Query the list of current prerender pages to see if the given web contents |
186 // is prerendering a page. | 179 // is prerendering a page. |
187 bool IsWebContentsPrerendering(content::WebContents* web_contents) const; | 180 bool IsWebContentsPrerendering(content::WebContents* web_contents) const; |
188 | 181 |
189 // Returns true if there is a prerendered page for the given URL and it has | |
190 // finished loading. Only valid if called before MaybeUsePrerenderedPage. | |
191 bool DidPrerenderFinishLoading(const GURL& url) const; | |
192 | |
193 // Maintaining and querying the set of WebContents belonging to this | 182 // Maintaining and querying the set of WebContents belonging to this |
194 // PrerenderManager that are currently showing prerendered pages. | 183 // PrerenderManager that are currently showing prerendered pages. |
195 void MarkWebContentsAsPrerendered(content::WebContents* web_contents); | 184 void MarkWebContentsAsPrerendered(content::WebContents* web_contents); |
196 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents); | 185 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents); |
197 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents); | 186 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents); |
198 bool IsWebContentsPrerendered(content::WebContents* web_contents) const; | 187 bool IsWebContentsPrerendered(content::WebContents* web_contents) const; |
199 bool WouldWebContentsBePrerendered(content::WebContents* web_contents) const; | 188 bool WouldWebContentsBePrerendered(content::WebContents* web_contents) const; |
200 | 189 |
201 // Checks whether |url| has been recently navigated to. | 190 // Checks whether |url| has been recently navigated to. |
202 bool HasRecentlyBeenNavigatedTo(const GURL& url); | 191 bool HasRecentlyBeenNavigatedTo(const GURL& url); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 | 243 |
255 protected: | 244 protected: |
256 void SetPrerenderContentsFactory( | 245 void SetPrerenderContentsFactory( |
257 PrerenderContents::Factory* prerender_contents_factory); | 246 PrerenderContents::Factory* prerender_contents_factory); |
258 | 247 |
259 // Utility method that is called from the virtual Shutdown method on this | 248 // Utility method that is called from the virtual Shutdown method on this |
260 // class but is called directly from the TestPrerenderManager in the unit | 249 // class but is called directly from the TestPrerenderManager in the unit |
261 // tests. | 250 // tests. |
262 void DoShutdown(); | 251 void DoShutdown(); |
263 | 252 |
253 const PrerenderContents* FindPrerender(const GURL& url) const; | |
dominich
2012/06/18 15:32:44
Should this still be needed? I thought the point w
| |
254 | |
255 PrerenderContents* ClaimPrerender( | |
256 content::WebContents* web_contents, | |
257 const GURL& url, | |
258 content::SessionStorageNamespace* session_storage_namespace); | |
259 | |
264 private: | 260 private: |
265 // Test that needs needs access to internal functions. | 261 // Test that needs needs access to internal functions. |
266 friend class PrerenderBrowserTest; | 262 friend class PrerenderBrowserTest; |
263 friend class PrerenderTest; | |
267 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, AliasURLTest); | 264 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, AliasURLTest); |
268 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, CancelAllTest); | 265 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, CancelAllTest); |
269 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, | 266 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, |
270 CancelOmniboxRemovesOmniboxTest); | 267 CancelOmniboxRemovesOmniboxTest); |
271 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, | 268 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, |
272 CancelOmniboxDoesNotRemoveLinkTest); | 269 CancelOmniboxDoesNotRemoveLinkTest); |
273 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ClearTest); | 270 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ClearTest); |
274 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ControlGroup); | 271 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ControlGroup); |
275 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropOldestRequestTest); | 272 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropOldestRequestTest); |
276 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropSecondRequestTest); | 273 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropSecondRequestTest); |
(...skipping 15 matching lines...) Expand all Loading... | |
292 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, NotSoRecentlyVisited); | 289 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, NotSoRecentlyVisited); |
293 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PageMatchesFragmentTest); | 290 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PageMatchesFragmentTest); |
294 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PendingPrerenderTest); | 291 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PendingPrerenderTest); |
295 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PPLTDummy); | 292 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PPLTDummy); |
296 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitInWindowTest); | 293 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitInWindowTest); |
297 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitOutsideWindowTest); | 294 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitOutsideWindowTest); |
298 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RecentlyVisitedPPLTDummy); | 295 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RecentlyVisitedPPLTDummy); |
299 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, SourceRenderViewClosed); | 296 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, SourceRenderViewClosed); |
300 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, TwoElementPrerenderTest); | 297 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, TwoElementPrerenderTest); |
301 | 298 |
302 struct PrerenderContentsData; | 299 class OnCloseTabContentsDeleter; |
300 class PrerenderContentsData; | |
303 struct NavigationRecord; | 301 struct NavigationRecord; |
304 | 302 |
305 class OnCloseTabContentsDeleter; | 303 typedef std::list<base::WeakPtr<PrerenderContentsData> > |
dominich
2012/06/18 15:32:44
Shouldn't this now be a list of PrerenderHandles f
gavinp
2012/06/18 16:40:48
I was really hoping to avoid a lot of "IsCanceled"
| |
306 | 304 PrerenderContentsDataList; |
307 typedef std::list<PrerenderContentsData> PrerenderContentsDataList; | |
308 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; | 305 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; |
309 | 306 |
310 // Time window for which we record old navigations, in milliseconds. | 307 // Time window for which we record old navigations, in milliseconds. |
311 static const int kNavigationRecordWindowMs = 5000; | 308 static const int kNavigationRecordWindowMs = 5000; |
312 | 309 |
313 // Adds a prerender for |url| from referrer |referrer| initiated from the | 310 // Adds a prerender for |url| from referrer |referrer| initiated from the |
314 // child process specified by |child_id|. The |origin| specifies how the | 311 // child process specified by |child_id|. The |origin| specifies how the |
315 // prerender was added. If the |size| is empty, then | 312 // prerender was added. If the |size| is empty, then |
316 // PrerenderContents::StartPrerendering will instead use the size of the | 313 // PrerenderContents::StartPrerendering will instead use the size of the |
317 // currently active tab. If the current active tab size cannot be found, it | 314 // currently active tab. If the current active tab size cannot be found, it |
318 // then uses a default from PrerenderConfig. | 315 // then uses a default from PrerenderConfig. |
319 bool AddPrerender( | 316 PrerenderHandle AddPrerender( |
320 Origin origin, | 317 Origin origin, |
321 int child_id, | 318 int child_id, |
322 const GURL& url, | 319 const GURL& url, |
323 const content::Referrer& referrer, | 320 const content::Referrer& referrer, |
324 const gfx::Size& size, | 321 const gfx::Size& size, |
325 content::SessionStorageNamespace* session_storage_namespace); | 322 content::SessionStorageNamespace* session_storage_namespace); |
326 | 323 |
327 // Retrieves the PrerenderContents object for the specified URL, if it | |
328 // has been prerendered. The caller will then have ownership of the | |
329 // PrerenderContents object and is responsible for freeing it. | |
330 // Returns NULL if the specified URL has not been prerendered. | |
331 PrerenderContents* GetEntry(const GURL& url); | |
332 | |
333 // Identical to GetEntry, with one exception: | |
334 // The WebContents specified indicates the WC in which to swap the | |
335 // prerendering into. If the WebContents specified is the one | |
336 // to doing the prerendered itself, will return NULL. | |
337 PrerenderContents* GetEntryButNotSpecifiedWC(const GURL& url, | |
338 content::WebContents* wc); | |
339 | |
340 // Starts scheduling periodic cleanups. | 324 // Starts scheduling periodic cleanups. |
341 void StartSchedulingPeriodicCleanups(); | 325 void StartSchedulingPeriodicCleanups(); |
342 // Stops scheduling periodic cleanups if they're no longer needed. | 326 // Stops scheduling periodic cleanups if they're no longer needed. |
343 void MaybeStopSchedulingPeriodicCleanups(); | 327 void MaybeStopSchedulingPeriodicCleanups(); |
344 | 328 |
345 // Deletes stale and cancelled prerendered PrerenderContents, as well as | 329 // Deletes stale and cancelled prerendered PrerenderContents, as well as |
346 // WebContents that have been replaced by prerendered WebContents. | 330 // WebContents that have been replaced by prerendered WebContents. |
347 // Also identifies and kills PrerenderContents that use too much | 331 // Also identifies and kills PrerenderContents that use too much |
348 // resources. | 332 // resources. |
349 void PeriodicCleanup(); | 333 void PeriodicCleanup(); |
350 | 334 |
351 // Posts a task to call PeriodicCleanup. Results in quicker destruction of | 335 // Posts a task to call PeriodicCleanup. Results in quicker destruction of |
352 // objects. If |this| is deleted before the task is run, the task will | 336 // objects. If |this| is deleted before the task is run, the task will |
353 // automatically be cancelled. | 337 // automatically be cancelled. |
354 void PostCleanupTask(); | 338 void PostCleanupTask(); |
355 | 339 |
356 base::TimeDelta GetMaxAge() const; | 340 base::TimeDelta GetMaxAge() const; |
357 bool IsPrerenderElementFresh(const base::Time start) const; | |
358 void DeleteOldEntries(); | 341 void DeleteOldEntries(); |
359 virtual base::Time GetCurrentTime() const; | 342 virtual base::Time GetCurrentTime() const; |
360 virtual base::TimeTicks GetCurrentTimeTicks() const; | 343 virtual base::TimeTicks GetCurrentTimeTicks() const; |
361 virtual PrerenderContents* CreatePrerenderContents( | 344 virtual PrerenderContents* CreatePrerenderContents( |
362 const GURL& url, | 345 const GURL& url, |
363 const content::Referrer& referrer, | 346 const content::Referrer& referrer, |
364 Origin origin, | 347 Origin origin, |
365 uint8 experiment_id); | 348 uint8 experiment_id); |
366 | 349 |
367 // Deletes any PrerenderContents that have been added to the pending delete | 350 // Deletes any PrerenderContents that have been added to the pending delete |
368 // list. | 351 // list. |
369 void DeletePendingDeleteEntries(); | 352 void DeletePendingDeleteEntries(); |
370 | 353 |
371 // Finds the specified PrerenderContentsData/PrerenderContents and returns it, | 354 const PrerenderContentsData* FindContentsData(PrerenderHandle handle) const; |
372 // if it exists. Returns NULL otherwise. Unlike GetEntry, the | 355 PrerenderContentsData* FindContentsDataMutable(PrerenderHandle handle); |
373 // PrerenderManager maintains ownership of the PrerenderContents. | |
374 PrerenderContentsData* FindEntryData(const GURL& url); | |
375 PrerenderContents* FindEntry(const GURL& url) const; | |
376 | 356 |
377 // Returns the iterator to the PrerenderContentsData entry that is being | 357 PrerenderContentsData* |
378 // prerendered from the given child route id pair. | 358 FindContentsDataForChildAndRouteId(int child_id, int route_id); |
379 PrerenderContentsDataList::iterator | |
380 FindPrerenderContentsForChildRouteIdPair( | |
381 const std::pair<int, int>& child_route_id_pair); | |
382 | |
383 PrerenderContentsDataList::iterator | |
384 FindPrerenderContentsForURL(const GURL& url); | |
385 | 359 |
386 bool DoesRateLimitAllowPrerender() const; | 360 bool DoesRateLimitAllowPrerender() const; |
387 | 361 |
388 // Deletes old WebContents that have been replaced by prerendered ones. This | 362 // Deletes old WebContents that have been replaced by prerendered ones. This |
389 // is needed because they're replaced in a callback from the old WebContents, | 363 // is needed because they're replaced in a callback from the old WebContents, |
390 // so cannot immediately be deleted. | 364 // so cannot immediately be deleted. |
391 void DeleteOldTabContents(); | 365 void DeleteOldTabContents(); |
392 | 366 |
393 // Cleans up old NavigationRecord's. | 367 // Cleans up old NavigationRecord's. |
394 void CleanUpOldNavigations(); | 368 void CleanUpOldNavigations(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 | 472 |
499 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); | 473 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); |
500 }; | 474 }; |
501 | 475 |
502 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( | 476 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( |
503 int render_process_id); | 477 int render_process_id); |
504 | 478 |
505 } // namespace prerender | 479 } // namespace prerender |
506 | 480 |
507 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ | 481 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ |
OLD | NEW |