Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Unified Diff: ios/net/cookies/cookie_store_ios.mm

Issue 1428673003: Injecting an NSHTTPCookieStorage dependency into CookieStoreIOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/net/cookies/cookie_store_ios.mm
diff --git a/ios/net/cookies/cookie_store_ios.mm b/ios/net/cookies/cookie_store_ios.mm
index be242f82e90d5964176f52b69af0f6add2bdb8d6..d2c5a90d034748649d19df10da1ab287fa2dd531 100644
--- a/ios/net/cookies/cookie_store_ios.mm
+++ b/ios/net/cookies/cookie_store_ios.mm
@@ -200,9 +200,9 @@ NSInteger CompareCookies(id a, id b, void* context) {
}
// Gets the cookies for |url| from the system cookie store.
-NSArray* GetCookiesForURL(const GURL& url, CookieCreationTimeManager* manager) {
- NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]
- cookiesForURL:net::NSURLWithGURL(url)];
+NSArray* GetCookiesForURL(NSHTTPCookieStorage* system_store,
+ const GURL& url, CookieCreationTimeManager* manager) {
+ NSArray* cookies = [system_store cookiesForURL:net::NSURLWithGURL(url)];
// Sort cookies by decreasing path length, then creation time, as per RFC6265.
return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
@@ -274,11 +274,21 @@ bool HasExplicitDomain(const std::string& cookie_line) {
CookieStoreIOS::CookieStoreIOS(
net::CookieMonster::PersistentCookieStore* persistent_store)
- : creation_time_manager_(new CookieCreationTimeManager),
+ : CookieStoreIOS(persistent_store,
+ [NSHTTPCookieStorage sharedHTTPCookieStorage]) {
+}
+
+CookieStoreIOS::CookieStoreIOS(
+ net::CookieMonster::PersistentCookieStore* persistent_store,
+ NSHTTPCookieStorage* system_store)
+ : system_store_(system_store),
+ creation_time_manager_(new CookieCreationTimeManager),
metrics_enabled_(false),
flush_delay_(base::TimeDelta::FromSeconds(10)),
synchronization_state_(NOT_SYNCHRONIZED),
cookie_cache_(new CookieCache()) {
+ DCHECK(system_store);
+
NotificationTrampoline::GetInstance()->AddObserver(this);
cookie_monster_ = new net::CookieMonster(persistent_store, nullptr);
cookie_monster_->SetPersistSessionCookies(true);
@@ -298,10 +308,11 @@ void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) {
NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged();
}
-CookieStoreIOS* CookieStoreIOS::CreateCookieStoreFromNSHTTPCookieStorage() {
+// static
+CookieStoreIOS* CookieStoreIOS::CreateCookieStore(
+ NSHTTPCookieStorage* cookie_storage) {
// TODO(huey): Update this when CrNet supports multiple cookie jars.
- [[NSHTTPCookieStorage sharedHTTPCookieStorage]
- setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
+ [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
// Create a cookie store with no persistent store backing. Then, populate
// it from the system's cookie jar.
@@ -332,8 +343,7 @@ void CookieStoreIOS::Flush(const base::Closure& closure) {
if (SystemCookiesAllowed()) {
// 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.
- WriteToCookieMonster(
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
+ WriteToCookieMonster([system_store_ cookies]);
}
cookie_monster_->FlushStore(closure);
flush_closure_.Cancel();
@@ -408,7 +418,7 @@ void CookieStoreIOS::SetCookieWithOptionsAsync(
(!has_explicit_domain || has_valid_domain);
if (success) {
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
+ [system_store_ setCookie:cookie];
creation_time_manager_->SetCreationTime(
cookie,
creation_time_manager_->MakeUniqueCreationTime(base::Time::Now()));
@@ -443,7 +453,8 @@ void CookieStoreIOS::GetCookiesWithOptionsAsync(
// engine.
DCHECK(!options.exclude_httponly());
- NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get());
+ NSArray* cookies = GetCookiesForURL(system_store_,
+ url, creation_time_manager_.get());
if (!callback.is_null())
callback.Run(BuildCookieLine(cookies, options));
break;
@@ -471,7 +482,8 @@ void CookieStoreIOS::GetAllCookiesForURLAsync(
return;
}
- NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get());
+ NSArray* cookies = GetCookiesForURL(system_store_,
+ url, creation_time_manager_.get());
net::CookieList cookie_list;
cookie_list.reserve([cookies count]);
for (NSHTTPCookie* cookie in cookies) {
@@ -500,11 +512,12 @@ void CookieStoreIOS::DeleteCookieAsync(const GURL& url,
WrapClosure(callback)));
break;
case SYNCHRONIZED:
- NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get());
+ NSArray* cookies = GetCookiesForURL(system_store_,
+ url, creation_time_manager_.get());
for (NSHTTPCookie* cookie in cookies) {
if ([[cookie name]
isEqualToString:base::SysUTF8ToNSString(cookie_name)]) {
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
+ [system_store_ deleteCookie:cookie];
creation_time_manager_->DeleteCreationTime(cookie);
}
}
@@ -613,13 +626,11 @@ CookieStoreIOS::~CookieStoreIOS() {
void CookieStoreIOS::ClearSystemStore() {
DCHECK(thread_checker_.CalledOnValidThread());
- NSHTTPCookieStorage* cookie_storage =
- [NSHTTPCookieStorage sharedHTTPCookieStorage];
base::scoped_nsobject<NSArray> copy(
- [[NSArray alloc] initWithArray:[cookie_storage cookies]]);
+ [[NSArray alloc] initWithArray:[system_store_ cookies]]);
for (NSHTTPCookie* cookie in copy.get())
- [cookie_storage deleteCookie:cookie];
- DCHECK_EQ(0u, [[cookie_storage cookies] count]);
+ [system_store_ deleteCookie:cookie];
+ DCHECK_EQ(0u, [[system_store_ cookies] count]);
creation_time_manager_->Clear();
}
@@ -630,10 +641,10 @@ void CookieStoreIOS::OnSystemCookiePolicyChanged() {
return;
NSHTTPCookieAcceptPolicy policy =
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy];
+ [system_store_ cookieAcceptPolicy];
if (policy == NSHTTPCookieAcceptPolicyAlways) {
// If cookies are disabled, the system cookie store should be empty.
- DCHECK(![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]);
+ DCHECK(![[system_store_ cookies] count]);
DCHECK(synchronization_state_ != SYNCHRONIZING);
synchronization_state_ = SYNCHRONIZING;
cookie_monster_->GetAllCookiesAsync(
@@ -642,8 +653,7 @@ void CookieStoreIOS::OnSystemCookiePolicyChanged() {
DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy);
// Flush() does not write the cookies to disk when they are disabled.
// Explicitly copy them.
- WriteToCookieMonster(
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
+ WriteToCookieMonster([system_store_ cookies]);
Flush(base::Closure());
ClearSystemStore();
if (synchronization_state_ == SYNCHRONIZING) {
@@ -677,13 +687,13 @@ void CookieStoreIOS::SetSynchronizedWithSystemStore(bool synchronized) {
#endif
NSHTTPCookieAcceptPolicy policy =
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy];
+ [system_store_ cookieAcceptPolicy];
DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
policy == NSHTTPCookieAcceptPolicyNever);
// If cookies are disabled, the system cookie store should be empty.
DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
- ![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]);
+ ![[system_store_ cookies] count]);
// If cookies are disabled, nothing is done now, the work will be done when
// cookies are re-enabled.
@@ -710,7 +720,7 @@ void CookieStoreIOS::SetSynchronizedWithSystemStore(bool synchronized) {
bool CookieStoreIOS::SystemCookiesAllowed() {
DCHECK(thread_checker_.CalledOnValidThread());
- return [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy] ==
+ return [system_store_ cookieAcceptPolicy] ==
NSHTTPCookieAcceptPolicyAlways;
}
@@ -738,7 +748,7 @@ void CookieStoreIOS::AddCookiesToSystemStore(const net::CookieList& cookies) {
// invalid characters.
if (!system_cookie)
continue;
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:system_cookie];
+ [system_store_ setCookie:system_cookie];
creation_time_manager_->SetCreationTime(system_cookie,
net_cookie.CreationDate());
}
@@ -783,7 +793,7 @@ void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter,
const DeleteCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_EQ(SYNCHRONIZED, synchronization_state_);
- NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
+ NSArray* cookies = [system_store_ cookies];
// Collect the cookies to delete.
base::scoped_nsobject<NSMutableArray> to_delete(
@@ -796,7 +806,7 @@ void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter,
// Delete them.
for (NSHTTPCookie* cookie in to_delete.get()) {
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
+ [system_store_ deleteCookie:cookie];
creation_time_manager_->DeleteCreationTime(cookie);
}
@@ -885,8 +895,7 @@ bool CookieStoreIOS::GetSystemCookies(
std::vector<net::CanonicalCookie>* cookies) {
DCHECK(thread_checker_.CalledOnValidThread());
NSURL* url = net::NSURLWithGURL(gurl);
- NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- NSArray* nscookies = [storage cookiesForURL:url];
+ NSArray* nscookies = [system_store_ cookiesForURL:url];
bool found_cookies = false;
for (NSHTTPCookie* nscookie in nscookies) {
if (nscookie.name.UTF8String == name) {
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698