Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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" | |
|
erikwright (departed)
2011/07/19 14:40:33
Include either here or in the .h, not both.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 10 #include "base/time.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "content/browser/browser_thread.h" | |
| 13 #include "googleurl/src/gurl.h" | |
| 14 #include "net/url_request/url_request_context.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | |
| 16 | |
| 17 using base::Time; | |
| 18 | |
| 19 BrowsingDataCookieHelper::BrowsingDataCookieHelper(Profile* profile) | |
| 20 : is_fetching_(false), | |
| 21 profile_(profile), | |
| 22 request_context_getter_(profile->GetRequestContext()) { | |
| 23 } | |
| 24 | |
| 25 void BrowsingDataCookieHelper::StartFetching( | |
| 26 const net::CookieMonster::GetCookieListCallback& callback) { | |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 28 DCHECK(!is_fetching_); | |
| 29 DCHECK(!callback.is_null()); | |
| 30 DCHECK(completion_callback_.is_null()); | |
| 31 is_fetching_ = true; | |
| 32 // cookie_list_.clear(); | |
|
erikwright (departed)
2011/07/19 14:40:33
Remove.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 33 completion_callback_ = callback; | |
| 34 BrowserThread::PostTask( | |
| 35 BrowserThread::IO, FROM_HERE, | |
| 36 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread, this)); | |
| 37 return; | |
|
erikwright (departed)
2011/07/19 14:40:33
Remove the return.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 38 } | |
| 39 | |
| 40 void BrowsingDataCookieHelper::CancelNotification() { | |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 42 completion_callback_.Reset(); | |
| 43 } | |
| 44 | |
| 45 void BrowsingDataCookieHelper::DeleteCookie( | |
| 46 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 48 BrowserThread::PostTask( | |
| 49 BrowserThread::IO, FROM_HERE, | |
| 50 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread, | |
| 51 this, cookie)); | |
| 52 return; | |
|
erikwright (departed)
2011/07/19 14:40:33
Remove.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 53 } | |
| 54 | |
| 55 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() { | |
|
erikwright (departed)
2011/07/19 14:40:33
Move next to constructor.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 56 } | |
| 57 | |
| 58 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() { | |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 60 scoped_refptr<net::CookieMonster> cookie_monster = | |
| 61 request_context_getter_->GetURLRequestContext()-> | |
| 62 cookie_store()->GetCookieMonster(); | |
| 63 if (cookie_monster) { | |
| 64 cookie_monster->GetAllCookiesAsync( | |
| 65 base::Bind(&BrowsingDataCookieHelper::OnFetchComplete, this)); | |
| 66 } else { | |
| 67 OnFetchComplete(net::CookieList()); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void BrowsingDataCookieHelper::OnFetchComplete(const net::CookieList& cookies) { | |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 73 BrowserThread::PostTask( | |
| 74 BrowserThread::UI, FROM_HERE, | |
| 75 base::Bind(&BrowsingDataCookieHelper::NotifyInUIThread, this, cookies)); | |
| 76 return; | |
|
erikwright (departed)
2011/07/19 14:40:33
Remove.
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 77 } | |
| 78 | |
| 79 void BrowsingDataCookieHelper::NotifyInUIThread( | |
| 80 const net::CookieList& cookies) { | |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 82 DCHECK(is_fetching_); | |
| 83 is_fetching_ = false; | |
| 84 // cookie_list_ = cookies; | |
| 85 if (!completion_callback_.is_null()) { | |
| 86 completion_callback_.Run(cookies); | |
| 87 completion_callback_.Reset(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 void BrowsingDataCookieHelper::DeleteCookieOnIOThread( | |
| 92 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 94 scoped_refptr<net::CookieMonster> cookie_monster = | |
| 95 request_context_getter_->GetURLRequestContext()-> | |
| 96 cookie_store()->GetCookieMonster(); | |
| 97 if (cookie_monster) | |
|
erikwright (departed)
2011/07/19 14:40:33
Use braces whenever the conditional block is more
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 98 cookie_monster->DeleteCanonicalCookieAsync( | |
| 99 cookie, | |
|
erikwright (departed)
2011/07/19 14:40:33
If these two arguments fit on one line, you can pu
ycxiao1
2011/07/19 22:12:48
Done.
| |
| 100 net::CookieMonster::DeleteCookieCallback()); | |
| 101 } | |
| 102 | |
| 103 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper( | |
| 104 Profile* profile) | |
| 105 : BrowsingDataCookieHelper(profile), | |
| 106 profile_(profile) { | |
| 107 } | |
| 108 | |
| 109 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { | |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 111 CannedBrowsingDataCookieHelper* clone = | |
| 112 new CannedBrowsingDataCookieHelper(profile_); | |
| 113 | |
| 114 clone->cookie_list_ = cookie_list_; | |
| 115 return clone; | |
| 116 } | |
| 117 | |
| 118 void CannedBrowsingDataCookieHelper::AddReadCookie( | |
| 119 const GURL& url, | |
| 120 const net::CookieList& cookie_list) { | |
| 121 typedef net::CookieList::const_iterator cookie_iterator; | |
| 122 for (cookie_iterator add_cookie = cookie_list.begin(); | |
| 123 add_cookie != cookie_list.end(); ++add_cookie) { | |
| 124 // May have multiple set cookies that result in the same system time. | |
| 125 Time creation_time = Time::Now(); | |
| 126 std::string mac_key; | |
| 127 std::string mac_algorithm; | |
| 128 scoped_ptr<net::CookieMonster::CanonicalCookie> cc; | |
| 129 cc.reset(net::CookieMonster::CanonicalCookie::Create( | |
| 130 url, | |
| 131 add_cookie->Name(), | |
| 132 add_cookie->Value(), | |
| 133 add_cookie->Domain(), | |
| 134 add_cookie->Path(), | |
| 135 mac_key, mac_algorithm, | |
| 136 creation_time, | |
| 137 add_cookie->ExpiryDate(), | |
| 138 add_cookie->IsSecure(), | |
| 139 add_cookie->IsHttpOnly())); | |
| 140 | |
| 141 if (cc.get() && !HasCookie(*cc)) { | |
| 142 cookie_list_.push_back(*cc); | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 void CannedBrowsingDataCookieHelper::AddChangeCookie( | |
| 148 const GURL& url, | |
| 149 const std::string& cookie_line, | |
| 150 const net::CookieOptions& options) { | |
|
erikwright (departed)
2011/07/19 14:40:33
options is not used. Remove it from the signature
ycxiao1
2011/07/19 22:12:48
Options is actually needed here.
| |
| 151 typedef net::CookieList::const_iterator cookie_iterator; | |
| 152 net::CookieMonster::ParsedCookie pc(cookie_line); | |
| 153 scoped_ptr<net::CookieMonster::CanonicalCookie> cc; | |
| 154 cc.reset(new net::CookieMonster::CanonicalCookie(url, pc)); | |
| 155 | |
| 156 if (cc.get()) { | |
| 157 for (cookie_iterator cookie = cookie_list_.begin(); | |
| 158 cookie != cookie_list_.end(); ++cookie) { | |
| 159 if (cookie->Name() == cc->Name() && | |
| 160 cookie->Domain() == cc->Domain()&& | |
| 161 cookie->Path() == cc->Path()) { | |
| 162 return; | |
| 163 } | |
| 164 } | |
| 165 cookie_list_.push_back(*cc); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void CannedBrowsingDataCookieHelper::Reset() { | |
| 170 cookie_list_.clear(); | |
| 171 } | |
| 172 | |
| 173 bool CannedBrowsingDataCookieHelper::empty() const { | |
| 174 return cookie_list_.empty(); | |
| 175 } | |
| 176 | |
| 177 const net::CookieList& CannedBrowsingDataCookieHelper::cookie_list() { | |
| 178 return cookie_list_; | |
| 179 } | |
| 180 | |
| 181 void CannedBrowsingDataCookieHelper::StartFetching( | |
| 182 const net::CookieMonster::GetCookieListCallback& callback) { | |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 184 if (!callback.is_null()) | |
| 185 callback.Run(cookie_list_); | |
| 186 } | |
| 187 | |
| 188 void CannedBrowsingDataCookieHelper::CancelNotification() {} | |
| 189 | |
| 190 bool CannedBrowsingDataCookieHelper::HasCookie( | |
| 191 const net::CookieMonster::CanonicalCookie& add_cookie) { | |
| 192 typedef net::CookieList::const_iterator cookie_iterator; | |
| 193 for (cookie_iterator cookie = cookie_list_.begin(); | |
| 194 cookie != cookie_list_.end(); ++cookie) { | |
| 195 if (cookie->Name() == add_cookie.Name() && | |
| 196 cookie->Domain() == add_cookie.Domain()&& | |
| 197 cookie->Path() == add_cookie.Path()) { | |
| 198 return true; | |
| 199 } | |
| 200 } | |
| 201 return false; | |
| 202 } | |
| 203 | |
| 204 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() {} | |
| OLD | NEW |