Chromium Code Reviews| Index: chrome/browser/prerender/prerender_manager.h |
| diff --git a/chrome/browser/prerender/prerender_manager.h b/chrome/browser/prerender/prerender_manager.h |
| index 5d357ac99087d7b15bbeea0f3e6594a3362747e4..da2218045ebdb9b5e7b9db6fef89a9f6350df568 100644 |
| --- a/chrome/browser/prerender/prerender_manager.h |
| +++ b/chrome/browser/prerender/prerender_manager.h |
| @@ -7,6 +7,7 @@ |
| #pragma once |
| #include <list> |
| +#include <map> |
| #include <string> |
| #include <utility> |
| @@ -55,6 +56,7 @@ struct hash<content::WebContents*> { |
| namespace prerender { |
| class PrerenderCondition; |
| +class PrerenderHandle; |
| class PrerenderHistograms; |
| class PrerenderHistory; |
| class PrerenderLocalPredictor; |
| @@ -101,36 +103,31 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| // Entry points for adding prerenders. |
| // Adds a prerender for |url| if valid. |process_id| and |route_id| identify |
| - // the RenderView that the prerender request came from. The |size| may be |
| - // empty, and the current tab size will be used if it is. If the current |
| - // active tab size cannot be found, we use a default from PrerenderConfig. |
| - // Returns true if the URL was added, false if it was not. |
| - // If the launching RenderView is itself prerendering, the prerender is added |
| - // as a pending prerender. |
| - bool AddPrerenderFromLinkRelPrerender( |
| + // the RenderView that the prerender request came from. If |size| is empty, a |
| + // default from the PrerenderConfig is used. Returns a caller-owned |
| + // PrerenderHandle* if the URL was added, NULL if it was not. If the launching |
| + // RenderView is itself prerendering, the prerender is added as a pending |
| + // prerender. |
| + PrerenderHandle* AddPrerenderFromLinkRelPrerender( |
| int process_id, |
| int route_id, |
| const GURL& url, |
| const content::Referrer& referrer, |
| - gfx::Size size); |
| + const gfx::Size& size); |
| // Adds a prerender for |url| if valid. As the prerender request is coming |
| // from a source without a RenderViewHost (i.e., the omnibox) we don't have a |
| // child or route id, or a referrer. This method uses sensible values for |
| // those. The |session_storage_namespace| matches the namespace of the active |
| - // tab at the time the prerender is generated from the omnibox. |
| - bool AddPrerenderFromOmnibox( |
| + // tab at the time the prerender is generated from the omnibox. Returns a |
| + // caller-owned PrerenderHandle*, or NULL. |
| + PrerenderHandle* AddPrerenderFromOmnibox( |
| const GURL& url, |
| content::SessionStorageNamespace* session_storage_namespace, |
| - gfx::Size size); |
| + const gfx::Size& size); |
| - // Request cancelation of a previously added prerender. If the |active_count_| |
| - // of the prerender is one, it will be canceled. Otherwise, |active_count_| |
| - // will be decremented by one. |
| - void MaybeCancelPrerender(const GURL& url); |
| - |
| - // Destroy all prerenders for the given child route id pair and assign a final |
| - // status to them. |
| + // If |process_id| and |view_id| refer to a running prerender, destroy |
| + // it with |final_status|. |
| virtual void DestroyPrerenderForRenderView(int process_id, |
| int view_id, |
| FinalStatus final_status); |
| @@ -184,10 +181,6 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| // is prerendering a page. |
| bool IsWebContentsPrerendering(content::WebContents* web_contents) const; |
| - // Returns true if there is a prerendered page for the given URL and it has |
| - // finished loading. Only valid if called before MaybeUsePrerenderedPage. |
| - bool DidPrerenderFinishLoading(const GURL& url) const; |
| - |
| // Maintaining and querying the set of WebContents belonging to this |
| // PrerenderManager that are currently showing prerendered pages. |
| void MarkWebContentsAsPrerendered(content::WebContents* web_contents); |
| @@ -237,11 +230,6 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| // Adds a condition. This is owned by the PrerenderManager. |
| void AddCondition(const PrerenderCondition* condition); |
| - bool IsPendingEntry(const GURL& url) const; |
| - |
| - // Returns true if |url| matches any URLs being prerendered. |
| - bool IsPrerendering(const GURL& url) const; |
| - |
| // Records that some visible tab navigated (or was redirected) to the |
| // provided URL. |
| void RecordNavigation(const GURL& url); |
| @@ -251,94 +239,73 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| PrerenderHistograms* histograms() const { return histograms_.get(); } |
| protected: |
| + struct PrerenderData { |
| + explicit PrerenderData(PrerenderManager* manager); |
| + explicit PrerenderData(const PrerenderData& other); |
| + PrerenderData(PrerenderManager* manager, |
| + PrerenderContents* contents); |
|
mmenke
2012/07/11 18:40:05
nit: May fit on a single line.
gavinp
2012/07/11 21:19:30
Done.
|
| + ~PrerenderData(); |
| + |
| + PrerenderManager* manager; |
| + PrerenderContents* contents; |
| + int instance_count; |
| + |
| + const scoped_ptr<base::WeakPtrFactory<PrerenderData> > weak_ptr_factory; |
|
mmenke
2012/07/11 18:40:05
I don't believe this needs to be a scoped_ptr.
gavinp
2012/07/11 21:19:30
Mooted once I gave up on the lists.
|
| + }; |
| + |
| void SetPrerenderContentsFactory( |
| PrerenderContents::Factory* prerender_contents_factory); |
| + // Adds a prerender from a pending Prerender, called by |
| + // PrerenderContents::StartPendingPrerenders. |
| + void StartPendingPrerender( |
| + PrerenderHandle* existing_prerender_handle, |
| + Origin origin, |
| + int process_id, |
| + const GURL& url, |
| + const content::Referrer& referrer, |
| + const gfx::Size& size, |
| + content::SessionStorageNamespace* session_storage_namespace); |
| + |
| + void DestroyPendingPrerenderData( |
| + PrerenderData* pending_prerender_data); |
| + |
| // Utility method that is called from the virtual Shutdown method on this |
| // class but is called directly from the TestPrerenderManager in the unit |
| // tests. |
| void DoShutdown(); |
| private: |
| - // Test that needs needs access to internal functions. |
| friend class PrerenderBrowserTest; |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, AliasURLTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, CancelAllTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, |
| - CancelOmniboxRemovesOmniboxTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, |
| - CancelOmniboxDoesNotRemoveLinkTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ClearTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ControlGroup); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropOldestRequestTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, DropSecondRequestTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, ExpireTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FoundTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FragmentMatchesFragmentTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, FragmentMatchesPageTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAbandon); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAddTwiceAbandonTwice); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerAddTwiceCancelTwice); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, |
| - LinkManagerAddTwiceCancelTwiceThenAbandonTwice); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancel); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelThenAbandon); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelThenAddAgain); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerCancelTwice); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerExpireThenAddAgain); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, LinkManagerExpireThenCancel); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, NotSoRecentlyVisited); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PageMatchesFragmentTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PendingPrerenderTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, PPLTDummy); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitInWindowTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RateLimitOutsideWindowTest); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, RecentlyVisitedPPLTDummy); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, SourceRenderViewClosed); |
| - FRIEND_TEST_ALL_PREFIXES(PrerenderTest, TwoElementPrerenderTest); |
| - |
| - struct PrerenderContentsData; |
| - struct NavigationRecord; |
| + friend class PrerenderContents; |
| + friend class PrerenderHandle; |
| + friend class UnitTestPrerenderManager; |
| class OnCloseTabContentsDeleter; |
| + struct NavigationRecord; |
| - typedef std::list<PrerenderContentsData> PrerenderContentsDataList; |
| typedef base::hash_map<content::WebContents*, bool> WouldBePrerenderedMap; |
| // Time window for which we record old navigations, in milliseconds. |
| static const int kNavigationRecordWindowMs = 5000; |
| - // Adds a prerender for |url| from referrer |referrer| initiated from the |
| - // child process specified by |child_id|. The |origin| specifies how the |
| - // prerender was added. If the |size| is empty, then |
| - // PrerenderContents::StartPrerendering will instead use the size of the |
| - // currently active tab. If the current active tab size cannot be found, it |
| - // then uses a default from PrerenderConfig. |
| - bool AddPrerender( |
| + // Adds a prerender for |url| from |referrer| initiated from the process |
| + // |child_id|. The |origin| specifies how the prerender was added. If |size| |
| + // is empty, then PrerenderContents::StartPrerendering will instead use a |
| + // default from PrerenderConfig. Returns a PrerenderHandle*, owned by the |
| + // caller, or NULL. |
| + PrerenderHandle* AddPrerender( |
| Origin origin, |
| int child_id, |
| const GURL& url, |
| const content::Referrer& referrer, |
| - gfx::Size size, |
| + const gfx::Size& size, |
| content::SessionStorageNamespace* session_storage_namespace); |
| - // Retrieves the PrerenderContents object for the specified URL, if it |
| - // has been prerendered. The caller will then have ownership of the |
| - // PrerenderContents object and is responsible for freeing it. |
| - // Returns NULL if the specified URL has not been prerendered. |
| - PrerenderContents* GetEntry(const GURL& url); |
| - |
| - // Identical to GetEntry, with one exception: |
| - // The WebContents specified indicates the WC in which to swap the |
| - // prerendering into. If the WebContents specified is the one |
| - // to doing the prerendered itself, will return NULL. |
| - PrerenderContents* GetEntryButNotSpecifiedWC(const GURL& url, |
| - content::WebContents* wc); |
| - |
| - // Starts scheduling periodic cleanups. |
| void StartSchedulingPeriodicCleanups(); |
| - // Stops scheduling periodic cleanups if they're no longer needed. |
| - void MaybeStopSchedulingPeriodicCleanups(); |
| + void StopSchedulingPeriodicCleanups(); |
| + |
| + void EvictOldestPrerendersIfNecessary(); |
| // Deletes stale and cancelled prerendered PrerenderContents, as well as |
| // WebContents that have been replaced by prerendered WebContents. |
| @@ -352,7 +319,7 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| void PostCleanupTask(); |
| base::TimeDelta GetMaxAge() const; |
| - bool IsPrerenderElementFresh(const base::Time start) const; |
| + bool IsPrerenderFresh(base::TimeTicks start) const; |
| void DeleteOldEntries(); |
| virtual base::Time GetCurrentTime() const; |
| virtual base::TimeTicks GetCurrentTimeTicks() const; |
| @@ -366,20 +333,23 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| // list. |
| void DeletePendingDeleteEntries(); |
| - // Finds the specified PrerenderContentsData/PrerenderContents and returns it, |
| - // if it exists. Returns NULL otherwise. Unlike GetEntry, the |
| - // PrerenderManager maintains ownership of the PrerenderContents. |
| - PrerenderContentsData* FindEntryData(const GURL& url); |
| - PrerenderContents* FindEntry(const GURL& url) const; |
| + // Finds the active PrerenderData object for a running prerender matching |
| + // |url| and |session_storage_namespace|. |
| + PrerenderData* FindPrerenderData( |
| + const GURL& url, |
| + const content::SessionStorageNamespace* session_storage_namespace); |
| - // Returns the iterator to the PrerenderContentsData entry that is being |
| - // prerendered from the given child route id pair. |
| - PrerenderContentsDataList::iterator |
| - FindPrerenderContentsForChildRouteIdPair( |
| - const std::pair<int, int>& child_route_id_pair); |
| + // If |child_id| and |route_id| correspond to a RenderView that is an active |
| + // prerender, returns the PrerenderData object for that prerender. Otherwise, |
| + // returns NULL. |
| + PrerenderData* FindPrerenderDataForChildAndRoute( |
| + int child_id, |
| + int route_id); |
|
mmenke
2012/07/11 18:40:05
nit: May fit onto one line now.
gavinp
2012/07/11 21:19:30
Done.
|
| - PrerenderContentsDataList::iterator |
| - FindPrerenderContentsForURL(const GURL& url); |
| + // Given the |prerender_contents|, find the iterator in active_prerender_list_ |
| + // correponding to the given prerender. |
| + std::list<PrerenderData>::iterator |
| + FindIteratorForPrerenderContents(PrerenderContents* prerender_contents); |
| bool DoesRateLimitAllowPrerender() const; |
| @@ -440,8 +410,14 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>, |
| PrerenderTracker* prerender_tracker_; |
| - // List of prerendered elements. |
| - PrerenderContentsDataList prerender_list_; |
| + // List of all running prerenders. |
| + // It is kept sorted, in increasing order by expiry time. The STL list was |
| + // chosen because it does not move items in memory (this lets handles continue |
| + // to point to their PrerenderData). |
| + std::list<PrerenderData> active_prerender_list_; |
| + |
| + // List of all pending prerenders. |
| + std::list<PrerenderData> pending_prerender_list_; |
| // List of recent navigations in this profile, sorted by ascending |
| // navigate_time_. |