| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 #include "chrome/browser/browsing_data_cookie_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "content/browser/browser_thread.h" | |
| 12 #include "googleurl/src/gurl.h" | |
| 13 #include "net/url_request/url_request_context.h" | |
| 14 #include "net/url_request/url_request_context_getter.h" | |
| 15 | |
| 16 BrowsingDataCookieHelper::BrowsingDataCookieHelper(Profile* profile) | |
| 17 : is_fetching_(false), | |
| 18 profile_(profile), | |
| 19 request_context_getter_(profile->GetRequestContext()) { | |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 21 } | |
| 22 | |
| 23 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() { | |
| 24 } | |
| 25 | |
| 26 void BrowsingDataCookieHelper::StartFetching( | |
| 27 const base::Callback<void(const net::CookieList& cookies)>& callback) { | |
| 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 29 DCHECK(!is_fetching_); | |
| 30 DCHECK(!callback.is_null()); | |
| 31 DCHECK(completion_callback_.is_null()); | |
| 32 is_fetching_ = true; | |
| 33 completion_callback_ = callback; | |
| 34 BrowserThread::PostTask( | |
| 35 BrowserThread::IO, FROM_HERE, | |
| 36 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread, this)); | |
| 37 } | |
| 38 | |
| 39 void BrowsingDataCookieHelper::CancelNotification() { | |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 41 completion_callback_.Reset(); | |
| 42 } | |
| 43 | |
| 44 void BrowsingDataCookieHelper::DeleteCookie( | |
| 45 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 47 BrowserThread::PostTask( | |
| 48 BrowserThread::IO, FROM_HERE, | |
| 49 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread, | |
| 50 this, cookie)); | |
| 51 } | |
| 52 | |
| 53 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() { | |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 55 scoped_refptr<net::CookieMonster> cookie_monster = | |
| 56 request_context_getter_->GetURLRequestContext()-> | |
| 57 cookie_store()->GetCookieMonster(); | |
| 58 if (cookie_monster) { | |
| 59 cookie_monster->GetAllCookiesAsync( | |
| 60 base::Bind(&BrowsingDataCookieHelper::OnFetchComplete, this)); | |
| 61 } else { | |
| 62 OnFetchComplete(net::CookieList()); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void BrowsingDataCookieHelper::OnFetchComplete(const net::CookieList& cookies) { | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 68 BrowserThread::PostTask( | |
| 69 BrowserThread::UI, FROM_HERE, | |
| 70 base::Bind(&BrowsingDataCookieHelper::NotifyInUIThread, this, cookies)); | |
| 71 } | |
| 72 | |
| 73 void BrowsingDataCookieHelper::NotifyInUIThread( | |
| 74 const net::CookieList& cookies) { | |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 76 DCHECK(is_fetching_); | |
| 77 is_fetching_ = false; | |
| 78 if (!completion_callback_.is_null()) { | |
| 79 completion_callback_.Run(cookies); | |
| 80 completion_callback_.Reset(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void BrowsingDataCookieHelper::DeleteCookieOnIOThread( | |
| 85 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 87 scoped_refptr<net::CookieMonster> cookie_monster = | |
| 88 request_context_getter_->GetURLRequestContext()-> | |
| 89 cookie_store()->GetCookieMonster(); | |
| 90 if (cookie_monster) { | |
| 91 cookie_monster->DeleteCanonicalCookieAsync( | |
| 92 cookie, net::CookieMonster::DeleteCookieCallback()); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper( | |
| 97 Profile* profile) | |
| 98 : BrowsingDataCookieHelper(profile), | |
| 99 profile_(profile) { | |
| 100 } | |
| 101 | |
| 102 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() {} | |
| 103 | |
| 104 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { | |
| 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 106 CannedBrowsingDataCookieHelper* clone = | |
| 107 new CannedBrowsingDataCookieHelper(profile_); | |
| 108 | |
| 109 clone->cookie_list_ = cookie_list_; | |
| 110 return clone; | |
| 111 } | |
| 112 | |
| 113 void CannedBrowsingDataCookieHelper::AddReadCookies( | |
| 114 const GURL& url, | |
| 115 const net::CookieList& cookie_list) { | |
| 116 typedef net::CookieList::const_iterator cookie_iterator; | |
| 117 for (cookie_iterator add_cookie = cookie_list.begin(); | |
| 118 add_cookie != cookie_list.end(); ++add_cookie) { | |
| 119 DeleteMetchingCookie(*add_cookie); | |
| 120 cookie_list_.push_back(*add_cookie); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void CannedBrowsingDataCookieHelper::AddChangedCookie( | |
| 125 const GURL& url, | |
| 126 const std::string& cookie_line, | |
| 127 const net::CookieOptions& options) { | |
| 128 typedef net::CookieList::iterator cookie_iterator; | |
| 129 | |
| 130 net::CookieMonster::ParsedCookie pc(cookie_line); | |
| 131 if (options.exclude_httponly() && pc.IsHttpOnly()) { | |
| 132 // Return if a Javascript cookie illegally specified the HTTP only flag. | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 scoped_ptr<net::CookieMonster::CanonicalCookie> cc; | |
| 137 cc.reset(net::CookieMonster::CanonicalCookie::Create(url, pc)); | |
| 138 // Fails creating canonical cookie, if the normalized cookie domain form | |
| 139 // cookie line and the url don't have the same domain+registry, or url host | |
| 140 // isn't cookie domain or one of its subdomains. | |
| 141 | |
| 142 if (cc.get()) { | |
| 143 DeleteMetchingCookie(*cc); | |
| 144 cookie_list_.push_back(*cc); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 void CannedBrowsingDataCookieHelper::Reset() { | |
| 149 cookie_list_.clear(); | |
| 150 } | |
| 151 | |
| 152 bool CannedBrowsingDataCookieHelper::empty() const { | |
| 153 return cookie_list_.empty(); | |
| 154 } | |
| 155 | |
| 156 void CannedBrowsingDataCookieHelper::StartFetching( | |
| 157 const net::CookieMonster::GetCookieListCallback& callback) { | |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 159 if (!callback.is_null()) | |
| 160 callback.Run(cookie_list_); | |
| 161 } | |
| 162 | |
| 163 void CannedBrowsingDataCookieHelper::CancelNotification() {} | |
| 164 | |
| 165 bool CannedBrowsingDataCookieHelper::DeleteMetchingCookie( | |
| 166 const net::CookieMonster::CanonicalCookie& add_cookie) { | |
| 167 typedef net::CookieList::iterator cookie_iterator; | |
| 168 for (cookie_iterator cookie = cookie_list_.begin(); | |
| 169 cookie != cookie_list_.end(); ++cookie) { | |
| 170 if (cookie->Name() == add_cookie.Name() && | |
| 171 cookie->Domain() == add_cookie.Domain()&& | |
| 172 cookie->Path() == add_cookie.Path()) { | |
| 173 cookie_list_.erase(cookie); | |
| 174 return true; | |
| 175 } | |
| 176 } | |
| 177 return false; | |
| 178 } | |
| OLD | NEW |