Commit e7d206d2 authored by Yuke Liao's avatar Yuke Liao Committed by Commit Bot

Revert "Convert SystemCookieStore to Async."

This reverts commit 8505818b.

Reason for revert: This CL is causing CookieStoreIOSTest.SameValueDoesNotCallHook to fail on iPhone 7, iOS 11 devices.

Original change's description:
> Convert SystemCookieStore to Async.
> 
> * Converts SystemCookieStore to Async
> * Add Async functions to NSHTTPSystemCookieStore and keep the synchronous
> functions.
> * Move creation time management to systemCookieStore.
> * Update cookie_store_ios_presistent_unittest & 
> cookie_store_ios_unittests to account for SystemCookieStore changes 
> * (As CookieStoreIOS is using systemCookieStore internally).
> 
> Another CL is in progress to add WKSystemCookieStore & unit_tests for 
> systemCookieStore (to handle async methods) with template for both 
> NSHTTP & WK systemCookieStore.
> 
> Bug: 759229, 759227, 767948
> Change-Id: I1c24c82540f4781a0bec77ebfa4a861ca7433391
> Reviewed-on: https://chromium-review.googlesource.com/675804
> Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
> Reviewed-by: Eugene But <eugenebut@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#505033}

TBR=mmenke@chromium.org,eugenebut@chromium.org,mrefaat@chromium.org

