Commit 9abf20e4 authored by Mohammad Refaat's avatar Mohammad Refaat Committed by Commit Bot

Allow URLFetcher to use WKHTTPCookieStore

With iOS11 there is API to access WKHTTPCookieStore and through
WKHTTPSystemCookieStore, URLFetcher can use this api to set and get
cookies from WKHTTPCookieStore

This should fix PDF download problems, it also should allow suggestions
queries to send cookies and will use cookies for NTP tiles.

Bug: 779106, 831678
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I59637ebde557fae09cfd39105d84675724ac0140
Reviewed-on: https://chromium-review.googlesource.com/1050404
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558824}
parent 433faf46
...@@ -66,6 +66,9 @@ source_set("unit_tests") { ...@@ -66,6 +66,9 @@ source_set("unit_tests") {
":net", ":net",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//ios/net",
"//ios/net:test_support",
"//ios/testing:ios_test_support",
"//ios/web/public/test", "//ios/web/public/test",
"//net", "//net",
"//net:test_support", "//net:test_support",
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/net/cookies/cookie_store_ios_persistent.h" #include "ios/net/cookies/cookie_store_ios_persistent.h"
#import "ios/net/cookies/system_cookie_store.h" #import "ios/net/cookies/system_cookie_store.h"
#include "ios/web/public/features.h"
#include "ios/web/public/web_thread.h" #include "ios/web/public/web_thread.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"
...@@ -88,6 +89,17 @@ std::unique_ptr<net::CookieStore> CreateCookieStore( ...@@ -88,6 +89,17 @@ std::unique_ptr<net::CookieStore> CreateCookieStore(
if (config.cookie_store_type == CookieStoreConfig::COOKIE_MONSTER) if (config.cookie_store_type == CookieStoreConfig::COOKIE_MONSTER)
return CreateCookieMonster(config); return CreateCookieMonster(config);
// On iOS 11, there is no need to use PersistentCookieStore or CookieMonster
// because there is a way to access cookies in WKHTTPCookieStore. This will
// allow URLFetcher and anyother users of net:CookieStore to in iOS to set
// and get cookies directly in WKHTTPCookieStore.
if (@available(iOS 11, *)) {
if (base::FeatureList::IsEnabled(web::features::kWKHTTPSystemCookieStore)) {
return std::make_unique<net::CookieStoreIOS>(
std::move(system_cookie_store));
}
}
scoped_refptr<net::SQLitePersistentCookieStore> persistent_store = nullptr; scoped_refptr<net::SQLitePersistentCookieStore> persistent_store = nullptr;
if (config.session_cookie_mode == if (config.session_cookie_mode ==
CookieStoreConfig::RESTORED_SESSION_COOKIES) { CookieStoreConfig::RESTORED_SESSION_COOKIES) {
......
...@@ -6,23 +6,48 @@ ...@@ -6,23 +6,48 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "base/mac/bind_objc_block.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "ios/net/cookies/cookie_store_ios_test_util.h"
#import "ios/net/cookies/ns_http_system_cookie_store.h"
#import "ios/net/cookies/system_cookie_store.h"
#import "ios/testing/wait_util.h"
#include "ios/web/public/features.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#include "ios/web/public/test/web_test.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.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
using testing::WaitUntilConditionOrTimeout;
using testing::kWaitForCookiesTimeout;
namespace { namespace {
// Date of the last cookie deletion. // Date of the last cookie deletion.
NSString* const kLastCookieDeletionDate = @"LastCookieDeletionDate"; NSString* const kLastCookieDeletionDate = @"LastCookieDeletionDate";
} // namespace class CookieUtilTest : public PlatformTest {
public:
CookieUtilTest()
: ns_http_cookie_store_([NSHTTPCookieStorage sharedHTTPCookieStorage]) {}
using CookieUtil = PlatformTest; ~CookieUtilTest() override {
// Make sure NSHTTPCookieStorage is empty.
[ns_http_cookie_store_ removeCookiesSinceDate:[NSDate distantPast]];
}
TEST_F(CookieUtil, ShouldClearSessionCookies) { protected:
NSHTTPCookieStorage* ns_http_cookie_store_;
};
TEST_F(CookieUtilTest, ShouldClearSessionCookies) {
time_t start_test_time; time_t start_test_time;
time(&start_test_time); time(&start_test_time);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
...@@ -44,3 +69,73 @@ TEST_F(CookieUtil, ShouldClearSessionCookies) { ...@@ -44,3 +69,73 @@ TEST_F(CookieUtil, ShouldClearSessionCookies) {
EXPECT_TRUE(cookie_util::ShouldClearSessionCookies()); EXPECT_TRUE(cookie_util::ShouldClearSessionCookies());
EXPECT_LE(now, [defaults integerForKey:kLastCookieDeletionDate]); EXPECT_LE(now, [defaults integerForKey:kLastCookieDeletionDate]);
} }
// Tests that CreateCookieStore returns the correct type of net::CookieStore
// based on the given parameters and the iOS version.
TEST_F(CookieUtilTest, CreateCookieStoreInIOS11) {
web::TestWebThreadBundle thread_bundle;
net::ScopedTestingCookieStoreIOSClient scoped_cookie_store_ios_client(
std::make_unique<net::TestCookieStoreIOSClient>());
// Testing while WKHTTPSystemCookieStore feature is enabled.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
web::features::kWKHTTPSystemCookieStore);
GURL test_url("http://foo.google.com/bar");
NSString* cookie_name = @"cookie_name";
NSString* cookie_value = @"cookie_value";
std::unique_ptr<net::SystemCookieStore> system_cookie_store =
std::make_unique<net::NSHTTPSystemCookieStore>(ns_http_cookie_store_);
net::SystemCookieStore* ns_cookie_store = system_cookie_store.get();
cookie_util::CookieStoreConfig config(
base::FilePath(),
cookie_util::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
cookie_util::CookieStoreConfig::CookieStoreType::COOKIE_STORE_IOS,
nullptr);
std::unique_ptr<net::CookieStore> cookie_store =
cookie_util::CreateCookieStore(config, std::move(system_cookie_store));
net::CookieOptions options;
options.set_include_httponly();
std::string cookie_line = base::SysNSStringToUTF8(cookie_name) + "=" +
base::SysNSStringToUTF8(cookie_value);
cookie_store->SetCookieWithOptionsAsync(
test_url, cookie_line, options, net::CookieStore::SetCookiesCallback());
__block NSArray<NSHTTPCookie*>* result_cookies = nil;
__block bool callback_called = false;
ns_cookie_store->GetCookiesForURLAsync(
test_url, base::BindBlockArc(^(NSArray<NSHTTPCookie*>* cookies) {
callback_called = true;
result_cookies = [cookies copy];
}));
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return callback_called;
}));
if (@available(iOS 11, *)) {
// When WKHTTPSystemCookieStore feature is enabled and the iOS version is
// 11+ the cookie should be set directly in the backing SystemCookieStore.
EXPECT_EQ(1U, result_cookies.count);
EXPECT_NSEQ(cookie_name, result_cookies[0].name);
EXPECT_NSEQ(cookie_value, result_cookies[0].value);
} else {
// Before iOS 11, cookies are not set in the backing SystemCookieStore
// instead they are found on CookieMonster.
EXPECT_EQ(0U, result_cookies.count);
}
// Clear cookies that was set in the test.
__block bool cookies_cleared = false;
cookie_store->DeleteAllAsync(base::BindBlockArc(^(unsigned int) {
cookies_cleared = true;
}));
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return cookies_cleared;
}));
}
} // namespace
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