OLD | NEW |
| (Empty) |
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_TRACKER_H_ | |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <set> | |
11 #include <vector> | |
12 | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "base/threading/non_thread_safe.h" | |
16 #include "chrome/browser/prerender/prerender_final_status.h" | |
17 #include "googleurl/src/gurl.h" | |
18 | |
19 namespace prerender { | |
20 | |
21 class PrerenderManager; | |
22 struct RenderViewInfo; | |
23 | |
24 // PrerenderTracker is responsible for keeping track of all prerendering | |
25 // RenderViews and their statuses. Its list is guaranteed to be up to date | |
26 // and can be modified on any thread. | |
27 class PrerenderTracker { | |
28 public: | |
29 PrerenderTracker(); | |
30 ~PrerenderTracker(); | |
31 | |
32 // Attempts to set the status of the specified RenderViewHost to | |
33 // FINAL_STATUS_USED. Returns true on success. Returns false if it has | |
34 // already been cancelled for any reason or is no longer prerendering. | |
35 // Can only be called only on the IO thread. This method will not call | |
36 // PrerenderContents::set_final_status() on the corresponding | |
37 // PrerenderContents. | |
38 // | |
39 // If it returns true, all subsequent calls to TryCancel and TryUse for the | |
40 // RenderView will return false. | |
41 bool TryUse(int child_id, int route_id); | |
42 | |
43 // Attempts to cancel prerendering by the specified RenderView, setting the | |
44 // FinalStatus to |final_status|. Returns true if the specified prerender has | |
45 // been cancelled, either as a result of this call or for any other reason. | |
46 // If the call results in cancelling a PrerenderContents, a task to destroy | |
47 // it is also posted to the UI thread. | |
48 // | |
49 // When true is returned, it is guaranteed that the RenderView will never | |
50 // be displayed. When false is returned, the RenderView has either been | |
51 // swapped into a tab or has already been destroyed. | |
52 bool TryCancel(int child_id, int route_id, FinalStatus final_status); | |
53 | |
54 // Same as above, but can only called on the IO Thread. Does not acquire a | |
55 // lock when the RenderView is not being prerendered. | |
56 bool TryCancelOnIOThread(int child_id, int route_id, | |
57 FinalStatus final_status); | |
58 | |
59 // Gets the FinalStatus of the specified prerendered RenderView. Returns | |
60 // |true| and sets |final_status| to the status of the RenderView if it | |
61 // is found, returns false otherwise. | |
62 bool GetFinalStatus(int child_id, int route_id, | |
63 FinalStatus* final_status) const; | |
64 | |
65 // Returns whether or not a RenderView is prerendering. Can only be called on | |
66 // the IO thread. Does not acquire a lock, so may claim a RenderView that has | |
67 // been displayed or destroyed is still prerendering. | |
68 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; | |
69 | |
70 private: | |
71 friend class PrerenderContents; | |
72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerNull); | |
73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | |
74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | |
75 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | |
76 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | |
77 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | |
78 | |
79 typedef std::pair<int, int> ChildRouteIdPair; | |
80 // Map of child/route id pairs to final statuses. | |
81 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | |
82 // Set of child/route id pairs that may be prerendering. | |
83 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; | |
84 | |
85 // Must be called when a RenderView starts prerendering, before the first | |
86 // navigation starts to avoid any races. | |
87 void OnPrerenderingStarted(int child_id, int route_id, | |
88 PrerenderManager* prerender_manager); | |
89 | |
90 // Must be called when a RenderView stops prerendering, either because the | |
91 // RenderView was used or prerendering was cancelled and it is being | |
92 // destroyed. | |
93 void OnPrerenderingFinished(int child_id, int route_id); | |
94 | |
95 // Attempts to set the FinalStatus of the specified RenderView to | |
96 // |desired_final_status|. If non-NULL, |actual_final_status| is set to the | |
97 // FinalStatus of the RenderView. | |
98 // | |
99 // If the FinalStatus of the RenderView is successfully set, returns true and | |
100 // sets |actual_final_status| to |desired_final_status|. | |
101 // | |
102 // If the FinalStatus of the RenderView was already set, returns false and | |
103 // sets |actual_final_status| to the actual FinalStatus of the RenderView. | |
104 // | |
105 // If the RenderView is not a prerendering RenderView, returns false and sets | |
106 // |actual_final_status| to FINAL_STATUS_MAX. | |
107 bool SetFinalStatus(int child_id, int route_id, | |
108 FinalStatus desired_final_status, | |
109 FinalStatus* actual_final_status); | |
110 | |
111 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on | |
112 // the IO Thread. | |
113 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | |
114 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | |
115 | |
116 // Tasks posted to the IO Thread to call the above functions. | |
117 static void AddPrerenderOnIOThreadTask( | |
118 const ChildRouteIdPair& child_route_id_pair); | |
119 static void RemovePrerenderOnIOThreadTask( | |
120 const ChildRouteIdPair& child_route_id_pair); | |
121 | |
122 static PrerenderTracker* GetDefault(); | |
123 | |
124 // |final_status_map_lock_| protects access to |final_status_map_|. | |
125 mutable base::Lock final_status_map_lock_; | |
126 // Map containing child/route id pairs and their final statuses. Must only be | |
127 // accessed while the lock is held. Values are always accurate and up to | |
128 // date. | |
129 FinalStatusMap final_status_map_; | |
130 | |
131 // Superset of child/route id pairs that are prerendering. Can only access on | |
132 // the IO thread. May contain entries that have since been displayed. Only | |
133 // used to prevent locking when not needed. | |
134 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | |
137 }; | |
138 | |
139 } // namespace prerender | |
140 | |
141 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | |
OLD | NEW |