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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ 7 8 #include <map> 9 #include <set> 10 #include <utility> 11 12 #include "base/containers/hash_tables.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/synchronization/lock.h" 16 #include "chrome/browser/prerender/prerender_cookie_store.h" 17 #include "content/public/browser/render_process_host_observer.h" 18 #include "url/gurl.h" 19 20 namespace net { 21 class URLRequestContextGetter; 22 } 23 24 namespace prerender { 25 26 class PrerenderPendingSwapThrottle; 27 28 // Global object for maintaining various prerender state on the IO thread. 29 class PrerenderTracker { 30 public: 31 typedef std::pair<int, int> ChildRouteIdPair; 32 33 PrerenderTracker(); 34 virtual ~PrerenderTracker(); 35 36 // Returns whether or not a RenderFrame and URL are regarding a pending 37 // prerender swap. Can only be called on the IO thread. 38 bool IsPendingSwapRequestOnIOThread(int render_process_id, 39 int render_frame_id, 40 const GURL& url) const; 41 42 // Called when a PrerenderPendingSwapThrottle defers a request. Cancel or 43 // Resume will be called on |throttle| when the prerender is canceled or used, 44 // respectively. 45 void AddPendingSwapThrottleOnIOThread( 46 int render_process_id, int render_frame_id, const GURL& url, 47 const base::WeakPtr<PrerenderPendingSwapThrottle>& throttle); 48 49 // Called to add throttles for a pending prerender swap. 50 void AddPrerenderPendingSwap( 51 const ChildRouteIdPair& render_frame_route_id_pair, 52 const GURL& url); 53 54 // Called to remove the throttles for a pending prerender swap. 55 void RemovePrerenderPendingSwap( 56 const ChildRouteIdPair& render_frame_route_id_pair, 57 bool swap_successful); 58 59 // Gets the Prerender Cookie Store for a specific render process, if it 60 // is a prerender. Only to be called from the IO thread. 61 scoped_refptr<PrerenderCookieStore> GetPrerenderCookieStoreForRenderProcess( 62 int process_id); 63 64 // Called when a given render process has changed a cookie for |url|, 65 // in |cookie_monster|. 66 // Only to be called from the IO thread. 67 void OnCookieChangedForURL(int process_id, 68 net::CookieMonster* cookie_monster, 69 const GURL& url); 70 71 void AddPrerenderCookieStoreOnIOThread( 72 int process_id, 73 scoped_refptr<net::URLRequestContextGetter> request_context, 74 const base::Closure& cookie_conflict_cb); 75 76 void RemovePrerenderCookieStoreOnIOThread(int process_id, bool was_swapped); 77 78 private: 79 // Add/remove prerenders pending swap on the IO Thread. 80 void AddPrerenderPendingSwapOnIOThread( 81 const ChildRouteIdPair& render_frame_route_id_pair, const GURL& url); 82 void RemovePrerenderPendingSwapOnIOThread( 83 const ChildRouteIdPair& render_frame_route_id_pair, 84 bool swap_successful); 85 86 struct PendingSwapThrottleData { 87 explicit PendingSwapThrottleData(const GURL& swap_url); 88 ~PendingSwapThrottleData(); 89 GURL url; 90 base::WeakPtr<PrerenderPendingSwapThrottle> throttle; 91 }; 92 93 // Map of pending prerender swaps and their associated throttles, 94 // maintained on the IO thread. The key is the routing ID pair 95 // of a RenderFrame. 96 typedef std::map<ChildRouteIdPair, PendingSwapThrottleData> 97 PendingSwapThrottleMap; 98 PendingSwapThrottleMap pending_swap_throttle_map_; 99 100 // Map of prerendering render process ids to PrerenderCookieStore used for 101 // the prerender. Only to be used on the IO thread. 102 typedef base::hash_map<int, scoped_refptr<PrerenderCookieStore> > 103 PrerenderCookieStoreMap; 104 PrerenderCookieStoreMap prerender_cookie_store_map_; 105 106 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); 107 }; 108 109 } // namespace prerender 110 111 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ 112