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 #include "chrome/browser/prerender/prerender_tracker.h" | 5 #include "chrome/browser/prerender/prerender_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h" | 9 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "net/url_request/url_request_context.h" |
| 13 #include "net/url_request/url_request_context_getter.h" |
11 | 14 |
12 using content::BrowserThread; | 15 using content::BrowserThread; |
13 | 16 |
14 namespace prerender { | 17 namespace prerender { |
15 | 18 |
16 PrerenderTracker::PrerenderTracker() { | 19 PrerenderTracker::PrerenderTracker() { |
17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
18 } | 21 } |
19 | 22 |
20 PrerenderTracker::~PrerenderTracker() { | 23 PrerenderTracker::~PrerenderTracker() { |
| 24 for (PrerenderProcessSet::const_iterator it = |
| 25 prerender_process_hosts_.begin(); |
| 26 it != prerender_process_hosts_.end(); |
| 27 ++it) { |
| 28 (*it)->RemoveObserver(this); |
| 29 } |
21 } | 30 } |
22 | 31 |
23 bool PrerenderTracker::IsPendingSwapRequestOnIOThread( | 32 bool PrerenderTracker::IsPendingSwapRequestOnIOThread( |
24 int render_process_id, int render_frame_id, const GURL& url) const { | 33 int render_process_id, int render_frame_id, const GURL& url) const { |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
26 | 35 |
27 ChildRouteIdPair render_frame_route_id_pair( | 36 ChildRouteIdPair render_frame_route_id_pair( |
28 render_process_id, render_frame_id); | 37 render_process_id, render_frame_id); |
29 PendingSwapThrottleMap::const_iterator it = | 38 PendingSwapThrottleMap::const_iterator it = |
30 pending_swap_throttle_map_.find(render_frame_route_id_pair); | 39 pending_swap_throttle_map_.find(render_frame_route_id_pair); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 105 } |
97 | 106 |
98 PrerenderTracker::PendingSwapThrottleData::PendingSwapThrottleData( | 107 PrerenderTracker::PendingSwapThrottleData::PendingSwapThrottleData( |
99 const GURL& swap_url) | 108 const GURL& swap_url) |
100 : url(swap_url) { | 109 : url(swap_url) { |
101 } | 110 } |
102 | 111 |
103 PrerenderTracker::PendingSwapThrottleData::~PendingSwapThrottleData() { | 112 PrerenderTracker::PendingSwapThrottleData::~PendingSwapThrottleData() { |
104 } | 113 } |
105 | 114 |
| 115 scoped_refptr<PrerenderCookieStore> |
| 116 PrerenderTracker::GetPrerenderCookieStoreForRenderProcess( |
| 117 int process_id) { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 119 PrerenderCookieStoreMap::const_iterator it = |
| 120 prerender_cookie_store_map_.find(process_id); |
| 121 |
| 122 if (it == prerender_cookie_store_map_.end()) |
| 123 return NULL; |
| 124 |
| 125 return it->second; |
| 126 } |
| 127 |
| 128 bool PrerenderTracker::IsProcessPrerendering( |
| 129 content::RenderProcessHost* process_host) { |
| 130 return (prerender_process_hosts_.find(process_host) != |
| 131 prerender_process_hosts_.end()); |
| 132 } |
| 133 |
| 134 void PrerenderTracker::OnCookieChangedForURL(int process_id, const GURL& url) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 136 |
| 137 for (PrerenderCookieStoreMap::iterator it = |
| 138 prerender_cookie_store_map_.begin(); |
| 139 it != prerender_cookie_store_map_.end(); |
| 140 ++it) { |
| 141 // Report the change to all prerender cookie stores which may be affected. |
| 142 // This does not include the one for the render process originating the |
| 143 // change. |
| 144 if (it->first != process_id) |
| 145 it->second->OnCookieChangedForURL(url); |
| 146 } |
| 147 } |
| 148 |
| 149 void PrerenderTracker::RenderProcessHostDestroyed( |
| 150 content::RenderProcessHost* host) { |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 152 prerender_process_hosts_.erase(host); |
| 153 BrowserThread::PostTask( |
| 154 BrowserThread::IO, FROM_HERE, |
| 155 base::Bind(&PrerenderTracker::RemovePrerenderCookieStoreOnIOThread, |
| 156 base::Unretained(this), host->GetID(), false)); |
| 157 } |
| 158 |
| 159 void PrerenderTracker::RemovePrerenderCookieStoreOnIOThread(int process_id, |
| 160 bool was_swapped) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 162 |
| 163 PrerenderCookieStoreMap::iterator it = |
| 164 prerender_cookie_store_map_.find(process_id); |
| 165 |
| 166 if (it == prerender_cookie_store_map_.end()) |
| 167 return; |
| 168 |
| 169 if (was_swapped) |
| 170 it->second->ApplyChanges(); |
| 171 |
| 172 prerender_cookie_store_map_.erase(it); |
| 173 } |
| 174 |
| 175 void PrerenderTracker::OnPrerenderSwapped(content::RenderProcessHost* host) { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 prerender_process_hosts_.erase(host); |
| 178 BrowserThread::PostTask( |
| 179 BrowserThread::IO, FROM_HERE, |
| 180 base::Bind(&PrerenderTracker::RemovePrerenderCookieStoreOnIOThread, |
| 181 base::Unretained(this), host->GetID(), true)); |
| 182 } |
| 183 |
| 184 void PrerenderTracker::AddPrerender( |
| 185 content::RenderProcessHost* host, |
| 186 net::URLRequestContextGetter* request_context, |
| 187 const base::Closure& cookie_conflict_cb) { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 189 DCHECK(request_context != NULL); |
| 190 DCHECK(prerender_process_hosts_.find(host)== prerender_process_hosts_.end()); |
| 191 prerender_process_hosts_.insert(host); |
| 192 host->AddObserver(this); |
| 193 BrowserThread::PostTask( |
| 194 BrowserThread::IO, FROM_HERE, |
| 195 base::Bind(&PrerenderTracker::AddPrerenderCookieStoreOnIOThread, |
| 196 base::Unretained(this), host->GetID(), |
| 197 make_scoped_refptr(request_context), |
| 198 cookie_conflict_cb)); |
| 199 } |
| 200 |
| 201 void PrerenderTracker::AddPrerenderCookieStoreOnIOThread( |
| 202 int process_id, |
| 203 scoped_refptr<net::URLRequestContextGetter> request_context, |
| 204 const base::Closure& cookie_conflict_cb) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 206 DCHECK(request_context != NULL); |
| 207 net::CookieMonster* cookie_monster = |
| 208 request_context->GetURLRequestContext()->cookie_store()-> |
| 209 GetCookieMonster(); |
| 210 DCHECK(cookie_monster != NULL); |
| 211 bool exists = (prerender_cookie_store_map_.find(process_id) != |
| 212 prerender_cookie_store_map_.end()); |
| 213 DCHECK(!exists); |
| 214 if (exists) |
| 215 return; |
| 216 prerender_cookie_store_map_[process_id] = |
| 217 new PrerenderCookieStore(make_scoped_refptr(cookie_monster), |
| 218 cookie_conflict_cb); |
| 219 } |
| 220 |
106 } // namespace prerender | 221 } // namespace prerender |
OLD | NEW |