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.
5 #include "chrome/browser/prerender/prerender_tracker.h"
8 #include "base/logging.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "net/url_request/url_request_context.h"
12 #include "net/url_request/url_request_context_getter.h"
14 using content::BrowserThread
;
18 PrerenderTracker::PrerenderTracker() {
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
22 PrerenderTracker::~PrerenderTracker() {
25 scoped_refptr
<PrerenderCookieStore
>
26 PrerenderTracker::GetPrerenderCookieStoreForRenderProcess(
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
29 PrerenderCookieStoreMap::const_iterator it
=
30 prerender_cookie_store_map_
.find(process_id
);
32 if (it
== prerender_cookie_store_map_
.end())
38 void PrerenderTracker::OnCookieChangedForURL(
40 net::CookieMonster
* cookie_monster
,
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
44 // We only care about cookie changes by non-prerender tabs, since only those
45 // get applied to the underlying cookie store. Therefore, if a cookie change
46 // originated from a prerender, there is nothing to do.
47 if (ContainsKey(prerender_cookie_store_map_
, process_id
))
50 // Since the cookie change did not come from a prerender, broadcast it too
51 // all prerenders so that they can be cancelled if there is a conflict.
52 for (PrerenderCookieStoreMap::iterator it
=
53 prerender_cookie_store_map_
.begin();
54 it
!= prerender_cookie_store_map_
.end();
56 it
->second
->OnCookieChangedForURL(cookie_monster
, url
);
60 void PrerenderTracker::RemovePrerenderCookieStoreOnIOThread(int process_id
,
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
64 PrerenderCookieStoreMap::iterator it
=
65 prerender_cookie_store_map_
.find(process_id
);
67 if (it
== prerender_cookie_store_map_
.end())
70 std::vector
<GURL
> cookie_change_urls
;
72 it
->second
->ApplyChanges(&cookie_change_urls
);
74 scoped_refptr
<net::CookieMonster
> cookie_monster(
75 it
->second
->default_cookie_monster());
77 prerender_cookie_store_map_
.erase(it
);
79 // For each cookie updated by ApplyChanges, we need to call
80 // OnCookieChangedForURL so that any potentially conflicting prerenders
82 for (std::vector
<GURL
>::const_iterator url_it
= cookie_change_urls
.begin();
83 url_it
!= cookie_change_urls
.end();
85 OnCookieChangedForURL(process_id
, cookie_monster
.get(), *url_it
);
89 void PrerenderTracker::AddPrerenderCookieStoreOnIOThread(
91 scoped_refptr
<net::URLRequestContextGetter
> request_context
,
92 const base::Closure
& cookie_conflict_cb
) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
94 DCHECK(request_context
.get() != NULL
);
95 net::CookieMonster
* cookie_monster
=
96 request_context
->GetURLRequestContext()->cookie_store()->
98 DCHECK(cookie_monster
!= NULL
);
99 bool exists
= (prerender_cookie_store_map_
.find(process_id
) !=
100 prerender_cookie_store_map_
.end());
104 prerender_cookie_store_map_
[process_id
] =
105 new PrerenderCookieStore(make_scoped_refptr(cookie_monster
),
109 } // namespace prerender