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_;
......
This diff is collapsed.
...@@ -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()),
}], }]);
base::BindOnce(&net::CookieStoreIOS::NotifySystemCookiesChanged)); 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();
system_store_->GetCookiesForURLAsync(
gurl, base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) {
for (NSHTTPCookie* cookie in cookies) { for (NSHTTPCookie* cookie in cookies) {
if ([[cookie name] isEqualToString:base::SysUTF8ToNSString(name)] && if (cookie.name.UTF8String == name) {
weak_system_store) { system_store_->DeleteCookie(cookie);
weak_system_store->DeleteCookieAsync(
cookie,
base::BindOnce(
&net::CookieStoreIOS::NotifySystemCookiesChanged));
break; break;
} }
} }
})); net::CookieStoreIOS::NotifySystemCookiesChanged();
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
...@@ -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