|
26 | 26 | #import "config.h"
|
27 | 27 | #import "NetworkStorageSession.h"
|
28 | 28 |
|
| 29 | +#import "ClientOrigin.h" |
29 | 30 | #import "Cookie.h"
|
30 | 31 | #import "CookieRequestHeaderFieldProxy.h"
|
31 | 32 | #import "CookieStorageObserver.h"
|
32 | 33 | #import "HTTPCookieAcceptPolicyCocoa.h"
|
| 34 | +#import "ResourceRequest.h" |
33 | 35 | #import "SameSiteInfo.h"
|
34 | 36 | #import <pal/spi/cf/CFNetworkSPI.h>
|
35 | 37 | #import <wtf/BlockObjCExceptions.h>
|
@@ -575,50 +577,58 @@ static NSHTTPCookieAcceptPolicy httpCookieAcceptPolicy(CFHTTPCookieStorageRef co
|
575 | 577 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), makeBlockPtr(WTFMove(work)).get());
|
576 | 578 | }
|
577 | 579 |
|
578 |
| -void NetworkStorageSession::deleteCookiesForHostnames(const Vector<String>& hostnames, IncludeHttpOnlyCookies includeHttpOnlyCookies, ScriptWrittenCookiesOnly scriptWrittenCookiesOnly, CompletionHandler<void()>&& completionHandler) |
| 580 | +void NetworkStorageSession::deleteCookiesMatching(const Function<bool(NSHTTPCookie *)>& matches, CompletionHandler<void()>&& completionHandler) |
579 | 581 | {
|
580 | 582 | ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies) || m_isInMemoryCookieStore);
|
581 | 583 |
|
582 |
| - auto aggregator = CallbackAggregator::create([completionHandler = WTFMove(completionHandler), cookieStorage = RetainPtr { nsCookieStorage() }] () mutable { |
583 |
| - [cookieStorage _saveCookies:makeBlockPtr([completionHandler = WTFMove(completionHandler)] () mutable { |
| 584 | + BEGIN_BLOCK_OBJC_EXCEPTIONS |
| 585 | + |
| 586 | + RetainPtr<CFHTTPCookieStorageRef> cookieStorage = this->cookieStorage(); |
| 587 | + auto nsCookieStorage = adoptNS([[NSHTTPCookieStorage alloc] _initWithCFHTTPCookieStorage:cookieStorage.get()]); |
| 588 | + auto aggregator = CallbackAggregator::create([completionHandler = WTFMove(completionHandler), nsCookieStorage = WTFMove(nsCookieStorage)] () mutable { |
| 589 | + [nsCookieStorage _saveCookies:makeBlockPtr([completionHandler = WTFMove(completionHandler)] () mutable { |
584 | 590 | ensureOnMainThread(WTFMove(completionHandler));
|
585 | 591 | }).get()];
|
586 | 592 | });
|
587 |
| - |
588 |
| - BEGIN_BLOCK_OBJC_EXCEPTIONS |
589 | 593 |
|
590 |
| - RetainPtr<CFHTTPCookieStorageRef> cookieStorage = this->cookieStorage(); |
591 | 594 | RetainPtr<NSArray> cookies = httpCookies(cookieStorage.get());
|
592 | 595 | if (!cookies)
|
593 | 596 | return;
|
594 | 597 |
|
595 |
| - HashMap<String, Vector<RetainPtr<NSHTTPCookie>>> cookiesByDomain; |
596 | 598 | for (NSHTTPCookie *cookie in cookies.get()) {
|
597 |
| - if (!cookie.domain || (includeHttpOnlyCookies == IncludeHttpOnlyCookies::No && cookie.isHTTPOnly)) |
598 |
| - continue; |
| 599 | + if (matches(cookie)) |
| 600 | + deleteHTTPCookie(cookieStorage.get(), cookie, [aggregator] { }); |
| 601 | + } |
599 | 602 |
|
| 603 | + END_BLOCK_OBJC_EXCEPTIONS |
| 604 | +} |
| 605 | + |
| 606 | +void NetworkStorageSession::deleteCookies(const ClientOrigin& origin, CompletionHandler<void()>&& completionHandler) |
| 607 | +{ |
| 608 | + auto cachePartition = origin.topOrigin == origin.clientOrigin ? emptyString() : ResourceRequest::partitionName(origin.topOrigin.host); |
| 609 | + deleteCookiesMatching([domain = origin.clientOrigin.host, &cachePartition](NSHTTPCookie* cookie) { |
| 610 | + return String(cookie.domain) == domain && equalIgnoringNullity(String(cookie._storagePartition), cachePartition); |
| 611 | + }, WTFMove(completionHandler)); |
| 612 | +} |
| 613 | + |
| 614 | +void NetworkStorageSession::deleteCookiesForHostnames(const Vector<String>& hostnames, IncludeHttpOnlyCookies includeHttpOnlyCookies, ScriptWrittenCookiesOnly scriptWrittenCookiesOnly, CompletionHandler<void()>&& completionHandler) |
| 615 | +{ |
| 616 | + HashSet<String> hostnamesSet; |
| 617 | + for (auto& hostname : hostnames) |
| 618 | + hostnamesSet.add(hostname); |
| 619 | + |
| 620 | + deleteCookiesMatching([&](NSHTTPCookie* cookie) { |
| 621 | + if (!cookie.domain || (includeHttpOnlyCookies == IncludeHttpOnlyCookies::No && cookie.isHTTPOnly)) |
| 622 | + return false; |
600 | 623 | #if ENABLE(JS_COOKIE_CHECKING)
|
601 | 624 | bool setInJS = [[cookie properties] valueForKey:@"SetInJavaScript"];
|
602 | 625 | if (scriptWrittenCookiesOnly == ScriptWrittenCookiesOnly::Yes && !setInJS)
|
603 |
| - continue; |
| 626 | + return false; |
604 | 627 | #else
|
605 | 628 | UNUSED_PARAM(scriptWrittenCookiesOnly);
|
606 | 629 | #endif
|
607 |
| - cookiesByDomain.ensure(cookie.domain, [] { |
608 |
| - return Vector<RetainPtr<NSHTTPCookie>>(); |
609 |
| - }).iterator->value.append(cookie); |
610 |
| - } |
611 |
| - |
612 |
| - for (const auto& hostname : hostnames) { |
613 |
| - auto it = cookiesByDomain.find(hostname); |
614 |
| - if (it == cookiesByDomain.end()) |
615 |
| - continue; |
616 |
| - |
617 |
| - for (auto& cookie : it->value) |
618 |
| - deleteHTTPCookie(cookieStorage.get(), cookie.get(), [aggregator] { }); |
619 |
| - } |
620 |
| - |
621 |
| - END_BLOCK_OBJC_EXCEPTIONS |
| 630 | + return hostnamesSet.contains(String(cookie.domain)); |
| 631 | + }, WTFMove(completionHandler)); |
622 | 632 | }
|
623 | 633 |
|
624 | 634 | void NetworkStorageSession::deleteAllCookiesModifiedSince(WallTime timePoint, CompletionHandler<void()>&& completionHandler)
|
|
0 commit comments