Commit 04349b4f authored by mrefaat's avatar mrefaat Committed by Commit Bot

Update WKSystemCookieStore and CookieCreationTimeManager Thread usage.

1- Updates WKSystemCookieStore so it can use wkcookiestore only from
   the main thread.
2- Update the CookieCreationTimeManager to only be used on IO thread
   (aside from creation on Main thread)

Bug: 779106,759229
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ifa0f9e5bb2d2c33c488759467dc8a25403f6e0ed
Reviewed-on: https://chromium-review.googlesource.com/809531
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521954}
parent d7cf94cf
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <set> #include <set>
#include "base/containers/hash_tables.h" #include "base/containers/hash_tables.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -17,6 +18,8 @@ namespace net { ...@@ -17,6 +18,8 @@ namespace net {
// The CookieCreationTimeManager allows to get and set the creation time of a // The CookieCreationTimeManager allows to get and set the creation time of a
// NSHTTPCookie. // NSHTTPCookie.
// All the methods of CookieCreationTimeManager must be called on the IO thread,
// except its constructor that can be called from any thread.
// Creation time data is stored either in the cookie properties (when the cookie // Creation time data is stored either in the cookie properties (when the cookie
// is created by the system) or in |creation_times_| (when the cookie is created // is created by the system) or in |creation_times_| (when the cookie is created
// by CookieStoreIOS). When both are available, |creation_times_| is used, // by CookieStoreIOS). When both are available, |creation_times_| is used,
...@@ -37,11 +40,14 @@ class CookieCreationTimeManager { ...@@ -37,11 +40,14 @@ class CookieCreationTimeManager {
void DeleteCreationTime(NSHTTPCookie* cookie); void DeleteCreationTime(NSHTTPCookie* cookie);
// Clears all the creation times. // Clears all the creation times.
void Clear(); void Clear();
// Gets base::WeakPtr to the object to be used in sorting.
base::WeakPtr<CookieCreationTimeManager> GetWeakPtr();
private: private:
base::hash_map<std::string, base::Time> creation_times_; base::hash_map<std::string, base::Time> creation_times_;
std::set<base::Time> unique_times_; std::set<base::Time> unique_times_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
base::WeakPtrFactory<CookieCreationTimeManager> weak_factory_;
}; };
} // namespace net } // namespace net
......
...@@ -49,7 +49,8 @@ std::string GetCookieUniqueID(NSHTTPCookie* cookie) { ...@@ -49,7 +49,8 @@ std::string GetCookieUniqueID(NSHTTPCookie* cookie) {
namespace net { namespace net {
CookieCreationTimeManager::CookieCreationTimeManager() { CookieCreationTimeManager::CookieCreationTimeManager() : weak_factory_(this) {
DETACH_FROM_THREAD(thread_checker_);
} }
CookieCreationTimeManager::~CookieCreationTimeManager() { CookieCreationTimeManager::~CookieCreationTimeManager() {
...@@ -122,4 +123,9 @@ void CookieCreationTimeManager::Clear() { ...@@ -122,4 +123,9 @@ void CookieCreationTimeManager::Clear() {
unique_times_.clear(); unique_times_.clear();
} }
base::WeakPtr<CookieCreationTimeManager>
CookieCreationTimeManager::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
} // namespace net } // namespace net
...@@ -74,8 +74,8 @@ class SystemCookieStore { ...@@ -74,8 +74,8 @@ class SystemCookieStore {
base::WeakPtr<SystemCookieStore> GetWeakPtr(); base::WeakPtr<SystemCookieStore> GetWeakPtr();
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 provided
// RFC6265. // by a non null creation time manager |context|, as per 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 // Internal cookie stores doesn't store creation time. This object is used
......
...@@ -36,6 +36,7 @@ base::WeakPtr<SystemCookieStore> SystemCookieStore::GetWeakPtr() { ...@@ -36,6 +36,7 @@ base::WeakPtr<SystemCookieStore> SystemCookieStore::GetWeakPtr() {
// protected static // protected static
NSInteger SystemCookieStore::CompareCookies(id a, id b, void* context) { NSInteger SystemCookieStore::CompareCookies(id a, id b, void* context) {
DCHECK(context);
NSHTTPCookie* cookie_a = static_cast<NSHTTPCookie*>(a); NSHTTPCookie* cookie_a = static_cast<NSHTTPCookie*>(a);
NSHTTPCookie* cookie_b = static_cast<NSHTTPCookie*>(b); NSHTTPCookie* cookie_b = static_cast<NSHTTPCookie*>(b);
// Compare path lengths first. // Compare path lengths first.
......
...@@ -65,71 +65,95 @@ void WKHTTPSystemCookieStore::GetCookiesForURLAsync( ...@@ -65,71 +65,95 @@ void WKHTTPSystemCookieStore::GetCookiesForURLAsync(
SystemCookieCallbackForCookies callback) { SystemCookieCallbackForCookies callback) {
__block SystemCookieCallbackForCookies shared_callback = std::move(callback); __block SystemCookieCallbackForCookies shared_callback = std::move(callback);
GURL block_url = url; GURL block_url = url;
[cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) { web::WebThread::PostTask(
NSMutableArray* result = [NSMutableArray array]; web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
for (NSHTTPCookie* cookie in cookies) { [cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) {
if (ShouldIncludeForRequestUrl(cookie, block_url)) { NSMutableArray* result = [NSMutableArray array];
[result addObject:cookie]; for (NSHTTPCookie* cookie in cookies) {
} if (ShouldIncludeForRequestUrl(cookie, block_url)) {
} [result addObject:cookie];
RunSystemCookieCallbackForCookies(std::move(shared_callback), result); }
}]; }
RunSystemCookieCallbackForCookies(std::move(shared_callback), result);
}];
}));
} }
void WKHTTPSystemCookieStore::GetAllCookiesAsync( void WKHTTPSystemCookieStore::GetAllCookiesAsync(
SystemCookieCallbackForCookies callback) { SystemCookieCallbackForCookies callback) {
__block SystemCookieCallbackForCookies shared_callback = std::move(callback); __block SystemCookieCallbackForCookies shared_callback = std::move(callback);
[cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) { web::WebThread::PostTask(
RunSystemCookieCallbackForCookies(std::move(shared_callback), cookies); web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
}]; [cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) {
RunSystemCookieCallbackForCookies(std::move(shared_callback),
cookies);
}];
}));
} }
void WKHTTPSystemCookieStore::DeleteCookieAsync(NSHTTPCookie* cookie, void WKHTTPSystemCookieStore::DeleteCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) { SystemCookieCallback callback) {
__block SystemCookieCallback shared_callback = std::move(callback); __block SystemCookieCallback shared_callback = std::move(callback);
[cookie_store_ deleteCookie:cookie NSHTTPCookie* block_cookie = cookie;
completionHandler:^{ web::WebThread::PostTask(
creation_time_manager_->DeleteCreationTime(cookie); web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
RunSystemCookieCallbackOnIOThread(std::move(shared_callback)); [cookie_store_ deleteCookie:block_cookie
}]; completionHandler:^{
RunSystemCookieCallbackOnIOThread(base::BindBlockArc(^{
creation_time_manager_->DeleteCreationTime(block_cookie);
if (!shared_callback.is_null())
std::move(shared_callback).Run();
}));
}];
}));
} }
void WKHTTPSystemCookieStore::SetCookieAsync( void WKHTTPSystemCookieStore::SetCookieAsync(
NSHTTPCookie* cookie, NSHTTPCookie* cookie,
const base::Time* optional_creation_time, const base::Time* optional_creation_time,
SystemCookieCallback callback) { SystemCookieCallback callback) {
__block SystemCookieCallback shared_callback = std::move(callback); __block SystemCookieCallback shared_callback = std::move(callback);
[cookie_store_ setCookie:cookie NSHTTPCookie* block_cookie = cookie;
completionHandler:^{ base::Time cookie_time = base::Time::Now();
// Set creation time as soon as possible if (optional_creation_time && !optional_creation_time->is_null())
base::Time cookie_time = base::Time::Now(); cookie_time = *optional_creation_time;
if (optional_creation_time && !optional_creation_time->is_null())
cookie_time = *optional_creation_time; web::WebThread::PostTask(
web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
creation_time_manager_->SetCreationTime( [cookie_store_ setCookie:block_cookie
cookie, completionHandler:^{
creation_time_manager_->MakeUniqueCreationTime(cookie_time)); RunSystemCookieCallbackOnIOThread(base::BindBlockArc(^{
RunSystemCookieCallbackOnIOThread(std::move(shared_callback)); creation_time_manager_->SetCreationTime(
}]; block_cookie,
creation_time_manager_->MakeUniqueCreationTime(
cookie_time));
if (!shared_callback.is_null())
std::move(shared_callback).Run();
}));
}];
}));
} }
void WKHTTPSystemCookieStore::ClearStoreAsync(SystemCookieCallback callback) { void WKHTTPSystemCookieStore::ClearStoreAsync(SystemCookieCallback callback) {
__block SystemCookieCallback shared_callback = std::move(callback); __block SystemCookieCallback shared_callback = std::move(callback);
[cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) { web::WebThread::PostTask(
web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
scoped_refptr<CallbackCounter> callback_counter = [cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) {
new CallbackCounter(base::BindBlockArc(^{ scoped_refptr<CallbackCounter> callback_counter =
creation_time_manager_->Clear(); new CallbackCounter(base::BindRepeating(
RunSystemCookieCallbackOnIOThread(std::move(shared_callback)); &RunSystemCookieCallbackOnIOThread, base::BindBlockArc(^{
})); creation_time_manager_->Clear();
// Add callback for each cookie std::move(shared_callback).Run();
callback_counter->IncrementCount(cookies.count); })));
for (NSHTTPCookie* cookie in cookies) { // Add callback for each cookie
[cookie_store_ deleteCookie:cookie callback_counter->IncrementCount(cookies.count);
completionHandler:^{ for (NSHTTPCookie* cookie in cookies) {
callback_counter->DecrementCount(); [cookie_store_ deleteCookie:cookie
}]; completionHandler:^{
} callback_counter->DecrementCount();
}]; }];
}
}];
}));
} }
NSHTTPCookieAcceptPolicy WKHTTPSystemCookieStore::GetCookieAcceptPolicy() { NSHTTPCookieAcceptPolicy WKHTTPSystemCookieStore::GetCookieAcceptPolicy() {
...@@ -145,11 +169,21 @@ void WKHTTPSystemCookieStore::RunSystemCookieCallbackForCookies( ...@@ -145,11 +169,21 @@ void WKHTTPSystemCookieStore::RunSystemCookieCallbackForCookies(
NSArray<NSHTTPCookie*>* cookies) { NSArray<NSHTTPCookie*>* cookies) {
if (callback.is_null()) if (callback.is_null())
return; return;
NSArray* result = [static_cast<NSArray*>(cookies) base::WeakPtr<net::CookieCreationTimeManager> weak_time_manager =
sortedArrayUsingFunction:CompareCookies creation_time_manager_->GetWeakPtr();
context:creation_time_manager_.get()]; NSArray<NSHTTPCookie*>* block_cookies = cookies;
RunSystemCookieCallbackOnIOThread( __block net::SystemCookieStore::SystemCookieCallbackForCookies
base::BindOnce(std::move(callback), result)); shared_callback = std::move(callback);
RunSystemCookieCallbackOnIOThread(base::BindBlockArc(^{
if (weak_time_manager) {
NSArray* result = [static_cast<NSArray*>(block_cookies)
sortedArrayUsingFunction:CompareCookies
context:weak_time_manager.get()];
std::move(shared_callback).Run(result);
} else {
std::move(shared_callback).Run(block_cookies);
}
}));
} }
} // namespace web } // namespace web
......
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