OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "chrome/browser/prerender/prerender_final_status.h" | 14 #include "chrome/browser/prerender/prerender_final_status.h" |
15 | 15 |
16 namespace prerender { | 16 namespace prerender { |
17 | 17 |
18 class PrerenderManager; | 18 class PrerenderManager; |
19 struct RenderViewInfo; | |
20 | 19 |
21 // PrerenderTracker is responsible for keeping track of all prerendering | 20 // PrerenderTracker is responsible for keeping track of all prerendering |
22 // RenderViews and their statuses. Its list is guaranteed to be up to date | 21 // RenderViews and their statuses. Its list is guaranteed to be up to date |
23 // and can be modified on any thread. | 22 // and can be modified on any thread. |
24 class PrerenderTracker { | 23 class PrerenderTracker { |
25 public: | 24 public: |
26 // Returns the PrerenderTracker singleton. | 25 PrerenderTracker(); |
27 static PrerenderTracker* GetInstance(); | 26 ~PrerenderTracker(); |
28 | 27 |
29 // Attempts to set the status of the specified RenderViewHost to | 28 // Attempts to set the status of the specified RenderViewHost to |
30 // FINAL_STATUS_USED. Returns true on success. Returns false if it has | 29 // FINAL_STATUS_USED. Returns true on success. Returns false if it has |
31 // already been cancelled for any reason or is no longer prerendering. | 30 // 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 | 31 // Can only be called only on the IO thread. This method will not call |
33 // PrerenderContents::set_final_status() on the corresponding | 32 // PrerenderContents::set_final_status() on the corresponding |
34 // PrerenderContents. | 33 // PrerenderContents. |
35 // | 34 // |
36 // If it returns true, all subsequent calls to TryCancel and TryUse for the | 35 // If it returns true, all subsequent calls to TryCancel and TryUse for the |
37 // RenderView will return false. | 36 // RenderView will return false. |
38 bool TryUse(int child_id, int route_id); | 37 bool TryUse(int child_id, int route_id); |
39 | 38 |
40 // Attempts to cancel prerendering by the specified RenderView, setting the | 39 // Attempts to cancel prerendering by the specified RenderView, setting the |
41 // FinalStatus to |final_status|. Returns true if the specified prerender has | 40 // 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. | 41 // 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 | 42 // If the call results in cancelling a PrerenderContents, a task to destroy |
44 // it is also posted to the UI thread. | 43 // it is also posted to the UI thread. |
45 // | 44 // |
46 // When true is returned, it is guaranteed that the RenderView will never | 45 // When true is returned, it is guaranteed that the RenderView will never |
47 // be displayed. When false is returned, the RenderView has either been | 46 // be displayed. When false is returned, the RenderView has either been |
48 // swapped into a tab or has already been destroyed. | 47 // swapped into a tab or has already been destroyed. |
49 bool TryCancel(int child_id, int route_id, FinalStatus final_status); | 48 bool TryCancel(int child_id, int route_id, FinalStatus final_status); |
50 | 49 |
51 // Same as above, but can only called on the IO Thread. Does not acquire a | 50 // Same as above, but can only called on the IO Thread. Does not acquire a |
52 // lock when the RenderView is not being prerendered. | 51 // lock when the RenderView is not being prerendered. |
53 bool TryCancelOnIOThread(int child_id, int route_id, | 52 bool TryCancelOnIOThread(int child_id, int route_id, |
54 FinalStatus final_status); | 53 FinalStatus final_status); |
55 | 54 |
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) const; | |
61 | |
62 // Gets the FinalStatus of the specified prerendered RenderView. Returns | 55 // Gets the FinalStatus of the specified prerendered RenderView. Returns |
63 // |true| and sets |final_status| to the status of the RenderView if it | 56 // |true| and sets |final_status| to the status of the RenderView if it |
64 // is found, returns false otherwise. | 57 // is found, returns false otherwise. |
65 bool GetFinalStatus(int child_id, int route_id, | 58 bool GetFinalStatus(int child_id, int route_id, |
66 FinalStatus* final_status) const; | 59 FinalStatus* final_status) const; |
67 | 60 |
61 // Returns whether or not a RenderView is prerendering. Can only be called on | |
62 // the IO thread. Does not acquire a lock, so may claim a RenderView that has | |
63 // been displayed or destroyed is still prerendering. | |
64 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; | |
65 | |
68 private: | 66 private: |
69 friend struct DefaultSingletonTraits<PrerenderTracker>; | |
70 friend class PrerenderContents; | 67 friend class PrerenderContents; |
71 | 68 |
69 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerNull); | |
72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | 70 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); |
73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | 71 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); |
74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | 72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); |
75 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | 73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); |
76 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | 74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); |
77 | 75 |
76 struct RenderViewInfo { | |
77 explicit RenderViewInfo(PrerenderManager* prerender_manager); | |
cbentzel
2011/05/24 17:13:54
Why did this need to move out of the .cc file?
dominich
2011/05/24 18:01:47
When PrerenderTracker was implementing Observer th
| |
78 ~RenderViewInfo(); | |
79 | |
80 FinalStatus final_status; | |
81 base::WeakPtr<PrerenderManager> prerender_manager; | |
82 }; | |
83 | |
78 typedef std::pair<int, int> ChildRouteIdPair; | 84 typedef std::pair<int, int> ChildRouteIdPair; |
79 | 85 |
80 // Map of child/route id pairs to final statuses. | 86 // Map of child/route id pairs to final statuses. |
81 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | 87 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; |
82 // Set of child/route id pairs that may be prerendering. | 88 // Set of child/route id pairs that may be prerendering. |
83 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; | 89 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; |
84 | 90 |
85 PrerenderTracker(); | |
86 ~PrerenderTracker(); | |
87 | |
88 // Must be called when a RenderView starts prerendering, before the first | 91 // Must be called when a RenderView starts prerendering, before the first |
89 // navigation starts to avoid any races. | 92 // navigation starts to avoid any races. |
90 void OnPrerenderingStarted(int child_id, int route_id, | 93 void OnPrerenderingStarted(int child_id, int route_id, |
91 PrerenderManager* prerender_manager); | 94 PrerenderManager* prerender_manager); |
92 | 95 |
93 // Must be called when a RenderView stops prerendering, either because the | 96 // Must be called when a RenderView stops prerendering, either because the |
94 // RenderView was used or prerendering was cancelled and it is being | 97 // RenderView was used or prerendering was cancelled and it is being |
95 // destroyed. | 98 // destroyed. |
96 void OnPrerenderingFinished(int child_id, int route_id); | 99 void OnPrerenderingFinished(int child_id, int route_id); |
97 | 100 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 // the IO thread. May contain entries that have since been displayed. Only | 136 // the IO thread. May contain entries that have since been displayed. Only |
134 // used to prevent locking when not needed. | 137 // used to prevent locking when not needed. |
135 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; | 138 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; |
136 | 139 |
137 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 140 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
138 }; | 141 }; |
139 | 142 |
140 } // namespace prerender | 143 } // namespace prerender |
141 | 144 |
142 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 145 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
OLD | NEW |