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 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 46 | #pragma mark NotificationTrampoline |
| 47 | |
| 48 | // NotificationTrampoline dispatches cookie notifications to all the existing |
| 49 | // CookieStoreIOS. |
| 50 | class NotificationTrampoline { |
| 51 | public: |
| 52 | static NotificationTrampoline* GetInstance(); |
| 53 | |
| 54 | void AddObserver(CookieNotificationObserver* obs); |
| 55 | void RemoveObserver(CookieNotificationObserver* obs); |
| 56 | |
| 57 | // Notify the observers. |
| 58 | void NotifyCookiesChanged(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | NotificationTrampoline(); |
| 62 | ~NotificationTrampoline(); |
| 63 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 64 | base::ObserverList<CookieNotificationObserver>::Unchecked observer_list_; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(NotificationTrampoline); |
| 67 | |
| 68 | static NotificationTrampoline* g_notification_trampoline; |
| 69 | }; |
| 70 | |
| 71 | #pragma mark NotificationTrampoline implementation |
| 72 | |
| 73 | NotificationTrampoline* NotificationTrampoline::GetInstance() { |
| 74 | if (!g_notification_trampoline) |
| 75 | g_notification_trampoline = new NotificationTrampoline; |
| 76 | return g_notification_trampoline; |
| 77 | } |
| 78 | |
| 79 | void NotificationTrampoline::AddObserver(CookieNotificationObserver* obs) { |
| 80 | observer_list_.AddObserver(obs); |
| 81 | } |
| 82 | |
| 83 | void NotificationTrampoline::RemoveObserver(CookieNotificationObserver* obs) { |
| 84 | observer_list_.RemoveObserver(obs); |
| 85 | } |
| 86 | |
| 87 | void NotificationTrampoline::NotifyCookiesChanged() { |
ericwilligers | ff4feda | 2016-10-18 00:33:44 | [diff] [blame] | 88 | for (auto& observer : observer_list_) |
| 89 | observer.OnSystemCookiesChanged(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 90 | } |
| 91 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 92 | NotificationTrampoline::NotificationTrampoline() { |
| 93 | } |
| 94 | |
| 95 | NotificationTrampoline::~NotificationTrampoline() { |
| 96 | } |
| 97 | |
| 98 | // Global instance of NotificationTrampoline. |
| 99 | NotificationTrampoline* NotificationTrampoline::g_notification_trampoline = |
| 100 | nullptr; |
| 101 | |
| 102 | #pragma mark Utility functions |
| 103 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 104 | // Returns an empty closure if |callback| is null callback or binds the |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 105 | // callback to |status|. |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 106 | base::OnceClosure BindSetCookiesCallback( |
| 107 | CookieStoreIOS::SetCookiesCallback* callback, |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 108 | net::CanonicalCookie::CookieInclusionStatus status) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 109 | base::OnceClosure set_callback; |
| 110 | if (!callback->is_null()) { |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 111 | set_callback = base::BindOnce(std::move(*callback), status); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 112 | } |
| 113 | return set_callback; |
| 114 | } |
| 115 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 116 | // Adds cookies in |cookies| with name |name| to |filtered|. |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 117 | void OnlyCookiesWithName(const net::CookieStatusList& cookies, |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 118 | const std::string& name, |
| 119 | net::CookieList* filtered) { |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 120 | for (const auto& cookie_with_status : cookies) { |
| 121 | if (cookie_with_status.cookie.Name() == name) |
| 122 | filtered->push_back(cookie_with_status.cookie); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 126 | } // namespace |
| 127 | |
| 128 | #pragma mark - |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 129 | |
Sergey Kuznetsov | 0edf161 | 2018-10-12 14:29:05 | [diff] [blame] | 130 | #pragma mark CookieStoreIOS::Subscription |
| 131 | |
| 132 | CookieStoreIOS::Subscription::Subscription( |
| 133 | std::unique_ptr<CookieChangeCallbackList::Subscription> subscription) |
| 134 | : subscription_(std::move(subscription)) { |
| 135 | DCHECK(subscription_); |
| 136 | } |
| 137 | |
| 138 | CookieStoreIOS::Subscription::~Subscription() { |
| 139 | if (!subscription_) { |
| 140 | // |CookieStoreIOS| already destroyed - bail out. |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // |CookieStoreIOS| is alive - unsubscribe. |
| 145 | RemoveFromList(); |
| 146 | } |
| 147 | |
| 148 | void CookieStoreIOS::Subscription::ResetSubscription() { |
| 149 | subscription_.reset(); |
| 150 | } |
| 151 | |
| 152 | #pragma mark - |
| 153 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 154 | #pragma mark CookieStoreIOS::CookieChangeDispatcherIOS |
| 155 | |
| 156 | CookieStoreIOS::CookieChangeDispatcherIOS::CookieChangeDispatcherIOS( |
| 157 | CookieStoreIOS* cookie_store) |
| 158 | : cookie_store_(cookie_store) { |
| 159 | DCHECK(cookie_store); |
| 160 | } |
| 161 | |
| 162 | CookieStoreIOS::CookieChangeDispatcherIOS::~CookieChangeDispatcherIOS() = |
| 163 | default; |
| 164 | |
| 165 | std::unique_ptr<CookieChangeSubscription> |
| 166 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForCookie( |
| 167 | const GURL& gurl, |
| 168 | const std::string& name, |
| 169 | CookieChangeCallback callback) { |
| 170 | return cookie_store_->AddCallbackForCookie(gurl, name, std::move(callback)); |
| 171 | } |
| 172 | |
| 173 | std::unique_ptr<CookieChangeSubscription> |
Victor Costan | 2a189167 | 2018-03-02 06:01:53 | [diff] [blame] | 174 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForUrl( |
| 175 | const GURL& gurl, |
| 176 | CookieChangeCallback callback) { |
| 177 | // Implement when needed by iOS consumers. |
| 178 | NOTIMPLEMENTED(); |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | std::unique_ptr<CookieChangeSubscription> |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 183 | CookieStoreIOS::CookieChangeDispatcherIOS::AddCallbackForAllChanges( |
| 184 | CookieChangeCallback callback) { |
| 185 | // Implement when needed by iOS consumers. |
| 186 | NOTIMPLEMENTED(); |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 190 | #pragma mark CookieStoreIOS |
| 191 | |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 192 | CookieStoreIOS::CookieStoreIOS( |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame] | 193 | std::unique_ptr<SystemCookieStore> system_cookie_store, |
| 194 | NetLog* net_log) |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 195 | : CookieStoreIOS(/*persistent_store=*/nullptr, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame] | 196 | std::move(system_cookie_store), |
| 197 | net_log) {} |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 198 | |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame] | 199 | CookieStoreIOS::CookieStoreIOS(NSHTTPCookieStorage* ns_cookie_store, |
| 200 | NetLog* net_log) |
| 201 | : CookieStoreIOS(std::make_unique<NSHTTPSystemCookieStore>(ns_cookie_store), |
| 202 | net_log) {} |
shreyasv | 1ae3c3a3 | 2015-11-03 20:13:12 | [diff] [blame] | 203 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 204 | CookieStoreIOS::~CookieStoreIOS() { |
| 205 | NotificationTrampoline::GetInstance()->RemoveObserver(this); |
Sergey Kuznetsov | 0edf161 | 2018-10-12 14:29:05 | [diff] [blame] | 206 | |
| 207 | // Reset subscriptions. |
| 208 | for (auto* node = all_subscriptions_.head(); node != all_subscriptions_.end(); |
| 209 | node = node->next()) { |
| 210 | node->value()->ResetSubscription(); |
| 211 | } |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 212 | } |
| 213 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 214 | // static |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 215 | void CookieStoreIOS::NotifySystemCookiesChanged() { |
| 216 | NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); |
| 217 | } |
| 218 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 219 | void CookieStoreIOS::SetMetricsEnabled() { |
| 220 | static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; |
| 221 | DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) |
| 222 | << "Only one cookie store may use metrics."; |
| 223 | g_cookie_store_with_metrics = this; |
| 224 | metrics_enabled_ = true; |
| 225 | } |
| 226 | |
| 227 | #pragma mark - |
| 228 | #pragma mark CookieStore methods |
| 229 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 230 | void CookieStoreIOS::SetCanonicalCookieAsync( |
| 231 | std::unique_ptr<net::CanonicalCookie> cookie, |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 232 | std::string source_scheme, |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 233 | const net::CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 234 | SetCookiesCallback callback) { |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 235 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 236 | |
| 237 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 238 | // instead. |
| 239 | DCHECK(SystemCookiesAllowed()); |
| 240 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 241 | DCHECK(cookie->IsCanonical()); |
| 242 | // The exclude_httponly() option would only be used by a javascript |
| 243 | // engine. |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 244 | DCHECK(!options.exclude_httponly()); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 245 | |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 246 | bool secure_source = |
| 247 | GURL::SchemeIsCryptographic(base::ToLowerASCII(source_scheme)); |
| 248 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 249 | if (cookie->IsSecure() && !secure_source) { |
| 250 | if (!callback.is_null()) |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 251 | std::move(callback).Run(net::CanonicalCookie::CookieInclusionStatus( |
| 252 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_SECURE_ONLY)); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | NSHTTPCookie* ns_cookie = SystemCookieFromCanonicalCookie(*cookie.get()); |
| 257 | |
| 258 | if (ns_cookie != nil) { |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 259 | system_store_->SetCookieAsync( |
| 260 | ns_cookie, &cookie->CreationDate(), |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 261 | BindSetCookiesCallback(&callback, |
| 262 | net::CanonicalCookie::CookieInclusionStatus())); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (!callback.is_null()) |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 267 | std::move(callback).Run(net::CanonicalCookie::CookieInclusionStatus( |
| 268 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE)); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 269 | } |
| 270 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 271 | void CookieStoreIOS::GetCookieListWithOptionsAsync( |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 272 | const GURL& url, |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 273 | const net::CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 274 | GetCookieListCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 275 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 276 | |
| 277 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 278 | // instead. |
| 279 | DCHECK(SystemCookiesAllowed()); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 280 | |
| 281 | // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass |
| 282 | // options in here as well. https://crbug.com/459154 |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 283 | system_store_->GetCookiesForURLAsync( |
| 284 | url, |
| 285 | base::BindOnce(&CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies, |
| 286 | weak_factory_.GetWeakPtr(), base::Passed(&callback))); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 287 | } |
| 288 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 289 | void CookieStoreIOS::GetAllCookiesAsync(GetAllCookiesCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 290 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 291 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 292 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 293 | // instead. |
| 294 | DCHECK(SystemCookiesAllowed()); |
| 295 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 296 | // TODO(crbug.com/459154): If/when iOS supports Same-Site cookies, we'll need |
| 297 | // to pass options in here as well. |
| 298 | system_store_->GetAllCookiesAsync( |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 299 | base::BindOnce(&CookieStoreIOS::RunGetAllCookiesCallbackOnSystemCookies, |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 300 | weak_factory_.GetWeakPtr(), base::Passed(&callback))); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 301 | } |
| 302 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 303 | void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
| 304 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 305 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 306 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 307 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 308 | // instead. |
| 309 | DCHECK(SystemCookiesAllowed()); |
| 310 | |
Maks Orlovich | f58b8da | 2019-02-11 17:12:45 | [diff] [blame] | 311 | DeleteCookiesMatchingPredicateAsync(base::BindRepeating( |
| 312 | [](const net::CanonicalCookie& target, |
| 313 | const net::CanonicalCookie& cc) { |
| 314 | return cc.IsEquivalent(target) && |
| 315 | cc.Value() == target.Value(); |
| 316 | }, |
| 317 | cookie), |
| 318 | std::move(callback)); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 319 | } |
| 320 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 321 | void CookieStoreIOS::DeleteAllCreatedInTimeRangeAsync( |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 322 | const CookieDeletionInfo::TimeRange& creation_range, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 323 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 324 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 325 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 326 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 327 | // instead. |
| 328 | DCHECK(SystemCookiesAllowed()); |
| 329 | |
| 330 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 331 | ResetCookieCountMetrics(); |
| 332 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 333 | CookieDeletionInfo delete_info(creation_range.start(), creation_range.end()); |
| 334 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 335 | } |
| 336 | |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 337 | void CookieStoreIOS::DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info, |
| 338 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 339 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 340 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 341 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 342 | // instead. |
| 343 | DCHECK(SystemCookiesAllowed()); |
| 344 | |
| 345 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 346 | ResetCookieCountMetrics(); |
| 347 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 348 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 349 | } |
| 350 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 351 | void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 352 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 353 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 354 | // If cookies are not allowed, a CookieStoreIOS subclass should be used |
| 355 | // instead. |
| 356 | DCHECK(SystemCookiesAllowed()); |
| 357 | |
| 358 | if (metrics_enabled()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 359 | ResetCookieCountMetrics(); |
| 360 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 361 | CookieDeletionInfo delete_info; |
| 362 | delete_info.session_control = |
| 363 | CookieDeletionInfo::SessionControl::SESSION_COOKIES; |
| 364 | DeleteCookiesMatchingInfoAsync(std::move(delete_info), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 365 | } |
| 366 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 367 | void CookieStoreIOS::FlushStore(base::OnceClosure closure) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 368 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 369 | |
| 370 | if (SystemCookiesAllowed()) { |
| 371 | // If cookies are disabled, the system store is empty, and the cookies are |
| 372 | // 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] | 373 | system_store_->GetAllCookiesAsync( |
| 374 | base ::BindOnce(&CookieStoreIOS::FlushStoreFromCookies, |
| 375 | weak_factory_.GetWeakPtr(), std::move(closure))); |
| 376 | return; |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 377 | } |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 378 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 379 | // This code path is used by a CookieStoreIOS subclass, which shares this |
| 380 | // implementation. |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 381 | cookie_monster_->FlushStore(std::move(closure)); |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 382 | flush_closure_.Cancel(); |
| 383 | } |
| 384 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 385 | #pragma mark - |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 386 | #pragma mark Protected methods |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 387 | |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 388 | CookieStoreIOS::CookieStoreIOS( |
| 389 | net::CookieMonster::PersistentCookieStore* persistent_store, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame] | 390 | std::unique_ptr<SystemCookieStore> system_store, |
| 391 | NetLog* net_log) |
| 392 | : cookie_monster_(new net::CookieMonster(persistent_store, |
Helen Li | 4e4cb0d | 2018-08-21 14:50:47 | [diff] [blame] | 393 | net_log)), |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 394 | system_store_(std::move(system_store)), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 395 | metrics_enabled_(false), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 396 | cookie_cache_(new CookieCache()), |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 397 | change_dispatcher_(this), |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 398 | weak_factory_(this) { |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 399 | DCHECK(system_store_); |
eugenebut | 2cbccb7 | 2016-12-30 16:27:52 | [diff] [blame] | 400 | |
| 401 | NotificationTrampoline::GetInstance()->AddObserver(this); |
| 402 | |
| 403 | cookie_monster_->SetPersistSessionCookies(true); |
| 404 | cookie_monster_->SetForceKeepSessionState(); |
| 405 | } |
| 406 | |
Victor Costan | fb17d1a | 2018-02-23 03:42:26 | [diff] [blame] | 407 | void CookieStoreIOS::FlushStoreFromCookies(base::OnceClosure closure, |
| 408 | NSArray<NSHTTPCookie*>* cookies) { |
| 409 | WriteToCookieMonster(cookies); |
| 410 | cookie_monster_->FlushStore(std::move(closure)); |
| 411 | flush_closure_.Cancel(); |
| 412 | } |
| 413 | |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 414 | CookieStoreIOS::SetCookiesCallback CookieStoreIOS::WrapSetCallback( |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 415 | SetCookiesCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 416 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 417 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterSet, |
| 418 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | CookieStoreIOS::DeleteCallback CookieStoreIOS::WrapDeleteCallback( |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 422 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 423 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 424 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterDelete, |
| 425 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 426 | } |
| 427 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 428 | base::OnceClosure CookieStoreIOS::WrapClosure(base::OnceClosure callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 429 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 430 | return base::BindOnce(&CookieStoreIOS::UpdateCachesAfterClosure, |
| 431 | weak_factory_.GetWeakPtr(), std::move(callback)); |
maksim.sisov | fdd9f09 | 2017-01-30 10:41:42 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | #pragma mark - |
| 435 | #pragma mark Private methods |
| 436 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 437 | bool CookieStoreIOS::SystemCookiesAllowed() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 438 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 6a427649 | 2017-09-08 00:18:52 | [diff] [blame] | 439 | return system_store_->GetCookieAcceptPolicy() != |
| 440 | NSHTTPCookieAcceptPolicyNever; |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 441 | } |
| 442 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 443 | void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 444 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 445 | // Copy the cookies from the global cookie store to |cookie_monster_|. |
| 446 | // Unlike the system store, CookieMonster requires unique creation times. |
| 447 | net::CookieList cookie_list; |
| 448 | NSUInteger cookie_count = [system_cookies count]; |
| 449 | cookie_list.reserve(cookie_count); |
| 450 | for (NSHTTPCookie* cookie in system_cookies) { |
| 451 | cookie_list.push_back(CanonicalCookieFromSystemCookie( |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 452 | cookie, system_store_->GetCookieCreationTime(cookie))); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 453 | } |
| 454 | cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback()); |
| 455 | |
| 456 | // Update metrics. |
| 457 | if (metrics_enabled_) |
| 458 | UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count); |
| 459 | } |
| 460 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 461 | void CookieStoreIOS::DeleteCookiesMatchingInfoAsync( |
Maks Orlovich | f58b8da | 2019-02-11 17:12:45 | [diff] [blame] | 462 | net::CookieDeletionInfo delete_info, |
| 463 | DeleteCallback callback) { |
| 464 | DeleteCookiesMatchingPredicateAsync( |
| 465 | base::BindRepeating( |
| 466 | [](const CookieDeletionInfo& delete_info, |
| 467 | const net::CanonicalCookie& cc) { |
| 468 | return delete_info.Matches(cc); |
| 469 | }, |
| 470 | std::move(delete_info)), |
| 471 | std::move(callback)); |
| 472 | } |
| 473 | |
| 474 | void CookieStoreIOS::DeleteCookiesMatchingPredicateAsync( |
| 475 | const base::RepeatingCallback<bool(const net::CanonicalCookie&)>& predicate, |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 476 | DeleteCallback callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 477 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 478 | __block DeleteCallback shared_callback = std::move(callback); |
Maks Orlovich | f58b8da | 2019-02-11 17:12:45 | [diff] [blame] | 479 | __block base::RepeatingCallback<bool(const net::CanonicalCookie&)> |
| 480 | shared_predicate = predicate; |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 481 | base::WeakPtr<SystemCookieStore> weak_system_store = |
| 482 | system_store_->GetWeakPtr(); |
| 483 | system_store_->GetAllCookiesAsync( |
Sylvain Defresne | a44edf4 | 2018-06-05 15:08:34 | [diff] [blame] | 484 | base::BindOnce(^(NSArray<NSHTTPCookie*>* cookies) { |
Mohammad Refaat | 2e6de02 | 2018-05-29 20:36:44 | [diff] [blame] | 485 | if (!weak_system_store) { |
| 486 | if (!shared_callback.is_null()) |
| 487 | std::move(shared_callback).Run(0); |
| 488 | return; |
| 489 | } |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 490 | int to_delete_count = 0; |
| 491 | for (NSHTTPCookie* cookie in cookies) { |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 492 | base::Time creation_time = |
| 493 | weak_system_store->GetCookieCreationTime(cookie); |
| 494 | CanonicalCookie cc = |
| 495 | CanonicalCookieFromSystemCookie(cookie, creation_time); |
Maks Orlovich | f58b8da | 2019-02-11 17:12:45 | [diff] [blame] | 496 | if (shared_predicate.Run(cc)) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 497 | weak_system_store->DeleteCookieAsync( |
| 498 | cookie, SystemCookieStore::SystemCookieCallback()); |
| 499 | to_delete_count++; |
| 500 | } |
| 501 | } |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 502 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 503 | if (!shared_callback.is_null()) |
| 504 | std::move(shared_callback).Run(to_delete_count); |
| 505 | })); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void CookieStoreIOS::OnSystemCookiesChanged() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 509 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 510 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 511 | for (const auto& hook_map_entry : hook_map_) { |
| 512 | std::pair<GURL, std::string> key = hook_map_entry.first; |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 513 | UpdateCacheForCookieFromSystem(key.first, key.second, |
| 514 | /*run_callbacks=*/true); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Do not schedule a flush if one is already scheduled. |
| 518 | if (!flush_closure_.IsCancelled()) |
| 519 | return; |
| 520 | |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 521 | flush_closure_.Reset(base::Bind(&CookieStoreIOS::FlushStore, |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 522 | weak_factory_.GetWeakPtr(), base::Closure())); |
skyostil | c274098 | 2015-06-05 19:15:31 | [diff] [blame] | 523 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
eugenebut | 8a37ce25 | 2017-01-10 14:13:13 | [diff] [blame] | 524 | FROM_HERE, flush_closure_.callback(), base::TimeDelta::FromSeconds(10)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 525 | } |
| 526 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 527 | CookieChangeDispatcher& CookieStoreIOS::GetChangeDispatcher() { |
| 528 | return change_dispatcher_; |
| 529 | } |
| 530 | |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 531 | void CookieStoreIOS::SetCookieableSchemes( |
| 532 | const std::vector<std::string>& schemes, |
| 533 | SetCookieableSchemesCallback callback) { |
| 534 | // Not supported on iOS. |
| 535 | std::move(callback).Run(false); |
| 536 | } |
| 537 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 538 | std::unique_ptr<CookieChangeSubscription> CookieStoreIOS::AddCallbackForCookie( |
| 539 | const GURL& gurl, |
| 540 | const std::string& name, |
| 541 | CookieChangeCallback callback) { |
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 | |
| 544 | // Prefill cookie cache with all pertinent cookies for |url| if needed. |
| 545 | std::pair<GURL, std::string> key(gurl, name); |
| 546 | if (hook_map_.count(key) == 0) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 547 | UpdateCacheForCookieFromSystem(gurl, name, /*run_callbacks=*/false); |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 548 | hook_map_[key] = std::make_unique<CookieChangeCallbackList>(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | DCHECK(hook_map_.find(key) != hook_map_.end()); |
Sergey Kuznetsov | 0edf161 | 2018-10-12 14:29:05 | [diff] [blame] | 552 | auto subscription = |
| 553 | std::make_unique<Subscription>(hook_map_[key]->Add(std::move(callback))); |
| 554 | all_subscriptions_.Append(subscription.get()); |
| 555 | |
| 556 | return subscription; |
nharper | 5babb5e6 | 2016-03-09 18:58:07 | [diff] [blame] | 557 | } |
| 558 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 559 | void CookieStoreIOS::UpdateCacheForCookieFromSystem( |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 560 | const GURL& gurl, |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 561 | const std::string& cookie_name, |
| 562 | bool run_callbacks) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 563 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 564 | system_store_->GetCookiesForURLAsync( |
| 565 | gurl, base::BindOnce(&CookieStoreIOS::UpdateCacheForCookies, |
| 566 | weak_factory_.GetWeakPtr(), gurl, cookie_name, |
| 567 | run_callbacks)); |
| 568 | } |
| 569 | |
| 570 | void CookieStoreIOS::UpdateCacheForCookies(const GURL& gurl, |
| 571 | const std::string& cookie_name, |
| 572 | bool run_callbacks, |
| 573 | NSArray<NSHTTPCookie*>* nscookies) { |
| 574 | std::vector<net::CanonicalCookie> cookies; |
| 575 | std::vector<net::CanonicalCookie> out_removed_cookies; |
| 576 | std::vector<net::CanonicalCookie> out_added_cookies; |
| 577 | for (NSHTTPCookie* nscookie in nscookies) { |
| 578 | if (base::SysNSStringToUTF8(nscookie.name) == cookie_name) { |
| 579 | net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie( |
| 580 | nscookie, system_store_->GetCookieCreationTime(nscookie)); |
| 581 | cookies.push_back(canonical_cookie); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | bool changes = cookie_cache_->Update( |
| 586 | gurl, cookie_name, cookies, &out_removed_cookies, &out_added_cookies); |
| 587 | if (run_callbacks && changes) { |
| 588 | RunCallbacksForCookies(gurl, cookie_name, out_removed_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 589 | net::CookieChangeCause::UNKNOWN_DELETION); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 590 | RunCallbacksForCookies(gurl, cookie_name, out_added_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 591 | net::CookieChangeCause::INSERTED); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 592 | } |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void CookieStoreIOS::RunCallbacksForCookies( |
| 596 | const GURL& url, |
| 597 | const std::string& name, |
| 598 | const std::vector<net::CanonicalCookie>& cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 599 | net::CookieChangeCause cause) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 600 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 601 | if (cookies.empty()) |
| 602 | return; |
| 603 | |
| 604 | std::pair<GURL, std::string> key(url, name); |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 605 | CookieChangeCallbackList* callbacks = hook_map_[key].get(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 606 | for (const auto& cookie : cookies) { |
| 607 | DCHECK_EQ(name, cookie.Name()); |
Lily Chen | 19cced42 | 2019-10-17 21:45:55 | [diff] [blame] | 608 | // TODO(crbug.com/978172): Support CookieAccessSemantics values on iOS and |
| 609 | // use it to check IncludeForRequestURL before notifying? |
| 610 | callbacks->Notify(net::CookieChangeInfo( |
| 611 | cookie, net::CookieAccessSemantics::UNKNOWN, cause)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 615 | void CookieStoreIOS::GotCookieListFor( |
| 616 | const std::pair<GURL, std::string> key, |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 617 | const net::CookieStatusList& cookies, |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 618 | const net::CookieStatusList& excluded_cookies) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 619 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 620 | |
| 621 | net::CookieList filtered; |
| 622 | OnlyCookiesWithName(cookies, key.second, &filtered); |
| 623 | std::vector<net::CanonicalCookie> removed_cookies; |
| 624 | std::vector<net::CanonicalCookie> added_cookies; |
| 625 | if (cookie_cache_->Update(key.first, key.second, filtered, &removed_cookies, |
| 626 | &added_cookies)) { |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 627 | RunCallbacksForCookies(key.first, key.second, removed_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 628 | net::CookieChangeCause::UNKNOWN_DELETION); |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 629 | RunCallbacksForCookies(key.first, key.second, added_cookies, |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 630 | net::CookieChangeCause::INSERTED); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 634 | void CookieStoreIOS::UpdateCachesFromCookieMonster() { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 635 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 636 | for (const auto& hook_map_entry : hook_map_) { |
| 637 | std::pair<GURL, std::string> key = hook_map_entry.first; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 638 | GetCookieListCallback callback = base::BindOnce( |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 639 | &CookieStoreIOS::GotCookieListFor, weak_factory_.GetWeakPtr(), key); |
Lily Chen | f2dc5f0 | 2019-08-12 19:43:11 | [diff] [blame] | 640 | cookie_monster_->GetCookieListWithOptionsAsync( |
| 641 | key.first, net::CookieOptions::MakeAllInclusive(), std::move(callback)); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 645 | void CookieStoreIOS::UpdateCachesAfterSet( |
| 646 | SetCookiesCallback callback, |
| 647 | net::CanonicalCookie::CookieInclusionStatus status) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 648 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 649 | if (status.IsInclude()) |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 650 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 651 | if (!callback.is_null()) |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 652 | std::move(callback).Run(status); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 653 | } |
| 654 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 655 | void CookieStoreIOS::UpdateCachesAfterDelete(DeleteCallback callback, |
rdsmith | a5beda16 | 2017-07-08 13:55:42 | [diff] [blame] | 656 | uint32_t num_deleted) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 657 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 658 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 659 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 660 | std::move(callback).Run(num_deleted); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 661 | } |
| 662 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 663 | void CookieStoreIOS::UpdateCachesAfterClosure(base::OnceClosure callback) { |
Victor Costan | b805fba | 2018-02-21 13:49:42 | [diff] [blame] | 664 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 665 | UpdateCachesFromCookieMonster(); |
droger | 4c3ea11 | 2016-02-22 19:19:33 | [diff] [blame] | 666 | if (!callback.is_null()) |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 667 | std::move(callback).Run(); |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 668 | } |
| 669 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 670 | net::CookieList |
| 671 | CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) { |
| 672 | net::CookieList cookie_list; |
| 673 | cookie_list.reserve([cookies count]); |
| 674 | for (NSHTTPCookie* cookie in cookies) { |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 675 | base::Time created = system_store_->GetCookieCreationTime(cookie); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 676 | cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); |
| 677 | } |
| 678 | return cookie_list; |
| 679 | } |
| 680 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 681 | net::CookieStatusList |
| 682 | CookieStoreIOS::CanonicalCookieWithStatusListFromSystemCookies( |
| 683 | NSArray* cookies) { |
| 684 | net::CookieStatusList cookie_list; |
| 685 | cookie_list.reserve([cookies count]); |
| 686 | for (NSHTTPCookie* cookie in cookies) { |
| 687 | base::Time created = system_store_->GetCookieCreationTime(cookie); |
Lily Chen | f53dfbcd | 2019-08-30 01:42:10 | [diff] [blame] | 688 | cookie_list.push_back({CanonicalCookieFromSystemCookie(cookie, created), |
| 689 | net::CanonicalCookie::CookieInclusionStatus()}); |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 690 | } |
| 691 | return cookie_list; |
| 692 | } |
| 693 | |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 694 | void CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies( |
| 695 | CookieStoreIOS::GetCookieListCallback callback, |
| 696 | NSArray<NSHTTPCookie*>* cookies) { |
| 697 | if (!callback.is_null()) { |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 698 | net::CookieStatusList excluded_cookies; |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 699 | std::move(callback).Run( |
| 700 | CanonicalCookieWithStatusListFromSystemCookies(cookies), |
| 701 | excluded_cookies); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void CookieStoreIOS::RunGetAllCookiesCallbackOnSystemCookies( |
| 706 | CookieStoreIOS::GetAllCookiesCallback callback, |
| 707 | NSArray<NSHTTPCookie*>* cookies) { |
| 708 | if (!callback.is_null()) { |
| 709 | std::move(callback).Run(CanonicalCookieListFromSystemCookies(cookies)); |
Mohammad Refaat | 4103280 | 2017-10-06 04:15:57 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
droger | 3e8d98b | 2015-03-18 15:29:53 | [diff] [blame] | 713 | } // namespace net |