Change-Id: I43c71a95910d14f4717111a66f0fb0556a91fb8f
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 759229, 759227, 767948
Reviewed-on: https://chromium-review.googlesource.com/691037Reviewed-by: default avatarYuke Liao <liaoyuke@chromium.org>
Commit-Queue: Yuke Liao <liaoyuke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505178}
parent c541be06
...@@ -57,7 +57,6 @@ source_set("startup") { ...@@ -57,7 +57,6 @@ source_set("startup") {
"//ios/chrome/browser:browser_internal", "//ios/chrome/browser:browser_internal",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/first_run", "//ios/chrome/browser/first_run",
"//ios/chrome/browser/net:net",
"//ios/chrome/browser/ntp_snippets", "//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/web", "//ios/chrome/browser/web",
"//ios/chrome/browser/web:web_internal", "//ios/chrome/browser/web:web_internal",
......
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
#include "ios/chrome/app/startup/client_registration.h" #include "ios/chrome/app/startup/client_registration.h"
#include "ios/chrome/browser/net/chrome_cookie_store_ios_client.h"
#import "ios/chrome/browser/web/chrome_web_client.h" #import "ios/chrome/browser/web/chrome_web_client.h"
#include "ios/net/cookies/cookie_store_ios_client.h"
#import "ios/web/public/web_client.h" #import "ios/web/public/web_client.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -17,9 +15,6 @@ ...@@ -17,9 +15,6 @@
+ (void)registerClients { + (void)registerClients {
web::SetWebClient(new ChromeWebClient()); web::SetWebClient(new ChromeWebClient());
// Register CookieStoreIOSClient, This is used to provide CookieStoreIOSClient
// users with WEB::IO task runner.
net::SetCookieStoreIOSClient(new ChromeCookieStoreIOSClient());
} }
@end @end
...@@ -7,18 +7,28 @@ ...@@ -7,18 +7,28 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/threading/thread_checker.h"
#include "ios/net/cookies/cookie_store_ios_client.h" #include "ios/net/cookies/cookie_store_ios_client.h"
@protocol BrowsingDataChangeListening;
// Chrome implementation of net::CookieStoreIOSClient. This class lives on the // Chrome implementation of net::CookieStoreIOSClient. This class lives on the
// IOThread. // IOThread.
class ChromeCookieStoreIOSClient : public net::CookieStoreIOSClient { class ChromeCookieStoreIOSClient : public net::CookieStoreIOSClient {
public: public:
ChromeCookieStoreIOSClient(); // Creates a CookieStoreIOSClient with a BrowsingDataChangeListening.
// |browsing_data_change_listener| cannot be nil.
explicit ChromeCookieStoreIOSClient(
id<BrowsingDataChangeListening> browsing_data_change_listener);
// CookieStoreIOSClient implementation. // CookieStoreIOSClient implementation.
void DidChangeCookieStorage() const override;
scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const override; scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const override;
private: private:
base::ThreadChecker thread_checker_;
// The listener that is informed of change in browsing data.
id<BrowsingDataChangeListening> browsing_data_change_listener_; // Weak.
DISALLOW_COPY_AND_ASSIGN(ChromeCookieStoreIOSClient); DISALLOW_COPY_AND_ASSIGN(ChromeCookieStoreIOSClient);
}; };
......
...@@ -4,15 +4,31 @@ ...@@ -4,15 +4,31 @@
#include "ios/chrome/browser/net/chrome_cookie_store_ios_client.h" #include "ios/chrome/browser/net/chrome_cookie_store_ios_client.h"
#include "base/logging.h"
#include "base/task_scheduler/post_task.h"
#import "ios/chrome/browser/browsing_data/browsing_data_change_listening.h"
#include "ios/web/public/web_thread.h" #include "ios/web/public/web_thread.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
ChromeCookieStoreIOSClient::ChromeCookieStoreIOSClient() {} ChromeCookieStoreIOSClient::ChromeCookieStoreIOSClient(
id<BrowsingDataChangeListening> browsing_data_change_listener)
: browsing_data_change_listener_(browsing_data_change_listener) {
DCHECK(browsing_data_change_listener);
DCHECK_CURRENTLY_ON(web::WebThread::IO);
}
void ChromeCookieStoreIOSClient::DidChangeCookieStorage() const {
DCHECK(thread_checker_.CalledOnValidThread());
[browsing_data_change_listener_ didChangeCookieStorage];
}
scoped_refptr<base::SequencedTaskRunner> scoped_refptr<base::SequencedTaskRunner>
ChromeCookieStoreIOSClient::GetTaskRunner() const { ChromeCookieStoreIOSClient::GetTaskRunner() const {
return web::WebThread::GetTaskRunnerForThread(web::WebThread::IO); DCHECK(thread_checker_.CalledOnValidThread());
return base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BACKGROUND});
} }
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
namespace net { namespace net {
class CookieCreationTimeManager;
// Observer for changes on |NSHTTPCookieStorge sharedHTTPCookieStorage|. // Observer for changes on |NSHTTPCookieStorge sharedHTTPCookieStorage|.
class CookieNotificationObserver { class CookieNotificationObserver {
public: public:
...@@ -156,6 +158,8 @@ class CookieStoreIOS : public net::CookieStore, ...@@ -156,6 +158,8 @@ class CookieStoreIOS : public net::CookieStore,
// deleted. // deleted.
typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction; typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction;
// Clears the system cookie store.
void ClearSystemStore();
// Returns true if the system cookie store policy is // Returns true if the system cookie store policy is
// |NSHTTPCookieAcceptPolicyAlways|. // |NSHTTPCookieAcceptPolicyAlways|.
bool SystemCookiesAllowed(); bool SystemCookiesAllowed();
...@@ -165,15 +169,12 @@ class CookieStoreIOS : public net::CookieStore, ...@@ -165,15 +169,12 @@ class CookieStoreIOS : public net::CookieStore,
// Inherited CookieNotificationObserver methods. // Inherited CookieNotificationObserver methods.
void OnSystemCookiesChanged() override; void OnSystemCookiesChanged() override;
void DeleteCookiesWithFilterAsync(CookieFilterFunction filter, void DeleteCookiesWithFilter(const CookieFilterFunction& filter,
DeleteCallback callback); DeleteCallback callback);
// Flush to CookieMonster from |cookies|, and run |callback|.
void FlushStoreFromCookies(base::OnceClosure callback,
NSArray<NSHTTPCookie*>* cookies);
std::unique_ptr<net::CookieMonster> cookie_monster_; std::unique_ptr<net::CookieMonster> cookie_monster_;
std::unique_ptr<SystemCookieStore> system_store_; std::unique_ptr<SystemCookieStore> system_store_;
std::unique_ptr<CookieCreationTimeManager> creation_time_manager_;
bool metrics_enabled_; bool metrics_enabled_;
base::CancelableClosure flush_closure_; base::CancelableClosure flush_closure_;
...@@ -188,22 +189,26 @@ class CookieStoreIOS : public net::CookieStore, ...@@ -188,22 +189,26 @@ class CookieStoreIOS : public net::CookieStore,
// the CookieStoreIOS is synchronized and the CookieStore when the // the CookieStoreIOS is synchronized and the CookieStore when the
// CookieStoreIOS is not synchronized. // CookieStoreIOS is not synchronized.
// Updates the cookie cache with cookies named |cookie_name| from the current // Fetches any cookies named |name| that would be sent with a request for
// set of |nscookies| that would be sent with a request for |url|. // |url| from the system cookie store and pushes them onto the back of the
// |run_callbacks| Run all callbacks registered for cookie named |name| if // vector pointed to by |cookies|. Returns true if any cookies were pushed
// CookieCache was changed. // onto the vector, and false otherwise.
void UpdateCacheForCookies(const GURL& gurl, bool GetSystemCookies(const GURL& url,
const std::string& cookie_name, const std::string& name,
bool run_callbacks, std::vector<net::CanonicalCookie>* cookies);
NSArray<NSHTTPCookie*>* nscookies);
// Updates the cookie cache with the current set of system cookies named // Updates the cookie cache with the current set of system cookies named
// |cookie_name| that would be sent with a request for |url|. // |name| that would be sent with a request for |url|. Returns whether the
// |run_callbacks| Run all callbacks registered for cookie named |name| if // cache changed.
// CookieCache was changed. // |out_removed_cookies|, if not null, will be populated with the cookies that
void UpdateCacheForCookieFromSystem(const GURL& gurl, // were removed.
const std::string& cookie_name, // |out_changed_cookies|, if not null, will be populated with the cookies that
bool run_callbacks); // were added.
bool UpdateCacheForCookieFromSystem(
const GURL& gurl,
const std::string& name,
std::vector<net::CanonicalCookie>* out_removed_cookies,
std::vector<net::CanonicalCookie>* out_added_cookies);
// Runs all callbacks registered for cookies named |name| that would be sent // Runs all callbacks registered for cookies named |name| that would be sent
// with a request for |url|. // with a request for |url|.
...@@ -223,6 +228,22 @@ class CookieStoreIOS : public net::CookieStore, ...@@ -223,6 +228,22 @@ class CookieStoreIOS : public net::CookieStore,
// asynchronously invoking callbacks if necessary. // asynchronously invoking callbacks if necessary.
void UpdateCachesFromCookieMonster(); void UpdateCachesFromCookieMonster();
// Called after cookies are cleared from NSHTTPCookieStorage so that cookies
// can be cleared from .binarycookies file. |callback| is called after all the
// cookies are deleted (with the total number of cookies deleted).
// |num_deleted| contains the number of cookies deleted from
// NSHTTPCookieStorage.
void DidClearNSHTTPCookieStorageCookies(DeleteCallback callback,
int num_deleted);
// Called after cookies are cleared from .binarycookies files. |callback| is
// called after all the cookies are deleted with the total number of cookies
// deleted.
// |num_deleted_from_nshttp_cookie_storage| contains the number of cookies
// deleted from NSHTTPCookieStorage.
void DidClearBinaryCookiesFileCookies(
DeleteCallback callback,
int num_deleted_from_nshttp_cookie_storage);
// Callback-wrapping: // Callback-wrapping:
// When this CookieStoreIOS object is synchronized with the system store, // When this CookieStoreIOS object is synchronized with the system store,
// OnSystemCookiesChanged is responsible for updating the cookie cache (and // OnSystemCookiesChanged is responsible for updating the cookie cache (and
...@@ -250,10 +271,6 @@ class CookieStoreIOS : public net::CookieStore, ...@@ -250,10 +271,6 @@ class CookieStoreIOS : public net::CookieStore,
// creation date. // creation date.
net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies); net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies);
// Runs |callback| on CanonicalCookie List converted from cookies.
void RunGetCookieListCallbackOnSystemCookies(GetCookieListCallback callback,
NSArray<NSHTTPCookie*>* cookies);
// Cached values of system cookies. Only cookies which have an observer added // Cached values of system cookies. Only cookies which have an observer added
// with AddCallbackForCookie are kept in this cache. // with AddCallbackForCookie are kept in this cache.
std::unique_ptr<CookieCache> cookie_cache_; std::unique_ptr<CookieCache> cookie_cache_;
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "base/ios/ios_util.h" #include "base/ios/ios_util.h"
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#import "base/mac/bind_objc_block.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -27,6 +26,7 @@ ...@@ -27,6 +26,7 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#import "ios/net/cookies/cookie_creation_time_manager.h"
#include "ios/net/cookies/cookie_store_ios_client.h" #include "ios/net/cookies/cookie_store_ios_client.h"
#import "ios/net/cookies/ns_http_system_cookie_store.h" #import "ios/net/cookies/ns_http_system_cookie_store.h"
#import "ios/net/cookies/system_cookie_util.h" #import "ios/net/cookies/system_cookie_util.h"
...@@ -102,6 +102,30 @@ NotificationTrampoline* NotificationTrampoline::g_notification_trampoline = ...@@ -102,6 +102,30 @@ NotificationTrampoline* NotificationTrampoline::g_notification_trampoline =
#pragma mark Utility functions #pragma mark Utility functions
// Returns the path to Cookie.binarycookies file on the file system where
// WKWebView flushes its cookies.
base::FilePath GetBinaryCookiesFilePath() {
base::FilePath path = base::mac::GetUserLibraryPath();
// The relative path of the file (from the user library folder) where
// WKWebView stores its cookies.
const std::string kCookiesFilePath = "Cookies/Cookies.binarycookies";
return path.Append(kCookiesFilePath);
}
// Clears all cookies from the .binarycookies file.
// Must be called from a thread where IO operations are allowed.
// Preconditions: There must be no active WKWebViews present in the app.
// Note that the .binarycookies file is present only on iOS8+.
void ClearAllCookiesFromBinaryCookiesFile() {
base::FilePath path = GetBinaryCookiesFilePath();
if (base::PathExists(path)) {
bool success = base::DeleteFile(path, false);
if (!success) {
DLOG(WARNING) << "Failed to remove binarycookies file.";
}
}
}
// Builds a NSHTTPCookie from a header cookie line ("Set-Cookie: xxx") and a // Builds a NSHTTPCookie from a header cookie line ("Set-Cookie: xxx") and a
// URL. // URL.
NSHTTPCookie* GetNSHTTPCookieFromCookieLine(const std::string& cookie_line, NSHTTPCookie* GetNSHTTPCookieFromCookieLine(const std::string& cookie_line,
...@@ -150,28 +174,6 @@ std::string BuildCookieLineWithOptions(NSArray* cookies, ...@@ -150,28 +174,6 @@ std::string BuildCookieLineWithOptions(NSArray* cookies,
return base::SysNSStringToUTF8([header valueForKey:@"Cookie"]); return base::SysNSStringToUTF8([header valueForKey:@"Cookie"]);
} }
// Returns an empty closure if |callback| is null callback or binds the
// callback to |success|.
base::OnceClosure BindSetCookiesCallback(
CookieStoreIOS::SetCookiesCallback* callback,
bool success) {
base::OnceClosure set_callback;
if (!callback->is_null()) {
set_callback = base::BindOnce(std::move(*callback), success);
}
return set_callback;
}
// Runs |callback| on CookieLine generated from |options| and |cookies|.
void RunGetCookiesCallbackOnSystemCookies(
const net::CookieOptions& options,
CookieStoreIOS::GetCookiesCallback callback,
NSArray<NSHTTPCookie*>* cookies) {
if (!callback.is_null()) {
std::move(callback).Run(BuildCookieLineWithOptions(cookies, options));
}
}
// Tests whether the |cookie| is a session cookie. // Tests whether the |cookie| is a session cookie.
bool IsCookieSessionCookie(NSHTTPCookie* cookie, base::Time time) { bool IsCookieSessionCookie(NSHTTPCookie* cookie, base::Time time) {
return [cookie isSessionOnly]; return [cookie isSessionOnly];
...@@ -297,13 +299,14 @@ void CookieStoreIOS::SetCookieWithOptionsAsync( ...@@ -297,13 +299,14 @@ void CookieStoreIOS::SetCookieWithOptionsAsync(
(!has_explicit_domain || has_valid_domain); (!has_explicit_domain || has_valid_domain);
if (success) { if (success) {
system_store_->SetCookieAsync(cookie, system_store_->SetCookie(cookie);
BindSetCookiesCallback(&callback, true)); creation_time_manager_->SetCreationTime(
return; cookie,
creation_time_manager_->MakeUniqueCreationTime(base::Time::Now()));
} }
if (!callback.is_null()) if (!callback.is_null())
std::move(callback).Run(false); std::move(callback).Run(success);
} }
void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url, void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url,
...@@ -324,6 +327,8 @@ void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url, ...@@ -324,6 +327,8 @@ void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url,
// should be written there instead. // should be written there instead.
DCHECK(SystemCookiesAllowed()); DCHECK(SystemCookiesAllowed());
bool success = false;
if (creation_time.is_null()) if (creation_time.is_null())
creation_time = base::Time::Now(); creation_time = base::Time::Now();
...@@ -367,14 +372,16 @@ void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url, ...@@ -367,14 +372,16 @@ void CookieStoreIOS::SetCookieWithDetailsAsync(const GURL& url,
NSHTTPCookie* cookie = SystemCookieFromCanonicalCookie(*canonical_cookie); NSHTTPCookie* cookie = SystemCookieFromCanonicalCookie(*canonical_cookie);
if (cookie != nil) { if (cookie != nil) {
system_store_->SetCookieAsync(cookie, &canonical_cookie->CreationDate(), system_store_->SetCookie(cookie);
BindSetCookiesCallback(&callback, true)); creation_time_manager_->SetCreationTime(
return; cookie, creation_time_manager_->MakeUniqueCreationTime(
canonical_cookie->CreationDate()));
success = true;
} }
} }
if (!callback.is_null()) if (!callback.is_null())
std::move(callback).Run(false); std::move(callback).Run(success);
} }
void CookieStoreIOS::SetCanonicalCookieAsync( void CookieStoreIOS::SetCanonicalCookieAsync(
...@@ -396,8 +403,14 @@ void CookieStoreIOS::SetCanonicalCookieAsync( ...@@ -396,8 +403,14 @@ void CookieStoreIOS::SetCanonicalCookieAsync(
NSHTTPCookie* ns_cookie = SystemCookieFromCanonicalCookie(*cookie.get()); NSHTTPCookie* ns_cookie = SystemCookieFromCanonicalCookie(*cookie.get());
if (ns_cookie != nil) { if (ns_cookie != nil) {
system_store_->SetCookieAsync(ns_cookie, &cookie->CreationDate(), system_store_->SetCookie(ns_cookie);
BindSetCookiesCallback(&callback, true)); creation_time_manager_->SetCreationTime(
ns_cookie,
creation_time_manager_->MakeUniqueCreationTime(
cookie->CreationDate().is_null() ? base::Time::Now()
: cookie->CreationDate()));
if (!callback.is_null())
std::move(callback).Run(true);
return; return;
} }
...@@ -418,11 +431,12 @@ void CookieStoreIOS::GetCookiesWithOptionsAsync( ...@@ -418,11 +431,12 @@ void CookieStoreIOS::GetCookiesWithOptionsAsync(
// engine. // engine.
DCHECK(!options.exclude_httponly()); DCHECK(!options.exclude_httponly());
// TODO(crbug.com/459154): If/when iOS supports Same-Site cookies, we'll need // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass
// to pass options in here as well. // options in here as well. https://crbug.com/459154
system_store_->GetCookiesForURLAsync( NSArray* cookies =
url, base::BindOnce(&RunGetCookiesCallbackOnSystemCookies, options, system_store_->GetCookiesForURL(url, creation_time_manager_.get());
base::Passed(&callback))); if (!callback.is_null())
std::move(callback).Run(BuildCookieLineWithOptions(cookies, options));
} }
void CookieStoreIOS::GetCookieListWithOptionsAsync( void CookieStoreIOS::GetCookieListWithOptionsAsync(
...@@ -440,10 +454,11 @@ void CookieStoreIOS::GetCookieListWithOptionsAsync( ...@@ -440,10 +454,11 @@ void CookieStoreIOS::GetCookieListWithOptionsAsync(
// TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass
// options in here as well. https://crbug.com/459154 // options in here as well. https://crbug.com/459154
system_store_->GetCookiesForURLAsync( NSArray* cookies =
url, system_store_->GetCookiesForURL(url, creation_time_manager_.get());
base::BindOnce(&CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies, net::CookieList cookie_list = CanonicalCookieListFromSystemCookies(cookies);
weak_factory_.GetWeakPtr(), base::Passed(&callback))); if (!callback.is_null())
std::move(callback).Run(cookie_list);
} }
void CookieStoreIOS::GetAllCookiesAsync(GetCookieListCallback callback) { void CookieStoreIOS::GetAllCookiesAsync(GetCookieListCallback callback) {
...@@ -455,33 +470,30 @@ void CookieStoreIOS::GetAllCookiesAsync(GetCookieListCallback callback) { ...@@ -455,33 +470,30 @@ void CookieStoreIOS::GetAllCookiesAsync(GetCookieListCallback callback) {
cookie_monster_->GetAllCookiesAsync(std::move(callback)); cookie_monster_->GetAllCookiesAsync(std::move(callback));
return; return;
} }
// TODO(crbug.com/459154): If/when iOS supports Same-Site cookies, we'll need
// to pass options in here as well. NSArray* cookies = system_store_->GetAllCookies(creation_time_manager_.get());
system_store_->GetAllCookiesAsync( net::CookieList cookie_list = CanonicalCookieListFromSystemCookies(cookies);
base::BindOnce(&CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies, if (!callback.is_null()) {
weak_factory_.GetWeakPtr(), base::Passed(&callback))); std::move(callback).Run(cookie_list);
}
} }
void CookieStoreIOS::DeleteCookieAsync(const GURL& url, void CookieStoreIOS::DeleteCookieAsync(const GURL& url,
const std::string& cookie_name, const std::string& cookie_name,
base::OnceClosure callback) { base::OnceClosure callback) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
__block base::OnceClosure shared_callback = std::move(callback);
base::WeakPtr<SystemCookieStore> weak_system_store = NSArray* cookies =
system_store_->GetWeakPtr(); system_store_->GetCookiesForURL(url, creation_time_manager_.get());
system_store_->GetCookiesForURLAsync( for (NSHTTPCookie* cookie in cookies) {
url, base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) { if ([[cookie name] isEqualToString:base::SysUTF8ToNSString(cookie_name)]) {
for (NSHTTPCookie* cookie in cookies) { system_store_->DeleteCookie(cookie);
if ([cookie.name creation_time_manager_->DeleteCreationTime(cookie);
isEqualToString:base::SysUTF8ToNSString(cookie_name)] && }
weak_system_store) { }
weak_system_store->DeleteCookieAsync(
cookie, SystemCookieStore::SystemCookieCallback()); if (!callback.is_null())
} std::move(callback).Run();
}
if (!shared_callback.is_null())
std::move(shared_callback).Run();
}));
} }
void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
...@@ -491,7 +503,7 @@ void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, ...@@ -491,7 +503,7 @@ void CookieStoreIOS::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
// This relies on the fact cookies are given unique creation dates. // This relies on the fact cookies are given unique creation dates.
CookieFilterFunction filter = base::Bind( CookieFilterFunction filter = base::Bind(
IsCookieCreatedBetween, cookie.CreationDate(), cookie.CreationDate()); IsCookieCreatedBetween, cookie.CreationDate(), cookie.CreationDate());
DeleteCookiesWithFilterAsync(std::move(filter), std::move(callback)); DeleteCookiesWithFilter(filter, std::move(callback));
} }
void CookieStoreIOS::DeleteAllCreatedBetweenAsync( void CookieStoreIOS::DeleteAllCreatedBetweenAsync(
...@@ -505,7 +517,7 @@ void CookieStoreIOS::DeleteAllCreatedBetweenAsync( ...@@ -505,7 +517,7 @@ void CookieStoreIOS::DeleteAllCreatedBetweenAsync(
CookieFilterFunction filter = base::Bind( CookieFilterFunction filter = base::Bind(
&IsCookieCreatedBetween, delete_begin, delete_end); &IsCookieCreatedBetween, delete_begin, delete_end);
DeleteCookiesWithFilterAsync(std::move(filter), std::move(callback)); DeleteCookiesWithFilter(filter, std::move(callback));
} }
void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync( void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync(
...@@ -520,7 +532,7 @@ void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync( ...@@ -520,7 +532,7 @@ void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync(
CookieFilterFunction filter = base::Bind( CookieFilterFunction filter = base::Bind(
IsCookieCreatedBetweenWithPredicate, delete_begin, delete_end, predicate); IsCookieCreatedBetweenWithPredicate, delete_begin, delete_end, predicate);
DeleteCookiesWithFilterAsync(std::move(filter), std::move(callback)); DeleteCookiesWithFilter(filter, std::move(callback));
} }
void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) { void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) {
...@@ -530,7 +542,7 @@ void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) { ...@@ -530,7 +542,7 @@ void CookieStoreIOS::DeleteSessionCookiesAsync(DeleteCallback callback) {
ResetCookieCountMetrics(); ResetCookieCountMetrics();
CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie); CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie);
DeleteCookiesWithFilterAsync(std::move(filter), std::move(callback)); DeleteCookiesWithFilter(filter, std::move(callback));
} }
void CookieStoreIOS::FlushStore(base::OnceClosure closure) { void CookieStoreIOS::FlushStore(base::OnceClosure closure) {
...@@ -539,19 +551,8 @@ void CookieStoreIOS::FlushStore(base::OnceClosure closure) { ...@@ -539,19 +551,8 @@ void CookieStoreIOS::FlushStore(base::OnceClosure closure) {
if (SystemCookiesAllowed()) { if (SystemCookiesAllowed()) {
// If cookies are disabled, the system store is empty, and the cookies are // If cookies are disabled, the system store is empty, and the cookies are
// stashed on disk. Do not delete the cookies on the disk in this case. // stashed on disk. Do not delete the cookies on the disk in this case.
system_store_->GetAllCookiesAsync( WriteToCookieMonster(system_store_->GetAllCookies());
base ::BindOnce(&CookieStoreIOS::FlushStoreFromCookies,
weak_factory_.GetWeakPtr(), std::move(closure)));
return;
} }
cookie_monster_->FlushStore(std::move(closure));
flush_closure_.Cancel();
}
void CookieStoreIOS::FlushStoreFromCookies(base::OnceClosure closure,
NSArray<NSHTTPCookie*>* cookies) {
WriteToCookieMonster(cookies);
cookie_monster_->FlushStore(std::move(closure)); cookie_monster_->FlushStore(std::move(closure));
flush_closure_.Cancel(); flush_closure_.Cancel();
} }
...@@ -564,6 +565,7 @@ CookieStoreIOS::CookieStoreIOS( ...@@ -564,6 +565,7 @@ CookieStoreIOS::CookieStoreIOS(
std::unique_ptr<SystemCookieStore> system_store) std::unique_ptr<SystemCookieStore> system_store)
: cookie_monster_(new net::CookieMonster(persistent_store)), : cookie_monster_(new net::CookieMonster(persistent_store)),
system_store_(std::move(system_store)), system_store_(std::move(system_store)),
creation_time_manager_(new CookieCreationTimeManager),
metrics_enabled_(false), metrics_enabled_(false),
cookie_cache_(new CookieCache()), cookie_cache_(new CookieCache()),
weak_factory_(this) { weak_factory_(this) {
...@@ -598,6 +600,12 @@ base::OnceClosure CookieStoreIOS::WrapClosure(base::OnceClosure callback) { ...@@ -598,6 +600,12 @@ base::OnceClosure CookieStoreIOS::WrapClosure(base::OnceClosure callback) {
#pragma mark - #pragma mark -
#pragma mark Private methods #pragma mark Private methods
void CookieStoreIOS::ClearSystemStore() {
DCHECK(thread_checker_.CalledOnValidThread());
system_store_->ClearStore();
creation_time_manager_->Clear();
}
bool CookieStoreIOS::SystemCookiesAllowed() { bool CookieStoreIOS::SystemCookiesAllowed() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
return system_store_->GetCookieAcceptPolicy() != return system_store_->GetCookieAcceptPolicy() !=
...@@ -613,7 +621,7 @@ void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { ...@@ -613,7 +621,7 @@ void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) {
cookie_list.reserve(cookie_count); cookie_list.reserve(cookie_count);
for (NSHTTPCookie* cookie in system_cookies) { for (NSHTTPCookie* cookie in system_cookies) {
cookie_list.push_back(CanonicalCookieFromSystemCookie( cookie_list.push_back(CanonicalCookieFromSystemCookie(
cookie, system_store_->GetCookieCreationTime(cookie))); cookie, creation_time_manager_->GetCreationTime(cookie)));
} }
cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback()); cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback());
...@@ -622,30 +630,28 @@ void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { ...@@ -622,30 +630,28 @@ void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) {
UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count); UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count);
} }
void CookieStoreIOS::DeleteCookiesWithFilterAsync(CookieFilterFunction filter, void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter,
DeleteCallback callback) { DeleteCallback callback) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!filter.is_null()); NSArray* cookies = system_store_->GetAllCookies();
__block DeleteCallback shared_callback = std::move(callback);
__block CookieFilterFunction shared_filter = std::move(filter); // Collect the cookies to delete.
base::WeakPtr<SystemCookieStore> weak_system_store = base::scoped_nsobject<NSMutableArray> to_delete(
system_store_->GetWeakPtr(); [[NSMutableArray alloc] init]);
system_store_->GetAllCookiesAsync( for (NSHTTPCookie* cookie in cookies) {
base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) { base::Time creation_time = creation_time_manager_->GetCreationTime(cookie);
int to_delete_count = 0; if (filter.Run(cookie, creation_time))
for (NSHTTPCookie* cookie in cookies) { [to_delete addObject:cookie];
if (weak_system_store && }
shared_filter.Run(
cookie, weak_system_store->GetCookieCreationTime(cookie))) { // Delete them.
weak_system_store->DeleteCookieAsync( for (NSHTTPCookie* cookie in to_delete.get()) {
cookie, SystemCookieStore::SystemCookieCallback()); system_store_->DeleteCookie(cookie);
to_delete_count++; creation_time_manager_->DeleteCreationTime(cookie);
} }
}
if (!callback.is_null())
if (!shared_callback.is_null()) std::move(callback).Run([to_delete count]);
std::move(shared_callback).Run(to_delete_count);
}));
} }
void CookieStoreIOS::OnSystemCookiesChanged() { void CookieStoreIOS::OnSystemCookiesChanged() {
...@@ -653,8 +659,15 @@ void CookieStoreIOS::OnSystemCookiesChanged() { ...@@ -653,8 +659,15 @@ void CookieStoreIOS::OnSystemCookiesChanged() {
for (const auto& hook_map_entry : hook_map_) { for (const auto& hook_map_entry : hook_map_) {
std::pair<GURL, std::string> key = hook_map_entry.first; std::pair<GURL, std::string> key = hook_map_entry.first;
UpdateCacheForCookieFromSystem(key.first, key.second, std::vector<net::CanonicalCookie> removed_cookies;
/*run_callbacks=*/true); std::vector<net::CanonicalCookie> added_cookies;
if (UpdateCacheForCookieFromSystem(key.first, key.second, &removed_cookies,
&added_cookies)) {
RunCallbacksForCookies(key.first, key.second, removed_cookies,
net::CookieStore::ChangeCause::UNKNOWN_DELETION);
RunCallbacksForCookies(key.first, key.second, added_cookies,
net::CookieStore::ChangeCause::INSERTED);
}
} }
// Do not schedule a flush if one is already scheduled. // Do not schedule a flush if one is already scheduled.
...@@ -676,8 +689,9 @@ CookieStoreIOS::AddCallbackForCookie(const GURL& gurl, ...@@ -676,8 +689,9 @@ CookieStoreIOS::AddCallbackForCookie(const GURL& gurl,
// Prefill cookie cache with all pertinent cookies for |url| if needed. // Prefill cookie cache with all pertinent cookies for |url| if needed.
std::pair<GURL, std::string> key(gurl, name); std::pair<GURL, std::string> key(gurl, name);
if (hook_map_.count(key) == 0) { if (hook_map_.count(key) == 0) {
UpdateCacheForCookieFromSystem(gurl, name, /*run_callbacks=*/false); UpdateCacheForCookieFromSystem(gurl, name, nullptr, nullptr);
hook_map_[key] = base::MakeUnique<CookieChangedCallbackList>(); if (hook_map_.count(key) == 0)
hook_map_[key] = base::MakeUnique<CookieChangedCallbackList>();
} }
DCHECK(hook_map_.find(key) != hook_map_.end()); DCHECK(hook_map_.find(key) != hook_map_.end());
...@@ -696,40 +710,16 @@ bool CookieStoreIOS::IsEphemeral() { ...@@ -696,40 +710,16 @@ bool CookieStoreIOS::IsEphemeral() {
return cookie_monster_->IsEphemeral(); return cookie_monster_->IsEphemeral();
} }
void CookieStoreIOS::UpdateCacheForCookieFromSystem( bool CookieStoreIOS::UpdateCacheForCookieFromSystem(
const GURL& gurl, const GURL& gurl,
const std::string& cookie_name, const std::string& name,
bool run_callbacks) { std::vector<net::CanonicalCookie>* out_removed_cookies,
std::vector<net::CanonicalCookie>* out_added_cookies) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
system_store_->GetCookiesForURLAsync( std::vector<net::CanonicalCookie> system_cookies;
gurl, base::BindOnce(&CookieStoreIOS::UpdateCacheForCookies, GetSystemCookies(gurl, name, &system_cookies);
weak_factory_.GetWeakPtr(), gurl, cookie_name, return cookie_cache_->Update(gurl, name, system_cookies, out_removed_cookies,
run_callbacks)); out_added_cookies);
}
void CookieStoreIOS::UpdateCacheForCookies(const GURL& gurl,
const std::string& cookie_name,
bool run_callbacks,
NSArray<NSHTTPCookie*>* nscookies) {
std::vector<net::CanonicalCookie> cookies;
std::vector<net::CanonicalCookie> out_removed_cookies;
std::vector<net::CanonicalCookie> out_added_cookies;
for (NSHTTPCookie* nscookie in nscookies) {
if (base::SysNSStringToUTF8(nscookie.name) == cookie_name) {
net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie(
nscookie, system_store_->GetCookieCreationTime(nscookie));
cookies.push_back(canonical_cookie);
}
}
bool changes = cookie_cache_->Update(
gurl, cookie_name, cookies, &out_removed_cookies, &out_added_cookies);
if (run_callbacks && changes) {
RunCallbacksForCookies(gurl, cookie_name, out_removed_cookies,
net::CookieStore::ChangeCause::UNKNOWN_DELETION);
RunCallbacksForCookies(gurl, cookie_name, out_added_cookies,
net::CookieStore::ChangeCause::INSERTED);
}
} }
void CookieStoreIOS::RunCallbacksForCookies( void CookieStoreIOS::RunCallbacksForCookies(
...@@ -749,6 +739,23 @@ void CookieStoreIOS::RunCallbacksForCookies( ...@@ -749,6 +739,23 @@ void CookieStoreIOS::RunCallbacksForCookies(
} }
} }
bool CookieStoreIOS::GetSystemCookies(
const GURL& url,
const std::string& name,
std::vector<net::CanonicalCookie>* cookies) {
DCHECK(thread_checker_.CalledOnValidThread());
NSArray* nscookies = system_store_->GetCookiesForURL(url);
bool found_cookies = false;
for (NSHTTPCookie* nscookie in nscookies) {
if (nscookie.name.UTF8String == name) {
net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie(
nscookie, creation_time_manager_->GetCreationTime(nscookie));
cookies->push_back(canonical_cookie);
found_cookies = true;
}
}
return found_cookies;
}
void CookieStoreIOS::GotCookieListFor(const std::pair<GURL, std::string> key, void CookieStoreIOS::GotCookieListFor(const std::pair<GURL, std::string> key,
const net::CookieList& cookies) { const net::CookieList& cookies) {
...@@ -767,6 +774,35 @@ void CookieStoreIOS::GotCookieListFor(const std::pair<GURL, std::string> key, ...@@ -767,6 +774,35 @@ void CookieStoreIOS::GotCookieListFor(const std::pair<GURL, std::string> key,
} }
} }
void CookieStoreIOS::DidClearNSHTTPCookieStorageCookies(
DeleteCallback delete_callback,
int num_deleted) {
DCHECK(thread_checker_.CalledOnValidThread());
CookieStoreIOSClient* client = net::GetCookieStoreIOSClient();
DCHECK(client);
auto sequenced_task_runner = client->GetTaskRunner();
DCHECK(sequenced_task_runner);
auto callback = base::BindOnce(
&CookieStoreIOS::DidClearBinaryCookiesFileCookies,
weak_factory_.GetWeakPtr(), std::move(delete_callback), num_deleted);
sequenced_task_runner.get()->PostTaskAndReply(
FROM_HERE, base::Bind(&ClearAllCookiesFromBinaryCookiesFile),
std::move(callback));
}
void CookieStoreIOS::DidClearBinaryCookiesFileCookies(
DeleteCallback callback,
int num_deleted_from_nshttp_cookie_storage) {
DCHECK(thread_checker_.CalledOnValidThread());
CookieStoreIOSClient* client = net::GetCookieStoreIOSClient();
DCHECK(client);
client->DidChangeCookieStorage();
if (!callback.is_null())
std::move(callback).Run(num_deleted_from_nshttp_cookie_storage);
}
void CookieStoreIOS::UpdateCachesFromCookieMonster() { void CookieStoreIOS::UpdateCachesFromCookieMonster() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
for (const auto& hook_map_entry : hook_map_) { for (const auto& hook_map_entry : hook_map_) {
...@@ -806,18 +842,10 @@ CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) { ...@@ -806,18 +842,10 @@ CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) {
net::CookieList cookie_list; net::CookieList cookie_list;
cookie_list.reserve([cookies count]); cookie_list.reserve([cookies count]);
for (NSHTTPCookie* cookie in cookies) { for (NSHTTPCookie* cookie in cookies) {
base::Time created = system_store_->GetCookieCreationTime(cookie); base::Time created = creation_time_manager_->GetCreationTime(cookie);
cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created));
} }
return cookie_list; return cookie_list;
} }
void CookieStoreIOS::RunGetCookieListCallbackOnSystemCookies(
CookieStoreIOS::GetCookieListCallback callback,
NSArray<NSHTTPCookie*>* cookies) {
if (!callback.is_null()) {
std::move(callback).Run(CanonicalCookieListFromSystemCookies(cookies));
}
}
} // namespace net } // namespace net
...@@ -24,6 +24,13 @@ class CookieStoreIOSClient { ...@@ -24,6 +24,13 @@ class CookieStoreIOSClient {
CookieStoreIOSClient(); CookieStoreIOSClient();
virtual ~CookieStoreIOSClient(); virtual ~CookieStoreIOSClient();
// Gives the embedder a chance to perform tasks before the cookie storage is
// changed.
virtual void WillChangeCookieStorage() const;
// Informs the embedder after the cookie storage has been changed.
virtual void DidChangeCookieStorage() const;
// Returns instance of SequencedTaskRunner used for blocking file I/O. // Returns instance of SequencedTaskRunner used for blocking file I/O.
virtual scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const; virtual scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const;
......
...@@ -27,6 +27,10 @@ CookieStoreIOSClient::CookieStoreIOSClient() {} ...@@ -27,6 +27,10 @@ CookieStoreIOSClient::CookieStoreIOSClient() {}
CookieStoreIOSClient::~CookieStoreIOSClient() {} CookieStoreIOSClient::~CookieStoreIOSClient() {}
void CookieStoreIOSClient::WillChangeCookieStorage() const {}
void CookieStoreIOSClient::DidChangeCookieStorage() const {}
scoped_refptr<base::SequencedTaskRunner> scoped_refptr<base::SequencedTaskRunner>
CookieStoreIOSClient::GetTaskRunner() const { CookieStoreIOSClient::GetTaskRunner() const {
return scoped_refptr<base::SequencedTaskRunner>(); return scoped_refptr<base::SequencedTaskRunner>();
......
...@@ -56,8 +56,6 @@ class CookieStoreIOSPersistentTest : public testing::Test { ...@@ -56,8 +56,6 @@ class CookieStoreIOSPersistentTest : public testing::Test {
public: public:
CookieStoreIOSPersistentTest() CookieStoreIOSPersistentTest()
: kTestCookieURL("http://foo.google.com/bar"), : kTestCookieURL("http://foo.google.com/bar"),
scoped_cookie_store_ios_client_(
base::MakeUnique<TestCookieStoreIOSClient>()),
backend_(new net::TestPersistentCookieStore), backend_(new net::TestPersistentCookieStore),
store_( store_(
base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get())) { base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get())) {
...@@ -87,7 +85,6 @@ class CookieStoreIOSPersistentTest : public testing::Test { ...@@ -87,7 +85,6 @@ class CookieStoreIOSPersistentTest : public testing::Test {
protected: protected:
base::MessageLoop loop_; base::MessageLoop loop_;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
scoped_refptr<net::TestPersistentCookieStore> backend_; scoped_refptr<net::TestPersistentCookieStore> backend_;
std::unique_ptr<net::CookieStoreIOS> store_; std::unique_ptr<net::CookieStoreIOS> store_;
std::unique_ptr<net::CookieStore::CookieChangedSubscription> std::unique_ptr<net::CookieStore::CookieChangedSubscription>
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include <vector> #include <vector>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/task_scheduler/post_task.h"
#include "ios/net/cookies/cookie_store_ios_client.h"
#include "net/cookies/cookie_monster.h" #include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_store.h" #include "net/cookies/cookie_store.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -68,26 +66,6 @@ class GetCookieCallback { ...@@ -68,26 +66,6 @@ class GetCookieCallback {
std::string cookie_line_; std::string cookie_line_;
}; };
class TestCookieStoreIOSClient : public CookieStoreIOSClient {
public:
TestCookieStoreIOSClient();
// CookieStoreIOSClient implementation.
scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const override;
};
class ScopedTestingCookieStoreIOSClient {
public:
explicit ScopedTestingCookieStoreIOSClient(
std::unique_ptr<CookieStoreIOSClient> cookie_store_client);
~ScopedTestingCookieStoreIOSClient();
CookieStoreIOSClient* Get();
private:
std::unique_ptr<CookieStoreIOSClient> cookie_store_client_;
CookieStoreIOSClient* original_client_;
};
void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies, void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies,
std::vector<bool>* out_removes, std::vector<bool>* out_removes,
const net::CanonicalCookie& cookie, const net::CanonicalCookie& cookie,
......
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#import "ios/net/cookies/cookie_store_ios.h" #import "ios/net/cookies/cookie_store_ios.h"
#include "net/cookies/canonical_cookie.h" #include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_options.h" #include "net/cookies/cookie_options.h"
...@@ -115,33 +113,6 @@ void GetCookieCallback::Run(const std::string& cookie_line) { ...@@ -115,33 +113,6 @@ void GetCookieCallback::Run(const std::string& cookie_line) {
cookie_line_ = cookie_line; cookie_line_ = cookie_line;
} }
#pragma mark -
#pragma mark TestCookieStoreIOSClient
TestCookieStoreIOSClient::TestCookieStoreIOSClient() {}
#pragma mark -
#pragma mark TestCookieStoreIOSClient methods
scoped_refptr<base::SequencedTaskRunner>
TestCookieStoreIOSClient::GetTaskRunner() const {
return base::ThreadTaskRunnerHandle::Get();
}
#pragma mark -
#pragma mark ScopedTestingCookieStoreIOSClient
ScopedTestingCookieStoreIOSClient::ScopedTestingCookieStoreIOSClient(
std::unique_ptr<CookieStoreIOSClient> cookie_store_client)
: cookie_store_client_(std::move(cookie_store_client)),
original_client_(GetCookieStoreIOSClient()) {
SetCookieStoreIOSClient(cookie_store_client_.get());
}
ScopedTestingCookieStoreIOSClient::~ScopedTestingCookieStoreIOSClient() {
SetCookieStoreIOSClient(original_client_);
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies, void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies,
......
...@@ -9,13 +9,11 @@ ...@@ -9,13 +9,11 @@
#include <memory> #include <memory>
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#import "base/mac/bind_objc_block.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "ios/net/cookies/cookie_store_ios_client.h"
#import "ios/net/cookies/cookie_store_ios_test_util.h" #import "ios/net/cookies/cookie_store_ios_test_util.h"
#import "ios/net/cookies/ns_http_system_cookie_store.h" #import "ios/net/cookies/ns_http_system_cookie_store.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
...@@ -28,21 +26,10 @@ ...@@ -28,21 +26,10 @@
namespace net { namespace net {
class TestingCookieStoreIOS : public CookieStoreIOS {
public:
TestingCookieStoreIOS(std::unique_ptr<SystemCookieStore> system_store)
: CookieStoreIOS(std::move(system_store)),
scoped_cookie_store_ios_client_(
base::MakeUnique<TestCookieStoreIOSClient>()) {}
private:
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
};
struct CookieStoreIOSTestTraits { struct CookieStoreIOSTestTraits {
static std::unique_ptr<net::CookieStore> Create() { static std::unique_ptr<net::CookieStore> Create() {
ClearCookies(); ClearCookies();
return base::MakeUnique<TestingCookieStoreIOS>( return base::MakeUnique<CookieStoreIOS>(
base::MakeUnique<NSHTTPSystemCookieStore>()); base::MakeUnique<NSHTTPSystemCookieStore>());
} }
...@@ -104,8 +91,6 @@ class CookieStoreIOSTest : public testing::Test { ...@@ -104,8 +91,6 @@ class CookieStoreIOSTest : public testing::Test {
kTestCookieURL2("http://foo.google.com/baz"), kTestCookieURL2("http://foo.google.com/baz"),
kTestCookieURL3("http://foo.google.com"), kTestCookieURL3("http://foo.google.com"),
kTestCookieURL4("http://bar.google.com/bar"), kTestCookieURL4("http://bar.google.com/bar"),
scoped_cookie_store_ios_client_(
base::MakeUnique<TestCookieStoreIOSClient>()),
backend_(new TestPersistentCookieStore) { backend_(new TestPersistentCookieStore) {
std::unique_ptr<NSHTTPSystemCookieStore> system_store( std::unique_ptr<NSHTTPSystemCookieStore> system_store(
base::MakeUnique<NSHTTPSystemCookieStore>()); base::MakeUnique<NSHTTPSystemCookieStore>());
...@@ -137,33 +122,25 @@ class CookieStoreIOSTest : public testing::Test { ...@@ -137,33 +122,25 @@ class CookieStoreIOSTest : public testing::Test {
void SetSystemCookie(const GURL& url, void SetSystemCookie(const GURL& url,
const std::string& name, const std::string& name,
const std::string& value) { const std::string& value) {
system_store_->SetCookieAsync( system_store_->SetCookie([NSHTTPCookie cookieWithProperties:@{
[NSHTTPCookie cookieWithProperties:@{ NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()),
NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()), NSHTTPCookieName : base::SysUTF8ToNSString(name),
NSHTTPCookieName : base::SysUTF8ToNSString(name), NSHTTPCookieValue : base::SysUTF8ToNSString(value),
NSHTTPCookieValue : base::SysUTF8ToNSString(value), NSHTTPCookieDomain : base::SysUTF8ToNSString(url.host()),
NSHTTPCookieDomain : base::SysUTF8ToNSString(url.host()), }]);
}], net::CookieStoreIOS::NotifySystemCookiesChanged();
base::BindOnce(&net::CookieStoreIOS::NotifySystemCookiesChanged));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
void DeleteSystemCookie(const GURL& gurl, const std::string& name) { void DeleteSystemCookie(const GURL& gurl, const std::string& name) {
base::WeakPtr<SystemCookieStore> weak_system_store = NSArray* cookies = system_store_->GetCookiesForURL(gurl);
system_store_->GetWeakPtr(); for (NSHTTPCookie* cookie in cookies) {
system_store_->GetCookiesForURLAsync( if (cookie.name.UTF8String == name) {
gurl, base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) { system_store_->DeleteCookie(cookie);
for (NSHTTPCookie* cookie in cookies) { break;
if ([[cookie name] isEqualToString:base::SysUTF8ToNSString(name)] && }
weak_system_store) { }
weak_system_store->DeleteCookieAsync( net::CookieStoreIOS::NotifySystemCookiesChanged();
cookie,
base::BindOnce(
&net::CookieStoreIOS::NotifySystemCookiesChanged));
break;
}
}
}));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
...@@ -174,7 +151,6 @@ class CookieStoreIOSTest : public testing::Test { ...@@ -174,7 +151,6 @@ class CookieStoreIOSTest : public testing::Test {
const GURL kTestCookieURL4; const GURL kTestCookieURL4;
base::MessageLoop loop_; base::MessageLoop loop_;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
scoped_refptr<TestPersistentCookieStore> backend_; scoped_refptr<TestPersistentCookieStore> backend_;
// |system_store_| will point to the NSHTTPSystemCookieStore object owned by // |system_store_| will point to the NSHTTPSystemCookieStore object owned by
// |store_|. Once the store_ object is deleted the NSHTTPSystemCookieStore // |store_|. Once the store_ object is deleted the NSHTTPSystemCookieStore
...@@ -233,9 +209,6 @@ TEST_F(CookieStoreIOSTest, SameValueDoesNotCallHook) { ...@@ -233,9 +209,6 @@ TEST_F(CookieStoreIOSTest, SameValueDoesNotCallHook) {
TEST(CookieStoreIOS, GetAllCookiesForURLAsync) { TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
base::MessageLoop loop; base::MessageLoop loop;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client(
base::MakeUnique<TestCookieStoreIOSClient>());
const GURL kTestCookieURL("http://foo.google.com/bar"); const GURL kTestCookieURL("http://foo.google.com/bar");
ClearCookies(); ClearCookies();
std::unique_ptr<CookieStoreIOS> cookie_store(base::MakeUnique<CookieStoreIOS>( std::unique_ptr<CookieStoreIOS> cookie_store(base::MakeUnique<CookieStoreIOS>(
...@@ -251,7 +224,6 @@ TEST(CookieStoreIOS, GetAllCookiesForURLAsync) { ...@@ -251,7 +224,6 @@ TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
cookie_store->GetAllCookiesForURLAsync( cookie_store->GetAllCookiesForURLAsync(
kTestCookieURL, kTestCookieURL,
base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback.did_run()); EXPECT_TRUE(callback.did_run());
EXPECT_EQ(1u, callback.cookie_list().size()); EXPECT_EQ(1u, callback.cookie_list().size());
net::CanonicalCookie cookie = callback.cookie_list()[0]; net::CanonicalCookie cookie = callback.cookie_list()[0];
......
...@@ -18,54 +18,17 @@ class NSHTTPSystemCookieStore : public net::SystemCookieStore { ...@@ -18,54 +18,17 @@ class NSHTTPSystemCookieStore : public net::SystemCookieStore {
// By default the underlying cookiestore is // By default the underlying cookiestore is
// |NSHTTPCookieStorage sharedHTTPCookieStorage| // |NSHTTPCookieStorage sharedHTTPCookieStorage|
NSHTTPSystemCookieStore(); NSHTTPSystemCookieStore();
explicit NSHTTPSystemCookieStore(NSHTTPCookieStorage* cookie_store); explicit NSHTTPSystemCookieStore(NSHTTPCookieStorage* cookie_store);
~NSHTTPSystemCookieStore() override; ~NSHTTPSystemCookieStore() override;
NSArray* GetCookiesForURL(const GURL& url,
// Gets cookies for URL and calls |callback| async on these cookies. CookieCreationTimeManager* manager) override;
void GetCookiesForURLAsync(const GURL& url, NSArray* GetAllCookies(CookieCreationTimeManager* manager) override;
SystemCookieCallbackForCookies callback) override; void DeleteCookie(NSHTTPCookie* cookie) override;
void SetCookie(NSHTTPCookie* cookie) override;
// Gets all cookies and calls |callback| async on these cookies. void ClearStore() override;
void GetAllCookiesAsync(SystemCookieCallbackForCookies callback) override;
// Deletes specific cookie and calls |callback| async after that.
void DeleteCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) override;
// Sets cookie, and calls |callback| async after that.
void SetCookieAsync(NSHTTPCookie* cookie,
const base::Time* optional_creation_time,
SystemCookieCallback callback) override;
// Clears all cookies from the store and call |callback| after all cookies are
// deleted.
void ClearStoreAsync(SystemCookieCallback callback) override;
NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() override; NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() override;
private: private:
// Returns all cookies for a specific |url| from the internal cookie store.
// Cookies are sorted, as per RFC6265.
NSArray* GetCookiesForURL(const GURL& url);
// Returns all cookies from the internal http cookie store.
// Cookies are sorted, as per RFC6265.
NSArray* GetAllCookies();
// Deletes a specific cookie from the internal http cookie store.
void DeleteCookie(NSHTTPCookie* cookie);
// Sets a specific cookie to the internal http cookie store.
// if the |optional_creation_time| is nullptr, uses Time::Now() as the
// creation time.
void SetCookie(NSHTTPCookie* cookie,
const base::Time* optional_creation_time);
// Clears all cookies from the internal cookie store.
void ClearStore();
NSHTTPCookieStorage* cookie_store_; NSHTTPCookieStorage* cookie_store_;
DISALLOW_COPY_AND_ASSIGN(NSHTTPSystemCookieStore); DISALLOW_COPY_AND_ASSIGN(NSHTTPSystemCookieStore);
......
...@@ -4,10 +4,7 @@ ...@@ -4,10 +4,7 @@
#import "ios/net/cookies/ns_http_system_cookie_store.h" #import "ios/net/cookies/ns_http_system_cookie_store.h"
#include "base/bind.h"
#include "base/time/time.h"
#import "ios/net/cookies/cookie_creation_time_manager.h" #import "ios/net/cookies/cookie_creation_time_manager.h"
#import "ios/net/cookies/cookie_store_ios_client.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -17,15 +14,6 @@ ...@@ -17,15 +14,6 @@
namespace net { namespace net {
// private
void RunCookieCallback(base::OnceClosure callback) {
if (callback.is_null())
return;
CookieStoreIOSClient* client = net::GetCookieStoreIOSClient();
auto sequenced_task_runner = client->GetTaskRunner();
sequenced_task_runner->PostTask(FROM_HERE, std::move(callback));
}
NSHTTPSystemCookieStore::NSHTTPSystemCookieStore() NSHTTPSystemCookieStore::NSHTTPSystemCookieStore()
: cookie_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]) {} : cookie_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]) {}
...@@ -38,71 +26,33 @@ NSHTTPSystemCookieStore::~NSHTTPSystemCookieStore() = default; ...@@ -38,71 +26,33 @@ NSHTTPSystemCookieStore::~NSHTTPSystemCookieStore() = default;
#pragma mark - #pragma mark -
#pragma mark SystemCookieStore methods #pragma mark SystemCookieStore methods
void NSHTTPSystemCookieStore::GetCookiesForURLAsync( NSArray* NSHTTPSystemCookieStore::GetCookiesForURL(
const GURL& url, const GURL& url,
SystemCookieCallbackForCookies callback) { CookieCreationTimeManager* manager) {
NSArray* cookies = GetCookiesForURL(url);
RunCookieCallback(base::BindOnce(std::move(callback), cookies));
}
void NSHTTPSystemCookieStore::GetAllCookiesAsync(
SystemCookieCallbackForCookies callback) {
NSArray* cookies = GetAllCookies();
RunCookieCallback(base::BindOnce(std::move(callback), cookies));
}
void NSHTTPSystemCookieStore::DeleteCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) {
DeleteCookie(cookie);
RunCookieCallback(std::move(callback));
}
void NSHTTPSystemCookieStore::SetCookieAsync(
NSHTTPCookie* cookie,
const base::Time* optional_creation_time,
SystemCookieCallback callback) {
SetCookie(cookie, optional_creation_time);
RunCookieCallback(std::move(callback));
}
void NSHTTPSystemCookieStore::ClearStoreAsync(SystemCookieCallback callback) {
ClearStore();
RunCookieCallback(std::move(callback));
}
NSHTTPCookieAcceptPolicy NSHTTPSystemCookieStore::GetCookieAcceptPolicy() {
return [cookie_store_ cookieAcceptPolicy];
}
#pragma mark private methods
NSArray* NSHTTPSystemCookieStore::GetCookiesForURL(const GURL& url) {
NSArray* cookies = [cookie_store_ cookiesForURL:NSURLWithGURL(url)]; NSArray* cookies = [cookie_store_ cookiesForURL:NSURLWithGURL(url)];
if (!manager)
return cookies;
// Sort cookies by decreasing path length, then creation time, as per // Sort cookies by decreasing path length, then creation time, as per
// RFC6265. // RFC6265.
return [cookies sortedArrayUsingFunction:CompareCookies return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
context:creation_time_manager_.get()];
} }
NSArray* NSHTTPSystemCookieStore::GetAllCookies() { NSArray* NSHTTPSystemCookieStore::GetAllCookies(
CookieCreationTimeManager* manager) {
NSArray* cookies = cookie_store_.cookies; NSArray* cookies = cookie_store_.cookies;
return [cookies sortedArrayUsingFunction:CompareCookies if (!manager)
context:creation_time_manager_.get()]; return cookies;
// Sort cookies by decreasing path length, then creation time, as per
// RFC6265.
return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
} }
void NSHTTPSystemCookieStore::DeleteCookie(NSHTTPCookie* cookie) { void NSHTTPSystemCookieStore::DeleteCookie(NSHTTPCookie* cookie) {
[cookie_store_ deleteCookie:cookie]; [cookie_store_ deleteCookie:cookie];
creation_time_manager_->DeleteCreationTime(cookie);
} }
void NSHTTPSystemCookieStore::SetCookie( void NSHTTPSystemCookieStore::SetCookie(NSHTTPCookie* cookie) {
NSHTTPCookie* cookie,
const base::Time* optional_creation_time) {
[cookie_store_ setCookie:cookie]; [cookie_store_ setCookie:cookie];
base::Time cookie_time = base::Time::Now();
if (optional_creation_time && !optional_creation_time->is_null())
cookie_time = *optional_creation_time;
creation_time_manager_->SetCreationTime(
cookie, creation_time_manager_->MakeUniqueCreationTime(cookie_time));
} }
void NSHTTPSystemCookieStore::ClearStore() { void NSHTTPSystemCookieStore::ClearStore() {
...@@ -110,6 +60,10 @@ void NSHTTPSystemCookieStore::ClearStore() { ...@@ -110,6 +60,10 @@ void NSHTTPSystemCookieStore::ClearStore() {
for (NSHTTPCookie* cookie in copy) for (NSHTTPCookie* cookie in copy)
[cookie_store_ deleteCookie:cookie]; [cookie_store_ deleteCookie:cookie];
DCHECK_EQ(0u, cookie_store_.cookies.count); DCHECK_EQ(0u, cookie_store_.cookies.count);
creation_time_manager_->Clear();
} }
NSHTTPCookieAcceptPolicy NSHTTPSystemCookieStore::GetCookieAcceptPolicy() {
return [cookie_store_ cookieAcceptPolicy];
}
} // namespace net } // namespace net
...@@ -8,13 +8,7 @@ ...@@ -8,13 +8,7 @@
#include <memory> #include <memory>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#import "ios/net/cookies/cookie_store_ios_test_util.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -26,45 +20,12 @@ ...@@ -26,45 +20,12 @@
namespace net { namespace net {
namespace {
// Helper callbacks to be passed to SetCookieAsync/GetCookiesAsync.
class SystemCookiesCallback {
public:
SystemCookiesCallback()
: did_run_with_cookies_(false), did_run_with_no_cookies_(false) {}
// Returns if the callback has been run.
bool did_run_with_cookies() { return did_run_with_cookies_; }
bool did_run_with_no_cookies() { return did_run_with_no_cookies_; }
// Returns the paremeter of the callback.
NSArray<NSHTTPCookie*>* cookies() { return cookies_; }
void RunWithCookies(NSArray<NSHTTPCookie*>* cookies) {
ASSERT_FALSE(did_run_with_cookies_);
did_run_with_cookies_ = true;
cookies_ = cookies;
}
void RunWithNoCookies() {
ASSERT_FALSE(did_run_with_no_cookies_);
did_run_with_no_cookies_ = true;
}
private:
bool did_run_with_cookies_;
bool did_run_with_no_cookies_;
NSArray<NSHTTPCookie*>* cookies_;
};
} // namespace
// Test fixture to exercise net::NSHTTPSystemCookieStore created with // Test fixture to exercise net::NSHTTPSystemCookieStore created with
// |[NSHTTPCookieStorage sharedHTTPCookieStorage]|. // |[NSHTTPCookieStorage sharedHTTPCookieStorage]|.
class NSHTTPSystemCookieStoreTest : public PlatformTest { class NSHTTPSystemCookieStoreTest : public PlatformTest {
public: public:
NSHTTPSystemCookieStoreTest() NSHTTPSystemCookieStoreTest()
: scoped_cookie_store_ios_client_( : shared_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]),
base::MakeUnique<TestCookieStoreIOSClient>()),
shared_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]),
store_(base::MakeUnique<net::NSHTTPSystemCookieStore>(shared_store_)), store_(base::MakeUnique<net::NSHTTPSystemCookieStore>(shared_store_)),
test_cookie_url1_([NSURL URLWithString:@"http://foo.google.com/bar"]), test_cookie_url1_([NSURL URLWithString:@"http://foo.google.com/bar"]),
test_cookie_url2_([NSURL URLWithString:@"http://bar.xyz.abc"]), test_cookie_url2_([NSURL URLWithString:@"http://bar.xyz.abc"]),
...@@ -85,22 +46,7 @@ class NSHTTPSystemCookieStoreTest : public PlatformTest { ...@@ -85,22 +46,7 @@ class NSHTTPSystemCookieStoreTest : public PlatformTest {
forURL:url] objectAtIndex:0]; forURL:url] objectAtIndex:0];
} }
bool IsCookieSet(NSHTTPCookie* system_cookie, NSURL* url) {
// Verify that cookie is set in system storage.
NSHTTPCookie* result_cookie = nil;
for (NSHTTPCookie* cookie in [shared_store_ cookiesForURL:url]) {
if ([cookie.name isEqualToString:system_cookie.name]) {
result_cookie = cookie;
break;
}
}
return [result_cookie.value isEqualToString:system_cookie.value];
}
protected: protected:
base::MessageLoop loop;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
NSHTTPCookieStorage* shared_store_; NSHTTPCookieStorage* shared_store_;
std::unique_ptr<net::NSHTTPSystemCookieStore> store_; std::unique_ptr<net::NSHTTPSystemCookieStore> store_;
NSURL* test_cookie_url1_; NSURL* test_cookie_url1_;
...@@ -108,126 +54,105 @@ class NSHTTPSystemCookieStoreTest : public PlatformTest { ...@@ -108,126 +54,105 @@ class NSHTTPSystemCookieStoreTest : public PlatformTest {
NSURL* test_cookie_url3_; NSURL* test_cookie_url3_;
}; };
TEST_F(NSHTTPSystemCookieStoreTest, GetCookieAcceptPolicy) { TEST_F(NSHTTPSystemCookieStoreTest, SetCookie) {
EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy()); NSHTTPCookie* system_cookie = CreateCookie(@"a=b", test_cookie_url1_);
shared_store_.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever; // Verify that cookie is not set in system storage.
EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy()); for (NSHTTPCookie* cookie in
shared_store_.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways; [shared_store_ cookiesForURL:test_cookie_url1_]) {
EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy()); EXPECT_FALSE([cookie.name isEqualToString:system_cookie.name]);
}
store_->SetCookie(system_cookie);
// Verify that cookie is set in system storage.
NSHTTPCookie* result_cookie = nil;
for (NSHTTPCookie* cookie in
[shared_store_ cookiesForURL:test_cookie_url1_]) {
if ([cookie.name isEqualToString:system_cookie.name]) {
result_cookie = cookie;
break;
}
}
EXPECT_TRUE([result_cookie.value isEqualToString:system_cookie.value]);
} }
TEST_F(NSHTTPSystemCookieStoreTest, GetCookiesAsync) { TEST_F(NSHTTPSystemCookieStoreTest, ClearCookies) {
[shared_store_ setCookie:CreateCookie(@"a=b", test_cookie_url1_)];
[shared_store_ setCookie:CreateCookie(@"x=d", test_cookie_url2_)];
EXPECT_EQ(2u, shared_store_.cookies.count);
store_->ClearStore();
EXPECT_EQ(0u, shared_store_.cookies.count);
}
TEST_F(NSHTTPSystemCookieStoreTest, GetCookies) {
NSMutableDictionary* input_cookies = [[NSMutableDictionary alloc] init]; NSMutableDictionary* input_cookies = [[NSMutableDictionary alloc] init];
NSHTTPCookie* system_cookie = CreateCookie(@"a=b", test_cookie_url1_); NSHTTPCookie* system_cookie = CreateCookie(@"a=b", test_cookie_url1_);
[input_cookies setValue:system_cookie forKey:@"a"]; [input_cookies setValue:system_cookie forKey:@"a"];
store_->SetCookieAsync(system_cookie, /*optional_creation_time=*/nullptr, [shared_store_ setCookie:system_cookie];
SystemCookieStore::SystemCookieCallback());
system_cookie = CreateCookie(@"x=d", test_cookie_url2_); system_cookie = CreateCookie(@"x=d", test_cookie_url2_);
[input_cookies setValue:system_cookie forKey:@"x"]; [input_cookies setValue:system_cookie forKey:@"x"];
store_->SetCookieAsync(system_cookie, /*optional_creation_time=*/nullptr, [shared_store_ setCookie:system_cookie];
SystemCookieStore::SystemCookieCallback());
system_cookie = CreateCookie(@"l=m", test_cookie_url3_); system_cookie = CreateCookie(@"l=m", test_cookie_url3_);
[input_cookies setValue:system_cookie forKey:@"l"]; [input_cookies setValue:system_cookie forKey:@"l"];
store_->SetCookieAsync(system_cookie, /*optional_creation_time=*/nullptr, [shared_store_ setCookie:system_cookie];
SystemCookieStore::SystemCookieCallback());
base::RunLoop().RunUntilIdle();
// Test GetCookieForURLAsync. EXPECT_EQ(3u, shared_store_.cookies.count);
NSHTTPCookie* input_cookie = [input_cookies valueForKey:@"a"];
SystemCookiesCallback callback_for_url;
store_->GetCookiesForURLAsync(
GURLWithNSURL(test_cookie_url1_),
base::BindOnce(&SystemCookiesCallback::RunWithCookies,
base::Unretained(&callback_for_url)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_for_url.did_run_with_cookies());
EXPECT_EQ(1u, callback_for_url.cookies().count);
NSHTTPCookie* result_cookie = callback_for_url.cookies()[0];
EXPECT_TRUE([input_cookie.name isEqualToString:result_cookie.name]);
EXPECT_TRUE([input_cookie.value isEqualToString:result_cookie.value]);
// Test GetAllCookies // Test GetAllCookies
SystemCookiesCallback callback_all_cookies; NSArray* cookies = store_->GetAllCookies(/*manager=*/nullptr);
store_->GetAllCookiesAsync( EXPECT_EQ(3u, cookies.count);
base::BindOnce(&SystemCookiesCallback::RunWithCookies, for (NSHTTPCookie* cookie in cookies) {
base::Unretained(&callback_all_cookies)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_all_cookies.did_run_with_cookies());
NSArray<NSHTTPCookie*>* result_cookies = callback_all_cookies.cookies();
EXPECT_EQ(3u, result_cookies.count);
for (NSHTTPCookie* cookie in result_cookies) {
NSHTTPCookie* existing_cookie = [input_cookies valueForKey:cookie.name]; NSHTTPCookie* existing_cookie = [input_cookies valueForKey:cookie.name];
EXPECT_TRUE(existing_cookie); EXPECT_TRUE(existing_cookie);
EXPECT_TRUE([existing_cookie.name isEqualToString:cookie.name]); EXPECT_TRUE([existing_cookie.name isEqualToString:cookie.name]);
EXPECT_TRUE([existing_cookie.value isEqualToString:cookie.value]); EXPECT_TRUE([existing_cookie.value isEqualToString:cookie.value]);
EXPECT_TRUE([existing_cookie.domain isEqualToString:cookie.domain]); EXPECT_TRUE([existing_cookie.domain isEqualToString:cookie.domain]);
} }
}
TEST_F(NSHTTPSystemCookieStoreTest, SetCookieAsync) { // Test GetCookiesForURL
NSHTTPCookie* system_cookie = CreateCookie(@"a=b", test_cookie_url1_); NSHTTPCookie* input_cookie = [input_cookies valueForKey:@"a"];
SystemCookiesCallback callback; NSArray* cookies_for_url = store_->GetCookiesForURL(
store_->SetCookieAsync( GURLWithNSURL(test_cookie_url1_), /*manager=*/nullptr);
system_cookie, /*optional_creation_time=*/nullptr, EXPECT_EQ(1u, cookies_for_url.count);
base::BindOnce(&SystemCookiesCallback::RunWithNoCookies, NSHTTPCookie* output_cookie = [cookies_for_url objectAtIndex:0];
base::Unretained(&callback))); EXPECT_TRUE([input_cookie.name isEqualToString:output_cookie.name]);
base::RunLoop().RunUntilIdle(); EXPECT_TRUE([input_cookie.value isEqualToString:output_cookie.value]);
// verify callback. EXPECT_TRUE([input_cookie.domain isEqualToString:output_cookie.domain]);
EXPECT_TRUE(callback.did_run_with_no_cookies());
EXPECT_TRUE(IsCookieSet(system_cookie, test_cookie_url1_));
} }
TEST_F(NSHTTPSystemCookieStoreTest, DeleteCookiesAsync) { TEST_F(NSHTTPSystemCookieStoreTest, DeleteCookies) {
NSHTTPCookie* system_cookie1 = CreateCookie(@"a=b", test_cookie_url1_); NSHTTPCookie* system_cookie1 = CreateCookie(@"a=b", test_cookie_url1_);
store_->SetCookieAsync(system_cookie1, /*optional_creation_time=*/nullptr, [shared_store_ setCookie:system_cookie1];
SystemCookieStore::SystemCookieCallback());
NSHTTPCookie* system_cookie2 = CreateCookie(@"x=d", test_cookie_url2_); NSHTTPCookie* system_cookie2 = CreateCookie(@"x=d", test_cookie_url2_);
store_->SetCookieAsync(system_cookie2, /*optional_creation_time=*/nullptr, store_->SetCookie(system_cookie2);
SystemCookieStore::SystemCookieCallback());
NSHTTPCookie* system_cookie3 = CreateCookie(@"l=m", test_cookie_url3_); NSHTTPCookie* system_cookie3 = CreateCookie(@"l=m", test_cookie_url3_);
store_->SetCookieAsync(system_cookie3, /*optional_creation_time=*/nullptr, [shared_store_ setCookie:system_cookie3];
SystemCookieStore::SystemCookieCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3u, shared_store_.cookies.count); EXPECT_EQ(3u, shared_store_.cookies.count);
SystemCookiesCallback callback; store_->DeleteCookie(system_cookie2);
EXPECT_EQ(1u, [shared_store_ cookiesForURL:test_cookie_url2_].count);
store_->DeleteCookieAsync(
system_cookie2, base::BindOnce(&SystemCookiesCallback::RunWithNoCookies,
base::Unretained(&callback)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback.did_run_with_no_cookies());
EXPECT_EQ(0u, [shared_store_ cookiesForURL:test_cookie_url2_].count); EXPECT_EQ(0u, [shared_store_ cookiesForURL:test_cookie_url2_].count);
EXPECT_EQ(2u, shared_store_.cookies.count); EXPECT_EQ(2u, shared_store_.cookies.count);
store_->DeleteCookieAsync(system_cookie1, store_->DeleteCookie(system_cookie1);
SystemCookieStore::SystemCookieCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, [shared_store_ cookiesForURL:test_cookie_url1_].count); EXPECT_EQ(0u, [shared_store_ cookiesForURL:test_cookie_url1_].count);
EXPECT_EQ(1u, shared_store_.cookies.count); EXPECT_EQ(1u, shared_store_.cookies.count);
store_->DeleteCookieAsync(system_cookie3, store_->DeleteCookie(system_cookie3);
SystemCookieStore::SystemCookieCallback());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, shared_store_.cookies.count); EXPECT_EQ(0u, shared_store_.cookies.count);
} }
TEST_F(NSHTTPSystemCookieStoreTest, ClearCookiesAsync) { TEST_F(NSHTTPSystemCookieStoreTest, GetCookieAcceptPolicy) {
store_->SetCookieAsync(CreateCookie(@"a=b", test_cookie_url1_), EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy());
/*optional_creation_time=*/nullptr, shared_store_.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
SystemCookieStore::SystemCookieCallback()); EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy());
store_->SetCookieAsync(CreateCookie(@"x=d", test_cookie_url2_), shared_store_.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
/*optional_creation_time=*/nullptr, EXPECT_EQ(shared_store_.cookieAcceptPolicy, store_->GetCookieAcceptPolicy());
SystemCookieStore::SystemCookieCallback());
SystemCookiesCallback callback;
EXPECT_EQ(2u, shared_store_.cookies.count);
store_->ClearStoreAsync(base::BindOnce(
&SystemCookiesCallback::RunWithNoCookies, base::Unretained(&callback)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback.did_run_with_no_cookies());
EXPECT_EQ(0u, shared_store_.cookies.count);
} }
} // namespace net } // namespace net
...@@ -7,10 +7,7 @@ ...@@ -7,10 +7,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
class GURL; class GURL;
...@@ -24,67 +21,39 @@ class CookieCreationTimeManager; ...@@ -24,67 +21,39 @@ class CookieCreationTimeManager;
// it directly without caring about the type of the underlying cookie store. // it directly without caring about the type of the underlying cookie store.
class SystemCookieStore { class SystemCookieStore {
public: public:
// Callback definitions.
typedef base::OnceClosure SystemCookieCallback;
typedef base::OnceCallback<void(NSArray<NSHTTPCookie*>*)>
SystemCookieCallbackForCookies;
SystemCookieStore();
virtual ~SystemCookieStore(); virtual ~SystemCookieStore();
// Calls |callback| on all cookies for a specific |url| in the internal // Returns cookies for specific URL without sorting.
// cookie store. NSArray* GetCookiesForURL(const GURL& url);
// If CookieCreationTimeManager was provided in the constructor, sort cookies
// as per RFC6265 before calling the |callback|.
virtual void GetCookiesForURLAsync(
const GURL& url,
SystemCookieCallbackForCookies callback) = 0;
// Calls |callback| on all cookies in the internal cookie store.
// If CookieCreationTimeManager was provided in the constructor, sort cookies
// as per RFC6265 before calling the |callback|.
virtual void GetAllCookiesAsync(SystemCookieCallbackForCookies callback) = 0;
// Deletes a specific cookie from the internal cookie store, and call
// |callback| after it's deleted.
virtual void DeleteCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) = 0;
// Sets a specific cookie to the internal cookie store, sets the cookie
// creation time |optional_creation_time| or to the current time if
// |optional_creation_time| is nil, then calls |callback| after it's set.
virtual void SetCookieAsync(NSHTTPCookie* cookie,
const base::Time* optional_creation_time,
SystemCookieCallback callback) = 0;
// Same as SetCookieAsync but uses actual time of setting the cookie.
void SetCookieAsync(NSHTTPCookie* cookie, SystemCookieCallback callback);
// Deletes all cookies from the internal http cookie store, and calls
// |callback| all cookies are deleted.
virtual void ClearStoreAsync(SystemCookieCallback callback) = 0;
// Returns the Cookie Accept policy for the internal cookie store. // Returns all cookies for a specific |url| from the internal cookie store.
virtual NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() = 0; // If |manager| is provided, use it to sort cookies, as per RFC6265.
virtual NSArray* GetCookiesForURL(const GURL& url,
CookieCreationTimeManager* manager) = 0;
// Returns all cookies from the internal http cookie store without sorting.
NSArray* GetAllCookies();
// Returns all cookies from the internal http cookie store.
// If |manager| is provided, use it to sort cookies, as per RFC6265.
virtual NSArray* GetAllCookies(CookieCreationTimeManager* manager) = 0;
// Delete a specific cookie from the internal http cookie store.
virtual void DeleteCookie(NSHTTPCookie* cookie) = 0;
// Returns the creation time of a specific cookie // Set a specific cookie to the internal http cookie store.
base::Time GetCookieCreationTime(NSHTTPCookie* cookie); virtual void SetCookie(NSHTTPCookie* cookie) = 0;
// Return WeakPtr of this object. // Delete all cookies from the internal http cookie store.
base::WeakPtr<SystemCookieStore> GetWeakPtr(); virtual void ClearStore() = 0;
// Returns the Cookie Accept policy for the internal cookie store.
virtual NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() = 0;
protected: protected:
// Compares cookies based on the path lengths and the creation times, as per // Compares cookies based on the path lengths and the creation times, as per
// RFC6265. // RFC6265.
static NSInteger CompareCookies(id a, id b, void* context); static NSInteger CompareCookies(id a, id b, void* context);
// Internal cookie stores doesn't store creation time. This object is used
// to keep track of the creation time of cookies, this is required for
// conversion between SystemCookie and Chromium CookieMonster.
std::unique_ptr<CookieCreationTimeManager> creation_time_manager_;
// Weak Ptr factory.
base::WeakPtrFactory<SystemCookieStore> weak_factory_;
}; };
} // namespace net } // namespace net
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#import "ios/net/cookies/system_cookie_store.h" #import "ios/net/cookies/system_cookie_store.h"
#include "base/memory/ptr_util.h" #include "base/time/time.h"
#import "ios/net/cookies/cookie_creation_time_manager.h" #import "ios/net/cookies/cookie_creation_time_manager.h"
#include "ios/net/ios_net_features.h" #include "ios/net/ios_net_features.h"
...@@ -16,22 +16,12 @@ namespace net { ...@@ -16,22 +16,12 @@ namespace net {
SystemCookieStore::~SystemCookieStore() = default; SystemCookieStore::~SystemCookieStore() = default;
SystemCookieStore::SystemCookieStore() NSArray* SystemCookieStore::GetCookiesForURL(const GURL& url) {
: creation_time_manager_(base::MakeUnique<CookieCreationTimeManager>()), return GetCookiesForURL(url, /* manager = */ nullptr);
weak_factory_(this) {}
void SystemCookieStore::SetCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) {
SetCookieAsync(cookie, /*optional_creation_time=*/nullptr,
std::move(callback));
}
base::Time SystemCookieStore::GetCookieCreationTime(NSHTTPCookie* cookie) {
return creation_time_manager_->GetCreationTime(cookie);
} }
base::WeakPtr<SystemCookieStore> SystemCookieStore::GetWeakPtr() { NSArray* SystemCookieStore::GetAllCookies() {
return weak_factory_.GetWeakPtr(); return GetAllCookies(/* manager = */ nullptr);
} }
// protected static // protected static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment