Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: chrome/browser/prerender/prerender_manager.h

Issue 10553029: Handle interface to prerenders. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediated, and cleaned up in prep for uploading for review Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 gfx::Size size); 116 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 gfx::Size size); 126 gfx::Size size);
126 127
127 // Request cancelation of a previously added prerender. If the |active_count_|
128 // of the prerender is one, it will be canceled. Otherwise, |active_count_|
129 // will be decremented by one.
130 void MaybeCancelPrerender(const GURL& url);
131
132 // Destroy all prerenders for the given child route id pair and assign a final 128 // Destroy all prerenders for the given child route id pair and assign a final
133 // status to them. 129 // status to them.
134 virtual void DestroyPrerenderForRenderView(int process_id, 130 virtual void DestroyPrerenderForRenderView(int process_id,
135 int view_id, 131 int view_id,
136 FinalStatus final_status); 132 FinalStatus final_status);
137 133
138 // Cancels all active prerenders. 134 // Cancels all active prerenders.
139 void CancelAllPrerenders(); 135 void CancelAllPrerenders();
140 136
141 // If |url| matches a valid prerendered page, try to swap it into 137 // If |url| matches a valid prerendered page, try to swap it into
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 static const char* GetModeString(); 173 static const char* GetModeString();
178 static bool IsPrerenderingPossible(); 174 static bool IsPrerenderingPossible();
179 static bool ActuallyPrerendering(); 175 static bool ActuallyPrerendering();
180 static bool IsControlGroup(); 176 static bool IsControlGroup();
181 static bool IsNoUseGroup(); 177 static bool IsNoUseGroup();
182 178
183 // Query the list of current prerender pages to see if the given web contents 179 // Query the list of current prerender pages to see if the given web contents
184 // is prerendering a page. 180 // is prerendering a page.
185 bool IsWebContentsPrerendering(content::WebContents* web_contents) const; 181 bool IsWebContentsPrerendering(content::WebContents* web_contents) const;
186 182
187 // Returns true if there is a prerendered page for the given URL and it has
188 // finished loading. Only valid if called before MaybeUsePrerenderedPage.
189 bool DidPrerenderFinishLoading(const GURL& url) const;
190
191 // Maintaining and querying the set of WebContents belonging to this 183 // Maintaining and querying the set of WebContents belonging to this
192 // PrerenderManager that are currently showing prerendered pages. 184 // PrerenderManager that are currently showing prerendered pages.
193 void MarkWebContentsAsPrerendered(content::WebContents* web_contents); 185 void MarkWebContentsAsPrerendered(content::WebContents* web_contents);
194 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents); 186 void MarkWebContentsAsWouldBePrerendered(content::WebContents* web_contents);
195 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents); 187 void MarkWebContentsAsNotPrerendered(content::WebContents* web_contents);
196 bool IsWebContentsPrerendered(content::WebContents* web_contents) const; 188 bool IsWebContentsPrerendered(content::WebContents* web_contents) const;
197 bool WouldWebContentsBePrerendered(content::WebContents* web_contents) const; 189 bool WouldWebContentsBePrerendered(content::WebContents* web_contents) const;
198 190
199 // Checks whether |url| has been recently navigated to. 191 // Checks whether |url| has been recently navigated to.
200 bool HasRecentlyBeenNavigatedTo(const GURL& url); 192 bool HasRecentlyBeenNavigatedTo(const GURL& url);
(...skipping 29 matching lines...) Expand all
230 FinalStatus final_status) const; 222 FinalStatus final_status) const;
231 223
232 const Config& config() const { return config_; } 224 const Config& config() const { return config_; }
233 Config& mutable_config() { return config_; } 225 Config& mutable_config() { return config_; }
234 226
235 PrerenderTracker* prerender_tracker() { return prerender_tracker_; } 227 PrerenderTracker* prerender_tracker() { return prerender_tracker_; }
236 228
237 // Adds a condition. This is owned by the PrerenderManager. 229 // Adds a condition. This is owned by the PrerenderManager.
238 void AddCondition(const PrerenderCondition* condition); 230 void AddCondition(const PrerenderCondition* condition);
239 231
240 bool IsPendingEntry(const GURL& url) const;
241
242 // Returns true if |url| matches any URLs being prerendered.
243 bool IsPrerendering(const GURL& url) const;
244
245 // Records that some visible tab navigated (or was redirected) to the 232 // Records that some visible tab navigated (or was redirected) to the
246 // provided URL. 233 // provided URL.
247 void RecordNavigation(const GURL& url); 234 void RecordNavigation(const GURL& url);
248 235
249 Profile* profile() const { return profile_; } 236 Profile* profile() const { return profile_; }
250 237
251 PrerenderHistograms* histograms() const { return histograms_.get(); } 238 PrerenderHistograms* histograms() const { return histograms_.get(); }
252 239
253 protected: 240 protected:
241 friend class PrerenderContents;
242
243 // Time window for which we record old navigations, in milliseconds.
244 static const int kNavigationRecordWindowMs = 5000;
245
254 void SetPrerenderContentsFactory( 246 void SetPrerenderContentsFactory(
255 PrerenderContents::Factory* prerender_contents_factory); 247 PrerenderContents::Factory* prerender_contents_factory);
256 248
249 // Adds a prerender from a pending Prerender, called by
250 // PrerenderContents::StartPendingPrerenders.
251 void StartPendingPrerender(
252 PrerenderHandleImpl* existing_prerender_handle,
253 Origin origin,
254 int process_id,
255 const GURL& url,
256 const content::Referrer& referrer,
257 const gfx::Size& size,
258 content::SessionStorageNamespace* session_storage_namespace);
259
257 // Utility method that is called from the virtual Shutdown method on this 260 // Utility method that is called from the virtual Shutdown method on this
258 // class but is called directly from the TestPrerenderManager in the unit 261 // class but is called directly from the TestPrerenderManager in the unit
259 // tests. 262 // tests.
260 void DoShutdown(); 263 void DoShutdown();
261 264
265 // Finds a prerender contents for the specified |url| and
266 // |session_storage_namespace| if it is prerendering. Used only in tests.
dominich 2012/06/28 00:34:32 #if UNIT_TEST?
267 PrerenderContents* FindEntry(
268 const GURL& url,
269 content::SessionStorageNamespace* session_storage_namespace);
270
271 void TreatPrerenderAsUsedForTest(
dominich 2012/06/28 00:34:32 This should be a member of TestPrerenderManager as
272 const GURL& url,
273 content::SessionStorageNamespace* session_storage_namespace);
274
275 base::TimeDelta GetMaxAge() const;
276
262 private: 277 private:
263 // Test that needs needs access to internal functions.
264 friend class PrerenderBrowserTest; 278 friend class PrerenderBrowserTest;
265 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, AliasURLTest);
266 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, CancelAllTest);
267 FRIEND_TEST_ALL_PREFIXES(PrerenderTest,
268 CancelOmniboxRemovesOmniboxTest);
269 FRIEND_TEST_ALL_PREFIXES(PrerenderTest,
270 CancelOmniboxDoesNotRemoveLinkTest);
271 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ClearTest);
272 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ControlGroup);
273 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropOldestRequestTest);
274 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropSecondRequestTest);
275 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ExpireTest);
276 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FoundTest);
277 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FragmentMatchesFragmentTest);
278 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FragmentMatchesPageTest);
279 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAbandon);
280 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAddTwiceAbandonTwice);
281 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAddTwiceCancelTwice);
282 FRIEND_TEST_ALL_PREFIXES(PrerenderTest,
283 LinkManagerAddTwiceCancelTwiceThenAbandonTwice);
284 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancel);
285 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelThenAbandon);
286 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelThenAddAgain);
287 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelTwice);
288 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerExpireThenAddAgain);
289 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerExpireThenCancel);
290 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, NotSoRecentlyVisited);
291 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PageMatchesFragmentTest);
292 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PendingPrerenderTest);
293 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PPLTDummy);
294 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitInWindowTest);
295 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitOutsideWindowTest);
296 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RecentlyVisitedPPLTDummy);
297 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, SourceRenderViewClosed);
298 FRIEND_TEST_ALL_PREFIXES(PrerenderTest, TwoElementPrerenderTest);
299 279
300 struct PrerenderContentsData;
301 struct NavigationRecord; 280 struct NavigationRecord;
302 281
303 class OnCloseTabContentsDeleter; 282 class OnCloseTabContentsDeleter;
304 283
305 typedef std::list<PrerenderContentsData> PrerenderContentsDataList; 284 typedef std::list<PrerenderHandleImpl*> PrerenderHandleList;
306 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; 285 typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap;
307 286
308 // Time window for which we record old navigations, in milliseconds. 287 // Adds a prerender for |url| from |referrer| initiated from the process
309 static const int kNavigationRecordWindowMs = 5000; 288 // |child_id|. The |origin| specifies how the prerender was added. If |size|
310 289 // is empty, then PrerenderContents::StartPrerendering will instead use the
311 // Adds a prerender for |url| from referrer |referrer| initiated from the 290 // size of the currently active tab. If the current active tab size cannot be
312 // child process specified by |child_id|. The |origin| specifies how the 291 // found, it then uses a default from PrerenderConfig. If |prerender_handle|
313 // prerender was added. If the |size| is empty, then 292 // is NULL, then AddPrerender will allocate a handle for the caller if
314 // PrerenderContents::StartPrerendering will instead use the size of the 293 // successful.
315 // currently active tab. If the current active tab size cannot be found, it 294 PrerenderHandle AddPrerender(
316 // then uses a default from PrerenderConfig.
317 bool AddPrerender(
318 Origin origin, 295 Origin origin,
319 int child_id, 296 int child_id,
320 const GURL& url, 297 const GURL& url,
321 const content::Referrer& referrer, 298 const content::Referrer& referrer,
322 gfx::Size size, 299 gfx::Size size,
323 content::SessionStorageNamespace* session_storage_namespace); 300 content::SessionStorageNamespace* session_storage_namespace);
324 301
325 // Retrieves the PrerenderContents object for the specified URL, if it 302 void StartSchedulingPeriodicCleanups();
326 // has been prerendered. The caller will then have ownership of the 303 void StopSchedulingPeriodicCleanups();
327 // PrerenderContents object and is responsible for freeing it.
328 // Returns NULL if the specified URL has not been prerendered.
329 PrerenderContents* GetEntry(const GURL& url);
330 304
331 // Identical to GetEntry, with one exception: 305 void EvictOldestPrerendersIfNecessary();
332 // The WebContents specified indicates the WC in which to swap the
333 // prerendering into. If the WebContents specified is the one
334 // to doing the prerendered itself, will return NULL.
335 PrerenderContents* GetEntryButNotSpecifiedWC(const GURL& url,
336 content::WebContents* wc);
337
338 // Starts scheduling periodic cleanups.
339 void StartSchedulingPeriodicCleanups();
340 // Stops scheduling periodic cleanups if they're no longer needed.
341 void MaybeStopSchedulingPeriodicCleanups();
342 306
343 // Deletes stale and cancelled prerendered PrerenderContents, as well as 307 // Deletes stale and cancelled prerendered PrerenderContents, as well as
344 // WebContents that have been replaced by prerendered WebContents. 308 // WebContents that have been replaced by prerendered WebContents.
345 // Also identifies and kills PrerenderContents that use too much 309 // Also identifies and kills PrerenderContents that use too much
346 // resources. 310 // resources.
347 void PeriodicCleanup(); 311 void PeriodicCleanup();
348 312
349 // Posts a task to call PeriodicCleanup. Results in quicker destruction of 313 // Posts a task to call PeriodicCleanup. Results in quicker destruction of
350 // objects. If |this| is deleted before the task is run, the task will 314 // objects. If |this| is deleted before the task is run, the task will
351 // automatically be cancelled. 315 // automatically be cancelled.
352 void PostCleanupTask(); 316 void PostCleanupTask();
353 317
354 base::TimeDelta GetMaxAge() const; 318 bool IsPrerenderFresh(base::TimeTicks start) const;
355 bool IsPrerenderElementFresh(const base::Time start) const;
356 void DeleteOldEntries(); 319 void DeleteOldEntries();
357 virtual base::Time GetCurrentTime() const; 320 virtual base::Time GetCurrentTime() const;
358 virtual base::TimeTicks GetCurrentTimeTicks() const; 321 virtual base::TimeTicks GetCurrentTimeTicks() const;
359 virtual PrerenderContents* CreatePrerenderContents( 322 virtual PrerenderContents* CreatePrerenderContents(
360 const GURL& url, 323 const GURL& url,
361 const content::Referrer& referrer, 324 const content::Referrer& referrer,
362 Origin origin, 325 Origin origin,
363 uint8 experiment_id); 326 uint8 experiment_id);
364 327
365 // Deletes any PrerenderContents that have been added to the pending delete 328 // Deletes any PrerenderContents that have been added to the pending delete
366 // list. 329 // list.
367 void DeletePendingDeleteEntries(); 330 void DeletePendingDeleteEntries();
368 331
369 // Finds the specified PrerenderContentsData/PrerenderContents and returns it,
370 // if it exists. Returns NULL otherwise. Unlike GetEntry, the
371 // PrerenderManager maintains ownership of the PrerenderContents. 332 // PrerenderManager maintains ownership of the PrerenderContents.
372 PrerenderContentsData* FindEntryData(const GURL& url); 333 PrerenderHandleImpl* FindHandle(
373 PrerenderContents* FindEntry(const GURL& url) const; 334 const GURL& url,
335 const content::SessionStorageNamespace* session_storage_namespace);
374 336
375 // Returns the iterator to the PrerenderContentsData entry that is being 337 // PrerenderManager maintains ownership of the PrerenderContents.
376 // prerendered from the given child route id pair. 338 PrerenderHandleImpl* FindHandleForChildAndRoute(int child_id, int route_id);
377 PrerenderContentsDataList::iterator
378 FindPrerenderContentsForChildRouteIdPair(
379 const std::pair<int, int>& child_route_id_pair);
380
381 PrerenderContentsDataList::iterator
382 FindPrerenderContentsForURL(const GURL& url);
383 339
384 bool DoesRateLimitAllowPrerender() const; 340 bool DoesRateLimitAllowPrerender() const;
385 341
386 // Deletes old WebContents that have been replaced by prerendered ones. This 342 // Deletes old WebContents that have been replaced by prerendered ones. This
387 // is needed because they're replaced in a callback from the old WebContents, 343 // is needed because they're replaced in a callback from the old WebContents,
388 // so cannot immediately be deleted. 344 // so cannot immediately be deleted.
389 void DeleteOldTabContents(); 345 void DeleteOldTabContents();
390 346
391 // Cleans up old NavigationRecord's. 347 // Cleans up old NavigationRecord's.
392 void CleanUpOldNavigations(); 348 void CleanUpOldNavigations();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 // of the PrerenderManager. 389 // of the PrerenderManager.
434 bool enabled_; 390 bool enabled_;
435 391
436 static bool is_prefetch_enabled_; 392 static bool is_prefetch_enabled_;
437 393
438 // The profile that owns this PrerenderManager. 394 // The profile that owns this PrerenderManager.
439 Profile* profile_; 395 Profile* profile_;
440 396
441 PrerenderTracker* prerender_tracker_; 397 PrerenderTracker* prerender_tracker_;
442 398
443 // List of prerendered elements. 399 // List of all running prerenders. This list owns each PrerenderHandle in it.
444 PrerenderContentsDataList prerender_list_; 400 PrerenderHandleList prerender_list_;
445 401
446 // List of recent navigations in this profile, sorted by ascending 402 // List of recent navigations in this profile, sorted by ascending
447 // navigate_time_. 403 // navigate_time_.
448 std::list<NavigationRecord> navigations_; 404 std::list<NavigationRecord> navigations_;
449 405
450 // List of prerender elements to be deleted 406 // List of prerender elements to be deleted
451 std::list<PrerenderContents*> pending_delete_list_; 407 std::list<PrerenderContents*> pending_delete_list_;
452 408
453 // Set of WebContents which are currently displaying a prerendered page. 409 // Set of WebContents which are currently displaying a prerendered page.
454 base::hash_set<content::WebContents*> prerendered_tab_contents_set_; 410 base::hash_set<content::WebContents*> prerendered_tab_contents_set_;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 452
497 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); 453 DISALLOW_COPY_AND_ASSIGN(PrerenderManager);
498 }; 454 };
499 455
500 PrerenderManager* FindPrerenderManagerUsingRenderProcessId( 456 PrerenderManager* FindPrerenderManagerUsingRenderProcessId(
501 int render_process_id); 457 int render_process_id);
502 458
503 } // namespace prerender 459 } // namespace prerender
504 460
505 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ 461 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698