Chromium Code Reviews| 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" | |
| 13 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 14 #include "chrome/browser/prerender/prerender_final_status.h" | 13 #include "chrome/browser/prerender/prerender_final_status.h" |
| 14 #include "content/browser/renderer_host/resource_dispatcher_host.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 : public ResourceDispatcherHost::Observer { |
| 25 public: | 24 public: |
| 26 // Returns the PrerenderTracker singleton. | 25 PrerenderTracker(); |
| 27 static PrerenderTracker* GetInstance(); | 26 virtual ~PrerenderTracker(); |
|
mmenke
2011/05/23 22:51:03
Since nothing inherits from PrerenderTracker, so w
dominich
2011/05/24 15:10:28
Done.
| |
| 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 for the RenderView | 35 // If it returns true, all subsequent calls to TryCancel for the RenderView |
| 37 // will return false. | 36 // 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 // ResourceDispatcherHost::Observer implementation. | |
| 62 virtual bool ShouldBeginRequest( | |
| 63 int child_id, int route_id, | |
| 64 const ResourceHostMsg_Request& request_data, | |
| 65 const content::ResourceContext& resource_context, | |
| 66 const GURL& referrer) OVERRIDE; | |
| 67 | |
| 68 virtual void MutateLoadFlags(int child_id, int route_id, | |
| 69 int* load_flags) OVERRIDE; | |
| 70 | |
| 68 protected: | 71 protected: |
| 69 friend class PrerenderContents; | 72 friend class PrerenderContents; |
| 70 | 73 |
| 71 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | |
| 72 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | |
| 73 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | |
| 74 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | |
| 75 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | |
| 76 | |
| 77 // Must be called when a RenderView starts prerendering, before the first | 74 // Must be called when a RenderView starts prerendering, before the first |
| 78 // navigation starts to avoid any races. | 75 // navigation starts to avoid any races. |
| 79 void OnPrerenderingStarted(int child_id, int route_id, | 76 void OnPrerenderingStarted(int child_id, int route_id, |
| 80 PrerenderManager* prerender_manager); | 77 PrerenderManager* prerender_manager); |
| 81 | 78 |
| 82 // Must be called when a RenderView stops prerendering, either because the | 79 // Must be called when a RenderView stops prerendering, either because the |
| 83 // RenderView was used or prerendering was cancelled and it is being | 80 // RenderView was used or prerendering was cancelled and it is being |
| 84 // destroyed. | 81 // destroyed. |
| 85 void OnPrerenderingFinished(int child_id, int route_id); | 82 void OnPrerenderingFinished(int child_id, int route_id); |
| 86 | 83 |
| 87 private: | 84 private: |
| 88 friend struct DefaultSingletonTraits<PrerenderTracker>; | 85 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerNull); |
| 86 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerUsed); | |
| 87 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelled); | |
| 88 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledOnIO); | |
| 89 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerCancelledFast); | |
| 90 FRIEND_TEST_ALL_PREFIXES(PrerenderTrackerTest, PrerenderTrackerMultiple); | |
| 91 | |
| 92 struct RenderViewInfo { | |
| 93 explicit RenderViewInfo(PrerenderManager* prerender_manager); | |
| 94 ~RenderViewInfo(); | |
| 95 | |
| 96 FinalStatus final_status; | |
| 97 base::WeakPtr<PrerenderManager> prerender_manager; | |
| 98 }; | |
| 89 | 99 |
| 90 typedef std::pair<int, int> ChildRouteIdPair; | 100 typedef std::pair<int, int> ChildRouteIdPair; |
| 91 | 101 |
| 92 // Map of child/route id pairs to final statuses. | 102 // Map of child/route id pairs to final statuses. |
| 93 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | 103 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; |
| 94 // Set of child/route id pairs that may be prerendering. | 104 // Set of child/route id pairs that may be prerendering. |
| 95 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; | 105 typedef std::set<ChildRouteIdPair> PossiblyPrerenderingChildRouteIdPairs; |
| 96 | 106 |
| 97 PrerenderTracker(); | |
| 98 ~PrerenderTracker(); | |
| 99 | |
| 100 // Attempts to set the FinalStatus of the specified RenderView to | 107 // Attempts to set the FinalStatus of the specified RenderView to |
| 101 // |final_status|. If the FinalStatus of the RenderView has already been | 108 // |final_status|. If the FinalStatus of the RenderView has already been |
| 102 // set, does nothing. Returns the resulting FinalStatus of that RenderView, | 109 // set, does nothing. Returns the resulting FinalStatus of that RenderView, |
| 103 // regardless of success or failure. If the RenderView isn't currently | 110 // regardless of success or failure. If the RenderView isn't currently |
| 104 // prerendering, returns FINAL_STATUS_MAX. | 111 // prerendering, returns FINAL_STATUS_MAX. |
| 105 FinalStatus SetFinalStatus(int child_id, int route_id, | 112 FinalStatus SetFinalStatus(int child_id, int route_id, |
| 106 FinalStatus final_status); | 113 FinalStatus final_status); |
| 107 | 114 |
| 108 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on | 115 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on |
| 109 // the IO Thread. | 116 // the IO Thread. |
| 110 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | 117 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); |
| 111 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | 118 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); |
| 112 | 119 |
| 120 // Returns whether or not a RenderView is prerendering. Can only be called on | |
| 121 // the IO thread. Does not acquire a lock, so may claim a RenderView that has | |
| 122 // been displayed or destroyed is still prerendering. | |
| 123 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; | |
| 124 | |
| 113 // Tasks posted to the IO Thread to call the above functions. | 125 // Tasks posted to the IO Thread to call the above functions. |
| 114 static void AddPrerenderOnIOThreadTask( | 126 static void AddPrerenderOnIOThreadTask( |
| 115 const ChildRouteIdPair& child_route_id_pair); | 127 const ChildRouteIdPair& child_route_id_pair); |
| 116 static void RemovePrerenderOnIOThreadTask( | 128 static void RemovePrerenderOnIOThreadTask( |
| 117 const ChildRouteIdPair& child_route_id_pair); | 129 const ChildRouteIdPair& child_route_id_pair); |
| 118 | 130 |
| 119 // |final_status_map_lock_| protects access to |final_status_map_|. | 131 // |final_status_map_lock_| protects access to |final_status_map_|. |
| 120 mutable base::Lock final_status_map_lock_; | 132 mutable base::Lock final_status_map_lock_; |
| 121 // Map containing child/route id pairs and their final statuses. Must only be | 133 // Map containing child/route id pairs and their final statuses. Must only be |
| 122 // accessed while the lock is held. Values are always accurate and up to | 134 // accessed while the lock is held. Values are always accurate and up to |
| 123 // date. | 135 // date. |
| 124 FinalStatusMap final_status_map_; | 136 FinalStatusMap final_status_map_; |
| 125 | 137 |
| 126 // Superset of child/route id pairs that are prerendering. Can only access on | 138 // Superset of child/route id pairs that are prerendering. Can only access on |
| 127 // the IO thread. May contain entries that have since been displayed. Only | 139 // the IO thread. May contain entries that have since been displayed. Only |
| 128 // used to prevent locking when not needed. | 140 // used to prevent locking when not needed. |
| 129 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; | 141 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; |
| 130 | 142 |
| 131 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 143 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
| 132 }; | 144 }; |
| 133 | 145 |
| 134 } // namespace prerender | 146 } // namespace prerender |
| 135 | 147 |
| 136 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 148 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
| OLD | NEW |