blob: 756bd29b42b54664b1db78acd19c5801adef2e22 [file]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/render_frame_impl.h"
#include <stdint.h>
#include <optional>
#include <tuple>
#include <utility>
#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/debug/leak_annotations.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/protected_memory_buildflags.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "content/common/features.h"
#include "content/common/renderer.mojom.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/extra_mojo_js_features.mojom.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/test/frame_load_waiter.h"
#include "content/public/test/local_frame_host_interceptor.h"
#include "content/public/test/policy_container_utils.h"
#include "content/public/test/render_view_test.h"
#include "content/public/test/test_utils.h"
#include "content/renderer/agent_scheduling_group.h"
#include "content/renderer/document_state.h"
#include "content/renderer/mojo/blink_interface_registry_impl.h"
#include "content/renderer/navigation_state.h"
#include "content/test/frame_host_test_interface.mojom.h"
#include "content/test/test_render_frame.h"
#include "media/media_buildflags.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/navigation/navigation_params.h"
#include "third_party/blink/public/common/navigation/navigation_params_mojom_traits.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/autoplay/autoplay.mojom.h"
#include "third_party/blink/public/mojom/browser_interface_broker.mojom-forward.h"
#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
#include "third_party/blink/public/mojom/frame/frame_replication_state.mojom.h"
#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom.h"
#include "third_party/blink/public/mojom/frame/viewport_intersection_state.mojom.h"
#include "third_party/blink/public/mojom/widget/platform_widget.mojom.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/platform/web_v8_value_converter.h"
#include "third_party/blink/public/test/test_web_frame_content_dumper.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_history_item.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_local_frame_observer.h"
#include "third_party/blink/public/web/web_script_source.h"
#include "third_party/blink/public/web/web_v8_features.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/display/screen_info.h"
#include "ui/display/screen_infos.h"
#include "ui/gfx/geometry/point.h"
#include "ui/native_theme/native_theme.h"
#include "url/origin.h"
#if BUILDFLAG(ENABLE_LIBRARY_CDMS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
#include "content/public/common/content_client.h"
#include "content/public/renderer/key_system_support.h"
#include "content/public/renderer/render_frame_observer.h"
#include "media/base/cdm_capability.h"
#include "media/base/key_system_capability.h"
#include "media/base/key_system_info.h"
#include "media/base/media_client.h"
#include "media/mojo/mojom/key_system_support.mojom.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom.h"
#endif
using blink::WebURLRequest;
namespace content {
namespace {
constexpr int32_t kSubframeRouteId = 20;
constexpr int32_t kSubframeWidgetRouteId = 21;
const char kParentFrameHTML[] = "Parent frame <iframe name='frame'></iframe>";
const char kSimpleScriptHtml[] = "<script>var x = 1;</script>";
const char kAutoplayTestOrigin[] = "https://www.google.com";
} // namespace
// RenderFrameImplTest creates a RenderFrameImpl that is a child of the
// main frame, and has its own RenderWidget. This behaves like an out
// of process frame even though it is in the same process as its parent.
class RenderFrameImplTest : public RenderViewTest {
public:
explicit RenderFrameImplTest(
RenderFrameImpl::CreateRenderFrameImplFunction hook_function = nullptr)
: RenderViewTest(/*hook_render_frame_creation=*/!hook_function) {
if (hook_function) {
RenderFrameImpl::InstallCreateHook(hook_function);
}
}
~RenderFrameImplTest() override = default;
void SetUp() override {
RenderViewTest::SetUp();
EXPECT_TRUE(GetMainRenderFrame()->is_main_frame_);
IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
LoadHTML(kParentFrameHTML);
LoadChildFrame();
}
void LoadChildFrame() {
child_frame_token_ = blink::LocalFrameToken();
mojom::CreateFrameWidgetParamsPtr widget_params =
mojom::CreateFrameWidgetParams::New();
widget_params->routing_id = kSubframeWidgetRouteId;
widget_params->visual_properties.new_size_device_px = gfx::Size(100, 100);
widget_params->visual_properties.screen_infos =
display::ScreenInfos(display::ScreenInfo());
widget_remote_.reset();
mojo::PendingAssociatedReceiver<blink::mojom::Widget>
blink_widget_receiver =
widget_remote_.BindNewEndpointAndPassDedicatedReceiver();
mojo::AssociatedRemote<blink::mojom::WidgetHost> blink_widget_host;
mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost>
blink_widget_host_receiver =
blink_widget_host.BindNewEndpointAndPassDedicatedReceiver();
widget_params->widget = std::move(blink_widget_receiver);
widget_params->widget_host = blink_widget_host.Unbind();
auto frame_replication_state = blink::mojom::FrameReplicationState::New();
frame_replication_state->name = "frame";
frame_replication_state->unique_name = "frame-uniqueName";
auto remote_frame_interfaces =
blink::mojom::RemoteFrameInterfacesFromBrowser::New();
mojo::AssociatedRemote<blink::mojom::RemoteFrame> frame;
remote_frame_interfaces->frame_receiver =
frame.BindNewEndpointAndPassDedicatedReceiver();
mojo::AssociatedRemote<blink::mojom::RemoteFrameHost> frame_host;
std::ignore = frame_host.BindNewEndpointAndPassDedicatedReceiver();
remote_frame_interfaces->frame_host = frame_host.Unbind();
auto remote_main_frame_interfaces =
blink::mojom::RemoteMainFrameInterfaces::New();
mojo::AssociatedRemote<blink::mojom::RemoteMainFrame> main_frame;
remote_main_frame_interfaces->main_frame =
main_frame.BindNewEndpointAndPassDedicatedReceiver();
mojo::AssociatedRemote<blink::mojom::RemoteMainFrameHost> main_frame_host;
std::ignore = main_frame_host.BindNewEndpointAndPassDedicatedReceiver();
remote_main_frame_interfaces->main_frame_host = main_frame_host.Unbind();
blink::RemoteFrameToken remote_child_token = blink::RemoteFrameToken();
RenderFrameImpl::FromWebFrame(
GetMainRenderFrame()->GetWebFrame()->FirstChild())
->Unload(false, frame_replication_state->Clone(), remote_child_token,
std::move(remote_frame_interfaces),
std::move(remote_main_frame_interfaces),
/*devtools_frame_token=*/std::nullopt);
MockPolicyContainerHost mock_policy_container_host;
RenderFrameImpl::CreateFrame(
*agent_scheduling_group_, child_frame_token_, kSubframeRouteId,
TestRenderFrame::CreateStubFrameReceiver(),
TestRenderFrame::CreateStubBrowserInterfaceBrokerRemote(),
TestRenderFrame::CreateStubAssociatedInterfaceProviderRemote(),
/*web_view=*/nullptr,
/*previous_frame_token=*/std::nullopt,
/*opener_frame_token=*/std::nullopt,
/*parent_frame_token=*/blink::FrameToken(remote_child_token),
/*previous_sibling_frame_token=*/std::nullopt,
base::UnguessableToken::Create(),
/*navigation_metrics_token=*/std::nullopt,
blink::mojom::TreeScopeType::kDocument,
std::move(frame_replication_state), std::move(widget_params),
blink::mojom::FrameOwnerProperties::New(),
/*has_committed_real_load=*/true, blink::DocumentToken(),
blink::InitiatorStateToken(),
blink::mojom::PolicyContainer::New(
blink::mojom::PolicyContainerPolicies::New(),
mock_policy_container_host.BindNewEndpointAndPassDedicatedRemote()),
/*is_for_nested_main_frame=*/false);
EXPECT_FALSE(child_frame().is_main_frame_);
}
void TearDown() override {
#if defined(LEAK_SANITIZER)
// Do this before shutting down V8 in RenderViewTest::TearDown().
// http://crbug.com/328552
__lsan_do_leak_check();
#endif
RenderViewTest::TearDown();
}
TestRenderFrame* GetMainRenderFrame() {
return static_cast<TestRenderFrame*>(RenderViewTest::GetMainRenderFrame());
}
TestRenderFrame& child_frame() const {
return CHECK_DEREF(
static_cast<TestRenderFrame*>(RenderFrameImpl::FromWebFrame(
blink::WebLocalFrame::FromFrameToken(child_frame_token_))));
}
blink::WebFrameWidget* frame_widget() const {
return child_frame().GetLocalRootWebFrameWidget();
}
mojo::AssociatedRemote<blink::mojom::Widget>& widget_remote() {
return widget_remote_;
}
static url::Origin GetOriginForFrame(TestRenderFrame* frame) {
return url::Origin(frame->GetWebFrame()->GetSecurityOrigin());
}
static int32_t AutoplayFlagsForFrame(const TestRenderFrame& frame) {
return frame.GetWebView()->AutoplayFlagsForTest();
}
static mojo::AssociatedRemote<blink::mojom::AutoplayConfigurationClient>
GetAutoplayConfigurationClient(TestRenderFrame* frame) {
mojo::AssociatedRemote<blink::mojom::AutoplayConfigurationClient> client;
mojo::ScopedInterfaceEndpointHandle handle =
client.BindNewEndpointAndPassDedicatedReceiver().PassHandle();
EXPECT_TRUE(frame->GetAssociatedInterfaceRegistry()->TryBindInterface(
blink::mojom::AutoplayConfigurationClient::Name_, &handle));
return client;
}
private:
mojo::AssociatedRemote<blink::mojom::Widget> widget_remote_;
blink::LocalFrameToken child_frame_token_;
};
class RenderFrameTestObserver : public RenderFrameObserver,
public blink::WebLocalFrameObserver {
public:
explicit RenderFrameTestObserver(RenderFrame* render_frame)
: RenderFrameObserver(render_frame),
blink::WebLocalFrameObserver(render_frame->GetWebFrame()),
visible_(false) {}
~RenderFrameTestObserver() override {}
// RenderFrameObserver implementation.
void OnDestruct() override { delete this; }
// blink::WebLocalFrameObserver implementation.
void OnFrameDetached() override {}
void WasShown() override { visible_ = true; }
void WasHidden() override { visible_ = false; }
bool visible() const { return visible_; }
private:
bool visible_;
};
// Verify that a frame with a WebRemoteFrame as a parent has its own
// RenderWidget.
TEST_F(RenderFrameImplTest, SubframeWidget) {
EXPECT_TRUE(frame_widget());
RenderFrameImpl* main_frame = GetMainRenderFrame();
blink::WebFrameWidget* main_frame_widget =
main_frame->GetLocalRootWebFrameWidget();
EXPECT_NE(frame_widget(), main_frame_widget);
}
// Verify a subframe RenderWidget properly processes its viewport being
// resized.
TEST_F(RenderFrameImplTest, FrameResize) {
// Make an update where the widget's size and the visible_viewport_size
// are not the same.
blink::VisualProperties visual_properties;
visual_properties.screen_infos = display::ScreenInfos(display::ScreenInfo());
gfx::Size widget_size(400, 200);
gfx::Size visible_size(350, 170);
visual_properties.new_size_device_px = widget_size;
visual_properties.compositor_viewport_pixel_rect = gfx::Rect(widget_size);
visual_properties.visible_viewport_size_device_px = visible_size;
blink::WebFrameWidget* main_frame_widget =
GetMainRenderFrame()->GetLocalRootWebFrameWidget();
// The main frame's widget will receive the resize message before the
// subframe's widget, and it will set the size for the WebView.
main_frame_widget->ApplyVisualProperties(visual_properties);
// The main frame widget's size is the "widget size", not the visible viewport
// size, which is given to blink separately.
EXPECT_EQ(gfx::Size(web_view_->MainFrameWidget()->Size()), widget_size);
EXPECT_EQ(gfx::SizeF(web_view_->VisualViewportSize()),
gfx::SizeF(visible_size));
// The main frame doesn't change other local roots directly.
EXPECT_NE(gfx::Size(frame_widget()->Size()), visible_size);
// A subframe in the same process does not modify the WebView.
frame_widget()->ApplyVisualProperties(visual_properties);
EXPECT_EQ(gfx::Size(frame_widget()->Size()), widget_size);
// A subframe in another process would use the |visible_viewport_size| as its
// size.
}
// Verify a subframe RenderWidget properly processes a WasShown message.
TEST_F(RenderFrameImplTest, FrameWasShown) {
RenderFrameTestObserver observer(&child_frame());
widget_remote()->WasShown(
/* was_evicted=*/false, std::nullopt);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(frame_widget()->IsHidden());
EXPECT_TRUE(observer.visible());
}
namespace {
class DownloadURLMockLocalFrameHost : public LocalFrameHostInterceptor {
public:
explicit DownloadURLMockLocalFrameHost(
blink::AssociatedInterfaceProvider* provider)
: LocalFrameHostInterceptor(provider) {}
MOCK_METHOD3(RunModalAlertDialog,
void(const std::u16string& alert_message,
bool disable_third_party_subframe_suppresion,
RunModalAlertDialogCallback callback));
MOCK_METHOD1(DownloadURL, void(blink::mojom::DownloadURLParamsPtr params));
};
class DownloadURLTestRenderFrame : public TestRenderFrame {
public:
static RenderFrameImpl* CreateTestRenderFrame(
RenderFrameImpl::CreateParams params) {
return new DownloadURLTestRenderFrame(std::move(params));
}
~DownloadURLTestRenderFrame() override = default;
blink::AssociatedInterfaceProvider* GetRemoteAssociatedInterfaces() override {
blink::AssociatedInterfaceProvider* associated_interface_provider =
RenderFrameImpl::GetRemoteAssociatedInterfaces();
// Attach our fake local frame host at the very first call to
// GetRemoteAssociatedInterfaces.
if (!local_frame_host_) {
local_frame_host_ = std::make_unique<DownloadURLMockLocalFrameHost>(
associated_interface_provider);
}
return associated_interface_provider;
}
DownloadURLMockLocalFrameHost* download_url_mock_local_frame_host() {
return local_frame_host_.get();
}
private:
explicit DownloadURLTestRenderFrame(RenderFrameImpl::CreateParams params)
: TestRenderFrame(std::move(params)) {}
std::unique_ptr<DownloadURLMockLocalFrameHost> local_frame_host_;
};
} // namespace
class RenderViewImplDownloadURLTest : public RenderFrameImplTest {
public:
RenderViewImplDownloadURLTest()
: RenderFrameImplTest(
&DownloadURLTestRenderFrame::CreateTestRenderFrame) {}
DownloadURLMockLocalFrameHost* download_url_mock_local_frame_host() {
return static_cast<DownloadURLTestRenderFrame*>(&child_frame())
->download_url_mock_local_frame_host();
}
};
// Tests that url download are throttled when reaching the limit.
TEST_F(RenderViewImplDownloadURLTest, DownloadUrlLimit) {
WebURLRequest request;
request.SetUrl(GURL("http://test/test.pdf"));
request.SetRequestorOrigin(
blink::WebSecurityOrigin::Create(GURL("http://test")));
EXPECT_CALL(*download_url_mock_local_frame_host(), DownloadURL(testing::_))
.Times(10);
for (int i = 0; i < 10; ++i) {
child_frame().GetWebFrame()->DownloadURL(
request, network::mojom::RedirectMode::kManual, mojo::NullRemote());
base::RunLoop().RunUntilIdle();
}
EXPECT_CALL(*download_url_mock_local_frame_host(), DownloadURL(testing::_))
.Times(0);
child_frame().GetWebFrame()->DownloadURL(
request, network::mojom::RedirectMode::kManual, mojo::NullRemote());
base::RunLoop().RunUntilIdle();
}
// Regression test for crbug.com/692557. It shouldn't crash if we inititate a
// text finding, and then delete the frame immediately before the text finding
// returns any text match.
TEST_F(RenderFrameImplTest, NoCrashWhenDeletingFrameDuringFind) {
child_frame().GetWebFrame()->FindForTesting(
1, "foo", true /* match_case */, true /* forward */,
true /* new_session */, true /* force */, false /* wrap_within_frame */,
false /* async */);
static_cast<mojom::Frame*>(&child_frame())
->Delete(mojom::FrameDeleteIntention::kNotMainFrame);
}
TEST_F(RenderFrameImplTest, NoCrashOnReceiveTitleWhenNavigatingToJavascript) {
LoadHTML(
"<html>"
" <everything id='outer'>"
" <title><empty></title>"
" <body>"
" <iframe id='iframe'></iframe>"
" <script>"
" iframe.contentWindow.onunload = () => {"
" document.adoptNode(outer);"
" };"
" window.location = 'javascript:\"PASS.\"';"
" </script>"
" </body>"
" </everything>"
"</html> ");
}
TEST_F(RenderFrameImplTest, AutoplayFlags) {
// Add autoplay flags to the page.
auto client = GetAutoplayConfigurationClient(GetMainRenderFrame());
client->AddAutoplayFlags(url::Origin::Create(GURL(kAutoplayTestOrigin)),
blink::mojom::kAutoplayFlagHighMediaEngagement);
client.FlushForTesting();
// Navigate the top frame.
LoadHTMLWithUrlOverride(kParentFrameHTML, kAutoplayTestOrigin);
// Check the flags have been set correctly.
EXPECT_EQ(blink::mojom::kAutoplayFlagHighMediaEngagement,
AutoplayFlagsForFrame(*GetMainRenderFrame()));
// Navigate the child frame.
LoadChildFrame();
// Check the flags are set on both frames.
EXPECT_EQ(blink::mojom::kAutoplayFlagHighMediaEngagement,
AutoplayFlagsForFrame(*GetMainRenderFrame()));
EXPECT_EQ(blink::mojom::kAutoplayFlagHighMediaEngagement,
AutoplayFlagsForFrame(child_frame()));
// Re-bind the interface and add another flag for the same origin across two
// separate bindings, verifying both re-binding reset and flag merging.
auto client2 = GetAutoplayConfigurationClient(GetMainRenderFrame());
client2->AddAutoplayFlags(url::Origin::Create(GURL(kAutoplayTestOrigin)),
blink::mojom::kAutoplayFlagUserException);
client2.FlushForTesting();
auto client3 = GetAutoplayConfigurationClient(GetMainRenderFrame());
client3->AddAutoplayFlags(url::Origin::Create(GURL(kAutoplayTestOrigin)),
blink::mojom::kAutoplayFlagForceAllow);
client3.FlushForTesting();
LoadHTMLWithUrlOverride(kParentFrameHTML, kAutoplayTestOrigin);
EXPECT_EQ(blink::mojom::kAutoplayFlagUserException |
blink::mojom::kAutoplayFlagForceAllow,
AutoplayFlagsForFrame(*GetMainRenderFrame()));
// Navigate the top frame.
LoadHTMLWithUrlOverride(kParentFrameHTML, "https://www.example.com");
LoadChildFrame();
// Check the flags have been cleared.
EXPECT_EQ(blink::mojom::kAutoplayFlagNone,
AutoplayFlagsForFrame(*GetMainRenderFrame()));
EXPECT_EQ(blink::mojom::kAutoplayFlagNone,
AutoplayFlagsForFrame(child_frame()));
}
TEST_F(RenderFrameImplTest, AutoplayFlags_WrongOrigin) {
// Add autoplay flags to the page.
auto client = GetAutoplayConfigurationClient(GetMainRenderFrame());
client->AddAutoplayFlags(url::Origin(),
blink::mojom::kAutoplayFlagHighMediaEngagement);
client.FlushForTesting();
// Navigate the top frame.
LoadHTMLWithUrlOverride(kParentFrameHTML, kAutoplayTestOrigin);
// Check the flags have been not been set.
EXPECT_EQ(blink::mojom::kAutoplayFlagNone,
AutoplayFlagsForFrame(*GetMainRenderFrame()));
}
TEST_F(RenderFrameImplTest, FileUrlPathAlias) {
const struct {
const char* original;
const char* transformed;
} kTestCases[] = {
{"file:///alias", "file:///replacement"},
{"file:///alias/path/to/file", "file:///replacement/path/to/file"},
{"file://alias/path/to/file", "file://alias/path/to/file"},
{"file:///notalias/path/to/file", "file:///notalias/path/to/file"},
{"file:///root/alias/path/to/file", "file:///root/alias/path/to/file"},
{"file:///", "file:///"},
};
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kFileUrlPathAlias, "/alias=/replacement");
for (const auto& test_case : kTestCases) {
WebURLRequest request;
request.SetUrl(GURL(test_case.original));
std::optional<blink::WebURL> updated =
GetMainRenderFrame()->WillSendRequest(
request.Url(), request.RequestorOrigin(), request.SiteForCookies(),
blink::WebLocalFrameClient::ForRedirect(false), blink::WebURL());
EXPECT_EQ(test_case.transformed, updated.has_value()
? updated->GetString().Utf8()
: request.Url().GetString().Utf8());
}
}
// Used to annotate the source of an interface request.
struct SourceAnnotation {
// The URL of the active document in the frame, at the time the interface was
// requested by the RenderFrame.
GURL document_url;
// The RenderFrameObserver event in response to which the interface is
// requested by the RenderFrame.
std::string render_frame_event;
bool operator==(const SourceAnnotation& rhs) const {
return document_url == rhs.document_url &&
render_frame_event == rhs.render_frame_event;
}
};
std::ostream& operator<<(std::ostream& out, const SourceAnnotation& s) {
out << s.document_url.possibly_invalid_spec() << " : "
<< s.render_frame_event;
return out;
}
// RenderFrameRemoteInterfacesTest ------------------------------------
namespace {
constexpr char kTestFirstURL[] = "http://foo.com/1";
constexpr char kTestSecondURL[] = "http://foo.com/2";
constexpr char kTestCrossOriginURL[] = "http://bar.com/";
constexpr char kAboutBlankURL[] = "about:blank";
constexpr char kFrameEventDidCreateNewFrame[] = "did-create-new-frame";
constexpr char kFrameEventDidCreateNewDocument[] = "did-create-new-document";
constexpr char kFrameEventDidCreateDocumentElement[] =
"did-create-document-element";
constexpr char kFrameEventReadyToCommitNavigation[] =
"ready-to-commit-navigation";
constexpr char kFrameEventDidCommitProvisionalLoad[] =
"did-commit-provisional-load";
constexpr char kFrameEventDidCommitSameDocumentLoad[] =
"did-commit-same-document-load";
constexpr char kFrameEventAfterCommit[] = "after-commit";
constexpr char kNoDocumentMarkerURL[] = "data:,No document.";
class TestSimpleBrowserInterfaceBrokerImpl
: public blink::mojom::BrowserInterfaceBroker {
public:
using BinderCallback =
base::RepeatingCallback<void(mojo::ScopedMessagePipeHandle)>;
// Incoming interface requests for |interface_name| will invoke |binder|.
// Everything else is ignored.
TestSimpleBrowserInterfaceBrokerImpl(const std::string& interface_name,
BinderCallback binder_callback)
: receiver_(this),
interface_name_(interface_name),
binder_callback_(binder_callback) {}
TestSimpleBrowserInterfaceBrokerImpl(
const TestSimpleBrowserInterfaceBrokerImpl&) = delete;
TestSimpleBrowserInterfaceBrokerImpl& operator=(
const TestSimpleBrowserInterfaceBrokerImpl&) = delete;
void BindAndFlush(
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker> receiver) {
ASSERT_FALSE(receiver_.is_bound());
receiver_.Bind(std::move(receiver));
receiver_.FlushForTesting();
}
private:
// blink::mojom::BrowserInterfaceBroker:
void GetInterface(mojo::GenericPendingReceiver receiver) override {
if (receiver.interface_name().value() == interface_name_) {
binder_callback_.Run(receiver.PassPipe());
}
}
mojo::Receiver<blink::mojom::BrowserInterfaceBroker> receiver_;
std::string interface_name_;
BinderCallback binder_callback_;
};
class FrameHostTestInterfaceImpl : public mojom::FrameHostTestInterface {
public:
FrameHostTestInterfaceImpl() = default;
FrameHostTestInterfaceImpl(const FrameHostTestInterfaceImpl&) = delete;
FrameHostTestInterfaceImpl& operator=(const FrameHostTestInterfaceImpl&) =
delete;
~FrameHostTestInterfaceImpl() override {}
void BindAndFlush(
mojo::PendingReceiver<mojom::FrameHostTestInterface> receiver) {
receiver_.Bind(std::move(receiver));
receiver_.WaitForIncomingCall();
}
const std::optional<SourceAnnotation>& ping_source() const {
return ping_source_;
}
protected:
void Ping(const GURL& url, const std::string& event) override {
ping_source_ = SourceAnnotation{url, event};
}
private:
mojo::Receiver<mojom::FrameHostTestInterface> receiver_{this};
std::optional<SourceAnnotation> ping_source_;
};
// RenderFrameObserver that issues FrameHostTestInterface interface requests
// through the RenderFrame's |remote_interfaces_| in response to observing
// important milestones in a frame's lifecycle.
class FrameHostTestInterfaceRequestIssuer : public RenderFrameObserver {
public:
explicit FrameHostTestInterfaceRequestIssuer(RenderFrame* render_frame)
: RenderFrameObserver(render_frame) {}
FrameHostTestInterfaceRequestIssuer(
const FrameHostTestInterfaceRequestIssuer&) = delete;
FrameHostTestInterfaceRequestIssuer& operator=(
const FrameHostTestInterfaceRequestIssuer&) = delete;
void RequestTestInterfaceOnFrameEvent(const std::string& event) {
mojo::Remote<mojom::FrameHostTestInterface> remote;
blink::WebDocument document = render_frame()->GetWebFrame()->GetDocument();
render_frame()->GetBrowserInterfaceBroker().GetInterface(
remote.BindNewPipeAndPassReceiver());
remote->Ping(
!document.IsNull() ? GURL(document.Url()) : GURL(kNoDocumentMarkerURL),
event);
}
private:
// RenderFrameObserver:
void OnDestruct() override {}
void DidCreateDocumentElement() override {
RequestTestInterfaceOnFrameEvent(kFrameEventDidCreateDocumentElement);
}
void DidCreateNewDocument() override {
RequestTestInterfaceOnFrameEvent(kFrameEventDidCreateNewDocument);
}
void ReadyToCommitNavigation(blink::WebDocumentLoader* loader) override {
RequestTestInterfaceOnFrameEvent(kFrameEventReadyToCommitNavigation);
}
void DidCommitProvisionalLoad(ui::PageTransition transition) override {
RequestTestInterfaceOnFrameEvent(kFrameEventDidCommitProvisionalLoad);
}
void DidFinishSameDocumentNavigation() override {
RequestTestInterfaceOnFrameEvent(kFrameEventDidCommitSameDocumentLoad);
}
};
// RenderFrameObserver that can be used to wait for the next commit in a frame.
class FrameCommitWaiter : public RenderFrameObserver {
public:
explicit FrameCommitWaiter(RenderFrame* render_frame)
: RenderFrameObserver(render_frame) {}
FrameCommitWaiter(const FrameCommitWaiter&) = delete;
FrameCommitWaiter& operator=(const FrameCommitWaiter&) = delete;
void Wait() {
if (did_commit_) {
return;
}
run_loop_.Run();
}
private:
// RenderFrameObserver:
void OnDestruct() override {}
void DidCommitProvisionalLoad(ui::PageTransition transition) override {
did_commit_ = true;
run_loop_.Quit();
}
base::RunLoop run_loop_;
bool did_commit_ = false;
};
// Testing ContentRendererClient implementation that fires the |callback|
// whenever a new frame is created.
class FrameCreationObservingRendererClient : public ContentRendererClient {
public:
using FrameCreatedCallback = base::RepeatingCallback<void(TestRenderFrame*)>;
FrameCreationObservingRendererClient() {}
FrameCreationObservingRendererClient(
const FrameCreationObservingRendererClient&) = delete;
FrameCreationObservingRendererClient& operator=(
const FrameCreationObservingRendererClient&) = delete;
~FrameCreationObservingRendererClient() override {}
void set_callback(FrameCreatedCallback callback) {
callback_ = std::move(callback);
}
void reset_callback() { callback_.Reset(); }
protected:
void RenderFrameCreated(RenderFrame* render_frame) override {
ContentRendererClient::RenderFrameCreated(render_frame);
if (callback_) {
callback_.Run(static_cast<TestRenderFrame*>(render_frame));
}
}
private:
FrameCreatedCallback callback_;
};
// Expects observing the creation of a new frame, and creates an instance of
// FrameHostTestInterfaceRequestIssuerRenderFrame for that new frame to exercise
// its RemoteInterfaceProvider interface.
class ScopedNewFrameInterfaceProviderExerciser {
public:
explicit ScopedNewFrameInterfaceProviderExerciser(
FrameCreationObservingRendererClient* frame_creation_observer,
const std::optional<std::string>& html_override_for_first_load =
std::nullopt)
: frame_creation_observer_(frame_creation_observer),
html_override_for_first_load_(html_override_for_first_load) {
frame_creation_observer_->set_callback(base::BindRepeating(
&ScopedNewFrameInterfaceProviderExerciser::OnFrameCreated,
base::Unretained(this)));
}
ScopedNewFrameInterfaceProviderExerciser(
const ScopedNewFrameInterfaceProviderExerciser&) = delete;
ScopedNewFrameInterfaceProviderExerciser& operator=(
const ScopedNewFrameInterfaceProviderExerciser&) = delete;
~ScopedNewFrameInterfaceProviderExerciser() {
frame_creation_observer_->reset_callback();
}
void ExpectNewFrameAndWaitForLoad(const GURL& expected_loaded_url) {
ASSERT_NE(nullptr, frame_);
frame_commit_waiter_->Wait();
ASSERT_FALSE(frame_->GetWebFrame()->GetCurrentHistoryItem().IsNull());
ASSERT_FALSE(frame_->GetWebFrame()->GetDocument().IsNull());
EXPECT_EQ(expected_loaded_url,
GURL(frame_->GetWebFrame()->GetDocument().Url()));
browser_interface_broker_receiver_for_first_document_ =
frame_->TakeLastBrowserInterfaceBrokerReceiver();
}
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
browser_interface_broker_receiver_for_initial_empty_document() {
return std::move(
browser_interface_broker_receiver_for_initial_empty_document_);
}
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
browser_interface_broker_receiver_for_first_document() {
return std::move(browser_interface_broker_receiver_for_first_document_);
}
private:
void OnFrameCreated(TestRenderFrame* frame) {
ASSERT_EQ(nullptr, frame_.get());
frame_ = frame;
frame_commit_waiter_.emplace(frame);
if (html_override_for_first_load_.has_value()) {
frame_->SetHTMLOverrideForNextNavigation(
std::move(html_override_for_first_load_).value());
}
// The FrameHostTestInterfaceRequestIssuer needs to stay alive even after
// this method returns, so that it continues to observe RenderFrame
// lifecycle events and request test interfaces in response.
test_request_issuer_.emplace(frame);
test_request_issuer_->RequestTestInterfaceOnFrameEvent(
kFrameEventDidCreateNewFrame);
browser_interface_broker_receiver_for_initial_empty_document_ =
frame_->TakeLastBrowserInterfaceBrokerReceiver();
EXPECT_TRUE(frame->GetWebFrame()->GetCurrentHistoryItem().IsNull());
}
raw_ptr<FrameCreationObservingRendererClient> frame_creation_observer_;
raw_ptr<TestRenderFrame> frame_ = nullptr;
std::optional<std::string> html_override_for_first_load_;
GURL first_committed_url_;
std::optional<FrameCommitWaiter> frame_commit_waiter_;
std::optional<FrameHostTestInterfaceRequestIssuer> test_request_issuer_;
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
browser_interface_broker_receiver_for_initial_empty_document_;
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
browser_interface_broker_receiver_for_first_document_;
};
// Extracts all interface receivers for FrameHostTestInterface pending on the
// specified |browser_interface_broker_receiver|, and returns a list of the
// source annotations that are provided in the pending Ping() call for each of
// these FrameHostTestInterface receivers.
void ExpectPendingInterfaceReceiversFromSources(
mojo::PendingReceiver<blink::mojom::BrowserInterfaceBroker>
browser_interface_broker_receiver,
std::vector<SourceAnnotation> expected_sources) {
ASSERT_TRUE(browser_interface_broker_receiver.is_valid());
std::vector<SourceAnnotation> browser_interface_broker_sources;
ASSERT_TRUE(browser_interface_broker_receiver.is_valid());
TestSimpleBrowserInterfaceBrokerImpl browser_broker(
mojom::FrameHostTestInterface::Name_,
base::BindLambdaForTesting([&browser_interface_broker_sources](
mojo::ScopedMessagePipeHandle handle) {
FrameHostTestInterfaceImpl impl;
impl.BindAndFlush(mojo::PendingReceiver<mojom::FrameHostTestInterface>(
std::move(handle)));
ASSERT_TRUE(impl.ping_source().has_value());
browser_interface_broker_sources.push_back(impl.ping_source().value());
}));
browser_broker.BindAndFlush(std::move(browser_interface_broker_receiver));
EXPECT_THAT(browser_interface_broker_sources,
::testing::ElementsAreArray(expected_sources));
}
} // namespace
class RenderFrameRemoteInterfacesTest : public RenderViewTest {
public:
RenderFrameRemoteInterfacesTest() = default;
RenderFrameRemoteInterfacesTest(const RenderFrameRemoteInterfacesTest&) =
delete;
RenderFrameRemoteInterfacesTest& operator=(
const RenderFrameRemoteInterfacesTest&) = delete;
~RenderFrameRemoteInterfacesTest() override = default;
protected:
void SetUp() override {
RenderViewTest::SetUp();
LoadHTML("Nothing to see here.");
}
void TearDown() override {
#if defined(LEAK_SANITIZER)
// Do this before shutting down V8 in RenderViewTest::TearDown().
// http://crbug.com/328552
__lsan_do_leak_check();
#endif
RenderViewTest::TearDown();
}
FrameCreationObservingRendererClient* frame_creation_observer() {
DCHECK(frame_creation_observer_);
return frame_creation_observer_;
}
TestRenderFrame* GetMainRenderFrame() {
return static_cast<TestRenderFrame*>(RenderViewTest::GetMainRenderFrame());
}
ContentRendererClient* CreateContentRendererClient() override {
frame_creation_observer_ = new FrameCreationObservingRendererClient();
return frame_creation_observer_;
}
private:
// Owned by RenderViewTest.
raw_ptr<FrameCreationObservingRendererClient> frame_creation_observer_ =
nullptr;
};
// Expect that |remote_interfaces_| is bound before the first committed load in
// a child frame, and then re-bound on the first commit.
TEST_F(RenderFrameRemoteInterfacesTest, ChildFrameAtFirstCommittedLoad) {
ScopedNewFrameInterfaceProviderExerciser child_frame_exerciser(
frame_creation_observer());
const std::string html = base::StringPrintf("<iframe src=\"%s\"></iframe>",
"data:text/html,Child");
LoadHTMLWithUrlOverride(html.c_str(), kTestSecondURL);
const GURL child_frame_url("data:text/html,Child");
ASSERT_NO_FATAL_FAILURE(
child_frame_exerciser.ExpectNewFrameAndWaitForLoad(child_frame_url));
// TODO(crbug.com/40553427): It is unfortunate how many internal
// details of frame/document creation this encodes. Need to decouple.
const GURL initial_empty_url(kAboutBlankURL);
ExpectPendingInterfaceReceiversFromSources(
child_frame_exerciser
.browser_interface_broker_receiver_for_initial_empty_document(),
{{initial_empty_url, kFrameEventDidCreateNewFrame},
{child_frame_url, kFrameEventReadyToCommitNavigation},
// TODO(crbug.com/40444754): It seems strange that the new
// document is created and DidCreateNewDocument is invoked *before* the
// provisional load would have even committed.
{child_frame_url, kFrameEventDidCreateNewDocument}});
ExpectPendingInterfaceReceiversFromSources(
child_frame_exerciser
.browser_interface_broker_receiver_for_first_document(),
{{child_frame_url, kFrameEventDidCommitProvisionalLoad},
{child_frame_url, kFrameEventDidCreateDocumentElement}});
}
// Expect that |remote_interfaces_| is bound before the first committed load in
// the main frame of an opened window, and then re-bound on the first commit.
TEST_F(RenderFrameRemoteInterfacesTest,
MainFrameOfOpenedWindowAtFirstCommittedLoad) {
const GURL new_window_url(kTestCrossOriginURL);
ScopedNewFrameInterfaceProviderExerciser main_frame_exerciser(
frame_creation_observer(), std::string("foo"));
const std::string html = base::StringPrintf(
"<script>window.open(\"%s\", \"_blank\")</script>", kTestCrossOriginURL);
LoadHTMLWithUrlOverride(html.c_str(), kTestSecondURL);
ASSERT_NO_FATAL_FAILURE(
main_frame_exerciser.ExpectNewFrameAndWaitForLoad(new_window_url));
// The URL of the initial empty document is "" for opened windows, in
// contrast to child frames, where it is "about:blank". See
// Document::Document and Document::SetURL for more details.
//
// Furthermore, for main frames, InitializeCoreFrame is invoked first, and
// RenderFrameImpl::Initialize is invoked second, in contrast to child
// frames where it is vice versa. ContentRendererClient::RenderFrameCreated
// is invoked from RenderFrameImpl::Initialize, so we miss the events
// related to initial empty document that is created from
// InitializeCoreFrame, and there is already a document when
// RenderFrameCreated is invoked.
//
// TODO(crbug.com/40553427): It is unfortunate how many internal
// details of frame/document creation this encodes. Need to decouple.
const GURL initial_empty_url;
ExpectPendingInterfaceReceiversFromSources(
main_frame_exerciser
.browser_interface_broker_receiver_for_initial_empty_document(),
{{initial_empty_url, kFrameEventDidCreateNewFrame},
{new_window_url, kFrameEventReadyToCommitNavigation},
{new_window_url, kFrameEventDidCreateNewDocument}});
ExpectPendingInterfaceReceiversFromSources(
main_frame_exerciser
.browser_interface_broker_receiver_for_first_document(),
{{new_window_url, kFrameEventDidCommitProvisionalLoad},
{new_window_url, kFrameEventDidCreateDocumentElement}});
}
// Expect that |remote_interfaces_| is not bound to a new pipe if the first
// committed load in the child frame has the same security origin as that of the
// initial empty document.
//
// In this case, the LocalDOMWindow object associated with the initial empty
// document will be re-used for the newly committed document. Here, we must
// continue using the InterfaceProvider connection created for the initial empty
// document to support the following use-case:
// 1) Parent frame dynamically injects an <iframe>.
// 2) The parent frame calls `child.contentDocument.write(...)` to inject
// Javascript that may stash objects on the child frame's global object
// (LocalDOMWindow). Internally, these objects may be using Mojo services
// exposed by the RenderFrameHost. The InterfaceRequests for these services
// might still be en-route to the RemnderFrameHost's InterfaceProvider.
// 3) The `child` frame commits the first real load, and it is same-origin.
// 4) The global object in the child frame's browsing context is re-used.
// 5) Javascript objects stashed on the global object should continue to work.
//
// TODO(japhet?): javascript: urls are untestable here, because they don't
// go through the normal commit pipeline. If we were to give javascript: urls
// their own DocumentLoader in blink and model them as a real navigation, we
// should add a test case here.
// TODO(crbug.com/40519010): when all clients are converted to use
// BrowserInterfaceBroker, PendingReceiver<InterfaceProvider>-related code will
// be removed.
TEST_F(RenderFrameRemoteInterfacesTest,
ChildFrameReusingWindowOfInitialDocument) {
const GURL main_frame_url(kTestFirstURL);
const GURL initial_empty_url(kAboutBlankURL);
constexpr const char* kTestCases[] = {kTestSecondURL, kAboutBlankURL};
for (const char* test_case : kTestCases) {
SCOPED_TRACE(::testing::Message() << "child_frame_url = " << test_case);
// Override the URL for the first navigation in the newly created frame to
// |child_frame_url|.
const GURL child_frame_url(test_case);
ScopedNewFrameInterfaceProviderExerciser child_frame_exerciser(
frame_creation_observer(), std::string("foo"));
std::string html = "<iframe src='" + child_frame_url.spec() + "'></iframe>";
LoadHTMLWithUrlOverride(html.c_str(), main_frame_url.spec().c_str());
ASSERT_NO_FATAL_FAILURE(
child_frame_exerciser.ExpectNewFrameAndWaitForLoad(child_frame_url));
ExpectPendingInterfaceReceiversFromSources(
child_frame_exerciser
.browser_interface_broker_receiver_for_initial_empty_document(),
{{initial_empty_url, kFrameEventDidCreateNewFrame},
{child_frame_url, kFrameEventReadyToCommitNavigation},
{child_frame_url, kFrameEventDidCreateNewDocument},
{child_frame_url, kFrameEventDidCommitProvisionalLoad},
{child_frame_url, kFrameEventDidCreateDocumentElement}});
auto browser_interface_broker_receiver =
child_frame_exerciser
.browser_interface_broker_receiver_for_first_document();
ASSERT_FALSE(browser_interface_broker_receiver.is_valid());
}
}
// Expect that |remote_interfaces_| is bound to a new pipe on cross-document
// navigations.
TEST_F(RenderFrameRemoteInterfacesTest, ReplacedOnNonSameDocumentNavigation) {
LoadHTMLWithUrlOverride("", kTestFirstURL);
auto browser_interface_broker_receiver_for_first_document =
GetMainRenderFrame()->TakeLastBrowserInterfaceBrokerReceiver();
FrameHostTestInterfaceRequestIssuer requester(GetMainRenderFrame());
requester.RequestTestInterfaceOnFrameEvent(kFrameEventAfterCommit);
LoadHTMLWithUrlOverride("", kTestSecondURL);
auto browser_interface_broker_receiver_for_second_document =
GetMainRenderFrame()->TakeLastBrowserInterfaceBrokerReceiver();
ASSERT_TRUE(browser_interface_broker_receiver_for_first_document.is_valid());
ExpectPendingInterfaceReceiversFromSources(
std::move(browser_interface_broker_receiver_for_first_document),
{{GURL(kTestFirstURL), kFrameEventAfterCommit},
{GURL(kTestSecondURL), kFrameEventReadyToCommitNavigation},
{GURL(kTestSecondURL), kFrameEventDidCreateNewDocument}});
ASSERT_TRUE(browser_interface_broker_receiver_for_second_document.is_valid());
ExpectPendingInterfaceReceiversFromSources(
std::move(browser_interface_broker_receiver_for_second_document),
{{GURL(kTestSecondURL), kFrameEventDidCommitProvisionalLoad},
{GURL(kTestSecondURL), kFrameEventDidCreateDocumentElement}});
}
// Expect that |remote_interfaces_| is not bound to a new pipe on same-document
// navigations, i.e. the existing InterfaceProvider connection is continued to
// be used.
TEST_F(RenderFrameRemoteInterfacesTest, ReusedOnSameDocumentNavigation) {
LoadHTMLWithUrlOverride("", kTestFirstURL);
auto browser_interface_broker_receiver =
GetMainRenderFrame()->TakeLastBrowserInterfaceBrokerReceiver();
FrameHostTestInterfaceRequestIssuer requester(GetMainRenderFrame());
OnSameDocumentNavigation(GetMainFrame(), true /* is_new_navigation */);
ASSERT_TRUE(browser_interface_broker_receiver.is_valid());
ExpectPendingInterfaceReceiversFromSources(
std::move(browser_interface_broker_receiver),
{{GURL(kTestFirstURL), kFrameEventDidCommitSameDocumentLoad}});
}
TEST_F(RenderFrameImplTest, LastCommittedUrlForUKM) {
// Test the case where we have a data url with a base_url.
GURL data_url = GURL("data:text/html,");
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = data_url;
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
common_params->transition = ui::PAGE_TRANSITION_TYPED;
common_params->base_url_for_data_url = GURL("about:blank");
auto commit_params = blink::CreateCommitNavigationParams();
auto waiter = std::make_unique<FrameLoadWaiter>(GetMainRenderFrame());
GetMainRenderFrame()->Navigate(std::move(common_params),
std::move(commit_params));
waiter->Wait();
EXPECT_EQ(GURL(GetMainRenderFrame()->LastCommittedUrlForUKM()), data_url);
// Test the case where we have an unreachable URL.
GURL unreachable_url = GURL("http://www.example.com");
waiter = std::make_unique<FrameLoadWaiter>(GetMainRenderFrame());
GetMainRenderFrame()->LoadHTMLStringForTesting(
"test", data_url, "UTF-8", unreachable_url,
false /* replace_current_item */);
waiter->Wait();
EXPECT_EQ(GURL(GetMainRenderFrame()->LastCommittedUrlForUKM()),
unreachable_url);
// Test the base case, normal load.
GURL override_url = GURL("http://example.com");
waiter = std::make_unique<FrameLoadWaiter>(GetMainRenderFrame());
LoadHTMLWithUrlOverride("Test", "http://example.com");
waiter->Wait();
EXPECT_EQ(GURL(GetMainRenderFrame()->LastCommittedUrlForUKM()), override_url);
}
// Verify that a frame with a pending update is cancelled when a forced update
// is sent.
TEST_F(RenderFrameImplTest, SendUpdateCancelsPending) {
RenderFrameImpl* main_frame = GetMainRenderFrame();
main_frame->StartDelayedSyncTimer();
EXPECT_TRUE(main_frame->delayed_state_sync_timer_.IsRunning());
main_frame->SendUpdateState();
EXPECT_FALSE(main_frame->delayed_state_sync_timer_.IsRunning());
}
namespace {
// All content setting tests use the same data url, which contains html which
// has different behavior depending on whether script is enabled or disabled.
blink::mojom::CommonNavigationParamsPtr
GetCommonParamsForContentSettingsTest() {
const char kHtml[] =
"<html>"
"<noscript>JS_DISABLED</noscript>"
"<script>document.write('JS_ENABLED');</script>"
"</html>";
std::string data_url_contents = "data:text/html,";
data_url_contents += kHtml;
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL(data_url_contents);
return common_params;
}
// Dump the layout tree and see whether it contains "text".
bool HasText(blink::WebLocalFrame* frame, const std::string& text) {
std::string layout_tree =
blink::TestWebFrameContentDumper::DumpLayoutTreeAsText(
frame, blink::TestWebFrameContentDumper::kLayoutAsTextNormal)
.Utf8();
return layout_tree.contains(text);
}
// Waits for the navigation to finish.
void NavigateAndWait(content::TestRenderFrame* frame,
blink::mojom::CommonNavigationParamsPtr common_params,
blink::mojom::CommitNavigationParamsPtr commit_params,
blink::WebView* web_view) {
FrameLoadWaiter waiter(frame);
frame->Navigate(std::move(common_params), std::move(commit_params));
waiter.Wait();
}
class FakeContentSettingsClient : public blink::WebContentSettingsClient {
public:
explicit FakeContentSettingsClient(content::RenderFrame* render_frame)
: render_frame_(render_frame) {
render_frame_->GetWebFrame()->SetContentSettingsClient(this);
}
~FakeContentSettingsClient() override {
render_frame_->GetWebFrame()->SetContentSettingsClient(nullptr);
}
// blink::WebContentSettingsClient implementation.
void DidNotAllowImage() override { ++did_not_allow_image_count_; }
void DidNotAllowScript() override { ++did_not_allow_script_count_; }
int did_not_allow_image_count_ = 0;
int did_not_allow_script_count_ = 0;
raw_ptr<content::RenderFrame> render_frame_;
};
constexpr char kGeneratedDocumentOwnerOrigin[] = "https://owner.example.com";
// Commits a document showing "generated" that the browser reports as
// generated for `owner`.
void NavigateToGeneratedDocument(content::TestRenderFrame* frame,
const url::Origin& owner,
blink::WebView* web_view) {
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL("data:text/html,<body>generated</body>");
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
commit_params->generated_document_owner_origin = owner;
NavigateAndWait(frame, std::move(common_params), std::move(commit_params),
web_view);
}
std::optional<url::Origin> GetGeneratedDocumentOwnerOrigin(
content::TestRenderFrame* frame) {
blink::WebSecurityOrigin owner =
frame->GetWebFrame()->GetGeneratedDocumentOwnerOrigin();
if (owner.IsNull()) {
return std::nullopt;
}
return url::Origin(owner);
}
} // namespace
// A javascript: URL result replaces the document inside the renderer, with no
// commit from the browser. It must not keep the owner of the document it
// replaced.
TEST_F(RenderFrameImplTest,
GeneratedDocumentOwnerOriginDroppedForJavascriptUrlResult) {
const url::Origin kOwner =
url::Origin::Create(GURL(kGeneratedDocumentOwnerOrigin));
TestRenderFrame* frame = GetMainRenderFrame();
NavigateToGeneratedDocument(frame, kOwner, web_view_);
ASSERT_TRUE(HasText(frame->GetWebFrame(), "generated"));
EXPECT_EQ(kOwner, GetGeneratedDocumentOwnerOrigin(frame));
FrameLoadWaiter waiter(frame);
ExecuteJavaScriptForTests("location = \"javascript:'<body>page</body>'\";");
waiter.Wait();
ASSERT_TRUE(HasText(frame->GetWebFrame(), "page"));
EXPECT_EQ(std::nullopt, GetGeneratedDocumentOwnerOrigin(frame));
}
// `document.open()` rewrites the document in place, without a new commit, so
// the document keeps the owner it was committed with.
TEST_F(RenderFrameImplTest, GeneratedDocumentOwnerOriginKeptAfterDocumentOpen) {
const url::Origin kOwner =
url::Origin::Create(GURL(kGeneratedDocumentOwnerOrigin));
TestRenderFrame* frame = GetMainRenderFrame();
NavigateToGeneratedDocument(frame, kOwner, web_view_);
ASSERT_TRUE(HasText(frame->GetWebFrame(), "generated"));
EXPECT_EQ(kOwner, GetGeneratedDocumentOwnerOrigin(frame));
ExecuteJavaScriptForTests(
"document.open(); document.write('<body>page</body>'); "
"document.close();");
ASSERT_TRUE(HasText(frame->GetWebFrame(), "page"));
EXPECT_EQ(kOwner, GetGeneratedDocumentOwnerOrigin(frame));
}
// A same-document navigation keeps the document, so the owner stays too.
TEST_F(RenderFrameImplTest,
GeneratedDocumentOwnerOriginKeptAfterSameDocumentNavigation) {
const url::Origin kOwner =
url::Origin::Create(GURL(kGeneratedDocumentOwnerOrigin));
TestRenderFrame* frame = GetMainRenderFrame();
NavigateToGeneratedDocument(frame, kOwner, web_view_);
ASSERT_TRUE(HasText(frame->GetWebFrame(), "generated"));
EXPECT_EQ(kOwner, GetGeneratedDocumentOwnerOrigin(frame));
// Model a browser-driven fragment navigation within the loaded data URL.
// Its commit params carry no owner.
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL("data:text/html,<body>generated</body>#section");
common_params->navigation_type = blink::mojom::NavigationType::SAME_DOCUMENT;
base::test::TestFuture<blink::mojom::CommitResult> commit_result;
static_cast<mojom::Frame*>(frame)->CommitSameDocumentNavigation(
std::move(common_params), blink::CreateCommitNavigationParams(),
commit_result.GetCallback());
ASSERT_EQ(blink::mojom::CommitResult::Ok, commit_result.Get());
ASSERT_EQ("section", GURL(frame->GetWebFrame()->GetDocument().Url()).ref());
EXPECT_EQ(kOwner, GetGeneratedDocumentOwnerOrigin(frame));
}
TEST_F(RenderFrameImplTest,
GeneratedDocumentOwnerOriginDroppedAfterCrossDocumentNavigation) {
const url::Origin owner =
url::Origin::Create(GURL(kGeneratedDocumentOwnerOrigin));
TestRenderFrame* frame = GetMainRenderFrame();
NavigateToGeneratedDocument(frame, owner, web_view_);
ASSERT_TRUE(HasText(frame->GetWebFrame(), "generated"));
ASSERT_EQ(owner, GetGeneratedDocumentOwnerOrigin(frame));
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL("data:text/html,<body>replacement</body>");
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
NavigateAndWait(frame, std::move(common_params),
blink::CreateCommitNavigationParams(), web_view_);
ASSERT_TRUE(HasText(frame->GetWebFrame(), "replacement"));
EXPECT_EQ(std::nullopt, GetGeneratedDocumentOwnerOrigin(frame));
}
// Checks that when images are blocked, the ContentSettingsAgent receives a
// callback.
TEST_F(RenderFrameImplTest, ContentSettingsCallbackImageBlocked) {
// Create a fake content settings client to track image blocked callbacks.
FakeContentSettingsClient fake_content_settings_client(GetMainRenderFrame());
// Navigate to a URL that consists of a red square.
std::string data_url_contents =
"data:image/"
"png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZAQMAAAD+JxcgAAAAA1BMVEX/"
"AAAZ4gk3AAAAC0lEQVR4AWMYSAAAAH0AAVFwgb4AAAAASUVORK5CYII=";
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL(data_url_contents);
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
commit_params->content_settings->allow_image = false;
content::TestRenderFrame* frame =
static_cast<TestRenderFrame*>(GetMainRenderFrame());
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
EXPECT_EQ(1, fake_content_settings_client.did_not_allow_image_count_);
}
// Checks that when script is blocked, the ContentSettingsAgent receives a
// callback.
TEST_F(RenderFrameImplTest, ContentSettingsCallbackScriptBlocked) {
// Create a fake content settings client to track script blocked callbacks.
FakeContentSettingsClient fake_content_settings_client(GetMainRenderFrame());
// Navigate to a URL with script disabled.
auto common_params = GetCommonParamsForContentSettingsTest();
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
commit_params->content_settings->allow_script = false;
content::TestRenderFrame* frame =
static_cast<TestRenderFrame*>(GetMainRenderFrame());
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
EXPECT_TRUE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_FALSE(HasText(GetMainFrame(), "JS_ENABLED"));
EXPECT_EQ(1, fake_content_settings_client.did_not_allow_script_count_);
}
// Checks that when script is allowed, the ContentSettingsAgent does not receive
// a callback.
TEST_F(RenderFrameImplTest, ContentSettingsCallbackScriptAllowed) {
// Create a fake content settings client to track script blocked callbacks.
FakeContentSettingsClient fake_content_settings_client(GetMainRenderFrame());
// Navigate to a URL with script enabled.
auto common_params = GetCommonParamsForContentSettingsTest();
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
content::TestRenderFrame* frame =
static_cast<TestRenderFrame*>(GetMainRenderFrame());
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
// Verify that the script was not blocked.
EXPECT_FALSE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_TRUE(HasText(GetMainFrame(), "JS_ENABLED"));
// Verify there was no script blocked callback.
EXPECT_EQ(0, fake_content_settings_client.did_not_allow_script_count_);
}
// Regression test for crbug.com/232410: Load a page with JS blocked. Then,
// allow JS and reload the page. In each case, only one of noscript or script
// tags should be enabled, but never both.
TEST_F(RenderFrameImplTest, ContentSettingsNoscriptTag) {
// Navigate to a URL with script disabled.
auto common_params = GetCommonParamsForContentSettingsTest();
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
commit_params->content_settings->allow_script = false;
content::TestRenderFrame* frame =
static_cast<TestRenderFrame*>(GetMainRenderFrame());
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
EXPECT_TRUE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_FALSE(HasText(GetMainFrame(), "JS_ENABLED"));
// Reload the page but allow Javascript.
common_params->navigation_type = blink::mojom::NavigationType::RELOAD;
commit_params->content_settings->allow_script = true;
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
EXPECT_FALSE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_TRUE(HasText(GetMainFrame(), "JS_ENABLED"));
}
// Checks that same document navigations don't update content settings for the
// page.
TEST_F(RenderFrameImplTest, ContentSettingsSameDocumentNavigation) {
// Load a page which contains a script.
auto common_params = GetCommonParamsForContentSettingsTest();
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
blink::mojom::CommitNavigationParamsPtr commit_params =
blink::CreateCommitNavigationParams();
content::TestRenderFrame* frame =
static_cast<TestRenderFrame*>(GetMainRenderFrame());
NavigateAndWait(frame, common_params->Clone(), commit_params->Clone(),
web_view_);
// Verify that the script was not blocked.
EXPECT_FALSE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_TRUE(HasText(GetMainFrame(), "JS_ENABLED"));
RenderFrameImpl* main_frame = GetMainRenderFrame();
main_frame->DidFinishSameDocumentNavigation(
blink::kWebStandardCommit,
/*is_synchronously_committed=*/true,
blink::mojom::SameDocumentNavigationType::kFragment,
/*is_client_redirect=*/false,
/*screenshot_destination=*/std::nullopt,
/*same_document_metrics_token=*/base::UnguessableToken::Create(),
/*caused_by_ad=*/false);
// Verify that the script was not blocked.
EXPECT_FALSE(HasText(GetMainFrame(), "JS_DISABLED"));
EXPECT_TRUE(HasText(GetMainFrame(), "JS_ENABLED"));
}
namespace {
class ReadyToCommitCallbackObserver : public RenderFrameObserver {
public:
ReadyToCommitCallbackObserver(RenderFrame* render_frame,
base::RepeatingClosure callback)
: RenderFrameObserver(render_frame), callback_(std::move(callback)) {}
void ReadyToCommitNavigation(
blink::WebDocumentLoader* document_loader) override {
if (callback_) {
callback_.Run();
}
}
void OnDestruct() override {}
private:
base::RepeatingClosure callback_;
};
} // namespace
// Regression test for crbug.com/568719363: if script runs inside a
// RenderFrameObserver during DidCommitNavigation and rejects a same-document
// navigation's precommitHandler (or triggers DidFailAsyncSameDocumentCommit),
// the in-flight cross-document NavigationState must not be destroyed.
TEST_F(RenderFrameImplTest,
SameDocumentPrecommitFailureDuringCrossDocumentCommit) {
TestRenderFrame* frame = static_cast<TestRenderFrame*>(GetMainRenderFrame());
bool observer_ran = false;
ReadyToCommitCallbackObserver observer(
frame, base::BindLambdaForTesting([&]() {
observer_ran = true;
GetMainFrame()->ExecuteScript(
blink::WebScriptSource(blink::WebString::FromUtf8(
"window.precommitRan = false;"
"navigation.addEventListener('navigate', e => {"
" e.intercept({"
" precommitHandler: () => {"
" window.precommitRan = true;"
" return Promise.reject(new Error('abort'));"
" }"
" });"
"}, { once: true });"
"history.pushState(null, '', '#rejected');")));
// Also exercise RenderFrameImpl::DidFailAsyncSameDocumentCommit()
// directly while the cross-document commit is still on the stack.
frame->DidFailAsyncSameDocumentCommit();
}));
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL("https://example.com/");
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
auto commit_params = blink::CreateCommitNavigationParams();
commit_params->origin_to_commit = url::Origin::Create(common_params->url);
const base::UnguessableToken expected_token =
base::UnguessableToken::Create();
commit_params->navigation_token = expected_token;
NavigateAndWait(frame, std::move(common_params), std::move(commit_params),
web_view_);
EXPECT_TRUE(observer_ran);
int precommit_ran = 0;
EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(
u"Number(window.precommitRan === true)", &precommit_ran));
EXPECT_EQ(1, precommit_ran);
auto last_commit_params = frame->TakeLastCommitParams();
ASSERT_TRUE(last_commit_params);
EXPECT_EQ(expected_token, last_commit_params->navigation_token);
}
TEST_F(RenderFrameImplTest, BrowserInitiatedAsyncSameDocumentPrecommitFailure) {
TestRenderFrame* frame = static_cast<TestRenderFrame*>(GetMainRenderFrame());
auto common_params = blink::CreateCommonNavigationParams();
common_params->url = GURL("https://example.com/");
common_params->navigation_type =
blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
auto commit_params = blink::CreateCommitNavigationParams();
commit_params->origin_to_commit = url::Origin::Create(common_params->url);
NavigateAndWait(frame, std::move(common_params), std::move(commit_params),
web_view_);
ExecuteJavaScriptForTests(
"window.rejectPrecommit = null;"
"navigation.addEventListener('navigate', e => {"
" e.intercept({"
" precommitHandler: () => new Promise((_, reject) => {"
" window.rejectPrecommit = reject;"
" })"
" });"
"}, { once: true });");
auto same_doc_common = blink::CreateCommonNavigationParams();
same_doc_common->url = GURL("https://example.com/#hash");
same_doc_common->navigation_type =
blink::mojom::NavigationType::SAME_DOCUMENT;
same_doc_common->should_replace_current_entry = false;
auto same_doc_commit = blink::CreateCommitNavigationParams();
same_doc_commit->is_browser_initiated = true;
std::optional<blink::mojom::CommitResult> callback_result;
static_cast<mojom::Frame*>(frame)->CommitSameDocumentNavigation(
std::move(same_doc_common), std::move(same_doc_commit),
base::BindLambdaForTesting([&](blink::mojom::CommitResult result) {
callback_result = result;
}));
// Deferred by precommitHandler, so the callback should not have run yet.
EXPECT_FALSE(callback_result.has_value());
ExecuteJavaScriptForTests("window.rejectPrecommit(new Error('abort'));");
EXPECT_EQ(blink::mojom::CommitResult::Aborted, callback_result);
}
TEST_F(RenderFrameImplTest, SubframeMojoJSDisabledByDefault) {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> child_context =
child_frame().GetWebFrame()->MainWorldScriptContext();
EXPECT_FALSE(blink::WebV8Features::IsMojoJSEnabledForTesting(child_context));
}
TEST_F(RenderFrameImplTest, SubframeMojoJSEnabled) {
// Enable MojoJS for the child frame BEFORE accessing its context.
child_frame().EnableMojoJsBindings(
content::mojom::ExtraMojoJsFeatures::New());
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> child_context =
child_frame().GetWebFrame()->MainWorldScriptContext();
EXPECT_TRUE(blink::WebV8Features::IsMojoJSEnabledForTesting(child_context));
}
#if BUILDFLAG(ENABLE_LIBRARY_CDMS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
namespace {
// Sends `capabilities` when the observer is set, as the browser does.
class FakeKeySystemSupport : public media::mojom::KeySystemSupport {
public:
explicit FakeKeySystemSupport(KeySystemCapabilities capabilities)
: capabilities_(std::move(capabilities)) {}
void Bind(mojo::ScopedMessagePipeHandle handle) {
receiver_.Bind(mojo::PendingReceiver<media::mojom::KeySystemSupport>(
std::move(handle)));
}
// Closes both connections, as the browser does when the document changes.
void Disconnect() {
receiver_.reset();
observer_.reset();
}
void UpdateCapabilities(KeySystemCapabilities capabilities) {
capabilities_ = std::move(capabilities);
observer_->OnKeySystemSupportUpdated(capabilities_);
}
void set_on_request_cb(
base::RepeatingCallback<void(std::string)> on_request_cb) {
on_request_cb_ = std::move(on_request_cb);
}
const std::vector<std::string>& requested_key_systems() const {
return requested_key_systems_;
}
// media::mojom::KeySystemSupport
void SetObserver(mojo::PendingRemote<media::mojom::KeySystemSupportObserver>
observer) override {
observer_.Bind(std::move(observer));
observer_->OnKeySystemSupportUpdated(capabilities_);
}
void RequestKeySystemCapabilities(const std::string& key_system) override {
requested_key_systems_.push_back(key_system);
if (on_request_cb_) {
on_request_cb_.Run(key_system);
}
}
private:
KeySystemCapabilities capabilities_;
std::vector<std::string> requested_key_systems_;
base::RepeatingCallback<void(std::string)> on_request_cb_;
mojo::Receiver<media::mojom::KeySystemSupport> receiver_{this};
mojo::Remote<media::mojom::KeySystemSupportObserver> observer_;
};
class TestKeySystemInfo : public media::KeySystemInfo {
public:
explicit TestKeySystemInfo(std::string key_system)
: key_system_(std::move(key_system)) {}
std::string GetBaseKeySystemName() const override { return key_system_; }
bool IsSupportedInitDataType(media::EmeInitDataType) const override {
return true;
}
media::EmeConfig::Rule GetEncryptionSchemeConfigRule(
media::EncryptionScheme) const override {
return media::EmeConfig::SupportedRule();
}
media::SupportedCodecs GetSupportedCodecs() const override {
return media::EME_CODEC_ALL;
}
media::EmeConfig::Rule GetRobustnessConfigRule(const std::string&,
media::EmeMediaType,
const std::string&,
const bool*) const override {
return media::EmeConfig::SupportedRule();
}
media::EmeConfig::Rule GetPersistentLicenseSessionSupport() const override {
return media::EmeConfig::UnsupportedRule();
}
media::EmeFeatureSupport GetPersistentStateSupport() const override {
return media::EmeFeatureSupport::NOT_SUPPORTED;
}
media::EmeFeatureSupport GetDistinctiveIdentifierSupport() const override {
return media::EmeFeatureSupport::REQUESTABLE;
}
bool UseAesDecryptor() const override { return true; }
private:
const std::string key_system_;
};
class FakeMediaClient : public media::MediaClient {
public:
bool IsDecoderSupportedAudioType(const media::AudioType&) override {
return true;
}
bool IsDecoderSupportedVideoType(const media::VideoType&) override {
return true;
}
bool IsEncoderSupportedVideoType(const media::VideoType&) override {
return true;
}
bool IsSupportedBitstreamAudioCodec(media::AudioCodec) override {
return false;
}
bool ShouldSuppressAudioTracks() override { return false; }
std::optional<media::AudioRendererAlgorithmParameters>
GetAudioRendererAlgorithmParameters(media::AudioParameters) override {
return std::nullopt;
}
media::ExternalMemoryAllocator* GetMediaAllocator() override {
return nullptr;
}
};
class KeySystemSupportContentRendererClient : public ContentRendererClient {
public:
std::unique_ptr<media::KeySystemSupportRegistration> GetSupportedKeySystems(
RenderFrame* render_frame,
media::GetSupportedKeySystemsCB cb) override {
return ObserveKeySystemSupportUpdate(
render_frame,
base::BindRepeating(
[](media::GetSupportedKeySystemsCB cb,
KeySystemCapabilities capabilities) {
media::KeySystemInfos key_systems;
base::flat_set<std::string> pending_key_systems;
for (const auto& [key_system, capability] : capabilities) {
if (capability.IsPending()) {
pending_key_systems.insert(key_system);
continue;
}
if (capability.sw_cdm_capability_or_status.has_value()) {
key_systems.push_back(
std::make_unique<TestKeySystemInfo>(key_system));
}
}
cb.Run(std::move(key_systems), std::move(pending_key_systems));
},
std::move(cb)));
}
};
class SameDocumentNavigationWaiter : public RenderFrameObserver {
public:
explicit SameDocumentNavigationWaiter(RenderFrame* render_frame)
: RenderFrameObserver(render_frame) {}
void Wait() { run_loop_.Run(); }
void DidFinishSameDocumentNavigation() override { run_loop_.Quit(); }
void OnDestruct() override {}
private:
base::RunLoop run_loop_;
};
} // namespace
// Pending capabilities are reported as unsupported once the connection to the
// browser closes, so that queries waiting for them don't hang.
TEST_F(RenderFrameImplTest, KeySystemSupportDisconnectResolvesPending) {
constexpr char kKeySystem[] = "com.example.foo";
KeySystemCapabilities capabilities;
capabilities.emplace(
kKeySystem,
media::KeySystemCapability(
media::CdmCapability(),
base::unexpected(media::CdmCapabilityQueryStatus::kPending)));
FakeKeySystemSupport key_system_support(std::move(capabilities));
GetMainRenderFrame()->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_,
base::BindRepeating(&FakeKeySystemSupport::Bind,
base::Unretained(&key_system_support)));
base::test::TestFuture<KeySystemCapabilities> update;
std::unique_ptr<media::KeySystemSupportRegistration> registration =
ObserveKeySystemSupportUpdate(GetMainRenderFrame(),
update.GetRepeatingCallback());
EXPECT_TRUE(update.Take().at(kKeySystem).IsPending());
key_system_support.Disconnect();
const media::KeySystemCapability capability = update.Take().at(kKeySystem);
EXPECT_TRUE(capability.sw_cdm_capability_or_status.has_value());
ASSERT_FALSE(capability.hw_cdm_capability_or_status.has_value());
EXPECT_EQ(capability.hw_cdm_capability_or_status.error(),
media::CdmCapabilityQueryStatus::kDisconnectionError);
GetMainRenderFrame()->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_, {});
}
// Navigating to a new document in the same RenderFrameImpl (both from the
// initial empty document to a same-origin document with reused
// BrowserInterfaceBroker, and across subsequent commits that reset
// BrowserInterfaceBroker) drops the old document's KeySystemSupport connection.
// Requests in flight and objects retained from the old document keep working,
// and the new document rebinds and requests previously unqueried key systems.
TEST_F(RenderFrameImplTest, KeySystemSupportRebindsAfterNavigation) {
constexpr char kLazyKeySystem[] = "com.example.foo";
constexpr char kPermKeySystem[] = "com.example.perm";
FakeMediaClient media_client;
media::SetMediaClient(&media_client);
KeySystemSupportContentRendererClient renderer_client;
ContentRendererClient* old_renderer_client =
SetRendererClientForTesting(&renderer_client);
// `src='javascript:void 0'` keeps `sub` on its initial empty document so the
// first same-origin commit reuses `LocalDOMWindow` and
// `BrowserInterfaceBroker`.
LoadHTMLWithUrlOverride(
"<iframe id='sub' src='javascript:void 0' "
"allow='encrypted-media *'></iframe>",
kAutoplayTestOrigin);
blink::WebLocalFrame* sub_web_frame =
GetMainFrame()->FirstChild()->ToWebLocalFrame();
RenderFrame* sub_frame = RenderFrame::FromWebFrame(sub_web_frame);
mojo::ScopedMessagePipeHandle held_permission_pipe;
base::ScopedClosureRunner cleanup(base::BindLambdaForTesting([&] {
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_, {});
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
blink::mojom::PermissionService::Name_, {});
SetRendererClientForTesting(old_renderer_client);
media::SetMediaClient(nullptr);
}));
// Hold the PermissionService pipe open so an in-flight RequestPermission()
// call stays suspended until the navigation fails it.
base::test::TestFuture<void> permission_requested;
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
blink::mojom::PermissionService::Name_,
base::BindLambdaForTesting([&](mojo::ScopedMessagePipeHandle pipe) {
held_permission_pipe = std::move(pipe);
if (!permission_requested.IsReady()) {
permission_requested.SetValue();
}
}));
auto make_capabilities = [&](bool lazy_finalized) {
KeySystemCapabilities capabilities;
capabilities.emplace(
kLazyKeySystem,
media::KeySystemCapability(
lazy_finalized
? media::CdmCapabilityOrStatus(media::CdmCapability())
: base::unexpected(media::CdmCapabilityQueryStatus::kPending),
base::unexpected(media::CdmCapabilityQueryStatus::kUnknown)));
capabilities.emplace(
kPermKeySystem,
media::KeySystemCapability(
media::CdmCapability(),
base::unexpected(media::CdmCapabilityQueryStatus::kUnknown)));
return capabilities;
};
// Document 1 (`sub_frame`'s initial empty document `about:blank`, inheriting
// `kAutoplayTestOrigin`): retain a ClearKey MediaKeySystemAccess in the
// parent window and leave a `kPermKeySystem` permission check in flight,
// without ever querying `kLazyKeySystem`.
FakeKeySystemSupport doc1_support(
make_capabilities(/*lazy_finalized=*/false));
base::test::TestFuture<std::string> requested_in_doc1;
doc1_support.set_on_request_cb(requested_in_doc1.GetRepeatingCallback());
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_,
base::BindRepeating(&FakeKeySystemSupport::Bind,
base::Unretained(&doc1_support)));
v8::HandleScope handle_scope(Isolate());
v8::Local<v8::Context> doc1_context = sub_web_frame->MainWorldScriptContext();
{
SameDocumentNavigationWaiter waiter(GetMainRenderFrame());
ExecuteJavaScriptForTests(
"const subWin = document.getElementById('sub').contentWindow;"
"subWin.navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{"
" initDataTypes: ['webm'],"
" audioCapabilities: [{contentType: 'audio/webm; codecs=\"opus\"'}]"
"}]).then((access) => {"
" window.doc1Access = access;"
" return access.createMediaKeys();"
"}).then((mediaKeys) => {"
" window.doc1MediaKeys = mediaKeys;"
" subWin.navigator.requestMediaKeySystemAccess('com.example.perm', [{"
" initDataTypes: ['cenc'],"
" distinctiveIdentifier: 'required',"
" audioCapabilities: [{contentType: 'audio/webm; codecs=\"opus\"'}]"
" }]).then(() => { window.doc1PermResult = 'OK'; },"
" () => { window.doc1PermResult = 'REJECTED'; });"
" location.hash = 'DOC1_OK';"
"}, () => { location.hash = 'DOC1_ERR'; });");
waiter.Wait();
}
ASSERT_EQ("DOC1_OK", GURL(GetMainFrame()->GetDocument().Url()).GetRef());
EXPECT_EQ(kPermKeySystem, requested_in_doc1.Take());
ASSERT_TRUE(permission_requested.Wait());
// Commit a same-origin document in `sub_frame` (from the initial empty
// document, so `should_reset_browser_interface_broker` is false).
FakeKeySystemSupport doc2_support(
make_capabilities(/*lazy_finalized=*/false));
base::test::TestFuture<std::string> requested_in_doc2;
doc2_support.set_on_request_cb(requested_in_doc2.GetRepeatingCallback());
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_, {});
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_,
base::BindRepeating(&FakeKeySystemSupport::Bind,
base::Unretained(&doc2_support)));
doc1_support.Disconnect();
{
// Within the same document after disconnect, `key_system_support_` stays
// bound (disconnected) without rebinding `doc2_support`, and unqueried
// pending key systems reject cleanly instead of hanging.
SameDocumentNavigationWaiter waiter(GetMainRenderFrame());
ExecuteJavaScriptForTests(
"document.getElementById('sub').contentWindow.navigator"
".requestMediaKeySystemAccess('com.example.foo', [{"
" initDataTypes: ['cenc'],"
" audioCapabilities: [{contentType: 'audio/webm; codecs=\"opus\"'}]"
"}]).then(() => { location.hash = 'DOC1_DISCONNECTED_UNEXPECTED_OK'; },"
" () => { location.hash = 'DOC1_DISCONNECTED_REJECTED'; });");
waiter.Wait();
}
EXPECT_EQ("DOC1_DISCONNECTED_REJECTED",
GURL(GetMainFrame()->GetDocument().Url()).GetRef());
EXPECT_FALSE(requested_in_doc2.IsReady());
auto* test_sub_frame = static_cast<TestRenderFrame*>(sub_frame);
std::ignore = test_sub_frame->TakeLastBrowserInterfaceBrokerReceiver();
{
FrameLoadWaiter load_waiter(sub_frame);
test_sub_frame->SetHTMLOverrideForNextNavigation("<html></html>");
ExecuteJavaScriptForTests(
"document.getElementById('sub').src = 'https://www.google.com/doc2';");
load_waiter.Wait();
}
ASSERT_FALSE(
test_sub_frame->TakeLastBrowserInterfaceBrokerReceiver().is_valid());
ASSERT_EQ(doc1_context, sub_web_frame->MainWorldScriptContext());
// The navigation failed the permission check that was in flight in Document
// 1, so that request is rejected instead of being left unsettled. The
// MediaKeySystemAccess retained from Document 1 can still create MediaKeys.
{
SameDocumentNavigationWaiter waiter(GetMainRenderFrame());
ExecuteJavaScriptForTests(
"window.doc1Access.createMediaKeys().then("
" () => { location.hash = 'CDM_OK_PERM_' + window.doc1PermResult; },"
" () => { location.hash = 'CDM_ERR'; });");
waiter.Wait();
}
EXPECT_EQ("CDM_OK_PERM_REJECTED",
GURL(GetMainFrame()->GetDocument().Url()).GetRef());
// A MediaKeys created in Document 1 survives in the reused LocalDOMWindow and
// can still call createSession() + generateRequest() without dangling
// KeySystems pointers.
{
SameDocumentNavigationWaiter waiter(GetMainRenderFrame());
ExecuteJavaScriptForTests(
"window.doc1MediaKeys.createSession()"
" .generateRequest('webm', new Uint8Array([1])).then("
" () => { location.hash = 'OLD_SESSION_OK'; },"
" () => { location.hash = 'OLD_SESSION_ERR'; });");
waiter.Wait();
}
EXPECT_EQ("OLD_SESSION_OK",
GURL(GetMainFrame()->GetDocument().Url()).GetRef());
// Document 2 queries `kLazyKeySystem`, rebinding KeySystemSupport and
// resolving once capabilities arrive.
{
SameDocumentNavigationWaiter waiter(sub_frame);
ExecuteJavaScriptForTests(
"const subWin2 = document.getElementById('sub').contentWindow;"
"subWin2.navigator.requestMediaKeySystemAccess('com.example.foo', [{"
" initDataTypes: ['cenc'],"
" audioCapabilities: [{contentType: 'audio/webm; codecs=\"opus\"'}]"
"}]).then(() => { subWin2.location.hash = 'DOC2_OK'; },"
" () => { subWin2.location.hash = 'DOC2_ERR'; });");
EXPECT_EQ(kLazyKeySystem, requested_in_doc2.Take());
doc2_support.UpdateCapabilities(make_capabilities(/*lazy_finalized=*/true));
waiter.Wait();
}
EXPECT_EQ("DOC2_OK", GURL(sub_web_frame->GetDocument().Url()).GetRef());
// Commit a third document in `sub_frame` (no longer on the initial empty
// document, so `should_reset_browser_interface_broker` is true) and verify
// rebinding works there too.
FakeKeySystemSupport doc3_support(
make_capabilities(/*lazy_finalized=*/false));
base::test::TestFuture<std::string> requested_in_doc3;
doc3_support.set_on_request_cb(requested_in_doc3.GetRepeatingCallback());
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_, {});
sub_frame->GetBrowserInterfaceBroker().SetBinderForTesting(
media::mojom::KeySystemSupport::Name_,
base::BindRepeating(&FakeKeySystemSupport::Bind,
base::Unretained(&doc3_support)));
doc2_support.Disconnect();
{
FrameLoadWaiter load_waiter(sub_frame);
test_sub_frame->SetHTMLOverrideForNextNavigation("<html></html>");
ExecuteJavaScriptForTests(
"document.getElementById('sub').src = 'https://www.google.com/doc3';");
load_waiter.Wait();
}
ASSERT_TRUE(
test_sub_frame->TakeLastBrowserInterfaceBrokerReceiver().is_valid());
ASSERT_NE(doc1_context, sub_web_frame->MainWorldScriptContext());
{
SameDocumentNavigationWaiter waiter(sub_frame);
ExecuteJavaScriptForTests(
"const subWin3 = document.getElementById('sub').contentWindow;"
"subWin3.navigator.requestMediaKeySystemAccess('com.example.foo', [{"
" initDataTypes: ['cenc'],"
" audioCapabilities: [{contentType: 'audio/webm; codecs=\"opus\"'}]"
"}]).then(() => { subWin3.location.hash = 'DOC3_OK'; },"
" () => { subWin3.location.hash = 'DOC3_ERR'; });");
EXPECT_EQ(kLazyKeySystem, requested_in_doc3.Take());
doc3_support.UpdateCapabilities(make_capabilities(/*lazy_finalized=*/true));
waiter.Wait();
}
EXPECT_EQ("DOC3_OK", GURL(sub_web_frame->GetDocument().Url()).GetRef());
}
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS) || BUILDFLAG(IS_WIN) ||
// BUILDFLAG(IS_ANDROID)
class RenderFrameImplMojoJsTest : public RenderViewTest {
public:
RenderFrameImplMojoJsTest() {
// An empty scoped feature list is used here to make sure that the feature
// list can be (re)initialized in the death test successfully, otherwise a
// CHECK will hit when tracing tries to reinitiailize in the same process.
// This is an unfortunate quirk of how death tests work for browser tests.
scoped_feature_list_.InitWithFeatures({}, {});
}
void SetUp() override {
RenderViewTest::SetUp();
// RenderViewTest::SetUp() enables every test-only feature, which includes
// MojoJS and MojoJSTest. Undo that grant here so test cases can exercise
// attempts to bypass the protection.
disallow_mojo_js_.emplace();
EXPECT_TRUE(GetMainRenderFrame()->IsMainFrame());
}
void TearDown() override {
disallow_mojo_js_.reset();
#if defined(LEAK_SANITIZER)
// Do this before shutting down V8 in RenderViewTest::TearDown().
// http://crbug.com/328552
__lsan_do_leak_check();
#endif
RenderViewTest::TearDown();
}
TestRenderFrame* GetMainRenderFrame() {
return static_cast<TestRenderFrame*>(RenderViewTest::GetMainRenderFrame());
}
// Gets the main world script context for the test main frame and returns if
// MojoJS bindings are enabled.
bool IsMojoJsEnabledForScriptContext() {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> local_v8_context =
GetMainFrame()->MainWorldScriptContext();
return blink::WebV8Features::IsMojoJSEnabledForTesting(local_v8_context);
}
// Method used to validate the final stage protected memory check in
// ContextFeatureSettings::isMojoJSEnabled constructs a scenario where
// the |enable_mojo_js_| value of the ContextFeatureSettings is tampered with
// directly before being used. We expect this to crash.
void ContextFeatureSettingsEnableMojoJsTampered() {
v8::Isolate* isolate = Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> local_v8_context =
GetMainFrame()->MainWorldScriptContext();
// Use |WebV8Features::EnableMojoJSForTesting| to enable the MojoJS bindings
// while bypassing the earlier protected memory checks. This mimics an
// attacker tampering with the |enable_mojo_js_| value of the
// ContextFeatureSettings.
blink::WebV8Features::EnableMojoJSWithoutSecurityChecksForTesting(
local_v8_context);
// Use |WebV8Features::isMojoJSEnabledForTesting| to manually access
// |ContextFeatureSettings::isMojoEnabled()| This is used to mimic a
// scenario where an attacker manages to directly tamper with
// |enable_mojo_js_| before the |ContextFeatureSettings::isMojoJSEnabled()|
// is called to determine if the bindings is enabled. This validates the
// final protected memory check and should crash.
blink::WebV8Features::IsMojoJSEnabledForTesting(local_v8_context);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::optional<blink::ScopedDisallowMojoJsForTesting> disallow_mojo_js_;
};
// Verifies enabling MojoJS bindings.
TEST_F(RenderFrameImplMojoJsTest, AllowMojoWebUIBindings) {
GetMainRenderFrame()->AllowBindings(
BindingsPolicySet({BindingsPolicyValue::kMojoWebUi}).ToEnumBitmask());
LoadHTML(kSimpleScriptHtml);
// Expect no crash and MojoJs bindings are enabled in the context.
EXPECT_TRUE(IsMojoJsEnabledForScriptContext());
}
// Verifies enabling MojoJS bindings via EnableMojoJsBindings method.
TEST_F(RenderFrameImplMojoJsTest, EnableMojoJSBindings) {
GetMainRenderFrame()->EnableMojoJsBindings(
content::mojom::ExtraMojoJsFeatures::New());
LoadHTML(kSimpleScriptHtml);
// Expect no crash and MojoJs bindings are enabled in the context.
EXPECT_TRUE(IsMojoJsEnabledForScriptContext());
}
// Verifies enabling MojoJS bindings via directly enabling mojo
TEST_F(RenderFrameImplMojoJsTest, EnableMojoJSBindingsWithBroker) {
GetMainRenderFrame()->EnableMojoJsBindingsWithBroker(
TestRenderFrame::CreateStubBrowserInterfaceBrokerRemote());
LoadHTML(kSimpleScriptHtml);
// Expect no crash and MojoJs bindings are enabled in the context.
EXPECT_TRUE(IsMojoJsEnabledForScriptContext());
}
#if BUILDFLAG(PROTECTED_MEMORY_ENABLED)
using RenderFrameImplMojoJsDeathTest = RenderFrameImplMojoJsTest;
// Verifies that tampering with enabled_bindings_ to enable MojoJS bindings
// crashes.
TEST_F(RenderFrameImplMojoJsDeathTest, EnabledBindingsTampered) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Should CHECK fail due to the bindings value differing from the protected
// memory value.
EXPECT_CHECK_DEATH_WITH(
{
GetMainRenderFrame()->enabled_bindings_.Put(
BindingsPolicyValue::kMojoWebUi);
LoadHTML(kSimpleScriptHtml);
},
"Check failed: IsMojoJSAllowedPerContextForProcess");
}
// Verifies that tampering with enable_mojo_js_bindings_ to enable MojoJS
// bindings crashes.
TEST_F(RenderFrameImplMojoJsDeathTest, EnableMojoJsBindingsTampered) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Should CHECK fail due to the bindings value differing from the protected
// memory value.
EXPECT_CHECK_DEATH_WITH(
{
GetMainRenderFrame()->enable_mojo_js_bindings_ = true;
LoadHTML(kSimpleScriptHtml);
},
"Check failed: IsMojoJSAllowedPerContextForProcess");
}
// Verifies that tampering with mojo_js_interface_broker_ to enable MojoJS
// bindings crashes.
TEST_F(RenderFrameImplMojoJsDeathTest, MojoJsInterfaceBrokerTampered) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Should CHECK fail due to the bindings value differing from the protected
// memory value.
EXPECT_CHECK_DEATH_WITH(
{
GetMainRenderFrame()->mojo_js_interface_broker_ =
TestRenderFrame::CreateStubBrowserInterfaceBrokerRemote();
LoadHTML(kSimpleScriptHtml);
},
"Check failed: IsMojoJSAllowedPerContextForProcess");
}
// Verifies that tampering with mojo_js_interface_broker_ to enable MojoJS
// bindings crashes.
TEST_F(RenderFrameImplMojoJsDeathTest,
ContextFeatureSettingsEnableMojoJsTampered) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Should CHECK fail due to the bindings value differing from the protected
// memory value.
EXPECT_CHECK_DEATH_WITH(ContextFeatureSettingsEnableMojoJsTampered(),
"Check failed: IsMojoJSAllowedPerContextForProcess");
}
#endif // BUILDFLAG(PROTECTED_MEMORY_ENABLED)
} // namespace content