| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "chrome/browser/prerender/prerender_contents.h" | |
| 18 #include "chrome/browser/prerender/prerender_final_status.h" | 17 #include "chrome/browser/prerender/prerender_final_status.h" |
| 19 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 20 | 19 |
| 21 namespace prerender { | 20 namespace prerender { |
| 22 | 21 |
| 23 class PrerenderManager; | 22 class PrerenderManager; |
| 24 class PrerenderPendingSwapThrottle; | 23 class PrerenderPendingSwapThrottle; |
| 25 class PrerenderResourceThrottle; | 24 class PrerenderResourceThrottle; |
| 26 struct RenderViewInfo; | 25 struct RenderViewInfo; |
| 27 | 26 |
| 28 // PrerenderTracker is responsible for keeping track of all prerendering | 27 // PrerenderTracker is responsible for keeping track of all prerendering |
| 29 // RenderViews. | 28 // RenderViews. |
| 30 class PrerenderTracker : public base::NonThreadSafe, | 29 class PrerenderTracker : public base::NonThreadSafe { |
| 31 public PrerenderContents::Observer { | |
| 32 public: | 30 public: |
| 33 typedef std::pair<int, int> ChildRouteIdPair; | 31 typedef std::pair<int, int> ChildRouteIdPair; |
| 34 | 32 |
| 35 PrerenderTracker(); | 33 PrerenderTracker(); |
| 36 virtual ~PrerenderTracker(); | 34 virtual ~PrerenderTracker(); |
| 37 | 35 |
| 38 // Returns whether or not a RenderView is prerendering. Can only be called on | |
| 39 // the IO thread. Does not acquire a lock, so may claim a RenderView that has | |
| 40 // been displayed or destroyed is still prerendering. | |
| 41 bool IsPrerenderingOnIOThread(int child_id, int route_id) const; | |
| 42 | |
| 43 // Returns whether or not a RenderFrame and URL are regarding a pending | 36 // Returns whether or not a RenderFrame and URL are regarding a pending |
| 44 // prerender swap. Can only be called on the IO thread. Does not acquire a | 37 // prerender swap. Can only be called on the IO thread. Does not acquire a |
| 45 // lock. | 38 // lock. |
| 46 bool IsPendingSwapRequestOnIOThread(int render_process_id, | 39 bool IsPendingSwapRequestOnIOThread(int render_process_id, |
| 47 int render_frame_id, | 40 int render_frame_id, |
| 48 const GURL& url) const; | 41 const GURL& url) const; |
| 49 | 42 |
| 50 // Called when a PrerenderResourceThrottle defers a request. Cancel | 43 // Called when a PrerenderResourceThrottle defers a request. Cancel |
| 51 // or Resume will be called on |throttle| when the prerender is | 44 // or Resume will be called on |throttle| when the prerender is |
| 52 // canceled or used, respectively. | 45 // canceled or used, respectively. |
| 53 void AddResourceThrottleOnIOThread( | |
| 54 int child_id, int route_id, | |
| 55 const base::WeakPtr<PrerenderResourceThrottle>& throttle); | |
| 56 | |
| 57 // Called when a PrerenderResourceThrottle defers a request. Cancel | |
| 58 // or Resume will be called on |throttle| when the prerender is | |
| 59 // canceled or used, respectively. | |
| 60 void AddPendingSwapThrottleOnIOThread( | 46 void AddPendingSwapThrottleOnIOThread( |
| 61 int render_process_id, int render_frame_id, const GURL& url, | 47 int render_process_id, int render_frame_id, const GURL& url, |
| 62 const base::WeakPtr<PrerenderPendingSwapThrottle>& throttle); | 48 const base::WeakPtr<PrerenderPendingSwapThrottle>& throttle); |
| 63 | 49 |
| 64 // Called to add throttles for a pending prerender swap. | 50 // Called to add throttles for a pending prerender swap. |
| 65 void AddPrerenderPendingSwap( | 51 void AddPrerenderPendingSwap( |
| 66 const ChildRouteIdPair& render_frame_route_id_pair, | 52 const ChildRouteIdPair& render_frame_route_id_pair, |
| 67 const GURL& url); | 53 const GURL& url); |
| 68 | 54 |
| 69 // Called to remove the throttles for a pending prerender swap. | 55 // Called to remove the throttles for a pending prerender swap. |
| 70 void RemovePrerenderPendingSwap( | 56 void RemovePrerenderPendingSwap( |
| 71 const ChildRouteIdPair& render_frame_route_id_pair, | 57 const ChildRouteIdPair& render_frame_route_id_pair, |
| 72 bool swap_successful); | 58 bool swap_successful); |
| 73 | 59 |
| 74 private: | 60 private: |
| 75 friend class PrerenderContents; | |
| 76 | |
| 77 // Map of child/route id pairs to final statuses. | 61 // Map of child/route id pairs to final statuses. |
| 78 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; | 62 typedef std::map<ChildRouteIdPair, RenderViewInfo> FinalStatusMap; |
| 79 // List of throttled requests. | |
| 80 typedef std::vector<base::WeakPtr<PrerenderResourceThrottle> > | |
| 81 ResourceThrottleList; | |
| 82 // Set of throttled requests. | |
| 83 typedef std::map<ChildRouteIdPair, ResourceThrottleList> ResourceThrottleMap; | |
| 84 struct PendingSwapThrottleData { | 63 struct PendingSwapThrottleData { |
| 85 explicit PendingSwapThrottleData(const GURL& swap_url); | 64 explicit PendingSwapThrottleData(const GURL& swap_url); |
| 86 ~PendingSwapThrottleData(); | 65 ~PendingSwapThrottleData(); |
| 87 GURL url; | 66 GURL url; |
| 88 base::WeakPtr<PrerenderPendingSwapThrottle> throttle; | 67 base::WeakPtr<PrerenderPendingSwapThrottle> throttle; |
| 89 }; | 68 }; |
| 90 // Set of throttles for pending swaps. The key is the routing ID pair | 69 // Set of throttles for pending swaps. The key is the routing ID pair |
| 91 // of a RenderFrame. | 70 // of a RenderFrame. |
| 92 typedef std::map<ChildRouteIdPair, PendingSwapThrottleData> | 71 typedef std::map<ChildRouteIdPair, PendingSwapThrottleData> |
| 93 PendingSwapThrottleMap; | 72 PendingSwapThrottleMap; |
| 94 | 73 |
| 95 // From PrerenderContents::Observer: | |
| 96 virtual void OnPrerenderStart(PrerenderContents* prerender_contents) OVERRIDE; | |
| 97 virtual void OnPrerenderStop(PrerenderContents* prerender_contents) OVERRIDE; | |
| 98 | |
| 99 // Add/remove the specified pair to |possibly_prerendering_io_thread_set_| on | |
| 100 // the IO Thread. | |
| 101 void AddPrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair); | |
| 102 void RemovePrerenderOnIOThread(const ChildRouteIdPair& child_route_id_pair, | |
| 103 FinalStatus final_status); | |
| 104 | |
| 105 // Add/remove prerenders pending swap on the IO Thread. | 74 // Add/remove prerenders pending swap on the IO Thread. |
| 106 void AddPrerenderPendingSwapOnIOThread( | 75 void AddPrerenderPendingSwapOnIOThread( |
| 107 const ChildRouteIdPair& render_frame_route_id_pair, const GURL& url); | 76 const ChildRouteIdPair& render_frame_route_id_pair, const GURL& url); |
| 108 void RemovePrerenderPendingSwapOnIOThread( | 77 void RemovePrerenderPendingSwapOnIOThread( |
| 109 const ChildRouteIdPair& render_frame_route_id_pair, | 78 const ChildRouteIdPair& render_frame_route_id_pair, |
| 110 bool swap_successful); | 79 bool swap_successful); |
| 111 | 80 |
| 112 static PrerenderTracker* GetDefault(); | 81 static PrerenderTracker* GetDefault(); |
| 113 | 82 |
| 114 // Resources that are throttled, pending a prerender use. The keys are a | |
| 115 // superset of child/route id pairs that are prerendering. Can only access on | |
| 116 // the IO thread. May contain entries that have since been displayed. Used | |
| 117 // to prevent locking when not needed. | |
| 118 ResourceThrottleMap resource_throttle_io_thread_map_; | |
| 119 | |
| 120 // Map of pending prerender swaps and their associated throttles, | 83 // Map of pending prerender swaps and their associated throttles, |
| 121 // maintained on the IO thread. | 84 // maintained on the IO thread. |
| 122 PendingSwapThrottleMap pending_swap_throttle_map_; | 85 PendingSwapThrottleMap pending_swap_throttle_map_; |
| 123 | 86 |
| 124 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 87 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
| 125 }; | 88 }; |
| 126 | 89 |
| 127 } // namespace prerender | 90 } // namespace prerender |
| 128 | 91 |
| 129 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 92 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
| OLD | NEW |