Commit 2877df5b authored by Nate Fischer's avatar Nate Fischer Committed by Commit Bot

AW NS: remove old cookie code paths

No change to NS path, this only removes the legacy path.

This removes some cookie code used in the legacy code path. This is
mostly deleting files, but also modifies a couple classes to remove
legacy-path-only code and reduce some complexity.

Bug: 990437
Test: run_webview_instrumentation_test_apk -f CookieManagerTest.*
Change-Id: I0418c6f76af19052d5ff597ba54f8ec773ea914d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1733847Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Commit-Queue: Nate Fischer <ntfschr@chromium.org>
Auto-Submit: Nate Fischer <ntfschr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#684041}
parent b1493b82
......@@ -579,16 +579,10 @@ source_set("common") {
"browser/js_java_interaction/js_java_configurator_host.h",
"browser/memory_metrics_logger.cc",
"browser/memory_metrics_logger.h",
"browser/net/aw_cookie_change_dispatcher_wrapper.cc",
"browser/net/aw_cookie_change_dispatcher_wrapper.h",
"browser/net/aw_cookie_store_wrapper.cc",
"browser/net/aw_cookie_store_wrapper.h",
"browser/net/aw_http_user_agent_settings.cc",
"browser/net/aw_http_user_agent_settings.h",
"browser/net/aw_proxy_config_monitor.cc",
"browser/net/aw_proxy_config_monitor.h",
"browser/net/init_native_callback.cc",
"browser/net/init_native_callback.h",
"browser/network_service/android_stream_reader_url_loader.cc",
"browser/network_service/android_stream_reader_url_loader.h",
"browser/network_service/aw_network_change_notifier.cc",
......
......@@ -7,15 +7,12 @@
#include <memory>
#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "net/base/net_errors.h"
#include "net/base/static_cookie_policy.h"
#include "net/url_request/url_request.h"
#include "services/network/public/cpp/features.h"
#include "url/gurl.h"
using base::AutoLock;
......@@ -66,20 +63,6 @@ bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
return io_thread_client->ShouldAcceptThirdPartyCookies();
}
bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
const net::URLRequest& request) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return false;
}
bool AwCookieAccessPolicy::AllowCookies(const net::URLRequest& request) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!base::FeatureList::IsEnabled(network::features::kNetworkService));
bool third_party = GetShouldAcceptThirdPartyCookies(request);
return CanAccessCookies(request.url(), request.site_for_cookies(),
third_party);
}
bool AwCookieAccessPolicy::AllowCookies(const GURL& url,
const GURL& first_party,
int render_process_id,
......
......@@ -38,10 +38,6 @@ class AwCookieAccessPolicy {
int frame_tree_node_id);
bool GetShouldAcceptThirdPartyCookies(const net::URLRequest& request);
// Whether or not to allow cookies to bet sent or set for |request|. Can only
// be called from the IO thread.
bool AllowCookies(const net::URLRequest& request);
// Whether or not to allow cookies for requests with these parameters.
bool AllowCookies(const GURL& url,
const GURL& first_party,
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/aw_cookie_access_policy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace android_webview {
namespace {
const GURL kUrlFirstParty("http://first.example");
const GURL kUrlThirdParty("http://third.example");
const GURL kFileUrl1("file:///path/to/any_file.html");
const GURL kFileUrl2("file:///path/to/some/other_file.html");
} // namespace
class AwCookieAccessPolicyTest : public testing::Test {
public:
AwCookieAccessPolicyTest() {}
bool CanAccessCookies(const GURL& url,
const GURL& site_for_cookies,
bool accept_cookies,
bool accept_third_party_cookies) {
AwCookieAccessPolicy policy;
policy.SetShouldAcceptCookies(accept_cookies);
return policy.CanAccessCookies(url, site_for_cookies,
accept_third_party_cookies);
}
};
TEST_F(AwCookieAccessPolicyTest, BlockAllCookies) {
EXPECT_FALSE(CanAccessCookies(kUrlFirstParty, kUrlFirstParty,
false /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kUrlFirstParty, kUrlThirdParty,
false /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kFileUrl1, kFileUrl1, false /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kFileUrl1, kFileUrl2, false /* allow_cookies */,
false /* allow_third_party_cookies */));
}
TEST_F(AwCookieAccessPolicyTest, BlockAllCookiesWithThirdPartySet) {
EXPECT_FALSE(CanAccessCookies(kUrlFirstParty, kUrlFirstParty,
false /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kUrlFirstParty, kUrlThirdParty,
false /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kFileUrl1, kFileUrl1, false /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kFileUrl1, kFileUrl2, false /* allow_cookies */,
true /* allow_third_party_cookies */));
}
TEST_F(AwCookieAccessPolicyTest, FirstPartyCookiesOnly) {
EXPECT_TRUE(CanAccessCookies(kUrlFirstParty, kUrlFirstParty,
true /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_FALSE(CanAccessCookies(kUrlFirstParty, kUrlThirdParty,
true /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_TRUE(CanAccessCookies(kFileUrl1, kFileUrl1, true /* allow_cookies */,
false /* allow_third_party_cookies */));
EXPECT_TRUE(CanAccessCookies(kFileUrl1, kFileUrl2, true /* allow_cookies */,
false /* allow_third_party_cookies */));
}
TEST_F(AwCookieAccessPolicyTest, AllowAllCookies) {
EXPECT_TRUE(CanAccessCookies(kUrlFirstParty, kUrlFirstParty,
true /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_TRUE(CanAccessCookies(kUrlFirstParty, kUrlThirdParty,
true /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_TRUE(CanAccessCookies(kFileUrl1, kFileUrl1, true /* allow_cookies */,
true /* allow_third_party_cookies */));
EXPECT_TRUE(CanAccessCookies(kFileUrl1, kFileUrl2, true /* allow_cookies */,
true /* allow_third_party_cookies */));
}
} // namespace android_webview
......@@ -12,7 +12,6 @@
#include "android_webview/browser/aw_browser_context.h"
#include "android_webview/browser/aw_cookie_access_policy.h"
#include "android_webview/browser/net/init_native_callback.h"
#include "android_webview/native_jni/AwCookieManager_jni.h"
#include "base/android/callback_android.h"
#include "base/android/jni_string.h"
......@@ -253,10 +252,6 @@ void CookieManager::RunPendingCookieTasks() {
}
}
base::SingleThreadTaskRunner* CookieManager::GetCookieStoreTaskRunner() {
return cookie_store_task_runner_.get();
}
net::CookieStore* CookieManager::GetCookieStore() {
DCHECK(cookie_store_task_runner_->RunsTasksInCurrentSequence());
......@@ -706,16 +701,4 @@ static void JNI_AwCookieManager_SetAcceptFileSchemeCookies(
return CookieManager::GetInstance()->SetAcceptFileSchemeCookies(accept);
}
// The following two methods are used to avoid a circular project dependency.
// TODO(mmenke): This is weird. Maybe there should be a leaky Singleton in
// browser/net that creates and owns there?
scoped_refptr<base::SingleThreadTaskRunner> GetCookieStoreTaskRunner() {
return CookieManager::GetInstance()->GetCookieStoreTaskRunner();
}
net::CookieStore* GetCookieStore() {
return CookieManager::GetInstance()->GetCookieStore();
}
} // namespace android_webview
......@@ -41,11 +41,6 @@ class CookieManager {
public:
static CookieManager* GetInstance();
// Returns the TaskRunner on which the CookieStore lives.
base::SingleThreadTaskRunner* GetCookieStoreTaskRunner();
// Returns the CookieStore, creating it if necessary. This must only be called
// on the CookieStore TaskRunner.
net::CookieStore* GetCookieStore();
// Passes a |cookie_manager_info|, which this will use for CookieManager APIs
// going forward. Only called in the Network Service path, with the intention
// this is called once during content initialization (when we create the
......@@ -77,6 +72,10 @@ class CookieManager {
CookieManager();
~CookieManager();
// Returns the CookieStore, creating it if necessary. This must only be called
// on the CookieStore TaskRunner.
net::CookieStore* GetCookieStore();
// Gets the Network Service CookieManager if it's been passed via
// |SetMojoCookieManager|. Otherwise (if Network Service is disabled or
// content layer has not yet initialized the NetworkContext), this returns
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/net/aw_cookie_change_dispatcher_wrapper.h"
#include "android_webview/browser/net/init_native_callback.h"
#include "base/bind.h"
#include "base/memory/ref_counted_delete_on_sequence.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_store.h"
#include "url/gurl.h"
namespace android_webview {
namespace {
using CookieChangeCallbackList =
base::CallbackList<void(const net::CanonicalCookie& cookie,
net::CookieChangeCause cause)>;
class AwCookieChangeSubscription : public net::CookieChangeSubscription {
public:
explicit AwCookieChangeSubscription(
std::unique_ptr<CookieChangeCallbackList::Subscription> subscription)
: subscription_(std::move(subscription)) {}
private:
std::unique_ptr<CookieChangeCallbackList::Subscription> subscription_;
DISALLOW_COPY_AND_ASSIGN(AwCookieChangeSubscription);
};
// Wraps a subscription to cookie change notifications for the global
// CookieStore for a consumer that lives on another thread. Handles passing
// messages between threads, and destroys itself when the consumer unsubscribes.
// Must be created on the consumer's thread. Each instance only supports a
// single subscription.
class SubscriptionWrapper {
public:
SubscriptionWrapper() : weak_factory_(this) {}
enum Mode {
kByCookie,
kByUrl,
};
std::unique_ptr<net::CookieChangeSubscription> Subscribe(
Mode mode,
const GURL& url,
const std::string& name,
net::CookieChangeCallback callback) {
// This class is only intended to be used for a single subscription.
DCHECK(callback_list_.empty());
nested_subscription_ =
NestedSubscription::Create(mode, url, name, weak_factory_.GetWeakPtr());
return std::make_unique<AwCookieChangeSubscription>(
callback_list_.Add(std::move(callback)));
}
private:
// The NestedSubscription is responsible for creating and managing the
// underlying subscription to the real CookieStore, and posting notifications
// back to |callback_list_|.
class NestedSubscription
: public base::RefCountedDeleteOnSequence<NestedSubscription> {
public:
static scoped_refptr<NestedSubscription> Create(
Mode mode,
const GURL& url,
const std::string& name,
base::WeakPtr<SubscriptionWrapper> subscription_wrapper) {
auto subscription = base::WrapRefCounted(
new NestedSubscription(std::move(subscription_wrapper)));
PostTaskToCookieStoreTaskRunner(base::BindOnce(
&NestedSubscription::Subscribe, subscription, mode, url, name));
return subscription;
}
private:
friend class base::RefCountedDeleteOnSequence<NestedSubscription>;
friend class base::DeleteHelper<NestedSubscription>;
explicit NestedSubscription(
base::WeakPtr<SubscriptionWrapper> subscription_wrapper)
: base::RefCountedDeleteOnSequence<NestedSubscription>(
GetCookieStoreTaskRunner()),
subscription_wrapper_(subscription_wrapper),
client_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
~NestedSubscription() {}
void Subscribe(Mode mode, const GURL& url, const std::string& name) {
switch (mode) {
case kByCookie:
subscription_ =
GetCookieStore()->GetChangeDispatcher().AddCallbackForCookie(
url, name,
base::BindRepeating(&NestedSubscription::OnChanged, this));
break;
case kByUrl:
subscription_ =
GetCookieStore()->GetChangeDispatcher().AddCallbackForUrl(
url,
base::BindRepeating(&NestedSubscription::OnChanged, this));
}
}
void OnChanged(const net::CanonicalCookie& cookie,
net::CookieChangeCause cause) {
client_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&SubscriptionWrapper::OnChanged,
subscription_wrapper_, cookie, cause));
}
base::WeakPtr<SubscriptionWrapper> subscription_wrapper_;
scoped_refptr<base::TaskRunner> client_task_runner_;
std::unique_ptr<net::CookieChangeSubscription> subscription_;
DISALLOW_COPY_AND_ASSIGN(NestedSubscription);
};
void OnChanged(const net::CanonicalCookie& cookie,
net::CookieChangeCause cause) {
callback_list_.Notify(cookie, cause);
}
// The "list" only had one entry, so can just clean up now.
void OnUnsubscribe() { delete this; }
scoped_refptr<NestedSubscription> nested_subscription_;
CookieChangeCallbackList callback_list_;
base::WeakPtrFactory<SubscriptionWrapper> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SubscriptionWrapper);
};
} // anonymous namespace
AwCookieChangeDispatcherWrapper::AwCookieChangeDispatcherWrapper()
#if DCHECK_IS_ON()
: client_task_runner_(base::ThreadTaskRunnerHandle::Get())
#endif // DCHECK_IS_ON()
{
}
AwCookieChangeDispatcherWrapper::~AwCookieChangeDispatcherWrapper() = default;
std::unique_ptr<net::CookieChangeSubscription>
AwCookieChangeDispatcherWrapper::AddCallbackForCookie(
const GURL& url,
const std::string& name,
net::CookieChangeCallback callback) {
#if DCHECK_IS_ON()
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
#endif // DCHECK_IS_ON()
// The SubscriptionWrapper is owned by the subscription itself, and has no
// connection to the AwCookieStoreWrapper after creation. Other CookieStore
// implementations DCHECK if a subscription outlasts the cookie store,
// unfortunately, this design makes DCHECKing if there's an outstanding
// subscription when the AwCookieStoreWrapper is destroyed a bit ugly.
// TODO(mmenke): Still worth adding a DCHECK?
SubscriptionWrapper* subscription = new SubscriptionWrapper();
return subscription->Subscribe(SubscriptionWrapper::kByCookie, url, name,
std::move(callback));
}
std::unique_ptr<net::CookieChangeSubscription>
AwCookieChangeDispatcherWrapper::AddCallbackForUrl(
const GURL& url,
net::CookieChangeCallback callback) {
#if DCHECK_IS_ON()
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
#endif // DCHECK_IS_ON()
SubscriptionWrapper* subscription = new SubscriptionWrapper();
return subscription->Subscribe(SubscriptionWrapper::kByUrl, url,
/* name=, ignored */ "", std::move(callback));
}
std::unique_ptr<net::CookieChangeSubscription>
AwCookieChangeDispatcherWrapper::AddCallbackForAllChanges(
net::CookieChangeCallback callback) {
// Implement when needed by Android Webview consumers.
NOTIMPLEMENTED();
return nullptr;
}
} // namespace android_webview
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_CHANGE_DISPATCHER_WRAPPER_H_
#define ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_CHANGE_DISPATCHER_WRAPPER_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/callback_list.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h"
#include "net/cookies/cookie_change_dispatcher.h"
class GURL;
namespace android_webview {
// CookieChangeDispatcher implementation used by AwCookieStoreWrapper.
//
// AwCookieStoreWrapper wraps a CookieStore that lives on a different thread and
// proxies calls and callbacks between the originating thread and the
// CookieStore's thread via task posting.
//
// By extension, AwCookieChangeDispatcherWrapper proxies cookie change
// notifications between the CookieStore's thread and the thread that subscribes
// to the notifications.
class AwCookieChangeDispatcherWrapper : public net::CookieChangeDispatcher {
public:
AwCookieChangeDispatcherWrapper();
~AwCookieChangeDispatcherWrapper() override;
// net::CookieChangeDispatcher
std::unique_ptr<net::CookieChangeSubscription> AddCallbackForCookie(
const GURL& url,
const std::string& name,
net::CookieChangeCallback callback) override WARN_UNUSED_RESULT;
std::unique_ptr<net::CookieChangeSubscription> AddCallbackForUrl(
const GURL& url,
net::CookieChangeCallback callback) override WARN_UNUSED_RESULT;
std::unique_ptr<net::CookieChangeSubscription> AddCallbackForAllChanges(
net::CookieChangeCallback callback) override WARN_UNUSED_RESULT;
private:
#if DCHECK_IS_ON()
scoped_refptr<base::TaskRunner> client_task_runner_;
#endif // DCHECK_IS_ON()
DISALLOW_COPY_AND_ASSIGN(AwCookieChangeDispatcherWrapper);
};
} // namespace android_webview
#endif // ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_CHANGE_DISPATCHER_WRAPPER_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/net/aw_cookie_store_wrapper.h"
#include <memory>
#include <string>
#include "android_webview/browser/net/init_native_callback.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/cookies/canonical_cookie.h"
#include "url/gurl.h"
namespace android_webview {
namespace {
void SetCookieWithOptionsAsyncOnCookieThread(
const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options,
net::CookieStore::SetCookiesCallback callback) {
GetCookieStore()->SetCookieWithOptionsAsync(url, cookie_line, options,
std::move(callback));
}
void SetCanonicalCookieAsyncOnCookieThread(
std::unique_ptr<net::CanonicalCookie> cookie,
std::string source_scheme,
const net::CookieOptions& options,
net::CookieStore::SetCookiesCallback callback) {
GetCookieStore()->SetCanonicalCookieAsync(std::move(cookie),
std::move(source_scheme), options,
std::move(callback));
}
void GetCookieListWithOptionsAsyncOnCookieThread(
const GURL& url,
const net::CookieOptions& options,
net::CookieStore::GetCookieListCallback callback) {
GetCookieStore()->GetCookieListWithOptionsAsync(url, options,
std::move(callback));
}
void GetAllCookiesAsyncOnCookieThread(
net::CookieStore::GetCookieListCallback callback) {
GetCookieStore()->GetAllCookiesAsync(std::move(callback));
}
void DeleteCanonicalCookieAsyncOnCookieThread(
const net::CanonicalCookie& cookie,
net::CookieStore::DeleteCallback callback) {
GetCookieStore()->DeleteCanonicalCookieAsync(cookie, std::move(callback));
}
void DeleteAllCreatedWithinRangeAsyncOnCookieThread(
const net::CookieDeletionInfo::TimeRange& creation_range,
net::CookieStore::DeleteCallback callback) {
GetCookieStore()->DeleteAllCreatedInTimeRangeAsync(creation_range,
std::move(callback));
}
void DeleteAllMatchingInfoAsyncOnCookieThread(
net::CookieDeletionInfo delete_info,
net::CookieStore::DeleteCallback callback) {
GetCookieStore()->DeleteAllMatchingInfoAsync(std::move(delete_info),
std::move(callback));
}
void DeleteSessionCookiesAsyncOnCookieThread(
net::CookieStore::DeleteCallback callback) {
GetCookieStore()->DeleteSessionCookiesAsync(std::move(callback));
}
void FlushStoreOnCookieThread(base::OnceClosure callback) {
GetCookieStore()->FlushStore(std::move(callback));
}
void SetForceKeepSessionStateOnCookieThread() {
GetCookieStore()->SetForceKeepSessionState();
}
void SetCookieableSchemesOnCookieThread(
const std::vector<std::string>& schemes,
net::CookieStore::SetCookieableSchemesCallback callback) {
GetCookieStore()->SetCookieableSchemes(schemes, std::move(callback));
}
} // namespace
AwCookieStoreWrapper::AwCookieStoreWrapper()
: client_task_runner_(base::ThreadTaskRunnerHandle::Get()),
weak_factory_(this) {}
AwCookieStoreWrapper::~AwCookieStoreWrapper() {}
void AwCookieStoreWrapper::SetCookieWithOptionsAsync(
const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options,
net::CookieStore::SetCookiesCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(base::BindOnce(
&SetCookieWithOptionsAsyncOnCookieThread, url, cookie_line, options,
CreateWrappedCallback<net::CanonicalCookie::CookieInclusionStatus>(
std::move(callback))));
}
void AwCookieStoreWrapper::SetCanonicalCookieAsync(
std::unique_ptr<net::CanonicalCookie> cookie,
std::string source_scheme,
const net::CookieOptions& options,
SetCookiesCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(base::BindOnce(
&SetCanonicalCookieAsyncOnCookieThread, std::move(cookie),
std::move(source_scheme), options,
CreateWrappedCallback<net::CanonicalCookie::CookieInclusionStatus>(
std::move(callback))));
}
void AwCookieStoreWrapper::GetCookieListWithOptionsAsync(
const GURL& url,
const net::CookieOptions& options,
GetCookieListCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&GetCookieListWithOptionsAsyncOnCookieThread, url, options,
CreateWrappedGetCookieListCallback(std::move(callback))));
}
void AwCookieStoreWrapper::GetAllCookiesAsync(GetCookieListCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&GetAllCookiesAsyncOnCookieThread,
CreateWrappedGetCookieListCallback(std::move(callback))));
}
void AwCookieStoreWrapper::DeleteCanonicalCookieAsync(
const net::CanonicalCookie& cookie,
DeleteCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&DeleteCanonicalCookieAsyncOnCookieThread, cookie,
CreateWrappedCallback<uint32_t>(std::move(callback))));
}
void AwCookieStoreWrapper::DeleteAllCreatedInTimeRangeAsync(
const net::CookieDeletionInfo::TimeRange& creation_range,
DeleteCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(base::BindOnce(
&DeleteAllCreatedWithinRangeAsyncOnCookieThread, creation_range,
CreateWrappedCallback<uint32_t>(std::move(callback))));
}
void AwCookieStoreWrapper::DeleteAllMatchingInfoAsync(
net::CookieDeletionInfo delete_info,
DeleteCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(base::BindOnce(
&DeleteAllMatchingInfoAsyncOnCookieThread, std::move(delete_info),
CreateWrappedCallback<uint32_t>(std::move(callback))));
}
void AwCookieStoreWrapper::DeleteSessionCookiesAsync(DeleteCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&DeleteSessionCookiesAsyncOnCookieThread,
CreateWrappedCallback<uint32_t>(std::move(callback))));
}
void AwCookieStoreWrapper::FlushStore(base::OnceClosure callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&FlushStoreOnCookieThread,
CreateWrappedClosureCallback(std::move(callback))));
}
void AwCookieStoreWrapper::SetForceKeepSessionState() {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&SetForceKeepSessionStateOnCookieThread));
}
net::CookieChangeDispatcher& AwCookieStoreWrapper::GetChangeDispatcher() {
return change_dispatcher_;
}
void AwCookieStoreWrapper::SetCookieableSchemes(
const std::vector<std::string>& schemes,
SetCookieableSchemesCallback callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
PostTaskToCookieStoreTaskRunner(
base::BindOnce(&SetCookieableSchemesOnCookieThread, schemes,
CreateWrappedCallback<bool>(std::move(callback))));
}
base::OnceClosure AwCookieStoreWrapper::CreateWrappedClosureCallback(
base::OnceClosure callback) {
if (callback.is_null())
return callback;
return base::BindOnce(
base::IgnoreResult(&base::TaskRunner::PostTask), client_task_runner_,
FROM_HERE,
base::BindOnce(&AwCookieStoreWrapper::RunClosureCallback,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void AwCookieStoreWrapper::RunClosureCallback(base::OnceClosure callback) {
DCHECK(client_task_runner_->RunsTasksInCurrentSequence());
std::move(callback).Run();
}
} // namespace android_webview
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_STORE_WRAPPER_H_
#define ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_STORE_WRAPPER_H_
#include <string>
#include "android_webview/browser/net/aw_cookie_change_dispatcher_wrapper.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/task_runner.h"
#include "base/time/time.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_change_dispatcher.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_store.h"
namespace android_webview {
// A cross-threaded cookie store implementation that wraps the CookieManager's
// CookieStore. It posts tasks to the CookieStore's thread, and invokes
// callbacks on the originating thread. Deleting it cancels pending callbacks.
// This is needed to allow Webview to run the CookieStore on its own thread, to
// enable synchronous calls into the store on the IO Thread from Java.
//
// AwCookieStoreWrapper will only grab the CookieStore pointer from the
// CookieManager when it's needed, allowing for lazy creation of the
// CookieStore.
//
// AwCookieStoreWrapper may only be called from the thread on which it's
// created.
//
// The CookieManager must outlive the AwCookieStoreWrapper.
class AwCookieStoreWrapper : public net::CookieStore {
public:
AwCookieStoreWrapper();
~AwCookieStoreWrapper() override;
// CookieStore implementation:
void SetCookieWithOptionsAsync(const GURL& url,
const std::string& cookie_line,
const net::CookieOptions& options,
SetCookiesCallback callback) override;
void SetCanonicalCookieAsync(std::unique_ptr<net::CanonicalCookie> cookie,
std::string source_scheme,
const net::CookieOptions& options,
SetCookiesCallback callback) override;
void GetCookieListWithOptionsAsync(const GURL& url,
const net::CookieOptions& options,
GetCookieListCallback callback) override;
void GetAllCookiesAsync(GetCookieListCallback callback) override;
void DeleteCanonicalCookieAsync(const net::CanonicalCookie& cookie,
DeleteCallback callback) override;
void DeleteAllCreatedInTimeRangeAsync(
const net::CookieDeletionInfo::TimeRange& creation_range,
DeleteCallback callback) override;
void DeleteAllMatchingInfoAsync(net::CookieDeletionInfo delete_info,
DeleteCallback callback) override;
void DeleteSessionCookiesAsync(DeleteCallback callback) override;
void FlushStore(base::OnceClosure callback) override;
void SetForceKeepSessionState() override;
net::CookieChangeDispatcher& GetChangeDispatcher() override;
void SetCookieableSchemes(const std::vector<std::string>& schemes,
SetCookieableSchemesCallback callback) override;
private:
// Used by CreateWrappedCallback below. Takes an argument of Type and posts
// a task to |task_runner| to invoke |callback| with that argument. If
// |weak_cookie_store| is deleted before the task is run, the task will not
// be run.
template <class Type>
static void RunCallbackOnClientThread(
base::TaskRunner* task_runner,
base::WeakPtr<AwCookieStoreWrapper> weak_cookie_store,
base::OnceCallback<void(Type)> callback,
Type argument) {
task_runner->PostTask(
FROM_HERE,
base::BindOnce(&AwCookieStoreWrapper::RunClosureCallback,
weak_cookie_store,
base::BindOnce(std::move(callback), argument)));
}
// Returns a base::Callback that takes an argument of Type and posts a task to
// the |client_task_runner_| to invoke |callback| with that argument.
template <class Type>
base::OnceCallback<void(Type)> CreateWrappedCallback(
base::OnceCallback<void(Type)> callback) {
if (callback.is_null())
return std::move(callback);
return base::BindOnce(
&AwCookieStoreWrapper::RunCallbackOnClientThread<Type>,
base::RetainedRef(client_task_runner_), weak_factory_.GetWeakPtr(),
std::move(callback));
}
// These are the same as above, but specifically for GetCookieListCallback,
// which has two arguments
static void RunGetCookieListCallbackOnClientThread(
base::TaskRunner* task_runner,
base::WeakPtr<AwCookieStoreWrapper> weak_cookie_store,
net::CookieStore::GetCookieListCallback callback,
const net::CookieList& cookies,
const net::CookieStatusList& excluded_cookies) {
task_runner->PostTask(
FROM_HERE,
base::BindOnce(
&AwCookieStoreWrapper::RunClosureCallback, weak_cookie_store,
base::BindOnce(std::move(callback), cookies, excluded_cookies)));
}
net::CookieStore::GetCookieListCallback CreateWrappedGetCookieListCallback(
net::CookieStore::GetCookieListCallback callback) {
if (callback.is_null())
return callback;
return base::BindOnce(
&AwCookieStoreWrapper::RunGetCookieListCallbackOnClientThread,
base::RetainedRef(client_task_runner_), weak_factory_.GetWeakPtr(),
std::move(callback));
}
// Returns a base::OnceClosure that posts a task to the |client_task_runner_|
// to invoke |callback|.
base::OnceClosure CreateWrappedClosureCallback(base::OnceClosure callback);
// Runs |callback|. Used to prevent callbacks from being invoked after the
// AwCookieStoreWrapper has been destroyed.
void RunClosureCallback(base::OnceClosure callback);
scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;
AwCookieChangeDispatcherWrapper change_dispatcher_;
base::WeakPtrFactory<AwCookieStoreWrapper> weak_factory_;
};
} // namespace android_webview
#endif // ANDROID_WEBVIEW_BROWSER_NET_AW_COOKIE_STORE_WRAPPER_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/net/aw_cookie_store_wrapper.h"
#include <stdint.h>
#include <memory>
#include "android_webview/browser/net/init_native_callback.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_store_change_unittest.h"
#include "net/cookies/cookie_store_test_callbacks.h"
#include "net/cookies/cookie_store_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace android_webview {
struct AwCookieStoreWrapperTestTraits {
static std::unique_ptr<net::CookieStore> Create() {
auto cookie_store = std::make_unique<AwCookieStoreWrapper>();
// Android Webview can run multiple tests without restarting the binary,
// so have to delete any cookies the global store may have from an earlier
// test.
net::ResultSavingCookieCallback<uint32_t> callback;
cookie_store->DeleteAllAsync(
base::BindOnce(&net::ResultSavingCookieCallback<uint32_t>::Run,
base::Unretained(&callback)));
callback.WaitUntilDone();
return cookie_store;
}
static void DeliverChangeNotifications() {
base::RunLoop run_loop;
GetCookieStoreTaskRunner()->PostTaskAndReply(
FROM_HERE, base::BindOnce([] { base::RunLoop().RunUntilIdle(); }),
base::BindOnce([](base::RunLoop* run_loop) { run_loop->Quit(); },
base::Unretained(&run_loop)));
run_loop.Run();
base::RunLoop().RunUntilIdle();
}
static const bool supports_http_only = true;
static const bool supports_non_dotted_domains = true;
static const bool preserves_trailing_dots = true;
static const bool filters_schemes = true;
static const bool has_path_prefix_bug = false;
static const bool forbids_setting_empty_name = false;
static const bool supports_global_cookie_tracking = false;
static const bool supports_url_cookie_tracking = false;
static const bool supports_named_cookie_tracking = true;
static const bool supports_multiple_tracking_callbacks = false;
static const bool has_exact_change_cause = true;
static const bool has_exact_change_ordering = true;
static const int creation_time_granularity_in_ms = 0;
};
} // namespace android_webview
// Run the standard cookie tests with AwCookieStoreWrapper. Macro must be in
// net namespace.
namespace net {
INSTANTIATE_TYPED_TEST_SUITE_P(AwCookieStoreWrapper,
CookieStoreTest,
android_webview::AwCookieStoreWrapperTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(AwCookieStoreWrapper,
CookieStoreChangeGlobalTest,
android_webview::AwCookieStoreWrapperTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(AwCookieStoreWrapper,
CookieStoreChangeUrlTest,
android_webview::AwCookieStoreWrapperTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(AwCookieStoreWrapper,
CookieStoreChangeNamedTest,
android_webview::AwCookieStoreWrapperTestTraits);
} // namespace net
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/net/init_native_callback.h"
#include "base/callback.h"
#include "base/single_thread_task_runner.h"
namespace android_webview {
void PostTaskToCookieStoreTaskRunner(base::OnceClosure task) {
GetCookieStoreTaskRunner()->PostTask(FROM_HERE, std::move(task));
}
} // namespace android_webview
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_WEBVIEW_BROWSER_NET_INIT_NATIVE_CALLBACK_H_
#define ANDROID_WEBVIEW_BROWSER_NET_INIT_NATIVE_CALLBACK_H_
#include <memory>
#include "base/memory/ref_counted.h"
namespace base {
class SingleThreadTaskRunner;
}
namespace net {
class CookieStore;
} // namespace net
namespace android_webview {
// Gets the TaskRunner that the CookieStore must be called on.
scoped_refptr<base::SingleThreadTaskRunner> GetCookieStoreTaskRunner();
// Posts |task| to the thread that the global CookieStore lives on.
void PostTaskToCookieStoreTaskRunner(base::OnceClosure task);
// Gets a pointer to the CookieStore managed by the CookieManager.
// The CookieStore is never deleted. May only be called on the
// CookieStore's TaskRunner.
net::CookieStore* GetCookieStore();
} // namespace android_webview
#endif // ANDROID_WEBVIEW_BROWSER_NET_INIT_NATIVE_CALLBACK_H_
......@@ -364,7 +364,6 @@ test("android_webview_unittests") {
"../browser/aw_browser_context_unittest.cc",
"../browser/aw_content_browser_client_unittest.cc",
"../browser/aw_contents_client_bridge_unittest.cc",
"../browser/aw_cookie_access_policy_unittest.cc",
"../browser/aw_form_database_service_unittest.cc",
"../browser/aw_media_url_interceptor_unittest.cc",
"../browser/aw_metrics_service_client_unittest.cc",
......@@ -375,7 +374,6 @@ test("android_webview_unittests") {
"../browser/gfx/test/rendering_test.cc",
"../browser/gfx/test/rendering_test.h",
"../browser/input_stream_unittest.cc",
"../browser/net/aw_cookie_store_wrapper_unittest.cc",
"../browser/network_service/android_stream_reader_url_loader_unittest.cc",
"../browser/network_service/input_stream_reader_unittest.cc",
"../browser/permission/media_access_permission_request_unittest.cc",
......
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