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/gtest_prod_util.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; | 19 struct RenderViewInfo; |
20 | 20 |
21 // PrerenderTracker is responsible for keeping track of all prerendering | 21 // PrerenderTracker is responsible for keeping track of all prerendering |
22 // RenderViews and their statuses. Its list is guaranteed to be up to date | 22 // RenderViews and their statuses. Its list is guaranteed to be up to date |
23 // and can be modified on any thread. | 23 // and can be modified on any thread. |
24 class PrerenderTracker { | 24 class PrerenderTracker { |
25 public: | 25 public: |
26 // Returns the PrerenderTracker singleton. | 26 PrerenderTracker(); |
27 static PrerenderTracker* GetInstance(); | 27 ~PrerenderTracker(); |
28 | 28 |
29 // Attempts to set the status of the specified RenderViewHost to | 29 // Attempts to set the status of the specified RenderViewHost to |
30 // FINAL_STATUS_USED. Returns true on success. Returns false if it has | 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. | 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 | 32 // Can only be called only on the IO thread. This method will not call |
33 // PrerenderContents::set_final_status() on the corresponding | 33 // PrerenderContents::set_final_status() on the corresponding |
34 // PrerenderContents. | 34 // PrerenderContents. |
35 // | 35 // |
36 // If it returns true, all subsequent calls to TryCancel and TryUse for the | 36 // If it returns true, all subsequent calls to TryCancel and TryUse for the |
37 // RenderView will return false. | 37 // RenderView will return false. |
38 bool TryUse(int child_id, int route_id); | 38 bool TryUse(int child_id, int route_id); |
39 | 39 |
40 // Attempts to cancel prerendering by the specified RenderView, setting the | 40 // Attempts to cancel prerendering by the specified RenderView, setting the |
41 // FinalStatus to |final_status|. Returns true if the specified prerender has | 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. | 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 | 43 // If the call results in cancelling a PrerenderContents, a task to destroy |
44 // it is also posted to the UI thread. | 44 // it is also posted to the UI thread. |
45 // | 45 // |
46 // When true is returned, it is guaranteed that the RenderView will never | 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 | 47 // be displayed. When false is returned, the RenderView has either been |
48 // swapped into a tab or has already been destroyed. | 48 // swapped into a tab or has already been destroyed. |
49 bool TryCancel(int child_id, int route_id, FinalStatus final_status); | 49 bool TryCancel(int child_id, int route_id, FinalStatus final_status); |
50 | 50 |
51 // Same as above, but can only called on the IO Thread. Does not acquire a | 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. | 52 // lock when the RenderView is not being prerendered. |
53 bool TryCancelOnIOThread(int child_id, int route_id, | 53 bool TryCancelOnIOThread(int child_id, int route_id, |
54 FinalStatus final_status); | 54 FinalStatus final_status); |
55 | 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) const; | |
61 | |
62 // Gets the FinalStatus of the specified prerendered RenderView. Returns | 56 // Gets the FinalStatus of the specified prerendered RenderView. Returns |
63 // |true| and sets |final_status| to the status of the RenderView if it | 57 // |true| and sets |final_status| to the status of the RenderView if it |
64 // is found, returns false otherwise. | 58 // is found, returns false otherwise. |
65 bool GetFinalStatus(int child_id, int route_id, | 59 bool GetFinalStatus(int child_id, int route_id, |
66 FinalStatus* final_status) const; | 60 FinalStatus* final_status) const; |
67 | 61 |
| 62 // Returns whether or not a RenderView is prerendering. Can only be called on |
| 63 // the IO thread. Does not acquire a lock, so may claim a RenderView that has |
| 64 // been displayed or destroyed is still prerendering. |
| 65 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; |
| 66 |
68 private: | 67 private: |
69 friend struct DefaultSingletonTraits<PrerenderTracker>; | |
70 friend class PrerenderContents; | 68 friend class PrerenderContents; |
71 | 69 |
| 70 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerNull); |
72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | 71 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); |
73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | 72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); |
74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | 73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); |
75 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | 74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); |
76 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | 75 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); |
77 | 76 |
78 typedef std::pair<int, int> ChildRouteIdPair; | 77 typedef std::pair<int, int> ChildRouteIdPair; |
79 | 78 |
80 // Map of child/route id pairs to final statuses. | 79 // Map of child/route id pairs to final statuses. |
81 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | 80 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; |
82 // Set of child/route id pairs that may be prerendering. | 81 // Set of child/route id pairs that may be prerendering. |
83 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; | 82 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; |
84 | 83 |
85 PrerenderTracker(); | |
86 ~PrerenderTracker(); | |
87 | |
88 // Must be called when a RenderView starts prerendering, before the first | 84 // Must be called when a RenderView starts prerendering, before the first |
89 // navigation starts to avoid any races. | 85 // navigation starts to avoid any races. |
90 void OnPrerenderingStarted(int child_id, int route_id, | 86 void OnPrerenderingStarted(int child_id, int route_id, |
91 PrerenderManager* prerender_manager); | 87 PrerenderManager* prerender_manager); |
92 | 88 |
93 // Must be called when a RenderView stops prerendering, either because the | 89 // Must be called when a RenderView stops prerendering, either because the |
94 // RenderView was used or prerendering was cancelled and it is being | 90 // RenderView was used or prerendering was cancelled and it is being |
95 // destroyed. | 91 // destroyed. |
96 void OnPrerenderingFinished(int child_id, int route_id); | 92 void OnPrerenderingFinished(int child_id, int route_id); |
97 | 93 |
(...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 | 129 // the IO thread. May contain entries that have since been displayed. Only |
134 // used to prevent locking when not needed. | 130 // used to prevent locking when not needed. |
135 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; | 131 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; |
136 | 132 |
137 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 133 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
138 }; | 134 }; |
139 | 135 |
140 } // namespace prerender | 136 } // namespace prerender |
141 | 137 |
142 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 138 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
OLD | NEW |