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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_ 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_ 7 8 #include <list> 9 10 #include "base/basictypes.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/time/time.h" 13 #include "chrome/browser/prerender/prerender_handle.h" 14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 15 #include "url/gurl.h" 16 17 class Profile; 18 19 namespace content { 20 struct Referrer; 21 } 22 23 namespace gfx { 24 class Size; 25 } 26 27 FORWARD_DECLARE_TEST(WebViewTest, NoPrerenderer); 28 29 namespace prerender { 30 31 class PrerenderHandle; 32 class PrerenderManager; 33 34 // PrerenderLinkManager implements the API on Link elements for all documents 35 // being rendered in this chrome instance. It receives messages from the 36 // renderer indicating addition, cancelation and abandonment of link elements, 37 // and controls the PrerenderManager accordingly. 38 class PrerenderLinkManager : public BrowserContextKeyedService, 39 public PrerenderHandle::Observer { 40 public: 41 explicit PrerenderLinkManager(PrerenderManager* manager); 42 virtual ~PrerenderLinkManager(); 43 44 // A <link rel=prerender ...> element has been inserted into the document. 45 // The |prerender_id| must be unique per |child_id|, and is assigned by the 46 // WebPrerendererClient. 47 void OnAddPrerender(int child_id, 48 int prerender_id, 49 const GURL& url, 50 const content::Referrer& referrer, 51 const gfx::Size& size, 52 int render_view_route_id); 53 54 // A <link rel=prerender ...> element has been explicitly removed from a 55 // document. 56 void OnCancelPrerender(int child_id, int prerender_id); 57 58 // A renderer launching <link rel=prerender ...> has navigated away from the 59 // launching page, the launching renderer process has crashed, or perhaps the 60 // renderer process was fast-closed when the last render view in it was 61 // closed. 62 void OnAbandonPrerender(int child_id, int prerender_id); 63 64 // If a renderer channel closes (crash, fast exit, etc...), that's effectively 65 // an abandon of any prerenders launched by that child. 66 void OnChannelClosing(int child_id); 67 68 private: 69 friend class PrerenderBrowserTest; 70 friend class PrerenderTest; 71 // WebViewTest.NoPrerenderer needs to access the private IsEmpty() method. 72 FRIEND_TEST_ALL_PREFIXES(::WebViewTest, NoPrerenderer); 73 74 struct LinkPrerender { 75 LinkPrerender(int launcher_child_id, 76 int prerender_id, 77 const GURL& url, 78 const content::Referrer& referrer, 79 const gfx::Size& size, 80 int render_view_route_id, 81 base::TimeTicks creation_time); 82 ~LinkPrerender(); 83 84 // Parameters from PrerenderLinkManager::OnAddPrerender(): 85 int launcher_child_id; 86 int prerender_id; 87 GURL url; 88 content::Referrer referrer; 89 gfx::Size size; 90 int render_view_route_id; 91 92 // The time at which this Prerender was added to PrerenderLinkManager. 93 base::TimeTicks creation_time; 94 95 // Initially NULL, |handle| is set once we start this prerender. It is owned 96 // by this struct, and must be deleted before destructing this struct. 97 PrerenderHandle* handle; 98 99 // True if this prerender has become a MatchComplete replacement. This state 100 // is maintained so the renderer is not notified of a stop twice. 101 bool is_match_complete_replacement; 102 103 // True if this prerender has been abandoned by its launcher. 104 bool has_been_abandoned; 105 }; 106 107 bool IsEmpty() const; 108 109 // Returns a count of currently running prerenders. 110 size_t CountRunningPrerenders() const; 111 112 // Start any prerenders that can be started, respecting concurrency limits for 113 // the system and per launcher. 114 void StartPrerenders(); 115 116 LinkPrerender* FindByLauncherChildIdAndPrerenderId(int child_id, 117 int prerender_id); 118 119 LinkPrerender* FindByPrerenderHandle(PrerenderHandle* prerender_handle); 120 121 // Removes |prerender| from the the prerender link manager. Deletes the 122 // PrerenderHandle as needed. 123 void RemovePrerender(LinkPrerender* prerender); 124 125 // Cancels |prerender| and removes |prerender| from the prerender link 126 // manager. 127 void CancelPrerender(LinkPrerender* prerender); 128 129 // From BrowserContextKeyedService: 130 virtual void Shutdown() OVERRIDE; 131 132 // From PrerenderHandle::Observer: 133 virtual void OnPrerenderStart(PrerenderHandle* prerender_handle) OVERRIDE; 134 virtual void OnPrerenderStopLoading(PrerenderHandle* prerender_handle) 135 OVERRIDE; 136 virtual void OnPrerenderStop(PrerenderHandle* prerender_handle) OVERRIDE; 137 virtual void OnPrerenderCreatedMatchCompleteReplacement( 138 PrerenderHandle* handle) OVERRIDE; 139 140 bool has_shutdown_; 141 142 PrerenderManager* manager_; 143 144 // All prerenders known to this PrerenderLinkManager. Insertions are always 145 // made at the back, so the oldest prerender is at the front, and the youngest 146 // at the back. 147 std::list<LinkPrerender> prerenders_; 148 149 DISALLOW_COPY_AND_ASSIGN(PrerenderLinkManager); 150 }; 151 152 } // namespace prerender 153 154 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_ 155 156