app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_tracker.cc
blob54cf28472de139134fb803d755f9a2a62ec30619
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"
7 #include "base/bind.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;
16 namespace prerender {
18 PrerenderTracker::PrerenderTracker() {
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
22 PrerenderTracker::~PrerenderTracker() {
25 scoped_refptr<PrerenderCookieStore>
26 PrerenderTracker::GetPrerenderCookieStoreForRenderProcess(
27 int process_id) {
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())
33 return NULL;
35 return it->second;
38 void PrerenderTracker::OnCookieChangedForURL(
39 int process_id,
40 net::CookieMonster* cookie_monster,
41 const GURL& url) {
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))
48 return;
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();
55 ++it) {
56 it->second->OnCookieChangedForURL(cookie_monster, url);
60 void PrerenderTracker::RemovePrerenderCookieStoreOnIOThread(int process_id,
61 bool was_swapped) {
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())
68 return;
70 std::vector<GURL> cookie_change_urls;
71 if (was_swapped)
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
81 // will be aborted.
82 for (std::vector<GURL>::const_iterator url_it = cookie_change_urls.begin();
83 url_it != cookie_change_urls.end();
84 ++url_it) {
85 OnCookieChangedForURL(process_id, cookie_monster.get(), *url_it);
89 void PrerenderTracker::AddPrerenderCookieStoreOnIOThread(
90 int process_id,
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()->
97 GetCookieMonster();
98 DCHECK(cookie_monster != NULL);
99 bool exists = (prerender_cookie_store_map_.find(process_id) !=
100 prerender_cookie_store_map_.end());
101 DCHECK(!exists);
102 if (exists)
103 return;
104 prerender_cookie_store_map_[process_id] =
105 new PrerenderCookieStore(make_scoped_refptr(cookie_monster),
106 cookie_conflict_cb);
109 } // namespace prerender