Commit 936b660c authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

Network Service: Implement OnCanEnablePrivacyMode in network delegate

This matches the implementation in ChromeNetworkDelegate.

Bug: 789632
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I68c00d635fd1eb527aa2e80851acf8a3fd7325af
Reviewed-on: https://chromium-review.googlesource.com/1104937
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568606}
parent b4c10fae
...@@ -199,6 +199,7 @@ source_set("tests") { ...@@ -199,6 +199,7 @@ source_set("tests") {
"keepalive_statistics_recorder_unittest.cc", "keepalive_statistics_recorder_unittest.cc",
"network_change_manager_unittest.cc", "network_change_manager_unittest.cc",
"network_context_unittest.cc", "network_context_unittest.cc",
"network_service_network_delegate_unittest.cc",
"network_service_unittest.cc", "network_service_unittest.cc",
"network_usage_accumulator_unittest.cc", "network_usage_accumulator_unittest.cc",
"proxy_config_service_mojo_unittest.cc", "proxy_config_service_mojo_unittest.cc",
......
...@@ -56,4 +56,12 @@ bool NetworkServiceNetworkDelegate::OnCanAccessFile( ...@@ -56,4 +56,12 @@ bool NetworkServiceNetworkDelegate::OnCanAccessFile(
return true; return true;
} }
bool NetworkServiceNetworkDelegate::OnCanEnablePrivacyMode(
const GURL& url,
const GURL& site_for_cookies) const {
return !network_context_->cookie_manager()
->cookie_settings()
.IsCookieAccessAllowed(url, site_for_cookies);
}
} // namespace network } // namespace network
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef SERVICES_NETWORK_NETWORK_SERVICE_NETWORK_DELEGATE_H_ #ifndef SERVICES_NETWORK_NETWORK_SERVICE_NETWORK_DELEGATE_H_
#define SERVICES_NETWORK_NETWORK_SERVICE_NETWORK_DELEGATE_H_ #define SERVICES_NETWORK_NETWORK_SERVICE_NETWORK_DELEGATE_H_
#include "base/component_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "net/base/network_delegate_impl.h" #include "net/base/network_delegate_impl.h"
...@@ -12,7 +13,8 @@ namespace network { ...@@ -12,7 +13,8 @@ namespace network {
class NetworkContext; class NetworkContext;
class NetworkServiceNetworkDelegate : public net::NetworkDelegateImpl { class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkServiceNetworkDelegate
: public net::NetworkDelegateImpl {
public: public:
// |network_context| is guaranteed to outlive this class. // |network_context| is guaranteed to outlive this class.
explicit NetworkServiceNetworkDelegate(NetworkContext* network_context); explicit NetworkServiceNetworkDelegate(NetworkContext* network_context);
...@@ -27,6 +29,8 @@ class NetworkServiceNetworkDelegate : public net::NetworkDelegateImpl { ...@@ -27,6 +29,8 @@ class NetworkServiceNetworkDelegate : public net::NetworkDelegateImpl {
bool OnCanAccessFile(const net::URLRequest& request, bool OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& original_path, const base::FilePath& original_path,
const base::FilePath& absolute_path) const override; const base::FilePath& absolute_path) const override;
bool OnCanEnablePrivacyMode(const GURL& url,
const GURL& site_for_cookies) const override;
NetworkContext* network_context_; NetworkContext* network_context_;
......
// 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 "services/network/network_service_network_delegate.h"
#include "base/test/scoped_task_environment.h"
#include "services/network/network_context.h"
#include "services/network/network_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
namespace {
const GURL kURL("http://foo.com");
const GURL kOtherURL("http://other.com");
class NetworkServiceNetworkDelegateTest : public testing::Test {
public:
NetworkServiceNetworkDelegateTest()
: network_service_(NetworkService::CreateForTesting()) {
mojom::NetworkContextPtr network_context_ptr;
network_context_ = std::make_unique<NetworkContext>(
network_service_.get(), mojo::MakeRequest(&network_context_ptr),
mojom::NetworkContextParams::New());
}
void SetContentSetting(const GURL& primary_pattern,
const GURL& secondary_pattern,
ContentSetting setting) {
network_context_->cookie_manager()->SetContentSettings(
{ContentSettingPatternSource(
ContentSettingsPattern::FromURL(primary_pattern),
ContentSettingsPattern::FromURL(secondary_pattern),
base::Value(setting), std::string(), false)});
}
void SetBlockThirdParty(bool block) {
network_context_->cookie_manager()->BlockThirdPartyCookies(block);
}
NetworkContext* network_context() const { return network_context_.get(); }
private:
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<NetworkService> network_service_;
std::unique_ptr<NetworkContext> network_context_;
};
TEST_F(NetworkServiceNetworkDelegateTest, PrivacyModeDisabledByDefault) {
NetworkServiceNetworkDelegate delegate(network_context());
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
}
TEST_F(NetworkServiceNetworkDelegateTest, PrivacyModeEnabledIfCookiesBlocked) {
NetworkServiceNetworkDelegate delegate(network_context());
SetContentSetting(kURL, kOtherURL, CONTENT_SETTING_BLOCK);
EXPECT_TRUE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
}
TEST_F(NetworkServiceNetworkDelegateTest, PrivacyModeDisabledIfCookiesAllowed) {
NetworkServiceNetworkDelegate delegate(network_context());
SetContentSetting(kURL, kOtherURL, CONTENT_SETTING_ALLOW);
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
}
TEST_F(NetworkServiceNetworkDelegateTest,
PrivacyModeDisabledIfCookiesSettingForOtherURL) {
NetworkServiceNetworkDelegate delegate(network_context());
// URLs are switched so setting should not apply.
SetContentSetting(kOtherURL, kURL, CONTENT_SETTING_BLOCK);
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
}
TEST_F(NetworkServiceNetworkDelegateTest,
PrivacyModeEnabledIfThirdPartyCookiesBlocked) {
NetworkServiceNetworkDelegate delegate(network_context());
SetBlockThirdParty(true);
EXPECT_TRUE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kURL));
SetBlockThirdParty(false);
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kOtherURL));
EXPECT_FALSE(delegate.CanEnablePrivacyMode(kURL, kURL));
}
} // namespace
} // namespace network
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