Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: content/common/cookie_service_impl.cc

Issue 2908443002: Initial implementation of Cookie service.
Patch Set: Partially written test. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/cookie_service_impl.h ('k') | content/common/cookie_service_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "content/common/cookie_service_impl.h"
6
7 #include "net/cookies/canonical_cookie.h"
8 #include "net/cookies/cookie_constants.h"
9 #include "net/cookies/cookie_options.h"
10 #include "url/gurl.h"
11
12 namespace {
13
14 // Class to wrap a CookieDeletionFilterPtr and provide a predicate for
15 // use by DeleteAllCreatedBetweenWithPredicateAsync.
16 class PredicateWrapper {
17 public:
18 explicit PredicateWrapper(content::mojom::CookieDeletionFilterPtr filter)
19 : use_blacklist_(filter->domain_blacklist.has_value()),
20 domain_blacklist_(filter->domain_blacklist.has_value()
21 ? std::set<std::string>(
22 filter->domain_blacklist.value().begin(),
23 filter->domain_blacklist.value().end())
24 : std::set<std::string>()),
25 use_whitelist_(filter->domain_whitelist.has_value()),
26 domain_whitelist_(filter->domain_whitelist.has_value()
27 ? std::set<std::string>(
28 filter->domain_whitelist.value().begin(),
29 filter->domain_whitelist.value().end())
30 : std::set<std::string>()),
31 session_control_(filter->session_control) {}
32
33 bool Predicate(const net::CanonicalCookie& cookie) {
34 // Ignore begin/end times; they're handled by method args.
35 if (use_blacklist_ && domain_blacklist_.count(cookie.Domain()) != 0)
36 return true;
37
38 if (use_whitelist_ && domain_whitelist_.count(cookie.Domain()) == 0)
39 return true;
40
41 if (session_control_ ==
42 content::mojom::CookieDeletionSessionControl::SESSION &&
43 !cookie.IsPersistent()) {
44 return true;
45 }
46
47 if (session_control_ ==
48 content::mojom::CookieDeletionSessionControl::PERSISTENT &&
49 cookie.IsPersistent()) {
50 return true;
51 }
52
53 return false;
54 }
55
56 private:
57 bool use_blacklist_;
58 std::set<std::string> domain_blacklist_;
59
60 bool use_whitelist_;
61 std::set<std::string> domain_whitelist_;
62
63 content::mojom::CookieDeletionSessionControl session_control_;
64
65 DISALLOW_COPY_AND_ASSIGN(PredicateWrapper);
66 };
67
68 content::mojom::CookieChangeCause ChangeCauseTranslation(
69 net::CookieStore::ChangeCause net_cause) {
70 switch (net_cause) {
71 case net::CookieStore::ChangeCause::INSERTED:
72 return content::mojom::CookieChangeCause::INSERTED;
73 case net::CookieStore::ChangeCause::EXPLICIT:
74 case net::CookieStore::ChangeCause::EXPLICIT_DELETE_BETWEEN:
75 case net::CookieStore::ChangeCause::EXPLICIT_DELETE_PREDICATE:
76 case net::CookieStore::ChangeCause::EXPLICIT_DELETE_SINGLE:
77 case net::CookieStore::ChangeCause::EXPLICIT_DELETE_CANONICAL:
78 return content::mojom::CookieChangeCause::EXPLICIT;
79 case net::CookieStore::ChangeCause::UNKNOWN_DELETION:
80 return content::mojom::CookieChangeCause::UNKNOWN_DELETION;
81 case net::CookieStore::ChangeCause::OVERWRITE:
82 return content::mojom::CookieChangeCause::OVERWRITE;
83 case net::CookieStore::ChangeCause::EXPIRED:
84 return content::mojom::CookieChangeCause::EXPIRED;
85 case net::CookieStore::ChangeCause::EVICTED:
86 return content::mojom::CookieChangeCause::EVICTED;
87 case net::CookieStore::ChangeCause::EXPIRED_OVERWRITE:
88 return content::mojom::CookieChangeCause::EXPIRED_OVERWRITE;
89 }
90 }
91
92 } // namespace
93
94 namespace content {
95
96 CookieServiceImpl::NotificationRegistration::~NotificationRegistration() {}
97
98 CookieServiceImpl::CookieServiceImpl(net::CookieStore* cookie_store)
99 : cookie_store_(cookie_store) {}
100
101 CookieServiceImpl::~CookieServiceImpl() {}
102
103 void CookieServiceImpl::AddRequest(
104 content::mojom::CookieServiceRequest request) {
105 bindings_.AddBinding(this, std::move(request));
106 }
107
108 void CookieServiceImpl::GetAllCookies(GetAllCookiesCallback callback) {
109 cookie_store_->GetAllCookiesAsync(std::move(callback));
110 }
111
112 void CookieServiceImpl::GetCookieList(const GURL& url,
113 const net::CookieOptions& cookie_options,
114 GetCookieListCallback callback) {
115 cookie_store_->GetCookieListWithOptionsAsync(url, cookie_options,
116 std::move(callback));
117 }
118
119 void CookieServiceImpl::SetCanonicalCookie(
120 const net::CanonicalCookie& cookie,
121 bool secure_source,
122 bool modify_http_only,
123 SetCanonicalCookieCallback callback) {
124 cookie_store_->SetCanonicalCookieAsync(
125 base::MakeUnique<net::CanonicalCookie>(cookie), secure_source,
126 modify_http_only, std::move(callback));
127 }
128
129 void CookieServiceImpl::DeleteCookies(
130 content::mojom::CookieDeletionFilterPtr filter,
131 DeleteCookiesCallback callback) {
132 base::Time start_time;
133 base::Time end_time;
134
135 if (filter->created_after_time.has_value())
136 end_time = filter->created_after_time.value();
137
138 if (filter->created_before_time.has_value())
139 start_time = filter->created_before_time.value();
140
141 cookie_store_->DeleteAllCreatedBetweenWithPredicateAsync(
142 start_time, end_time,
143 base::Bind(&PredicateWrapper::Predicate,
144 base::MakeUnique<PredicateWrapper>(std::move(filter))),
145 std::move(callback));
146 }
147
148 void CookieServiceImpl::RequestNotification(
149 const GURL& url,
150 const std::string& name,
151 RequestNotificationCallback callback) {
152 mojom::CookieChangeNotificationPtr notification_pointer;
153 mojom::CookieChangeNotificationRequest notification_request =
154 mojo::MakeRequest(&notification_pointer);
155
156 std::unique_ptr<NotificationRegistration> notification_registration;
157 notification_registration->notification_pointer =
158 std::move(notification_pointer);
159
160 notification_registration->subscription = cookie_store_->AddCallbackForCookie(
161 url, name,
162 base::Bind(&CookieServiceImpl::CookieChanged,
163 // base::Unretained is safe as destruction of the
164 // CookieServiceImpl will also destroy the
165 // notifications_registered list (which this object will be
166 // inserted into, below), which will destroy the
167 // CookieChangedSubscription, unregistering the callback.
168 base::Unretained(this),
169 // base::Unretained is safe as destruction of the
170 // NotificationRegistration will also destroy the
171 // CookieChangedSubscription, unregistering the callback.
172 base::Unretained(notification_registration.get())));
173
174 notification_registration->notification_pointer.set_connection_error_handler(
175 base::Bind(&CookieServiceImpl::NotificationPipeBroken,
176 // base::Unretained is safe as destruction of the
177 // CookieServiceImpl will also destroy the
178 // notifications_registered list (which this object will be
179 // inserted into, below), which will destroy the
180 // notification_pointer, rendering this callback moot.
181 base::Unretained(this),
182 // base::Unretained is safe as destruction of the
183 // NotificationRegistration will also destroy the
184 // CookieChangedSubscription, unregistering the callback.
185 base::Unretained(notification_registration.get())));
186
187 notifications_registered_.push_back(std::move(notification_registration));
188 }
189
190 void CookieServiceImpl::CookieChanged(NotificationRegistration* registration,
191 const net::CanonicalCookie& cookie,
192 net::CookieStore::ChangeCause cause) {
193 registration->notification_pointer->OnCookieChanged(
194 cookie, ChangeCauseTranslation(cause));
195 }
196
197 void CookieServiceImpl::NotificationPipeBroken(
198 NotificationRegistration* registration) {
199 for (auto it = notifications_registered_.begin();
200 it != notifications_registered_.end(); ++it) {
201 if (it->get() == registration) {
202 // It isn't expected this will be a common enough operation for
203 // the performance of std::vector::erase() to matter.
204 notifications_registered_.erase(it);
205 return;
206 }
207 }
208 // A broken connection error should never be raised for an unknown pipe.
209 NOTREACHED();
210 }
211
212 void CookieServiceImpl::CloneInterface(
213 mojom::CookieServiceRequest new_interface) {
214 AddRequest(std::move(new_interface));
215 }
216
217 } // namespace content
OLDNEW
« no previous file with comments | « content/common/cookie_service_impl.h ('k') | content/common/cookie_service_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698