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_TRACKER_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "chrome/browser/prerender/prerender_contents.h"
18 #include "chrome/browser/prerender/prerender_final_status.h"
23 class PrerenderManager
;
24 class PrerenderPendingSwapThrottle
;
25 class PrerenderResourceThrottle
;
26 struct RenderViewInfo
;
28 // PrerenderTracker is responsible for keeping track of all prerendering
30 class PrerenderTracker
: public base::NonThreadSafe
,
31 public PrerenderContents::Observer
{
33 typedef std::pair
<int, int> ChildRouteIdPair
;
36 virtual ~PrerenderTracker();
38 // Returns whether or not a RenderView is prerendering. Can only be called on
39 // the IO thread. Does not acquire a lock, so may claim a RenderView that has
40 // been displayed or destroyed is still prerendering.
41 bool IsPrerenderingOnIOThread(int child_id
, int route_id
) const;
43 // Returns whether or not a RenderFrame and URL are regarding a pending
44 // prerender swap. Can only be called on the IO thread. Does not acquire a
46 bool IsPendingSwapRequestOnIOThread(int render_process_id
,
48 const GURL
& url
) const;
50 // Called when a PrerenderResourceThrottle defers a request. Cancel
51 // or Resume will be called on |throttle| when the prerender is
52 // canceled or used, respectively.
53 void AddResourceThrottleOnIOThread(
54 int child_id
, int route_id
,
55 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
);
57 // Called when a PrerenderResourceThrottle defers a request. Cancel
58 // or Resume will be called on |throttle| when the prerender is
59 // canceled or used, respectively.
60 void AddPendingSwapThrottleOnIOThread(
61 int render_process_id
, int render_frame_id
, const GURL
& url
,
62 const base::WeakPtr
<PrerenderPendingSwapThrottle
>& throttle
);
64 // Called to add throttles for a pending prerender swap.
65 void AddPrerenderPendingSwap(
66 const ChildRouteIdPair
& render_frame_route_id_pair
,
69 // Called to remove the throttles for a pending prerender swap.
70 void RemovePrerenderPendingSwap(
71 const ChildRouteIdPair
& render_frame_route_id_pair
,
72 bool swap_successful
);
75 friend class PrerenderContents
;
77 // Map of child/route id pairs to final statuses.
78 typedef std::map
<ChildRouteIdPair
, RenderViewInfo
> FinalStatusMap
;
79 // List of throttled requests.
80 typedef std::vector
<base::WeakPtr
<PrerenderResourceThrottle
> >
82 // Set of throttled requests.
83 typedef std::map
<ChildRouteIdPair
, ResourceThrottleList
> ResourceThrottleMap
;
84 struct PendingSwapThrottleData
{
85 explicit PendingSwapThrottleData(const GURL
& swap_url
);
86 ~PendingSwapThrottleData();
88 base::WeakPtr
<PrerenderPendingSwapThrottle
> throttle
;
90 // Set of throttles for pending swaps. The key is the routing ID pair
92 typedef std::map
<ChildRouteIdPair
, PendingSwapThrottleData
>
93 PendingSwapThrottleMap
;
95 // From PrerenderContents::Observer:
96 virtual void OnPrerenderStart(PrerenderContents
* prerender_contents
) OVERRIDE
;
97 virtual void OnPrerenderStop(PrerenderContents
* prerender_contents
) OVERRIDE
;
99 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on
101 void AddPrerenderOnIOThread(const ChildRouteIdPair
& child_route_id_pair
);
102 void RemovePrerenderOnIOThread(const ChildRouteIdPair
& child_route_id_pair
,
103 FinalStatus final_status
);
105 // Add/remove prerenders pending swap on the IO Thread.
106 void AddPrerenderPendingSwapOnIOThread(
107 const ChildRouteIdPair
& render_frame_route_id_pair
, const GURL
& url
);
108 void RemovePrerenderPendingSwapOnIOThread(
109 const ChildRouteIdPair
& render_frame_route_id_pair
,
110 bool swap_successful
);
112 static PrerenderTracker
* GetDefault();
114 // Resources that are throttled, pending a prerender use. The keys are a
115 // superset of child/route id pairs that are prerendering. Can only access on
116 // the IO thread. May contain entries that have since been displayed. Used
117 // to prevent locking when not needed.
118 ResourceThrottleMap resource_throttle_io_thread_map_
;
120 // Map of pending prerender swaps and their associated throttles,
121 // maintained on the IO thread.
122 PendingSwapThrottleMap pending_swap_throttle_map_
;
124 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker
);
127 } // namespace prerender
129 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_