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/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
17 #include "net/cookies/canonical_cookie.h" | 17 #include "net/cookies/canonical_cookie.h" |
18 #include "net/cookies/cookie_util.h" | 18 #include "net/cookies/cookie_util.h" |
19 #include "net/cookies/parsed_cookie.h" | 19 #include "net/cookies/parsed_cookie.h" |
20 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 const GURL& url, | 102 const GURL& url, |
103 const std::string& cookie_line, | 103 const std::string& cookie_line, |
104 const net::CookieOptions& options) { | 104 const net::CookieOptions& options) { |
105 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( | 105 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( |
106 url, cookie_line, base::Time::Now(), options)); | 106 url, cookie_line, base::Time::Now(), options)); |
107 if (cookie.get()) | 107 if (cookie.get()) |
108 AddCookie(frame_url, *cookie); | 108 AddCookie(frame_url, *cookie); |
109 } | 109 } |
110 | 110 |
111 void CannedBrowsingDataCookieHelper::Reset() { | 111 void CannedBrowsingDataCookieHelper::Reset() { |
112 base::STLDeleteContainerPairSecondPointers(origin_cookie_set_map_.begin(), | |
113 origin_cookie_set_map_.end()); | |
114 origin_cookie_set_map_.clear(); | 112 origin_cookie_set_map_.clear(); |
115 } | 113 } |
116 | 114 |
117 bool CannedBrowsingDataCookieHelper::empty() const { | 115 bool CannedBrowsingDataCookieHelper::empty() const { |
118 for (const auto& pair : origin_cookie_set_map_) { | 116 for (const auto& pair : origin_cookie_set_map_) { |
119 if (!pair.second->empty()) | 117 if (!pair.second->empty()) |
120 return false; | 118 return false; |
121 } | 119 } |
122 return true; | 120 return true; |
123 } | 121 } |
(...skipping 14 matching lines...) Expand all Loading... | |
138 for (const auto& pair : origin_cookie_set_map_) { | 136 for (const auto& pair : origin_cookie_set_map_) { |
139 cookie_list.insert(cookie_list.begin(), pair.second->begin(), | 137 cookie_list.insert(cookie_list.begin(), pair.second->begin(), |
140 pair.second->end()); | 138 pair.second->end()); |
141 } | 139 } |
142 callback.Run(cookie_list); | 140 callback.Run(cookie_list); |
143 } | 141 } |
144 | 142 |
145 void CannedBrowsingDataCookieHelper::DeleteCookie( | 143 void CannedBrowsingDataCookieHelper::DeleteCookie( |
146 const net::CanonicalCookie& cookie) { | 144 const net::CanonicalCookie& cookie) { |
147 for (const auto& pair : origin_cookie_set_map_) | 145 for (const auto& pair : origin_cookie_set_map_) |
148 DeleteMatchingCookie(cookie, pair.second); | 146 DeleteMatchingCookie(cookie, pair.second.get()); |
149 BrowsingDataCookieHelper::DeleteCookie(cookie); | 147 BrowsingDataCookieHelper::DeleteCookie(cookie); |
150 } | 148 } |
151 | 149 |
152 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( | 150 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( |
153 const net::CanonicalCookie& add_cookie, | 151 const net::CanonicalCookie& add_cookie, |
154 canonical_cookie::CookieHashSet* cookie_set) { | 152 canonical_cookie::CookieHashSet* cookie_set) { |
155 return cookie_set->erase(add_cookie) > 0; | 153 return cookie_set->erase(add_cookie) > 0; |
156 } | 154 } |
157 | 155 |
158 canonical_cookie::CookieHashSet* CannedBrowsingDataCookieHelper::GetCookiesFor( | 156 canonical_cookie::CookieHashSet* CannedBrowsingDataCookieHelper::GetCookiesFor( |
159 const GURL& first_party_origin) { | 157 const GURL& first_party_origin) { |
160 OriginCookieSetMap::iterator it = | 158 auto it = origin_cookie_set_map_.find(first_party_origin); |
161 origin_cookie_set_map_.find(first_party_origin); | 159 if (it != origin_cookie_set_map_.end()) |
162 if (it == origin_cookie_set_map_.end()) { | 160 return it->second.get(); |
163 canonical_cookie::CookieHashSet* cookies = | 161 |
164 new canonical_cookie::CookieHashSet; | 162 origin_cookie_set_map_[first_party_origin] = |
165 origin_cookie_set_map_.insert( | 163 base::MakeUnique<canonical_cookie::CookieHashSet>(); |
166 std::pair<GURL, canonical_cookie::CookieHashSet*>(first_party_origin, | 164 return origin_cookie_set_map_[first_party_origin].get(); |
Nico
2016/09/22 15:56:11
This walks the tree twice. Either use insert() as
Avi (use Gerrit)
2016/09/22 19:17:16
I can't use insert, because Linux doesn't correctl
| |
167 cookies)); | |
168 return cookies; | |
169 } | |
170 return it->second; | |
171 } | 165 } |
172 | 166 |
173 void CannedBrowsingDataCookieHelper::AddCookie( | 167 void CannedBrowsingDataCookieHelper::AddCookie( |
174 const GURL& frame_url, | 168 const GURL& frame_url, |
175 const net::CanonicalCookie& cookie) { | 169 const net::CanonicalCookie& cookie) { |
176 // Storing cookies in separate cookie sets per frame origin makes the | 170 // Storing cookies in separate cookie sets per frame origin makes the |
177 // GetCookieCount method count a cookie multiple times if it is stored in | 171 // GetCookieCount method count a cookie multiple times if it is stored in |
178 // multiple sets. | 172 // multiple sets. |
179 // E.g. let "example.com" be redirected to "www.example.com". A cookie set | 173 // E.g. let "example.com" be redirected to "www.example.com". A cookie set |
180 // with the cookie string "A=B; Domain=.example.com" would be sent to both | 174 // with the cookie string "A=B; Domain=.example.com" would be sent to both |
181 // hosts. This means it would be stored in the separate cookie sets for both | 175 // hosts. This means it would be stored in the separate cookie sets for both |
182 // hosts ("example.com", "www.example.com"). The method GetCookieCount would | 176 // hosts ("example.com", "www.example.com"). The method GetCookieCount would |
183 // count this cookie twice. To prevent this, we us a single global cookie | 177 // count this cookie twice. To prevent this, we us a single global cookie |
184 // set as a work-around to store all added cookies. Per frame URL cookie | 178 // set as a work-around to store all added cookies. Per frame URL cookie |
185 // sets are currently not used. In the future they will be used for | 179 // sets are currently not used. In the future they will be used for |
186 // collecting cookies per origin in redirect chains. | 180 // collecting cookies per origin in redirect chains. |
187 // TODO(markusheintz): A) Change the GetCookiesCount method to prevent | 181 // TODO(markusheintz): A) Change the GetCookiesCount method to prevent |
188 // counting cookies multiple times if they are stored in multiple cookie | 182 // counting cookies multiple times if they are stored in multiple cookie |
189 // sets. B) Replace the GetCookieFor method call below with: | 183 // sets. B) Replace the GetCookieFor method call below with: |
190 // "GetCookiesFor(frame_url.GetOrigin());" | 184 // "GetCookiesFor(frame_url.GetOrigin());" |
191 CR_DEFINE_STATIC_LOCAL(const GURL, origin_cookie_url, (kGlobalCookieSetURL)); | 185 CR_DEFINE_STATIC_LOCAL(const GURL, origin_cookie_url, (kGlobalCookieSetURL)); |
192 canonical_cookie::CookieHashSet* cookie_set = | 186 canonical_cookie::CookieHashSet* cookie_set = |
193 GetCookiesFor(origin_cookie_url); | 187 GetCookiesFor(origin_cookie_url); |
194 DeleteMatchingCookie(cookie, cookie_set); | 188 DeleteMatchingCookie(cookie, cookie_set); |
195 cookie_set->insert(cookie); | 189 cookie_set->insert(cookie); |
196 } | 190 } |
OLD | NEW |