Commit 109cbbf8 authored by Sharon Yang's avatar Sharon Yang Committed by Commit Bot

[fuchsia] Add navigation blocking support to WebEngine

Use NavigationPolicyThrottle and NavigationPolicyHandler in FrameImpl
and WebEngineContentRendererClient so the new functionality is exposed
to clients

Tests: NavigationPolicyTest
Bug: 1117601
Change-Id: If4b96b72b60792912f071513562bec0d0f920254
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2504581
Commit-Queue: Sharon Yang <yangsharon@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827137}
parent 3e9df476
......@@ -57,6 +57,9 @@ class TestNavigationListener : public fuchsia::web::NavigationEventListener {
// Returns the title.
std::string title() { return current_state_.title(); }
// Returns the current navigation state.
fuchsia::web::NavigationState* current_state() { return &current_state_; }
// Register a callback which intercepts the execution of the event
// acknowledgement callback. |before_ack| takes ownership of the
// acknowledgement callback and the responsibility for executing it.
......
......@@ -305,6 +305,8 @@ test("web_engine_browsertests") {
"browser/cast_streaming_browsertest.cc",
"browser/content_directory_browsertest.cc",
"browser/context_impl_browsertest.cc",
"browser/fake_navigation_policy_provider.cc",
"browser/fake_navigation_policy_provider.h",
"browser/fake_semantic_tree.cc",
"browser/fake_semantic_tree.h",
"browser/fake_semantics_manager.cc",
......@@ -312,6 +314,7 @@ test("web_engine_browsertests") {
"browser/frame_impl_browsertest.cc",
"browser/headless_browsertest.cc",
"browser/media_browsertest.cc",
"browser/navigation_policy_browsertest.cc",
"browser/theme_manager_browsertest.cc",
]
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
......
......@@ -4,7 +4,6 @@
#include "fuchsia/engine/browser/fake_navigation_policy_provider.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "testing/gtest/include/gtest/gtest.h"
......
......@@ -43,6 +43,7 @@
#include "fuchsia/engine/browser/frame_layout_manager.h"
#include "fuchsia/engine/browser/frame_window_tree_host.h"
#include "fuchsia/engine/browser/media_player_impl.h"
#include "fuchsia/engine/browser/navigation_policy_handler.h"
#include "fuchsia/engine/browser/web_engine_devtools_controller.h"
#include "fuchsia/engine/common/cast_streaming.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
......@@ -867,6 +868,13 @@ void FrameImpl::GetPrivateMemorySize(GetPrivateMemorySizeCallback callback) {
callback(task_stats.mem_private_bytes);
}
void FrameImpl::SetNavigationPolicyProvider(
fuchsia::web::NavigationPolicyProviderParams params,
fidl::InterfaceHandle<fuchsia::web::NavigationPolicyProvider> provider) {
navigation_policy_handler_ = std::make_unique<NavigationPolicyHandler>(
std::move(params), std::move(provider));
}
void FrameImpl::SetPreferredTheme(fuchsia::settings::ThemeType theme) {
theme_manager_.SetTheme(theme, base::BindOnce(
[](FrameImpl* frame_impl, bool result) {
......
......@@ -42,6 +42,7 @@ class ContextImpl;
class FrameWindowTreeHost;
class FrameLayoutManager;
class MediaPlayerImpl;
class NavigationPolicyHandler;
// Implementation of fuchsia.web.Frame based on content::WebContents.
class FrameImpl : public fuchsia::web::Frame,
......@@ -75,6 +76,10 @@ class FrameImpl : public fuchsia::web::Frame,
return &url_request_rewrite_rules_manager_;
}
NavigationPolicyHandler* navigation_policy_handler() {
return navigation_policy_handler_.get();
}
zx::unowned_channel GetBindingChannelForTest() const;
content::WebContents* web_contents_for_test() const {
return web_contents_.get();
......@@ -191,6 +196,10 @@ class FrameImpl : public fuchsia::web::Frame,
const content::MediaPlayerId& id,
WebContentsObserver::MediaStoppedReason reason) override;
void GetPrivateMemorySize(GetPrivateMemorySizeCallback callback) override;
void SetNavigationPolicyProvider(
fuchsia::web::NavigationPolicyProviderParams params,
fidl::InterfaceHandle<fuchsia::web::NavigationPolicyProvider> provider)
override;
void SetPreferredTheme(fuchsia::settings::ThemeType theme) override;
// content::WebContentsDelegate implementation.
......@@ -259,6 +268,7 @@ class FrameImpl : public fuchsia::web::Frame,
logging::LogSeverity log_level_;
UrlRequestRewriteRulesManager url_request_rewrite_rules_manager_;
FramePermissionController permission_controller_;
std::unique_ptr<NavigationPolicyHandler> navigation_policy_handler_;
// Session ID to use for fuchsia.media.AudioConsumer. Set with
// SetMediaSessionId().
......
// Copyright 2020 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 <fuchsia/web/cpp/fidl.h>
#include "content/public/test/browser_test.h"
#include "fuchsia/base/frame_test_util.h"
#include "fuchsia/base/test_navigation_listener.h"
#include "fuchsia/engine/browser/fake_navigation_policy_provider.h"
#include "fuchsia/engine/browser/frame_impl.h"
#include "fuchsia/engine/browser/navigation_policy_handler.h"
#include "fuchsia/engine/test/test_data.h"
#include "fuchsia/engine/test/web_engine_browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kPagePath[] = "/title1.html";
const char kPageTitle[] = "title 1";
} // namespace
class NavigationPolicyTest : public cr_fuchsia::WebEngineBrowserTest {
public:
NavigationPolicyTest() : policy_provider_binding_(&policy_provider_) {
cr_fuchsia::WebEngineBrowserTest::set_test_server_root(
base::FilePath(cr_fuchsia::kTestServerRoot));
}
~NavigationPolicyTest() override = default;
NavigationPolicyTest(const NavigationPolicyTest&) = delete;
NavigationPolicyTest& operator=(const NavigationPolicyTest&) = delete;
void SetUp() override { cr_fuchsia::WebEngineBrowserTest::SetUp(); }
void SetUpOnMainThread() override {
frame_ptr_ =
cr_fuchsia::WebEngineBrowserTest::CreateFrame(&navigation_listener_);
frame_impl_ = context_impl()->GetFrameImplForTest(&frame_ptr_);
frame_ptr_->GetNavigationController(navigation_controller_.NewRequest());
ASSERT_TRUE(embedded_test_server()->Start());
// Register a NavigationPolicyProvider to the frame.
fuchsia::web::NavigationPolicyProviderParams params;
*params.mutable_main_frame_phases() = fuchsia::web::NavigationPhase::START;
*params.mutable_subframe_phases() = fuchsia::web::NavigationPhase::REDIRECT;
frame_ptr_->SetNavigationPolicyProvider(
std::move(params), policy_provider_binding_.NewBinding());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(
frame_impl_->navigation_policy_handler()->is_provider_connected());
}
protected:
fuchsia::web::FramePtr frame_ptr_;
FrameImpl* frame_impl_;
fidl::Binding<fuchsia::web::NavigationPolicyProvider>
policy_provider_binding_;
FakeNavigationPolicyProvider policy_provider_;
cr_fuchsia::TestNavigationListener navigation_listener_;
fuchsia::web::NavigationControllerPtr navigation_controller_;
};
IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Proceed) {
policy_provider_.set_should_reject_request(false);
GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
ASSERT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
navigation_controller_.get(), fuchsia::web::LoadUrlParams(),
page_url.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(page_url, kPageTitle);
}
IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Deferred) {
policy_provider_.set_should_reject_request(true);
GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
// Make sure the page has had time to load, but has not actually loaded, since
// we cannot check for the absence of page data.
ASSERT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
navigation_controller_.get(), fuchsia::web::LoadUrlParams(),
page_url.spec()));
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(200));
run_loop.Run();
// Make sure an up to date NavigationState is used.
fuchsia::web::NavigationState state;
state.set_url(page_url.spec());
navigation_listener_.RunUntilNavigationStateMatches(state);
auto* current_state = navigation_listener_.current_state();
EXPECT_TRUE(current_state->has_is_main_document_loaded());
EXPECT_FALSE(current_state->is_main_document_loaded());
}
......@@ -13,11 +13,13 @@
#include "base/strings/string_split.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/devtools_manager_delegate.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/user_agent.h"
#include "fuchsia/base/fuchsia_dir_scheme.h"
#include "fuchsia/engine/browser/frame_impl.h"
#include "fuchsia/engine/browser/navigation_policy_throttle.h"
#include "fuchsia/engine/browser/url_request_rewrite_rules_manager.h"
#include "fuchsia/engine/browser/web_engine_browser_context.h"
#include "fuchsia/engine/browser/web_engine_browser_interface_binders.h"
......@@ -188,6 +190,22 @@ void WebEngineContentBrowserClient::AppendExtraCommandLineSwitches(
kSwitchesToCopy, base::size(kSwitchesToCopy));
}
std::vector<std::unique_ptr<content::NavigationThrottle>>
WebEngineContentBrowserClient::CreateThrottlesForNavigation(
content::NavigationHandle* navigation_handle) {
std::vector<std::unique_ptr<content::NavigationThrottle>> throttles;
auto* frame_impl =
FrameImpl::FromWebContents(navigation_handle->GetWebContents());
// Only create throttle if FrameImpl has a NavigationPolicyProvider,
// indicating an interest in navigations.
if (frame_impl->navigation_policy_handler()) {
throttles.push_back(std::make_unique<NavigationPolicyThrottle>(
navigation_handle, frame_impl->navigation_policy_handler()));
}
return throttles;
}
std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
WebEngineContentBrowserClient::CreateURLLoaderThrottles(
const network::ResourceRequest& request,
......
......@@ -51,6 +51,9 @@ class WebEngineContentBrowserClient : public content::ContentBrowserClient {
bool ShouldEnableStrictSiteIsolation() final;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) final;
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(
content::NavigationHandle* navigation_handle) override;
std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
CreateURLLoaderThrottles(
const network::ResourceRequest& request,
......
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