droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 1 | // Copyright 2012 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 | |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 5 | #import "ios/net/cookies/cookie_store_ios.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 6 | |
| 7 | #import <Foundation/Foundation.h> |
avi | 57194367 | 2015-12-22 02:12:49 | [diff] [blame] | 8 | #include <stddef.h> |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 9 | |
| 10 | #include "base/bind.h" |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/files/file_util.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 13 | #include "base/location.h" |
| 14 | #include "base/logging.h" |
| 15 | #include "base/mac/foundation_util.h" |
avi | 57194367 | 2015-12-22 02:12:49 | [diff] [blame] | 16 | #include "base/macros.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
asvitkine | f1899e3 | 2017-01-27 16:30:29 | [diff] [blame] | 18 | #include "base/metrics/histogram_macros.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 19 | #include "base/observer_list.h" |
| 20 | #include "base/sequenced_task_runner.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 21 | #include "base/strings/sys_string_conversions.h" |
| 22 | #include "base/task_runner_util.h" |
| 23 | #include "base/threading/thread_restrictions.h" |
gab | 3d426fa6 | 2016-05-11 18:13:33 | [diff] [blame] | 24 | #include "base/threading/thread_task_runner_handle.h" |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 25 | #include "base/time/time.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 26 | #include "ios/net/cookies/cookie_store_ios_client.h" |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 27 | #import "ios/net/cookies/ns_http_system_cookie_store.h" |
| 28 | #import "ios/net/cookies/system_cookie_util.h" |
Scott Violet | d9ec3a6d | 2018-03-27 03:08:56 | [diff] [blame] | 29 | #include "ios/net/ios_net_buildflags.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 30 | #import "net/base/mac/url_conversions.h" |
| 31 | #include "net/cookies/cookie_util.h" |
ellyjones | 98a0b920 | 2015-09-04 17:22:24 | [diff] [blame] | 32 | #include "net/cookies/parsed_cookie.h" |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 33 | #include "net/log/net_log.h" |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 34 | #include "url/gurl.h" |
| 35 | |
stkhapugin | d335d77 | 2016-11-16 16:48:45 | [diff] [blame] | 36 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 37 | #error "This file requires ARC support." |
| 38 | #endif |
| 39 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 40 | namespace net { |
| 41 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 42 | using CookieDeletionInfo = CookieDeletionInfo; |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 43 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 44 | namespace { |
| 45 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 46 | class CookieStoreIOSCookieChangeSubscription : public CookieChangeSubscription { |
Randy Smith | 4780ccc | 2017-09-29 17:39:41 | [diff] [blame] | 47 | public: |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 48 | using CookieChangeCallbackList = |
| 49 | base::CallbackList<void(const CanonicalCookie& cookie, |
| 50 | CookieChangeCause cause)>; |
| 51 | CookieStoreIOSCookieChangeSubscription( |
| 52 | std::unique_ptr<CookieChangeCallbackList::Subscription> subscription) |
Randy Smith | 4780ccc | 2017-09-29 17:39:41 | [diff] [blame] | 53 | : subscription_(std::move(subscription)) {} |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 54 | ~CookieStoreIOSCookieChangeSubscription() override {} |
Randy Smith | 4780ccc | 2017-09-29 17:39:41 | [diff] [blame] | 55 | |
| 56 | private: |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 57 | std::unique_ptr<CookieChangeCallbackList::Subscription> subscription_; |
Randy Smith | 4780ccc | 2017-09-29 17:39:41 | [diff] [blame] | 58 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 59 | DISALLOW_COPY_AND_ASSIGN(CookieStoreIOSCookieChangeSubscription); |
Randy Smith | 4780ccc | 2017-09-29 17:39:41 | [diff] [blame] | 60 | }; |
| 61 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 62 | #pragma mark NotificationTrampoline |
| 63 | |
| 64 | // NotificationTrampoline dispatches cookie notifications to all the existing |
| 65 | // CookieStoreIOS. |
| 66 | class NotificationTrampoline { |
| 67 | public: |
| 68 | static NotificationTrampoline* GetInstance(); |
| 69 | |
| 70 | void AddObserver(CookieNotificationObserver* obs); |
| 71 | void RemoveObserver(CookieNotificationObserver* obs); |
| 72 | |
| 73 | // Notify the observers. |
| 74 | void NotifyCookiesChanged(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | NotificationTrampoline(); |
| 78 | ~NotificationTrampoline(); |
| 79 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 80 | base::ObserverList<CookieNotificationObserver>::Unchecked observer_list_; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(NotificationTrampoline); |
| 83 | |
| 84 | static NotificationTrampoline* g_notification_trampoline; |
| 85 | }; |
| 86 | |
| 87 | #pragma mark NotificationTrampoline implementation |
| 88 | |
| 89 | NotificationTrampoline* NotificationTrampoline::GetInstance() { |
| 90 | if (!g_notification_trampoline) |
| 91 | g_notification_trampoline = new NotificationTrampoline; |
| 92 | return g_notification_trampoline; |
| 93 | } |
| 94 | |
| 95 | void NotificationTrampoline::AddObserver(CookieNotificationObserver* obs) { |
| 96 | observer_list_.AddObserver(obs); |
| 97 | } |
| 98 | |
| 99 | void NotificationTrampoline::RemoveObserver(CookieNotificationObserver* obs) { |
| 100 | observer_list_.RemoveObserver(obs); |
| 101 | } |
| 102 | |
| 103 | void NotificationTrampoline::NotifyCookiesChanged() { |
ericwilligers | ff4feda | 2016-10-18 00:33:44 | [diff] [blame] | 104 | for (auto& observer : observer_list_) |
| 105 | observer.OnSystemCookiesChanged(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 106 | } |
| 107 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 108 | NotificationTrampoline::NotificationTrampoline() { |
| 109 | } |
| 110 | |
| 111 | NotificationTrampoline::~NotificationTrampoline() { |
| 112 | } |
| 113 | |
| 114 | // Global instance of NotificationTrampoline. |
| 115 | NotificationTrampoline* NotificationTrampoline::g_notification_trampoline = |
| 116 | nullptr; |
| 117 | |
| 118 | #pragma mark Utility functions |
| 119 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 120 | // Builds a NSHTTPCookie from a header cookie line ("Set-Cookie: xxx") and a |
| 121 | // URL. |
| 122 | NSHTTPCookie* GetNSHTTPCookieFromCookieLine(const std::string& cookie_line, |
| 123 | const GURL& url, |
| 124 | base::Time server_time) { |
| 125 | NSURL* nsurl = net::NSURLWithGURL(url); |
| 126 | NSString* ns_cookie_line = base::SysUTF8ToNSString(cookie_line); |
| 127 | if (!ns_cookie_line) { |
| 128 | DLOG(ERROR) << "Cookie line is not UTF8: " << cookie_line; |
| 129 | return nil; |
| 130 | } |
| 131 | NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:@{ |
| 132 | @"Set-Cookie" : ns_cookie_line |
| 133 | } forURL:nsurl]; |
| 134 | if ([cookies count] != 1) |
| 135 | return nil; |
| 136 | |
| 137 | NSHTTPCookie* cookie = [cookies objectAtIndex:0]; |
| 138 | if (![cookie expiresDate] || server_time.is_null()) |
| 139 | return cookie; |
| 140 | |
| 141 | // Perform clock skew correction. |
| 142 | base::TimeDelta clock_skew = base::Time::Now() - server_time; |
| 143 | NSDate* corrected_expire_date = |
| 144 | [[cookie expiresDate] dateByAddingTimeInterval:clock_skew.InSecondsF()]; |
| 145 | NSMutableDictionary* properties = |
| 146 | [NSMutableDictionary dictionaryWithDictionary:[cookie properties]]; |
| 147 | [properties setObject:corrected_expire_date forKey:NSHTTPCookieExpires]; |
| 148 | NSHTTPCookie* corrected_cookie = |
| 149 | [NSHTTPCookie cookieWithProperties:properties]; |
| 150 | DCHECK(corrected_cookie); |
| 151 | return corrected_cookie; |
| 152 | } |
| 153 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 154 | // Returns an empty closure if |callback| is null callback or binds the |
| 155 | // callback to |success|. |
| 156 | base::OnceClosure BindSetCookiesCallback( |
| 157 | CookieStoreIOS::SetCookiesCallback* callback, |
| 158 | bool success) { |
| 159 | base::OnceClosure set_callback; |
| 160 | if (!callback->is_null()) { |
| 161 | set_callback = base::BindOnce(std::move(*callback), success); |
| 162 | } |
| 163 | return set_callback; |
| 164 | } |
| 165 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 166 | // Adds cookies in |cookies| with name |name| to |filtered|. |
| 167 | void OnlyCookiesWithName(const net::CookieList& cookies, |
| 168 | const std::string& name, |
| 169 | net::CookieList* filtered) { |
| 170 | for (const auto& cookie : cookies) { |
| 171 | if (cookie.Name() == name) |
| 172 | filtered->push_back(cookie); |
| 173 | } |
| 174 | } |
| 175 | |
ellyjones | 98a0b920 | 2015-09-04 17:22:24 | [diff] [blame] | 176 | // Returns whether the specified cookie line has an explicit Domain attribute or |
| 177 | // not. |
| 178 | bool HasExplicitDomain(const std::string& cookie_line) { |
| 179 | ParsedCookie cookie(cookie_line); |
| 180 | return cookie.HasDomain(); |
| 181 | } |
| 182 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 183 | } // namespace |
| 184 | |
| 185 | #pragma mark - |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 186 | |
| 187 | #pragma mark CookieStoreIOS::CookieChangeDispatcherIOS |
| 188 | |
| 189 | CookieStoreIOS::CookieChangeDispatcherIOS::CookieChangeDispatcherIOS( |
| 190 | CookieStoreIOS* cookie_store) |
| 191 | : cookie_store_(cookie_store) { |
| 192 | DCHECK(cookie_store); |
| 193 | } |
| 194 | |
| 195 | CookieStoreIOS::CookieChangeDispatcherIOS::~CookieChangeDispatcherIOS() = |
| 196 | default; |
| 197 | |
| 198 | std::unique_ptr<CookieChangeSubscription> |
| 199 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForCookie( |
| 200 | const GURL& gurl, |
| 201 | const std::string& name, |
| 202 | CookieChangeCallback callback) { |
| 203 | return cookie_store_->AddCallbackForCookie(gurl, name, std::move(callback)); |
| 204 | } |
| 205 | |
| 206 | std::unique_ptr<CookieChangeSubscription> |
Victor Costan | 2a189167 | 2018-03-02 06:01:53 | [diff] [blame] | 207 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForUrl( |
| 208 | const GURL& gurl, |
| 209 | CookieChangeCallback callback) { |
| 210 | // Implement when needed by iOS consumers. |
| 211 | NOTIMPLEMENTED(); |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | std::unique_ptr<CookieChangeSubscription> |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 216 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForAllChanges( |
| 217 | CookieChangeCallback callback) { |
| 218 | // Implement when needed by iOS consumers. |
| 219 | NOTIMPLEMENTED(); |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 223 | #pragma mark CookieStoreIOS |
| 224 | |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 225 | CookieStoreIOS::CookieStoreIOS( |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 226 | std::unique_ptr<SystemCookieStore> system_cookie_store, |
| 227 | NetLog* net_log) |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 228 | : CookieStoreIOS(/*persistent_store=*/nullptr, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 229 | std::move(system_cookie_store), |
| 230 | net_log) {} |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 231 | |
Thomas Anderson | 1a03bbe | 2018-03-02 19:05:47 | [diff] [blame] | 232 | CookieStoreIOS::CookieStoreIOS(NSHTTPCookieStorage* ns_cookie_store) |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 233 | : CookieStoreIOS(std::make_unique<NSHTTPSystemCookieStore>(ns_cookie_store), |
| 234 | nullptr /* net_log */) {} |
| 235 | |
| 236 | CookieStoreIOS::CookieStoreIOS(NSHTTPCookieStorage* ns_cookie_store, |
| 237 | NetLog* net_log) |
| 238 | : CookieStoreIOS(std::make_unique<NSHTTPSystemCookieStore>(ns_cookie_store), |
| 239 | net_log) {} |
shreyasv | 1ae3c3a3 | 2015-11-03 20:13:12 | [diff] [blame] | 240 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 241 | CookieStoreIOS::~CookieStoreIOS() { |
| 242 | NotificationTrampoline::GetInstance()->RemoveObserver(this); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 243 | } |
| 244 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 245 | // static |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 246 | void CookieStoreIOS::NotifySystemCookiesChanged() { |
| 247 | NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); |
| 248 | } |
| 249 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 250 | void CookieStoreIOS::SetMetricsEnabled() { |
| 251 | static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; |
| 252 | DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) |
| 253 | << "Only one cookie store may use metrics."; |
| 254 | g_cookie_store_with_metrics = this; |
| 255 | metrics_enabled_ = true; |
| 256 | } |
| 257 | |
| 258 | #pragma mark - |
| 259 | #pragma mark CookieStore methods |
| 260 | |
| 261 | void CookieStoreIOS::SetCookieWithOptionsAsync( |
| 262 | const GURL& url, |
| 263 | const std::string& cookie_line, |
| 264 | const net::CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 265 | SetCookiesCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 266 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 267 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 268 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 269 | // instead. |
| 270 | DCHECK(SystemCookiesAllowed()); |
| 271 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 272 | // The exclude_httponly() option would only be used by a javascript |
| 273 | // engine. |
| 274 | DCHECK(!options.exclude_httponly()); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 275 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 276 | base::Time server_time = |
| 277 | options.has_server_time() ? options.server_time() : base::Time(); |
| 278 | NSHTTPCookie* cookie = |
| 279 | GetNSHTTPCookieFromCookieLine(cookie_line, url, server_time); |
| 280 | DLOG_IF(WARNING, !cookie) << "Could not create cookie for line: " |
| 281 | << cookie_line; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 282 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 283 | // On iOS, [cookie domain] is not empty when the cookie domain is not |
| 284 | // specified: it is inferred from the URL instead. The only case when it |
| 285 | // is empty is when the domain attribute is incorrectly formatted. |
| 286 | std::string domain_string(base::SysNSStringToUTF8([cookie domain])); |
| 287 | std::string dummy; |
| 288 | bool has_explicit_domain = HasExplicitDomain(cookie_line); |
| 289 | bool has_valid_domain = |
| 290 | net::cookie_util::GetCookieDomainWithString(url, domain_string, &dummy); |
| 291 | // A cookie can be set if all of: |
| 292 | // a) The cookie line is well-formed |
| 293 | // b) The Domain attribute, if present, was not malformed |
| 294 | // c) At least one of: |
| 295 | // 1) The cookie had no explicit Domain, so the Domain was inferred |
| 296 | // from the URL, or |
| 297 | // 2) The cookie had an explicit Domain for which the URL is allowed |
| 298 | // to set cookies. |
| 299 | bool success = (cookie != nil) && !domain_string.empty() && |
| 300 | (!has_explicit_domain || has_valid_domain); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 301 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 302 | if (success) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 303 | system_store_->SetCookieAsync(cookie, |
| 304 | BindSetCookiesCallback(&callback, true)); |
| 305 | return; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 306 | } |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 307 | |
| 308 | if (!callback.is_null()) |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 309 | std::move(callback).Run(false); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 310 | } |
| 311 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 312 | void CookieStoreIOS::SetCanonicalCookieAsync( |
| 313 | std::unique_ptr<net::CanonicalCookie> cookie, |
| 314 | bool secure_source, |
| 315 | bool modify_http_only, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 316 | SetCookiesCallback callback) { |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 317 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 318 | |
| 319 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 320 | // instead. |
| 321 | DCHECK(SystemCookiesAllowed()); |
| 322 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 323 | DCHECK(cookie->IsCanonical()); |
| 324 | // The exclude_httponly() option would only be used by a javascript |
| 325 | // engine. |
| 326 | DCHECK(modify_http_only); |
| 327 | |
| 328 | if (cookie->IsSecure() && !secure_source) { |
| 329 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 330 | std::move(callback).Run(false); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
| 334 | NSHTTPCookie* ns_cookie = SystemCookieFromCanonicalCookie(*cookie.get()); |
| 335 | |
| 336 | if (ns_cookie != nil) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 337 | system_store_->SetCookieAsync(ns_cookie, &cookie->CreationDate(), |
| 338 | BindSetCookiesCallback(&callback, true)); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | |
| 342 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 343 | std::move(callback).Run(false); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 344 | } |
| 345 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 346 | void CookieStoreIOS::GetCookieListWithOptionsAsync( |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 347 | const GURL& url, |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 348 | const net::CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 349 | GetCookieListCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 350 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 351 | |
| 352 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 353 | // instead. |
| 354 | DCHECK(SystemCookiesAllowed()); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 355 | |
| 356 | // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass |
| 357 | // options in here as well. https://crbug.com/459154 |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 358 | system_store_->GetCookiesForURLAsync( |
| 359 | url, |
| 360 | base::BindOnce(&CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies, |
| 361 | weak_factory_.GetWeakPtr(), base::Passed(&callback))); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 362 | } |
| 363 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 364 | void CookieStoreIOS::GetAllCookiesAsync(GetCookieListCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 365 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 366 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 367 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 368 | // instead. |
| 369 | DCHECK(SystemCookiesAllowed()); |
| 370 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 371 | // TODO(crbug.com/459154): If/when iOS supports Same-Site cookies, we'll need |
| 372 | // to pass options in here as well. |
| 373 | system_store_->GetAllCookiesAsync( |
| 374 | base::BindOnce(&CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies, |
| 375 | weak_factory_.GetWeakPtr(), base::Passed(&callback))); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 376 | } |
| 377 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 378 | void CookieStoreIOS::DeleteCookieAsync(const GURL& url, |
| 379 | const std::string& cookie_name, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 380 | base::OnceClosure callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 381 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 382 | |
| 383 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 384 | // instead. |
| 385 | DCHECK(SystemCookiesAllowed()); |
| 386 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 387 | __block base::OnceClosure shared_callback = std::move(callback); |
| 388 | base::WeakPtr<SystemCookieStore> weak_system_store = |
| 389 | system_store_->GetWeakPtr(); |
| 390 | system_store_->GetCookiesForURLAsync( |
Sylvain Defresne | a44edf4 | 2018-06-05 15:08:34 | [diff] [blame] | 391 | url, base::BindOnce(^(NSArray<NSHTTPCookie*>* cookies) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 392 | for (NSHTTPCookie* cookie in cookies) { |
| 393 | if ([cookie.name |
| 394 | isEqualToString:base::SysUTF8ToNSString(cookie_name)] && |
| 395 | weak_system_store) { |
| 396 | weak_system_store->DeleteCookieAsync( |
| 397 | cookie, SystemCookieStore::SystemCookieCallback()); |
| 398 | } |
| 399 | } |
| 400 | if (!shared_callback.is_null()) |
| 401 | std::move(shared_callback).Run(); |
| 402 | })); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 403 | } |
| 404 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 405 | void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
| 406 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 407 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 408 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 409 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 410 | // instead. |
| 411 | DCHECK(SystemCookiesAllowed()); |
| 412 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 413 | // This relies on the fact cookies are given unique creation dates. |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 414 | CookieDeletionInfo delete_info(cookie.CreationDate(), cookie.CreationDate()); |
| 415 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 416 | } |
| 417 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 418 | void CookieStoreIOS::DeleteAllCreatedInTimeRangeAsync( |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 419 | const CookieDeletionInfo::TimeRange& creation_range, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 420 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 421 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 422 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 423 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 424 | // instead. |
| 425 | DCHECK(SystemCookiesAllowed()); |
| 426 | |
| 427 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 428 | ResetCookieCountMetrics(); |
| 429 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 430 | CookieDeletionInfo delete_info(creation_range.start(), creation_range.end()); |
| 431 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 432 | } |
| 433 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 434 | void CookieStoreIOS::DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info, |
| 435 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 436 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 437 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 438 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 439 | // instead. |
| 440 | DCHECK(SystemCookiesAllowed()); |
| 441 | |
| 442 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 443 | ResetCookieCountMetrics(); |
| 444 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 445 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 446 | } |
| 447 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 448 | void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 449 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 450 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 451 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 452 | // instead. |
| 453 | DCHECK(SystemCookiesAllowed()); |
| 454 | |
| 455 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 456 | ResetCookieCountMetrics(); |
| 457 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 458 | CookieDeletionInfo delete_info; |
| 459 | delete_info.session_control = |
| 460 | CookieDeletionInfo::SessionControl::SESSION_COOKIES; |
| 461 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 462 | } |
| 463 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 464 | void CookieStoreIOS::FlushStore(base::OnceClosure closure) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 465 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 466 | |
| 467 | if (SystemCookiesAllowed()) { |
| 468 | // If cookies are disabled, the system store is empty, and the cookies are |
| 469 | // stashed on disk. Do not delete the cookies on the disk in this case. |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 470 | system_store_->GetAllCookiesAsync( |
| 471 | base ::BindOnce(&CookieStoreIOS::FlushStoreFromCookies, |
| 472 | weak_factory_.GetWeakPtr(), std::move(closure))); |
| 473 | return; |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 474 | } |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 475 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 476 | // This code path is used by a CookieStoreIOS subclass, which shares this |
| 477 | // implementation. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 478 | cookie_monster_->FlushStore(std::move(closure)); |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 479 | flush_closure_.Cancel(); |
| 480 | } |
| 481 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 482 | #pragma mark - |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 483 | #pragma mark Protected methods |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 484 | |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 485 | CookieStoreIOS::CookieStoreIOS( |
| 486 | net::CookieMonster::PersistentCookieStore* persistent_store, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 487 | std::unique_ptr<SystemCookieStore> system_store, |
| 488 | NetLog* net_log) |
| 489 | : cookie_monster_(new net::CookieMonster(persistent_store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 490 | nullptr /* channel_id_service */, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame^] | 491 | net_log)), |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 492 | system_store_(std::move(system_store)), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 493 | metrics_enabled_(false), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 494 | cookie_cache_(new CookieCache()), |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 495 | change_dispatcher_(this), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 496 | weak_factory_(this) { |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 497 | DCHECK(system_store_); |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 498 | |
| 499 | NotificationTrampoline::GetInstance()->AddObserver(this); |
| 500 | |
| 501 | cookie_monster_->SetPersistSessionCookies(true); |
| 502 | cookie_monster_->SetForceKeepSessionState(); |
| 503 | } |
| 504 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 505 | void CookieStoreIOS::FlushStoreFromCookies(base::OnceClosure closure, |
| 506 | NSArray<NSHTTPCookie*>* cookies) { |
| 507 | WriteToCookieMonster(cookies); |
| 508 | cookie_monster_->FlushStore(std::move(closure)); |
| 509 | flush_closure_.Cancel(); |
| 510 | } |
| 511 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 512 | CookieStoreIOS::SetCookiesCallback CookieStoreIOS::WrapSetCallback( |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 513 | SetCookiesCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 514 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 515 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterSet, |
| 516 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | CookieStoreIOS::DeleteCallback CookieStoreIOS::WrapDeleteCallback( |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 520 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 521 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 522 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterDelete, |
| 523 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 524 | } |
| 525 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 526 | base::OnceClosure CookieStoreIOS::WrapClosure(base::OnceClosure callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 527 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 528 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterClosure, |
| 529 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | #pragma mark - |
| 533 | #pragma mark Private methods |
| 534 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 535 | bool CookieStoreIOS::SystemCookiesAllowed() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 536 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 537 | return system_store_->GetCookieAcceptPolicy() != |
| 538 | NSHTTPCookieAcceptPolicyNever; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 539 | } |
| 540 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 541 | void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 542 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 543 | // Copy the cookies from the global cookie store to |cookie_monster_|. |
| 544 | // Unlike the system store, CookieMonster requires unique creation times. |
| 545 | net::CookieList cookie_list; |
| 546 | NSUInteger cookie_count = [system_cookies count]; |
| 547 | cookie_list.reserve(cookie_count); |
| 548 | for (NSHTTPCookie* cookie in system_cookies) { |
| 549 | cookie_list.push_back(CanonicalCookieFromSystemCookie( |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 550 | cookie, system_store_->GetCookieCreationTime(cookie))); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 551 | } |
| 552 | cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback()); |
| 553 | |
| 554 | // Update metrics. |
| 555 | if (metrics_enabled_) |
| 556 | UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count); |
| 557 | } |
| 558 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 559 | void CookieStoreIOS::DeleteCookiesMatchingInfoAsync( |
| 560 | CookieDeletionInfo delete_info, |
| 561 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 562 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 563 | __block DeleteCallback shared_callback = std::move(callback); |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 564 | __block CookieDeletionInfo shared_delete_info = std::move(delete_info); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 565 | base::WeakPtr<SystemCookieStore> weak_system_store = |
| 566 | system_store_->GetWeakPtr(); |
| 567 | system_store_->GetAllCookiesAsync( |
Sylvain Defresne | a44edf4 | 2018-06-05 15:08:34 | [diff] [blame] | 568 | base::BindOnce(^(NSArray<NSHTTPCookie*>* cookies) { |
Mohammad Refaat | 2e6de02 | 2018-05-29 20:36:44 | [diff] [blame] | 569 | if (!weak_system_store) { |
| 570 | if (!shared_callback.is_null()) |
| 571 | std::move(shared_callback).Run(0); |
| 572 | return; |
| 573 | } |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 574 | int to_delete_count = 0; |
| 575 | for (NSHTTPCookie* cookie in cookies) { |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 576 | base::Time creation_time = |
| 577 | weak_system_store->GetCookieCreationTime(cookie); |
| 578 | CanonicalCookie cc = |
| 579 | CanonicalCookieFromSystemCookie(cookie, creation_time); |
Mohammad Refaat | 2e6de02 | 2018-05-29 20:36:44 | [diff] [blame] | 580 | if (shared_delete_info.Matches(cc)) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 581 | weak_system_store->DeleteCookieAsync( |
| 582 | cookie, SystemCookieStore::SystemCookieCallback()); |
| 583 | to_delete_count++; |
| 584 | } |
| 585 | } |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 586 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 587 | if (!shared_callback.is_null()) |
| 588 | std::move(shared_callback).Run(to_delete_count); |
| 589 | })); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void CookieStoreIOS::OnSystemCookiesChanged() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 593 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 594 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 595 | for (const auto& hook_map_entry : hook_map_) { |
| 596 | std::pair<GURL, std::string> key = hook_map_entry.first; |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 597 | UpdateCacheForCookieFromSystem(key.first, key.second, |
| 598 | /*run_callbacks=*/true); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // Do not schedule a flush if one is already scheduled. |
| 602 | if (!flush_closure_.IsCancelled()) |
| 603 | return; |
| 604 | |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 605 | flush_closure_.Reset(base::Bind(&CookieStoreIOS::FlushStore, |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 606 | weak_factory_.GetWeakPtr(), base::Closure())); |
skyostil | c274098 | 2015-06-05 19:15:31 | [diff] [blame] | 607 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
eugenebut | 8a37ce25 | 2017-01-10 14:13:13 | [diff] [blame] | 608 | FROM_HERE, flush_closure_.callback(), base::TimeDelta::FromSeconds(10)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 609 | } |
| 610 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 611 | CookieChangeDispatcher& CookieStoreIOS::GetChangeDispatcher() { |
| 612 | return change_dispatcher_; |
| 613 | } |
| 614 | |
| 615 | bool CookieStoreIOS::IsEphemeral() { |
| 616 | return cookie_monster_->IsEphemeral(); |
| 617 | } |
| 618 | |
| 619 | std::unique_ptr<CookieChangeSubscription> CookieStoreIOS::AddCallbackForCookie( |
| 620 | const GURL& gurl, |
| 621 | const std::string& name, |
| 622 | CookieChangeCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 623 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 624 | |
| 625 | // Prefill cookie cache with all pertinent cookies for |url| if needed. |
| 626 | std::pair<GURL, std::string> key(gurl, name); |
| 627 | if (hook_map_.count(key) == 0) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 628 | UpdateCacheForCookieFromSystem(gurl, name, /*run_callbacks=*/false); |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 629 | hook_map_[key] = std::make_unique<CookieChangeCallbackList>(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | DCHECK(hook_map_.find(key) != hook_map_.end()); |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 633 | return std::make_unique<CookieStoreIOSCookieChangeSubscription>( |
| 634 | hook_map_[key]->Add(std::move(callback))); |
nharper | 5babb5e6 | 2016-03-09 18:58:07 | [diff] [blame] | 635 | } |
| 636 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 637 | void CookieStoreIOS::UpdateCacheForCookieFromSystem( |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 638 | const GURL& gurl, |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 639 | const std::string& cookie_name, |
| 640 | bool run_callbacks) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 641 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 642 | system_store_->GetCookiesForURLAsync( |
| 643 | gurl, base::BindOnce(&CookieStoreIOS::UpdateCacheForCookies, |
| 644 | weak_factory_.GetWeakPtr(), gurl, cookie_name, |
| 645 | run_callbacks)); |
| 646 | } |
| 647 | |
| 648 | void CookieStoreIOS::UpdateCacheForCookies(const GURL& gurl, |
| 649 | const std::string& cookie_name, |
| 650 | bool run_callbacks, |
| 651 | NSArray<NSHTTPCookie*>* nscookies) { |
| 652 | std::vector<net::CanonicalCookie> cookies; |
| 653 | std::vector<net::CanonicalCookie> out_removed_cookies; |
| 654 | std::vector<net::CanonicalCookie> out_added_cookies; |
| 655 | for (NSHTTPCookie* nscookie in nscookies) { |
| 656 | if (base::SysNSStringToUTF8(nscookie.name) == cookie_name) { |
| 657 | net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie( |
| 658 | nscookie, system_store_->GetCookieCreationTime(nscookie)); |
| 659 | cookies.push_back(canonical_cookie); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | bool changes = cookie_cache_->Update( |
| 664 | gurl, cookie_name, cookies, &out_removed_cookies, &out_added_cookies); |
| 665 | if (run_callbacks && changes) { |
| 666 | RunCallbacksForCookies(gurl, cookie_name, out_removed_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 667 | net::CookieChangeCause::UNKNOWN_DELETION); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 668 | RunCallbacksForCookies(gurl, cookie_name, out_added_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 669 | net::CookieChangeCause::INSERTED); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 670 | } |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void CookieStoreIOS::RunCallbacksForCookies( |
| 674 | const GURL& url, |
| 675 | const std::string& name, |
| 676 | const std::vector<net::CanonicalCookie>& cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 677 | net::CookieChangeCause cause) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 678 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 679 | if (cookies.empty()) |
| 680 | return; |
| 681 | |
| 682 | std::pair<GURL, std::string> key(url, name); |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 683 | CookieChangeCallbackList* callbacks = hook_map_[key].get(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 684 | for (const auto& cookie : cookies) { |
| 685 | DCHECK_EQ(name, cookie.Name()); |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 686 | callbacks->Notify(cookie, cause); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 690 | void CookieStoreIOS::GotCookieListFor(const std::pair<GURL, std::string> key, |
| 691 | const net::CookieList& cookies) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 692 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 693 | |
| 694 | net::CookieList filtered; |
| 695 | OnlyCookiesWithName(cookies, key.second, &filtered); |
| 696 | std::vector<net::CanonicalCookie> removed_cookies; |
| 697 | std::vector<net::CanonicalCookie> added_cookies; |
| 698 | if (cookie_cache_->Update(key.first, key.second, filtered, &removed_cookies, |
| 699 | &added_cookies)) { |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 700 | RunCallbacksForCookies(key.first, key.second, removed_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 701 | net::CookieChangeCause::UNKNOWN_DELETION); |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 702 | RunCallbacksForCookies(key.first, key.second, added_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 703 | net::CookieChangeCause::INSERTED); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 707 | void CookieStoreIOS::UpdateCachesFromCookieMonster() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 708 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 709 | for (const auto& hook_map_entry : hook_map_) { |
| 710 | std::pair<GURL, std::string> key = hook_map_entry.first; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 711 | GetCookieListCallback callback = base::BindOnce( |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 712 | &CookieStoreIOS::GotCookieListFor, weak_factory_.GetWeakPtr(), key); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 713 | cookie_monster_->GetAllCookiesForURLAsync(key.first, std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 717 | void CookieStoreIOS::UpdateCachesAfterSet(SetCookiesCallback callback, |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 718 | bool success) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 719 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 720 | if (success) |
| 721 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 722 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 723 | std::move(callback).Run(success); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 724 | } |
| 725 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 726 | void CookieStoreIOS::UpdateCachesAfterDelete(DeleteCallback callback, |
rdsmith | a5beda16 | 2017-07-08 13:55:42 | [diff] [blame] | 727 | uint32_t num_deleted) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 728 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 729 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 730 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 731 | std::move(callback).Run(num_deleted); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 732 | } |
| 733 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 734 | void CookieStoreIOS::UpdateCachesAfterClosure(base::OnceClosure callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 735 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 736 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 737 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 738 | std::move(callback).Run(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 739 | } |
| 740 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 741 | net::CookieList |
| 742 | CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) { |
| 743 | net::CookieList cookie_list; |
| 744 | cookie_list.reserve([cookies count]); |
| 745 | for (NSHTTPCookie* cookie in cookies) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 746 | base::Time created = system_store_->GetCookieCreationTime(cookie); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 747 | cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); |
| 748 | } |
| 749 | return cookie_list; |
| 750 | } |
| 751 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 752 | void CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies( |
| 753 | CookieStoreIOS::GetCookieListCallback callback, |
| 754 | NSArray<NSHTTPCookie*>* cookies) { |
| 755 | if (!callback.is_null()) { |
| 756 | std::move(callback).Run(CanonicalCookieListFromSystemCookies(cookies)); |
| 757 | } |
| 758 | } |
| 759 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 760 | } // namespace net |