Commit 3e2804af authored by Junbo Ke's avatar Junbo Ke Committed by Commit Bot

[Chromecast] Add a NetworkDelegate.

Bug: internal b/72129709
Test: mannual test, cast_shell_unittests
Change-Id: I54a0e8971e7869cd6c222d9abf089161b84ac467
Reviewed-on: https://chromium-review.googlesource.com/1053293
Commit-Queue: Junbo Ke <juke@chromium.org>
Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561312}
parent 33010a8f
......@@ -41,6 +41,8 @@ cast_source_set("browser") {
"cast_net_log.h",
"cast_network_delegate.cc",
"cast_network_delegate.h",
"cast_network_request_interceptor.cc",
"cast_network_request_interceptor.h",
"cast_permission_manager.cc",
"cast_permission_manager.h",
"cast_quota_permission_context.cc",
......@@ -95,7 +97,7 @@ cast_source_set("browser") {
sources += [
"cast_browser_main_parts_simple.cc",
"cast_content_browser_client_simple.cc",
"cast_network_delegate_simple.cc",
"cast_network_request_interceptor_simple.cc",
"pref_service_helper_simple.cc",
]
}
......@@ -458,6 +460,7 @@ cast_source_set("unittests") {
sources = [
"bluetooth/cast_bluetooth_chooser_unittest.cc",
"cast_media_blocker_unittest.cc",
"cast_network_delegate_unittest.cc",
"cast_touch_device_manager_unittest.cc",
"devtools/cast_devtools_manager_delegate_unittest.cc",
"lru_renderer_cache_test.cc",
......
......@@ -4,20 +4,47 @@
#include "chromecast/browser/cast_network_delegate.h"
#include <utility>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "chromecast/base/chromecast_switches.h"
#include "chromecast/browser/cast_navigation_ui_data.h"
#include "chromecast/browser/cast_network_request_interceptor.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/common/child_process_host.h"
#include "net/base/net_errors.h"
namespace chromecast {
namespace shell {
CastNetworkDelegate::CastNetworkDelegate() {
std::unique_ptr<CastNetworkDelegate> CastNetworkDelegate::Create() {
return std::make_unique<CastNetworkDelegate>(
CastNetworkRequestInterceptor::Create());
}
CastNetworkDelegate::CastNetworkDelegate(
std::unique_ptr<CastNetworkRequestInterceptor> network_request_interceptor)
: network_request_interceptor_(std::move(network_request_interceptor)) {
DCHECK(network_request_interceptor_);
DETACH_FROM_THREAD(thread_checker_);
}
CastNetworkDelegate::~CastNetworkDelegate() {
}
void CastNetworkDelegate::Initialize() {
network_request_interceptor_->Initialize();
}
bool CastNetworkDelegate::IsWhitelisted(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth) const {
return network_request_interceptor_->IsWhiteListed(
gurl, session_id, render_process_id, for_device_auth);
}
bool CastNetworkDelegate::OnCanAccessFile(
const net::URLRequest& request,
const base::FilePath& original_path,
......@@ -32,5 +59,50 @@ bool CastNetworkDelegate::OnCanAccessFile(
return false;
}
int CastNetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
if (!network_request_interceptor_->IsInitialized())
return net::OK;
// Get session id
std::string session_id;
const content::ResourceRequestInfo* request_info =
content::ResourceRequestInfo::ForRequest(request);
CastNavigationUIData* nav_data =
request_info ? static_cast<CastNavigationUIData*>(
request_info->GetNavigationUIData())
: nullptr;
if (nav_data) {
session_id = nav_data->session_id();
}
// Get render process PID
int render_process_id;
int render_frame_id;
if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
request, &render_process_id, &render_frame_id)) {
render_process_id = content::ChildProcessHost::kInvalidUniqueID;
}
return network_request_interceptor_->OnBeforeURLRequest(
request, session_id, render_process_id, callback, new_url);
}
int CastNetworkDelegate::OnBeforeStartTransaction(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
if (!network_request_interceptor_->IsInitialized())
return net::OK;
return network_request_interceptor_->OnBeforeStartTransaction(
request, callback, headers);
}
void CastNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
if (network_request_interceptor_->IsInitialized())
network_request_interceptor_->OnURLRequestDestroyed(request);
}
} // namespace shell
} // namespace chromecast
......@@ -5,31 +5,48 @@
#ifndef CHROMECAST_BROWSER_CAST_NETWORK_DELEGATE_H_
#define CHROMECAST_BROWSER_CAST_NETWORK_DELEGATE_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "net/base/network_delegate_impl.h"
namespace chromecast {
class CastNetworkRequestInterceptor;
namespace shell {
class CastNetworkDelegate : public net::NetworkDelegateImpl {
public:
static std::unique_ptr<CastNetworkDelegate> Create();
CastNetworkDelegate();
explicit CastNetworkDelegate(std::unique_ptr<CastNetworkRequestInterceptor>
network_request_interceptor);
~CastNetworkDelegate() override;
virtual void Initialize() = 0;
void Initialize();
virtual bool IsWhitelisted(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth) const = 0;
bool IsWhitelisted(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth) const;
private:
// net::NetworkDelegate implementation:
bool OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& original_path,
const base::FilePath& absolute_path) const override;
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnURLRequestDestroyed(net::URLRequest* request) override;
const std::unique_ptr<CastNetworkRequestInterceptor>
network_request_interceptor_;
DISALLOW_COPY_AND_ASSIGN(CastNetworkDelegate);
};
......
// 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 <memory>
#include "chromecast/browser/cast_network_delegate.h"
#include "base/macros.h"
#include "url/gurl.h"
namespace chromecast {
namespace shell {
namespace {
class CastNetworkDelegateSimple : public CastNetworkDelegate {
public:
CastNetworkDelegateSimple() {}
private:
// CastNetworkDelegate implementation:
void Initialize() override {}
bool IsWhitelisted(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth) const override {
return false;
}
DISALLOW_COPY_AND_ASSIGN(CastNetworkDelegateSimple);
};
} // namespace
// static
std::unique_ptr<CastNetworkDelegate> CastNetworkDelegate::Create() {
return std::make_unique<CastNetworkDelegateSimple>();
}
} // namespace shell
} // namespace chromecast
// 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 "chromecast/browser/cast_network_delegate.h"
#include <utility>
#include "base/test/scoped_task_environment.h"
#include "chromecast/browser/cast_network_request_interceptor.h"
#include "net/base/request_priority.h"
#include "net/base/test_completion_callback.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Return;
namespace chromecast {
namespace shell {
namespace {
class MockCastNetworkRequestInterceptor : public CastNetworkRequestInterceptor {
public:
MockCastNetworkRequestInterceptor() {}
~MockCastNetworkRequestInterceptor() override {}
MOCK_CONST_METHOD4(IsWhiteListed,
bool(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth));
MOCK_METHOD0(Initialize, void());
MOCK_METHOD0(IsInitialized, bool());
MOCK_METHOD5(OnBeforeURLRequest,
int(net::URLRequest* request,
const std::string& session_id,
int render_process_id,
const net::CompletionCallback& callback,
GURL* new_url));
MOCK_METHOD3(OnBeforeStartTransaction,
int(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers));
MOCK_METHOD1(OnURLRequestDestroyed, void(net::URLRequest* request));
private:
DISALLOW_COPY_AND_ASSIGN(MockCastNetworkRequestInterceptor);
};
} // namespace
class CastNetworkDelegateTest : public testing::Test {
public:
CastNetworkDelegateTest()
: task_env_(base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT,
base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED),
context_(true) {
context_.Init();
std::unique_ptr<MockCastNetworkRequestInterceptor>
cast_network_request_interceptor_ =
std::make_unique<MockCastNetworkRequestInterceptor>();
cast_network_request_interceptor_ptr_ =
cast_network_request_interceptor_.get();
cast_network_delegate_ = std::make_unique<CastNetworkDelegate>(
std::move(cast_network_request_interceptor_));
}
~CastNetworkDelegateTest() override{};
protected:
base::test::ScopedTaskEnvironment task_env_;
std::unique_ptr<CastNetworkDelegate> cast_network_delegate_;
MockCastNetworkRequestInterceptor* cast_network_request_interceptor_ptr_;
net::TestURLRequestContext context_;
net::TestDelegate delegate_;
};
TEST_F(CastNetworkDelegateTest, NotifyBeforeURLRequest) {
std::unique_ptr<net::URLRequest> request = context_.CreateRequest(
GURL(), net::IDLE, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
net::TestCompletionCallback completion_callback;
EXPECT_CALL(*cast_network_request_interceptor_ptr_, IsInitialized())
.WillOnce(Return(true));
EXPECT_CALL(*cast_network_request_interceptor_ptr_,
OnBeforeURLRequest(_, _, _, _, _));
cast_network_delegate_->NotifyBeforeURLRequest(
request.get(), completion_callback.callback(), NULL);
task_env_.RunUntilIdle();
}
TEST_F(CastNetworkDelegateTest, NotifyBeforeStartTransaction) {
std::unique_ptr<net::URLRequest> request = context_.CreateRequest(
GURL(), net::IDLE, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
net::TestCompletionCallback completion_callback;
std::unique_ptr<net::HttpRequestHeaders> request_headers(
new net::HttpRequestHeaders());
EXPECT_CALL(*cast_network_request_interceptor_ptr_, IsInitialized())
.WillOnce(Return(true));
EXPECT_CALL(*cast_network_request_interceptor_ptr_,
OnBeforeStartTransaction(_, _, _));
cast_network_delegate_->NotifyBeforeStartTransaction(
request.get(), completion_callback.callback(), request_headers.get());
task_env_.RunUntilIdle();
}
TEST_F(CastNetworkDelegateTest, NotifyBeforeURLRequestDestroyed) {
std::unique_ptr<net::URLRequest> request = context_.CreateRequest(
GURL(), net::IDLE, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
EXPECT_CALL(*cast_network_request_interceptor_ptr_, IsInitialized())
.WillOnce(Return(true));
EXPECT_CALL(*cast_network_request_interceptor_ptr_,
OnURLRequestDestroyed(request.get()));
cast_network_delegate_->NotifyURLRequestDestroyed(request.get());
task_env_.RunUntilIdle();
}
} // namespace shell
} // namespace chromecast
// 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 "chromecast/browser/cast_network_request_interceptor.h"
#include "net/base/net_errors.h"
namespace chromecast {
CastNetworkRequestInterceptor::CastNetworkRequestInterceptor() = default;
CastNetworkRequestInterceptor::~CastNetworkRequestInterceptor() = default;
bool CastNetworkRequestInterceptor::IsWhiteListed(
const GURL& /* gurl */,
const std::string& /* session_id */,
int /* render_process_id */,
bool /* for_device_auth */) const {
return false;
}
void CastNetworkRequestInterceptor::Initialize() {}
bool CastNetworkRequestInterceptor::IsInitialized() {
return true;
}
int CastNetworkRequestInterceptor::OnBeforeURLRequest(
net::URLRequest* /* request */,
const std::string& /* session_id */,
int /* render_process_id */,
const net::CompletionCallback& /* callback */,
GURL* /* new_url */) {
return net::OK;
}
int CastNetworkRequestInterceptor::OnBeforeStartTransaction(
net::URLRequest* /* request */,
const net::CompletionCallback& /* callback */,
net::HttpRequestHeaders* headers) {
return net::OK;
}
void CastNetworkRequestInterceptor::OnURLRequestDestroyed(
net::URLRequest* /* request */) {}
} // namespace chromecast
// 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 CHROMECAST_BROWSER_CAST_NETWORK_REQUEST_INTERCEPTOR_H_
#define CHROMECAST_BROWSER_CAST_NETWORK_REQUEST_INTERCEPTOR_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/sequence_checker.h"
#include "net/base/completion_callback.h"
class GURL;
namespace net {
class HttpRequestHeaders;
class URLRequest;
} // namespace net
namespace chromecast {
// Used to intercept the network request and modify the headers.
class CastNetworkRequestInterceptor {
public:
static std::unique_ptr<CastNetworkRequestInterceptor> Create();
CastNetworkRequestInterceptor();
virtual ~CastNetworkRequestInterceptor();
// TODO(juke): Remove render_process_id.
virtual bool IsWhiteListed(const GURL& gurl,
const std::string& session_id,
int render_process_id,
bool for_device_auth) const;
virtual void Initialize();
virtual bool IsInitialized();
virtual int OnBeforeURLRequest(net::URLRequest* request,
const std::string& session_id,
int render_process_id,
const net::CompletionCallback& callback,
GURL* new_url);
virtual int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers);
virtual void OnURLRequestDestroyed(net::URLRequest* request);
private:
DISALLOW_COPY_AND_ASSIGN(CastNetworkRequestInterceptor);
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_CAST_NETWORK_REQUEST_INTERCEPTOR_H_
// 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 "chromecast/browser/cast_network_request_interceptor.h"
namespace chromecast {
std::unique_ptr<CastNetworkRequestInterceptor>
CastNetworkRequestInterceptor::Create() {
return std::make_unique<CastNetworkRequestInterceptor>();
}
} // namespace chromecast
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