OLD | NEW |
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_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
17 #include "chrome/browser/prerender/prerender_contents.h" | 17 #include "chrome/browser/prerender/prerender_contents.h" |
18 #include "chrome/browser/prerender/prerender_final_status.h" | 18 #include "chrome/browser/prerender/prerender_final_status.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 namespace prerender { | 21 namespace prerender { |
22 | 22 |
23 class PrerenderManager; | 23 class PrerenderManager; |
24 class PrerenderPendingSwapThrottle; | 24 class PrerenderPendingSwapThrottle; |
25 class PrerenderResourceThrottle; | 25 class PrerenderResourceThrottle; |
26 struct RenderViewInfo; | 26 struct RenderViewInfo; |
27 | 27 |
28 // PrerenderTracker is responsible for keeping track of all prerendering | 28 // PrerenderTracker is responsible for keeping track of all prerendering |
29 // RenderViews and their statuses. Its list is guaranteed to be up to date | 29 // RenderViews. |
30 // and can be modified on any thread. | |
31 class PrerenderTracker : public base::NonThreadSafe, | 30 class PrerenderTracker : public base::NonThreadSafe, |
32 public PrerenderContents::Observer { | 31 public PrerenderContents::Observer { |
33 public: | 32 public: |
34 typedef std::pair<int, int> ChildRouteIdPair; | 33 typedef std::pair<int, int> ChildRouteIdPair; |
35 | 34 |
36 PrerenderTracker(); | 35 PrerenderTracker(); |
37 virtual ~PrerenderTracker(); | 36 virtual ~PrerenderTracker(); |
38 | 37 |
39 // Attempts to set the status of the specified RenderViewHost to | |
40 // FINAL_STATUS_USED. Returns true on success. Returns false if it has | |
41 // already been cancelled for any reason or is no longer prerendering. | |
42 // Can only be called only on the UI thread. This method will not call | |
43 // PrerenderContents::SetFinalStatus() on the corresponding PrerenderContents. | |
44 // | |
45 // If it returns true, all subsequent calls to TryCancel and TryUse for the | |
46 // RenderView will return false. | |
47 bool TryUse(int child_id, int route_id); | |
48 | |
49 // Attempts to cancel prerendering by the specified RenderView, setting the | |
50 // FinalStatus to |final_status|. Returns true if the specified prerender has | |
51 // been cancelled, either as a result of this call or for any other reason. | |
52 // If the call results in cancelling a PrerenderContents, a task to destroy | |
53 // it is also posted to the UI thread. | |
54 // | |
55 // When true is returned, it is guaranteed that the RenderView will never | |
56 // be displayed. When false is returned, the RenderView has either been | |
57 // swapped into a tab or has already been destroyed. | |
58 bool TryCancel(int child_id, int route_id, FinalStatus final_status); | |
59 | |
60 // Same as above, but can only called on the IO Thread. Does not acquire a | |
61 // lock when the RenderView is not being prerendered. | |
62 bool TryCancelOnIOThread(int child_id, int route_id, | |
63 FinalStatus final_status); | |
64 | |
65 // Gets the FinalStatus of the specified prerendered RenderView. Returns | |
66 // |true| and sets |final_status| to the status of the RenderView if it | |
67 // is found, returns false otherwise. | |
68 bool GetFinalStatus(int child_id, int route_id, | |
69 FinalStatus* final_status) const; | |
70 | |
71 // Returns whether or not a RenderView is prerendering. Can only be called on | 38 // Returns whether or not a RenderView is prerendering. Can only be called on |
72 // the IO thread. Does not acquire a lock, so may claim a RenderView that has | 39 // the IO thread. Does not acquire a lock, so may claim a RenderView that has |
73 // been displayed or destroyed is still prerendering. | 40 // been displayed or destroyed is still prerendering. |
74 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; | 41 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; |
75 | 42 |
76 // Returns whether or not a RenderView and URL are regarding a pending | 43 // Returns whether or not a RenderView and URL are regarding a pending |
77 // prerender swap. Can only be called on the IO thread. Does not acquire a | 44 // prerender swap. Can only be called on the IO thread. Does not acquire a |
78 // lock. | 45 // lock. |
79 bool IsPendingSwapRequestOnIOThread(int child_id, int route_id, | 46 bool IsPendingSwapRequestOnIOThread(int child_id, int route_id, |
80 const GURL& url) const; | 47 const GURL& url) const; |
(...skipping 15 matching lines...) Expand all Loading... |
96 // Called to add throttles for a pending prerender swap. | 63 // Called to add throttles for a pending prerender swap. |
97 void AddPrerenderPendingSwap(const ChildRouteIdPair& child_route_id_pair, | 64 void AddPrerenderPendingSwap(const ChildRouteIdPair& child_route_id_pair, |
98 const GURL& url); | 65 const GURL& url); |
99 | 66 |
100 // Called to remove the throttles for a pending prerender swap. | 67 // Called to remove the throttles for a pending prerender swap. |
101 void RemovePrerenderPendingSwap(const ChildRouteIdPair& child_route_id_pair, | 68 void RemovePrerenderPendingSwap(const ChildRouteIdPair& child_route_id_pair, |
102 bool swap_successful); | 69 bool swap_successful); |
103 | 70 |
104 private: | 71 private: |
105 friend class PrerenderContents; | 72 friend class PrerenderContents; |
106 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerNull); | |
107 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | |
108 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | |
109 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | |
110 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | |
111 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | |
112 | 73 |
113 // Map of child/route id pairs to final statuses. | 74 // Map of child/route id pairs to final statuses. |
114 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | 75 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; |
115 // List of throttled requests. | 76 // List of throttled requests. |
116 typedef std::vector<base::WeakPtr<PrerenderResourceThrottle> > | 77 typedef std::vector<base::WeakPtr<PrerenderResourceThrottle> > |
117 ResourceThrottleList; | 78 ResourceThrottleList; |
118 // Set of throttled requests. | 79 // Set of throttled requests. |
119 typedef std::map<ChildRouteIdPair, ResourceThrottleList> ResourceThrottleMap; | 80 typedef std::map<ChildRouteIdPair, ResourceThrottleList> ResourceThrottleMap; |
120 struct PendingSwapThrottleData { | 81 struct PendingSwapThrottleData { |
121 explicit PendingSwapThrottleData(const GURL& swap_url); | 82 explicit PendingSwapThrottleData(const GURL& swap_url); |
122 ~PendingSwapThrottleData(); | 83 ~PendingSwapThrottleData(); |
123 GURL url; | 84 GURL url; |
124 std::vector<base::WeakPtr<PrerenderPendingSwapThrottle> > throttles; | 85 std::vector<base::WeakPtr<PrerenderPendingSwapThrottle> > throttles; |
125 }; | 86 }; |
126 // Set of throttles for pending swaps. | 87 // Set of throttles for pending swaps. |
127 typedef std::map<ChildRouteIdPair, PendingSwapThrottleData> | 88 typedef std::map<ChildRouteIdPair, PendingSwapThrottleData> |
128 PendingSwapThrottleMap; | 89 PendingSwapThrottleMap; |
129 | 90 |
130 // From PrerenderContents::Observer: | 91 // From PrerenderContents::Observer: |
131 virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE; | 92 virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE; |
132 virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE; | 93 virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE; |
133 | 94 |
134 // Attempts to set the FinalStatus of the specified RenderView to | |
135 // |desired_final_status|. If non-NULL, |actual_final_status| is set to the | |
136 // FinalStatus of the RenderView. | |
137 // | |
138 // If the FinalStatus of the RenderView is successfully set, returns true and | |
139 // sets |actual_final_status| to |desired_final_status|. | |
140 // | |
141 // If the FinalStatus of the RenderView was already set, returns false and | |
142 // sets |actual_final_status| to the actual FinalStatus of the RenderView. | |
143 // | |
144 // If the RenderView is not a prerendering RenderView, returns false and sets | |
145 // |actual_final_status| to FINAL_STATUS_MAX. | |
146 bool SetFinalStatus(int child_id, int route_id, | |
147 FinalStatus desired_final_status, | |
148 FinalStatus* actual_final_status); | |
149 | |
150 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on | 95 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on |
151 // the IO Thread. | 96 // the IO Thread. |
152 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | 97 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); |
153 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair, | 98 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair, |
154 FinalStatus final_status); | 99 FinalStatus final_status); |
155 | 100 |
156 // Add/remove prerenders pending swap on the IO Thread. | 101 // Add/remove prerenders pending swap on the IO Thread. |
157 void AddPrerenderPendingSwapOnIOThread( | 102 void AddPrerenderPendingSwapOnIOThread( |
158 const ChildRouteIdPair& child_route_id_pair, const GURL& url); | 103 const ChildRouteIdPair& child_route_id_pair, const GURL& url); |
159 void RemovePrerenderPendingSwapOnIOThread( | 104 void RemovePrerenderPendingSwapOnIOThread( |
160 const ChildRouteIdPair& child_route_id_pair, | 105 const ChildRouteIdPair& child_route_id_pair, |
161 bool swap_successful); | 106 bool swap_successful); |
162 | 107 |
163 // Tasks posted to the IO Thread to call the above functions. | 108 // Tasks posted to the IO Thread to call the above functions. |
164 static void AddPrerenderOnIOThreadTask( | 109 static void AddPrerenderOnIOThreadTask( |
165 const ChildRouteIdPair& child_route_id_pair); | 110 const ChildRouteIdPair& child_route_id_pair); |
166 static void RemovePrerenderOnIOThreadTask( | 111 static void RemovePrerenderOnIOThreadTask( |
167 const ChildRouteIdPair& child_route_id_pair, | 112 const ChildRouteIdPair& child_route_id_pair, |
168 FinalStatus final_status); | 113 FinalStatus final_status); |
169 static void AddPrerenderPendingSwapOnIOThreadTask( | 114 static void AddPrerenderPendingSwapOnIOThreadTask( |
170 const ChildRouteIdPair& child_route_id_pair, const GURL& url); | 115 const ChildRouteIdPair& child_route_id_pair, const GURL& url); |
171 static void RemovePrerenderPendingSwapOnIOThreadTask( | 116 static void RemovePrerenderPendingSwapOnIOThreadTask( |
172 const ChildRouteIdPair& child_route_id_pair, | 117 const ChildRouteIdPair& child_route_id_pair, |
173 bool swap_successful); | 118 bool swap_successful); |
174 | 119 |
175 static PrerenderTracker* GetDefault(); | 120 static PrerenderTracker* GetDefault(); |
176 | 121 |
177 // |final_status_map_lock_| protects access to |final_status_map_|. | |
178 mutable base::Lock final_status_map_lock_; | |
179 // Map containing child/route id pairs and their final statuses. Must only be | |
180 // accessed while the lock is held. Values are always accurate and up to | |
181 // date. | |
182 FinalStatusMap final_status_map_; | |
183 | |
184 // Resources that are throttled, pending a prerender use. The keys are a | 122 // Resources that are throttled, pending a prerender use. The keys are a |
185 // superset of child/route id pairs that are prerendering. Can only access on | 123 // superset of child/route id pairs that are prerendering. Can only access on |
186 // the IO thread. May contain entries that have since been displayed. Used | 124 // the IO thread. May contain entries that have since been displayed. Used |
187 // to prevent locking when not needed. | 125 // to prevent locking when not needed. |
188 ResourceThrottleMap resource_throttle_io_thread_map_; | 126 ResourceThrottleMap resource_throttle_io_thread_map_; |
189 | 127 |
190 // Map of pending prerender swaps and their associated throttles, | 128 // Map of pending prerender swaps and their associated throttles, |
191 // maintained on the IO thread. | 129 // maintained on the IO thread. |
192 PendingSwapThrottleMap pending_swap_throttle_map_; | 130 PendingSwapThrottleMap pending_swap_throttle_map_; |
193 | 131 |
194 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 132 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
195 }; | 133 }; |
196 | 134 |
197 } // namespace prerender | 135 } // namespace prerender |
198 | 136 |
199 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 137 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
OLD | NEW |