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