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.
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_
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/keyed_service/core/keyed_service.h"
27 FORWARD_DECLARE_TEST(WebViewTest
, NoPrerenderer
);
31 class PrerenderContents
;
32 class PrerenderManager
;
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 KeyedService
,
39 public PrerenderHandle::Observer
{
41 explicit PrerenderLinkManager(PrerenderManager
* manager
);
42 ~PrerenderLinkManager() override
;
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
,
51 const content::Referrer
& referrer
,
52 const gfx::Size
& size
,
53 int render_view_route_id
);
55 // A <link rel=prerender ...> element has been explicitly removed from a
57 void OnCancelPrerender(int child_id
, int prerender_id
);
59 // A renderer launching <link rel=prerender ...> has navigated away from the
60 // launching page, the launching renderer process has crashed, or perhaps the
61 // renderer process was fast-closed when the last render view in it was
63 void OnAbandonPrerender(int child_id
, int prerender_id
);
65 // If a renderer channel closes (crash, fast exit, etc...), that's effectively
66 // an abandon of any prerenders launched by that child.
67 void OnChannelClosing(int child_id
);
70 friend class PrerenderBrowserTest
;
71 friend class PrerenderTest
;
72 // WebViewTest.NoPrerenderer needs to access the private IsEmpty() method.
73 FRIEND_TEST_ALL_PREFIXES(::WebViewTest
, NoPrerenderer
);
75 struct LinkPrerender
{
76 LinkPrerender(int launcher_child_id
,
80 const content::Referrer
& referrer
,
81 const gfx::Size
& size
,
82 int render_view_route_id
,
83 base::TimeTicks creation_time
,
84 PrerenderContents
* deferred_launcher
);
87 // Parameters from PrerenderLinkManager::OnAddPrerender():
88 int launcher_child_id
;
92 content::Referrer referrer
;
94 int render_view_route_id
;
96 // The time at which this Prerender was added to PrerenderLinkManager.
97 base::TimeTicks creation_time
;
99 // If non-NULL, this link prerender was launched by an unswapped prerender,
100 // |deferred_launcher|. When |deferred_launcher| is swapped in, the field is
102 PrerenderContents
* deferred_launcher
;
104 // Initially NULL, |handle| is set once we start this prerender. It is owned
105 // by this struct, and must be deleted before destructing this struct.
106 PrerenderHandle
* handle
;
108 // True if this prerender has become a MatchComplete replacement. This state
109 // is maintained so the renderer is not notified of a stop twice.
110 bool is_match_complete_replacement
;
112 // True if this prerender has been abandoned by its launcher.
113 bool has_been_abandoned
;
116 class PendingPrerenderManager
;
118 bool IsEmpty() const;
120 // Returns a count of currently running prerenders.
121 size_t CountRunningPrerenders() const;
123 // Start any prerenders that can be started, respecting concurrency limits for
124 // the system and per launcher.
125 void StartPrerenders();
127 LinkPrerender
* FindByLauncherChildIdAndPrerenderId(int child_id
,
130 LinkPrerender
* FindByPrerenderHandle(PrerenderHandle
* prerender_handle
);
132 // Removes |prerender| from the the prerender link manager. Deletes the
133 // PrerenderHandle as needed.
134 void RemovePrerender(LinkPrerender
* prerender
);
136 // Cancels |prerender| and removes |prerender| from the prerender link
138 void CancelPrerender(LinkPrerender
* prerender
);
140 // Called when |launcher| is swapped in.
141 void StartPendingPrerendersForLauncher(PrerenderContents
* launcher
);
143 // Called when |launcher| is aborted.
144 void CancelPendingPrerendersForLauncher(PrerenderContents
* launcher
);
146 // From KeyedService:
147 void Shutdown() override
;
149 // From PrerenderHandle::Observer:
150 void OnPrerenderStart(PrerenderHandle
* prerender_handle
) override
;
151 void OnPrerenderStopLoading(PrerenderHandle
* prerender_handle
) override
;
152 void OnPrerenderDomContentLoaded(PrerenderHandle
* prerender_handle
) override
;
153 void OnPrerenderStop(PrerenderHandle
* prerender_handle
) override
;
154 void OnPrerenderCreatedMatchCompleteReplacement(
155 PrerenderHandle
* handle
) override
;
159 PrerenderManager
* manager_
;
161 // All prerenders known to this PrerenderLinkManager. Insertions are always
162 // made at the back, so the oldest prerender is at the front, and the youngest
164 std::list
<LinkPrerender
> prerenders_
;
166 // Helper object to manage prerenders which are launched by other prerenders
167 // and must be deferred until the launcher is swapped in.
168 scoped_ptr
<PendingPrerenderManager
> pending_prerender_manager_
;
170 DISALLOW_COPY_AND_ASSIGN(PrerenderLinkManager
);
173 } // namespace prerender
175 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_LINK_MANAGER_H_