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") {
"//ios/chrome/browser:browser_internal",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/first_run",
"//ios/chrome/browser/net:net",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/web",
"//ios/chrome/browser/web:web_internal",
......
......@@ -4,9 +4,7 @@
#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"
#include "ios/net/cookies/cookie_store_ios_client.h"
#import "ios/web/public/web_client.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -17,9 +15,6 @@
+ (void)registerClients {
web::SetWebClient(new ChromeWebClient());
// Register CookieStoreIOSClient, This is used to provide CookieStoreIOSClient
// users with WEB::IO task runner.
net::SetCookieStoreIOSClient(new ChromeCookieStoreIOSClient());
}
@end
......@@ -7,18 +7,28 @@
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/thread_checker.h"
#include "ios/net/cookies/cookie_store_ios_client.h"
@protocol BrowsingDataChangeListening;
// Chrome implementation of net::CookieStoreIOSClient. This class lives on the
// IOThread.
class ChromeCookieStoreIOSClient : public net::CookieStoreIOSClient {
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.
void DidChangeCookieStorage() const override;
scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const override;
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);
};
......
......@@ -4,15 +4,31 @@
#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"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#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>
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 @@
namespace net {
class CookieCreationTimeManager;
// Observer for changes on |NSHTTPCookieStorge sharedHTTPCookieStorage|.
class CookieNotificationObserver {
public:
......@@ -156,6 +158,8 @@ class CookieStoreIOS : public net::CookieStore,
// deleted.
typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction;
// Clears the system cookie store.
void ClearSystemStore();
// Returns true if the system cookie store policy is
// |NSHTTPCookieAcceptPolicyAlways|.
bool SystemCookiesAllowed();
......@@ -165,15 +169,12 @@ class CookieStoreIOS : public net::CookieStore,
// Inherited CookieNotificationObserver methods.
void OnSystemCookiesChanged() override;
void DeleteCookiesWithFilterAsync(CookieFilterFunction filter,
DeleteCallback callback);
// Flush to CookieMonster from |cookies|, and run |callback|.
void FlushStoreFromCookies(base::OnceClosure callback,
NSArray<NSHTTPCookie*>* cookies);
void DeleteCookiesWithFilter(const CookieFilterFunction& filter,
DeleteCallback callback);
std::unique_ptr<net::CookieMonster> cookie_monster_;
std::unique_ptr<SystemCookieStore> system_store_;
std::unique_ptr<CookieCreationTimeManager> creation_time_manager_;
bool metrics_enabled_;
base::CancelableClosure flush_closure_;
......@@ -188,22 +189,26 @@ class CookieStoreIOS : public net::CookieStore,
// the CookieStoreIOS is synchronized and the CookieStore when the
// CookieStoreIOS is not synchronized.
// Updates the cookie cache with cookies named |cookie_name| from the current
// set of |nscookies| that would be sent with a request for |url|.
// |run_callbacks| Run all callbacks registered for cookie named |name| if
// CookieCache was changed.
void UpdateCacheForCookies(const GURL& gurl,
const std::string& cookie_name,
bool run_callbacks,
NSArray<NSHTTPCookie*>* nscookies);
// Fetches any cookies named |name| that would be sent with a request for
// |url| from the system cookie store and pushes them onto the back of the
// vector pointed to by |cookies|. Returns true if any cookies were pushed
// onto the vector, and false otherwise.
bool GetSystemCookies(const GURL& url,
const std::string& name,
std::vector<net::CanonicalCookie>* cookies);
// Updates the cookie cache with the current set of system cookies named
// |cookie_name| that would be sent with a request for |url|.
// |run_callbacks| Run all callbacks registered for cookie named |name| if
// CookieCache was changed.
void UpdateCacheForCookieFromSystem(const GURL& gurl,
const std::string& cookie_name,
bool run_callbacks);
// |name| that would be sent with a request for |url|. Returns whether the
// cache changed.
// |out_removed_cookies|, if not null, will be populated with the cookies that
// were removed.
// |out_changed_cookies|, if not null, will be populated with the cookies that
// 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
// with a request for |url|.
......@@ -223,6 +228,22 @@ class CookieStoreIOS : public net::CookieStore,
// asynchronously invoking callbacks if necessary.
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:
// When this CookieStoreIOS object is synchronized with the system store,
// OnSystemCookiesChanged is responsible for updating the cookie cache (and
......@@ -250,10 +271,6 @@ class CookieStoreIOS : public net::CookieStore,
// creation date.
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
// with AddCallbackForCookie are kept in this cache.
std::unique_ptr<CookieCache> cookie_cache_;
......
This diff is collapsed.
......@@ -24,6 +24,13 @@ class CookieStoreIOSClient {
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.
virtual scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() const;
......
......@@ -27,6 +27,10 @@ CookieStoreIOSClient::CookieStoreIOSClient() {}
CookieStoreIOSClient::~CookieStoreIOSClient() {}
void CookieStoreIOSClient::WillChangeCookieStorage() const {}
void CookieStoreIOSClient::DidChangeCookieStorage() const {}
scoped_refptr<base::SequencedTaskRunner>
CookieStoreIOSClient::GetTaskRunner() const {
return scoped_refptr<base::SequencedTaskRunner>();
......
......@@ -56,8 +56,6 @@ class CookieStoreIOSPersistentTest : public testing::Test {
public:
CookieStoreIOSPersistentTest()
: kTestCookieURL("http://foo.google.com/bar"),
scoped_cookie_store_ios_client_(
base::MakeUnique<TestCookieStoreIOSClient>()),
backend_(new net::TestPersistentCookieStore),
store_(
base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get())) {
......@@ -87,7 +85,6 @@ class CookieStoreIOSPersistentTest : public testing::Test {
protected:
base::MessageLoop loop_;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
scoped_refptr<net::TestPersistentCookieStore> backend_;
std::unique_ptr<net::CookieStoreIOS> store_;
std::unique_ptr<net::CookieStore::CookieChangedSubscription>
......
......@@ -9,8 +9,6 @@
#include <vector>
#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_store.h"
#include "url/gurl.h"
......@@ -68,26 +66,6 @@ class GetCookieCallback {
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,
std::vector<bool>* out_removes,
const net::CanonicalCookie& cookie,
......
......@@ -8,8 +8,6 @@
#include "base/memory/ptr_util.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"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_options.h"
......@@ -115,33 +113,6 @@ void GetCookieCallback::Run(const std::string& 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,
......
......@@ -9,13 +9,11 @@
#include <memory>
#include "base/bind_helpers.h"
#import "base/mac/bind_objc_block.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.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/ns_http_system_cookie_store.h"
#import "net/base/mac/url_conversions.h"
......@@ -28,21 +26,10 @@
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 {
static std::unique_ptr<net::CookieStore> Create() {
ClearCookies();
return base::MakeUnique<TestingCookieStoreIOS>(
return base::MakeUnique<CookieStoreIOS>(
base::MakeUnique<NSHTTPSystemCookieStore>());
}
......@@ -104,8 +91,6 @@ class CookieStoreIOSTest : public testing::Test {
kTestCookieURL2("http://foo.google.com/baz"),
kTestCookieURL3("http://foo.google.com"),
kTestCookieURL4("http://bar.google.com/bar"),
scoped_cookie_store_ios_client_(
base::MakeUnique<TestCookieStoreIOSClient>()),
backend_(new TestPersistentCookieStore) {
std::unique_ptr<NSHTTPSystemCookieStore> system_store(
base::MakeUnique<NSHTTPSystemCookieStore>());
......@@ -137,33 +122,25 @@ class CookieStoreIOSTest : public testing::Test {
void SetSystemCookie(const GURL& url,
const std::string& name,
const std::string& value) {
system_store_->SetCookieAsync(
[NSHTTPCookie cookieWithProperties:@{
NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()),
NSHTTPCookieName : base::SysUTF8ToNSString(name),
NSHTTPCookieValue : base::SysUTF8ToNSString(value),
NSHTTPCookieDomain : base::SysUTF8ToNSString(url.host()),
}],
base::BindOnce(&net::CookieStoreIOS::NotifySystemCookiesChanged));
system_store_->SetCookie([NSHTTPCookie cookieWithProperties:@{
NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()),
NSHTTPCookieName : base::SysUTF8ToNSString(name),
NSHTTPCookieValue : base::SysUTF8ToNSString(value),
NSHTTPCookieDomain : base::SysUTF8ToNSString(url.host()),
}]);
net::CookieStoreIOS::NotifySystemCookiesChanged();
base::RunLoop().RunUntilIdle();
}
void DeleteSystemCookie(const GURL& gurl, const std::string& name) {
base::WeakPtr<SystemCookieStore> weak_system_store =
system_store_->GetWeakPtr();
system_store_->GetCookiesForURLAsync(
gurl, base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) {
for (NSHTTPCookie* cookie in cookies) {
if ([[cookie name] isEqualToString:base::SysUTF8ToNSString(name)] &&
weak_system_store) {
weak_system_store->DeleteCookieAsync(
cookie,
base::BindOnce(
&net::CookieStoreIOS::NotifySystemCookiesChanged));
break;
}
}
}));
NSArray* cookies = system_store_->GetCookiesForURL(gurl);
for (NSHTTPCookie* cookie in cookies) {
if (cookie.name.UTF8String == name) {
system_store_->DeleteCookie(cookie);
break;
}
}
net::CookieStoreIOS::NotifySystemCookiesChanged();
base::RunLoop().RunUntilIdle();
}
......@@ -174,7 +151,6 @@ class CookieStoreIOSTest : public testing::Test {
const GURL kTestCookieURL4;
base::MessageLoop loop_;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client_;
scoped_refptr<TestPersistentCookieStore> backend_;
// |system_store_| will point to the NSHTTPSystemCookieStore object owned by
// |store_|. Once the store_ object is deleted the NSHTTPSystemCookieStore
......@@ -233,9 +209,6 @@ TEST_F(CookieStoreIOSTest, SameValueDoesNotCallHook) {
TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
base::MessageLoop loop;
ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client(
base::MakeUnique<TestCookieStoreIOSClient>());
const GURL kTestCookieURL("http://foo.google.com/bar");
ClearCookies();
std::unique_ptr<CookieStoreIOS> cookie_store(base::MakeUnique<CookieStoreIOS>(
......@@ -251,7 +224,6 @@ TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
cookie_store->GetAllCookiesForURLAsync(
kTestCookieURL,
base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback.did_run());
EXPECT_EQ(1u, callback.cookie_list().size());
net::CanonicalCookie cookie = callback.cookie_list()[0];
......
......@@ -18,54 +18,17 @@ class NSHTTPSystemCookieStore : public net::SystemCookieStore {
// By default the underlying cookiestore is
// |NSHTTPCookieStorage sharedHTTPCookieStorage|
NSHTTPSystemCookieStore();
explicit NSHTTPSystemCookieStore(NSHTTPCookieStorage* cookie_store);
~NSHTTPSystemCookieStore() override;
// Gets cookies for URL and calls |callback| async on these cookies.
void GetCookiesForURLAsync(const GURL& url,
SystemCookieCallbackForCookies callback) override;
// Gets all cookies and calls |callback| async on these cookies.
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;
NSArray* GetCookiesForURL(const GURL& url,
CookieCreationTimeManager* manager) override;
NSArray* GetAllCookies(CookieCreationTimeManager* manager) override;
void DeleteCookie(NSHTTPCookie* cookie) override;
void SetCookie(NSHTTPCookie* cookie) override;
void ClearStore() override;
NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() override;
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_;
DISALLOW_COPY_AND_ASSIGN(NSHTTPSystemCookieStore);
......
......@@ -4,10 +4,7 @@
#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_store_ios_client.h"
#import "net/base/mac/url_conversions.h"
#include "url/gurl.h"
......@@ -17,15 +14,6 @@
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()
: cookie_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]) {}
......@@ -38,71 +26,33 @@ NSHTTPSystemCookieStore::~NSHTTPSystemCookieStore() = default;
#pragma mark -
#pragma mark SystemCookieStore methods
void NSHTTPSystemCookieStore::GetCookiesForURLAsync(
NSArray* NSHTTPSystemCookieStore::GetCookiesForURL(
const GURL& url,
SystemCookieCallbackForCookies callback) {
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) {
CookieCreationTimeManager* manager) {
NSArray* cookies = [cookie_store_ cookiesForURL:NSURLWithGURL(url)];
if (!manager)
return cookies;
// Sort cookies by decreasing path length, then creation time, as per
// RFC6265.
return [cookies sortedArrayUsingFunction:CompareCookies
context:creation_time_manager_.get()];
return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
}
NSArray* NSHTTPSystemCookieStore::GetAllCookies() {
NSArray* NSHTTPSystemCookieStore::GetAllCookies(
CookieCreationTimeManager* manager) {
NSArray* cookies = cookie_store_.cookies;
return [cookies sortedArrayUsingFunction:CompareCookies
context:creation_time_manager_.get()];
if (!manager)
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) {
[cookie_store_ deleteCookie:cookie];
creation_time_manager_->DeleteCreationTime(cookie);
}
void NSHTTPSystemCookieStore::SetCookie(
NSHTTPCookie* cookie,
const base::Time* optional_creation_time) {
void NSHTTPSystemCookieStore::SetCookie(NSHTTPCookie* 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() {
......@@ -110,6 +60,10 @@ void NSHTTPSystemCookieStore::ClearStore() {
for (NSHTTPCookie* cookie in copy)
[cookie_store_ deleteCookie:cookie];
DCHECK_EQ(0u, cookie_store_.cookies.count);
creation_time_manager_->Clear();
}
NSHTTPCookieAcceptPolicy NSHTTPSystemCookieStore::GetCookieAcceptPolicy() {
return [cookie_store_ cookieAcceptPolicy];
}
} // namespace net
......@@ -7,10 +7,7 @@
#import <Foundation/Foundation.h>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
class GURL;
......@@ -24,67 +21,39 @@ class CookieCreationTimeManager;
// it directly without caring about the type of the underlying cookie store.
class SystemCookieStore {
public:
// Callback definitions.
typedef base::OnceClosure SystemCookieCallback;
typedef base::OnceCallback<void(NSArray<NSHTTPCookie*>*)>
SystemCookieCallbackForCookies;
SystemCookieStore();
virtual ~SystemCookieStore();
// Calls |callback| on all cookies for a specific |url| in the internal
// cookie store.
// 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 cookies for specific URL without sorting.
NSArray* GetCookiesForURL(const GURL& url);
// Returns the Cookie Accept policy for the internal cookie store.
virtual NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() = 0;
// Returns all cookies for a specific |url| from the internal cookie store.
// 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
base::Time GetCookieCreationTime(NSHTTPCookie* cookie);
// Set a specific cookie to the internal http cookie store.
virtual void SetCookie(NSHTTPCookie* cookie) = 0;
// Return WeakPtr of this object.
base::WeakPtr<SystemCookieStore> GetWeakPtr();
// Delete all cookies from the internal http cookie store.
virtual void ClearStore() = 0;
// Returns the Cookie Accept policy for the internal cookie store.
virtual NSHTTPCookieAcceptPolicy GetCookieAcceptPolicy() = 0;
protected:
// Compares cookies based on the path lengths and the creation times, as per
// RFC6265.
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
......
......@@ -4,7 +4,7 @@
#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"
#include "ios/net/ios_net_features.h"
......@@ -16,22 +16,12 @@ namespace net {
SystemCookieStore::~SystemCookieStore() = default;
SystemCookieStore::SystemCookieStore()
: creation_time_manager_(base::MakeUnique<CookieCreationTimeManager>()),
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);
NSArray* SystemCookieStore::GetCookiesForURL(const GURL& url) {
return GetCookiesForURL(url, /* manager = */ nullptr);
}
base::WeakPtr<SystemCookieStore> SystemCookieStore::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
NSArray* SystemCookieStore::GetAllCookies() {
return GetAllCookies(/* manager = */ nullptr);
}
// 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