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/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data_cookie_helper.h" |
6 | 6 |
| 7 #include "utility" |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/stl_util.h" |
10 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
11 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
12 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/registry_controlled_domain.h" |
13 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
14 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
15 | 19 |
16 using content::BrowserThread; | 20 using content::BrowserThread; |
17 | 21 |
18 BrowsingDataCookieHelper::BrowsingDataCookieHelper(Profile* profile) | 22 BrowsingDataCookieHelper::BrowsingDataCookieHelper(Profile* profile) |
19 : is_fetching_(false), | 23 : is_fetching_(false), |
20 profile_(profile), | 24 profile_(profile), |
21 request_context_getter_(profile->GetRequestContext()) { | 25 request_context_getter_(profile->GetRequestContext()) { |
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 cookie_monster->DeleteCanonicalCookieAsync( | 90 cookie_monster->DeleteCanonicalCookieAsync( |
87 cookie, net::CookieMonster::DeleteCookieCallback()); | 91 cookie, net::CookieMonster::DeleteCookieCallback()); |
88 } | 92 } |
89 } | 93 } |
90 | 94 |
91 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper( | 95 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper( |
92 Profile* profile) | 96 Profile* profile) |
93 : BrowsingDataCookieHelper(profile) { | 97 : BrowsingDataCookieHelper(profile) { |
94 } | 98 } |
95 | 99 |
96 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() {} | 100 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() { |
| 101 Reset(); |
| 102 } |
97 | 103 |
98 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { | 104 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
100 CannedBrowsingDataCookieHelper* clone = | 106 CannedBrowsingDataCookieHelper* clone = |
101 new CannedBrowsingDataCookieHelper(profile()); | 107 new CannedBrowsingDataCookieHelper(profile()); |
102 | 108 |
103 clone->cookie_list_ = cookie_list_; | 109 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); |
| 110 it != origin_cookie_list_map_.end(); |
| 111 ++it) { |
| 112 net::CookieList* cookies = clone->GetCookiesFor(it->first); |
| 113 cookies->insert(cookies->begin(), it->second->begin(), it->second->end()); |
| 114 } |
104 return clone; | 115 return clone; |
105 } | 116 } |
106 | 117 |
107 void CannedBrowsingDataCookieHelper::AddReadCookies( | 118 void CannedBrowsingDataCookieHelper::AddReadCookies( |
| 119 const GURL& frame_url, |
108 const GURL& url, | 120 const GURL& url, |
109 const net::CookieList& cookie_list) { | 121 const net::CookieList& cookie_list) { |
110 typedef net::CookieList::const_iterator cookie_iterator; | 122 typedef net::CookieList::const_iterator cookie_iterator; |
111 for (cookie_iterator add_cookie = cookie_list.begin(); | 123 for (cookie_iterator add_cookie = cookie_list.begin(); |
112 add_cookie != cookie_list.end(); ++add_cookie) { | 124 add_cookie != cookie_list.end(); ++add_cookie) { |
113 DeleteMetchingCookie(*add_cookie); | 125 AddCookie(frame_url, *add_cookie); |
114 cookie_list_.push_back(*add_cookie); | |
115 } | 126 } |
116 } | 127 } |
117 | 128 |
118 void CannedBrowsingDataCookieHelper::AddChangedCookie( | 129 void CannedBrowsingDataCookieHelper::AddChangedCookie( |
| 130 const GURL& frame_url, |
119 const GURL& url, | 131 const GURL& url, |
120 const std::string& cookie_line, | 132 const std::string& cookie_line, |
121 const net::CookieOptions& options) { | 133 const net::CookieOptions& options) { |
122 typedef net::CookieList::iterator cookie_iterator; | 134 net::CookieMonster::ParsedCookie parsed_cookie(cookie_line); |
123 | 135 if (options.exclude_httponly() && parsed_cookie.IsHttpOnly()) { |
124 net::CookieMonster::ParsedCookie pc(cookie_line); | |
125 if (options.exclude_httponly() && pc.IsHttpOnly()) { | |
126 // Return if a Javascript cookie illegally specified the HTTP only flag. | 136 // Return if a Javascript cookie illegally specified the HTTP only flag. |
127 return; | 137 return; |
128 } | 138 } |
129 | 139 |
130 scoped_ptr<net::CookieMonster::CanonicalCookie> cc; | |
131 // This fails to create a canonical cookie, if the normalized cookie domain | 140 // This fails to create a canonical cookie, if the normalized cookie domain |
132 // form cookie line and the url don't have the same domain+registry, or url | 141 // form cookie line and the url don't have the same domain+registry, or url |
133 // host isn't cookie domain or one of its subdomains. | 142 // host isn't cookie domain or one of its subdomains. |
134 cc.reset(net::CookieMonster::CanonicalCookie::Create(url, pc)); | 143 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( |
135 | 144 net::CookieMonster::CanonicalCookie::Create(url, parsed_cookie)); |
136 if (cc.get()) { | 145 if (cookie.get()) |
137 DeleteMetchingCookie(*cc); | 146 AddCookie(frame_url, *cookie); |
138 cookie_list_.push_back(*cc); | |
139 } | |
140 } | 147 } |
141 | 148 |
142 void CannedBrowsingDataCookieHelper::Reset() { | 149 void CannedBrowsingDataCookieHelper::Reset() { |
143 cookie_list_.clear(); | 150 STLDeleteContainerPairSecondPointers(origin_cookie_list_map_.begin(), |
| 151 origin_cookie_list_map_.end()); |
| 152 origin_cookie_list_map_.clear(); |
144 } | 153 } |
145 | 154 |
146 bool CannedBrowsingDataCookieHelper::empty() const { | 155 bool CannedBrowsingDataCookieHelper::empty() const { |
147 return cookie_list_.empty(); | 156 for (OriginCookieListMap::const_iterator it = |
| 157 origin_cookie_list_map_.begin(); |
| 158 it != origin_cookie_list_map_.end(); |
| 159 ++it) { |
| 160 if (!it->second->empty()) |
| 161 return false; |
| 162 } |
| 163 return true; |
148 } | 164 } |
149 | 165 |
| 166 |
150 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const { | 167 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const { |
151 return cookie_list_.size(); | 168 size_t count = 0; |
| 169 for (OriginCookieListMap::const_iterator it = origin_cookie_list_map_.begin(); |
| 170 it != origin_cookie_list_map_.end(); |
| 171 ++it) { |
| 172 count += it->second->size(); |
| 173 } |
| 174 return count; |
152 } | 175 } |
153 | 176 |
| 177 |
154 void CannedBrowsingDataCookieHelper::StartFetching( | 178 void CannedBrowsingDataCookieHelper::StartFetching( |
155 const net::CookieMonster::GetCookieListCallback& callback) { | 179 const net::CookieMonster::GetCookieListCallback& callback) { |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
157 callback.Run(cookie_list_); | 181 net::CookieList cookie_list; |
| 182 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); |
| 183 it != origin_cookie_list_map_.end(); |
| 184 ++it) { |
| 185 cookie_list.insert(cookie_list.begin(), |
| 186 it->second->begin(), |
| 187 it->second->end()); |
| 188 } |
| 189 callback.Run(cookie_list); |
158 } | 190 } |
159 | 191 |
160 bool CannedBrowsingDataCookieHelper::DeleteMetchingCookie( | 192 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( |
161 const net::CookieMonster::CanonicalCookie& add_cookie) { | 193 const net::CookieMonster::CanonicalCookie& add_cookie, |
| 194 net::CookieList* cookie_list) { |
162 typedef net::CookieList::iterator cookie_iterator; | 195 typedef net::CookieList::iterator cookie_iterator; |
163 for (cookie_iterator cookie = cookie_list_.begin(); | 196 for (cookie_iterator cookie = cookie_list->begin(); |
164 cookie != cookie_list_.end(); ++cookie) { | 197 cookie != cookie_list->end(); ++cookie) { |
165 if (cookie->Name() == add_cookie.Name() && | 198 if (cookie->Name() == add_cookie.Name() && |
166 cookie->Domain() == add_cookie.Domain()&& | 199 cookie->Domain() == add_cookie.Domain()&& |
167 cookie->Path() == add_cookie.Path()) { | 200 cookie->Path() == add_cookie.Path()) { |
168 cookie_list_.erase(cookie); | 201 cookie_list->erase(cookie); |
169 return true; | 202 return true; |
170 } | 203 } |
171 } | 204 } |
172 return false; | 205 return false; |
173 } | 206 } |
| 207 |
| 208 net::CookieList* CannedBrowsingDataCookieHelper::GetCookiesFor( |
| 209 const GURL& first_party_origin) { |
| 210 OriginCookieListMap::iterator it = |
| 211 origin_cookie_list_map_.find(first_party_origin); |
| 212 if (it == origin_cookie_list_map_.end()) { |
| 213 net::CookieList* cookies = new net::CookieList(); |
| 214 origin_cookie_list_map_.insert( |
| 215 std::pair<GURL, net::CookieList*>(first_party_origin, cookies)); |
| 216 return cookies; |
| 217 } |
| 218 return it->second; |
| 219 } |
| 220 |
| 221 void CannedBrowsingDataCookieHelper::AddCookie( |
| 222 const GURL& frame_url, |
| 223 const net::CookieMonster::CanonicalCookie& cookie) { |
| 224 net::CookieList* cookie_list = |
| 225 GetCookiesFor(frame_url.GetOrigin()); |
| 226 DeleteMatchingCookie(cookie, cookie_list); |
| 227 cookie_list->push_back(cookie); |
| 228 } |
OLD | NEW |