Commit dc8219b9 authored by Kevin Marshall's avatar Kevin Marshall Committed by Commit Bot

[fuchsia] Apply fuchsia.web.AutoplayPolicy from the FIDL API.

Applies the fuchsia.web.AutoplayPolicy which may be passed in at Frame
creation time to the Frame's top-level WebContents.

Test: autoplay_browsertest.cc
Bug: 1067101
Change-Id: Id3b69ddea474c9b8d6f2a1b05ec195270cad3cf5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533555
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827132}
parent 842ac7e1
......@@ -287,6 +287,7 @@ source_set("browsertest_core") {
]
data = [ "test/data" ]
deps = [
":switches",
":web_engine_core",
"//content/public/browser",
"//content/test:test_support",
......@@ -300,6 +301,7 @@ source_set("browsertest_core") {
test("web_engine_browsertests") {
sources = [
"browser/accessibility_bridge_browsertest.cc",
"browser/autoplay_browsertest.cc",
"browser/cast_streaming_browsertest.cc",
"browser/content_directory_browsertest.cc",
"browser/context_impl_browsertest.cc",
......
// Copyright 2019 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/engine/test/web_engine_browser_test.h"
#include "base/files/file_path.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/frame_impl.h"
#include "fuchsia/engine/test/test_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.h"
class AutoplayTest : public cr_fuchsia::WebEngineBrowserTest {
public:
AutoplayTest() {
set_test_server_root(base::FilePath(cr_fuchsia::kTestServerRoot));
}
~AutoplayTest() override = default;
AutoplayTest(const AutoplayTest&) = delete;
AutoplayTest& operator=(const AutoplayTest&) = delete;
void SetUpOnMainThread() override {
CHECK(embedded_test_server()->Start());
cr_fuchsia::WebEngineBrowserTest::SetUpOnMainThread();
}
void SetUpCommandLine(base::CommandLine* command_line) final {
SetHeadlessInCommandLine(command_line);
cr_fuchsia::WebEngineBrowserTest::SetUpCommandLine(command_line);
}
protected:
// Creates a Frame with |navigation_listener_| attached and |policy|
// applied.
fuchsia::web::FramePtr CreateFrame(fuchsia::web::AutoplayPolicy policy) {
fuchsia::web::CreateFrameParams params;
params.set_autoplay_policy(policy);
fuchsia::web::FramePtr frame = WebEngineBrowserTest::CreateFrameWithParams(
&navigation_listener_, std::move(params));
frame->GetNavigationController(controller_.NewRequest());
return frame;
}
cr_fuchsia::TestNavigationListener navigation_listener_;
fuchsia::web::NavigationControllerPtr controller_;
};
IN_PROC_BROWSER_TEST_F(
AutoplayTest,
UserActivationPolicy_UserActivatedViaSimulatedInteraction) {
const GURL kUrl(embedded_test_server()->GetURL("/play_vp8.html?autoplay=1"));
fuchsia::web::FramePtr frame =
CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
fuchsia::web::LoadUrlParams params;
EXPECT_TRUE(
cr_fuchsia::LoadUrlAndExpectResponse(controller_.get(), {}, kUrl.spec()));
navigation_listener_.RunUntilUrlEquals(kUrl);
context_impl()
->GetFrameImplForTest(&frame)
->web_contents_for_test()
->GetMainFrame()
->NotifyUserActivation(
blink::mojom::UserActivationNotificationType::kTest);
navigation_listener_.RunUntilUrlAndTitleEquals(kUrl, "playing");
}
IN_PROC_BROWSER_TEST_F(AutoplayTest,
UserActivationPolicy_UserActivatedNavigation) {
const GURL kUrl(embedded_test_server()->GetURL("/play_vp8.html?autoplay=1"));
fuchsia::web::FramePtr frame =
CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
fuchsia::web::LoadUrlParams params;
params.set_was_user_activated(true);
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
controller_.get(), std::move(params), kUrl.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(kUrl, "playing");
}
IN_PROC_BROWSER_TEST_F(AutoplayTest, UserActivationPolicy_NoUserActivation) {
const GURL kUrl(embedded_test_server()->GetURL("/play_vp8.html?autoplay=1"));
fuchsia::web::FramePtr frame =
CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
fuchsia::web::LoadUrlParams params;
params.set_was_user_activated(false);
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
controller_.get(), std::move(params), kUrl.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(kUrl, "blocked");
}
IN_PROC_BROWSER_TEST_F(AutoplayTest,
AllowAllPolicy_DefaultNotUserActivatedNavigation) {
const GURL kUrl(embedded_test_server()->GetURL("/play_vp8.html?autoplay=1"));
fuchsia::web::FramePtr frame =
CreateFrame(fuchsia::web::AutoplayPolicy::ALLOW);
// The page is deliberately not user activated.
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
controller_.get(), fuchsia::web::LoadUrlParams(), kUrl.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(kUrl, "playing");
}
......@@ -17,6 +17,8 @@
#include "fuchsia/cast_streaming/public/cast_streaming_session.h"
#include "fuchsia/engine/browser/frame_impl.h"
#include "fuchsia/engine/browser/web_engine_devtools_controller.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom.h"
ContextImpl::ContextImpl(content::BrowserContext* browser_context,
WebEngineDevToolsController* devtools_controller)
......@@ -71,6 +73,27 @@ void ContextImpl::CreateFrameWithParams(
create_params.initially_hidden = true;
auto web_contents = content::WebContents::Create(create_params);
blink::web_pref::WebPreferences web_preferences =
web_contents->GetOrCreateWebPreferences();
// REQUIRE_USER_ACTIVATION is the default per the FIDL API.
web_preferences.autoplay_policy =
blink::mojom::AutoplayPolicy::kDocumentUserActivationRequired;
if (params.has_autoplay_policy()) {
switch (params.autoplay_policy()) {
case fuchsia::web::AutoplayPolicy::ALLOW:
web_preferences.autoplay_policy =
blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
break;
case fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION:
web_preferences.autoplay_policy =
blink::mojom::AutoplayPolicy::kDocumentUserActivationRequired;
break;
}
}
web_contents->SetWebPreferences(web_preferences);
// Register the new Frame with the DevTools controller. The controller will
// reject registration if user-debugging is requested, but it is not enabled
// in the controller.
......
......@@ -127,11 +127,6 @@ void WebEngineContentBrowserClient::OverrideWebkitPrefs(
if (allow_insecure_content_)
web_prefs->allow_running_insecure_content = true;
// Allow media to autoplay.
// TODO(crbug.com/1067101): Provide a FIDL API to configure AutoplayPolicy.
web_prefs->autoplay_policy =
blink::mojom::AutoplayPolicy::kNoUserGestureRequired;
}
void WebEngineContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
......
......@@ -4,6 +4,7 @@
<script>
var bear = document.createElement('video');
var isMetadataLoaded = false;
var isPlaying = false;
var autoplay = (window.location.href.indexOf("autoplay") > 0);
if (autoplay) {
......@@ -11,10 +12,24 @@
}
bear.onerror = function() { document.title = 'error'; }
bear.onloadeddata = function() { document.title = 'loaded'; }
bear.onloadeddata = function() {
document.title = 'loaded';
if (autoplay) {
// No events are generated if autoplay's been denied, so use a
// 0.2s timeout to detect and report the denial.
window.setTimeout(function() {
if (!isPlaying)
document.title = 'blocked';
}, 200);
}
}
bear.onloadedmetadata = function() { isMetadataLoaded = true; }
bear.onpause = function () { isPlaying = false; }
bear.onplay = function() { document.title = 'playing'; }
bear.onplay = function() {
isPlaying = true;
document.title = 'playing';
}
bear.onstalled = function() { document.title = 'stalled'; }
bear.src = 'bear-vp8a.webm';
</script>
......
......@@ -6,13 +6,17 @@
#include <fuchsia/web/cpp/fidl.h>
#include "base/command_line.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/run_loop.h"
#include "fuchsia/engine/browser/web_engine_browser_context.h"
#include "fuchsia/engine/browser/web_engine_browser_main_parts.h"
#include "fuchsia/engine/browser/web_engine_content_browser_client.h"
#include "fuchsia/engine/switches.h"
#include "fuchsia/engine/web_engine_main_delegate.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "ui/gfx/switches.h"
#include "ui/ozone/public/ozone_switches.h"
namespace cr_fuchsia {
......@@ -59,8 +63,15 @@ void WebEngineBrowserTest::PostRunTestOnMainThread() {
fuchsia::web::FramePtr WebEngineBrowserTest::CreateFrame(
fuchsia::web::NavigationEventListener* listener) {
return CreateFrameWithParams(listener, {});
}
fuchsia::web::FramePtr WebEngineBrowserTest::CreateFrameWithParams(
fuchsia::web::NavigationEventListener* listener,
fuchsia::web::CreateFrameParams params) {
fuchsia::web::FramePtr frame;
context_->CreateFrame(frame.NewRequest());
context_->CreateFrameWithParams(std::move(params), frame.NewRequest());
if (listener) {
frame->SetNavigationEventListener(
......@@ -74,6 +85,13 @@ fuchsia::web::FramePtr WebEngineBrowserTest::CreateFrame(
return frame;
}
void WebEngineBrowserTest::SetHeadlessInCommandLine(
base::CommandLine* command_line) {
command_line->AppendSwitchNative(switches::kOzonePlatform,
switches::kHeadless);
command_line->AppendSwitch(switches::kHeadless);
}
// static
void WebEngineBrowserTest::SetContextClientChannel(zx::channel channel) {
DCHECK(channel);
......
......@@ -10,6 +10,7 @@
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/optional.h"
#include "content/public/test/browser_test_base.h"
#include "fuchsia/engine/browser/context_impl.h"
......@@ -26,11 +27,18 @@ class WebEngineBrowserTest : public content::BrowserTestBase {
// object by WebEngineBrowserTest.
static void SetContextClientChannel(zx::channel channel);
// Creates a Frame for this Context.
// Creates a Frame for this Context using default parameters.
// |listener|: If set, specifies the navigation listener for the Frame.
fuchsia::web::FramePtr CreateFrame(
fuchsia::web::NavigationEventListener* listener);
// Creates a Frame for this Context using non-default parameters.
// |listener|: If set, specifies the navigation listener for the Frame.
// |params|: The CreateFrameParams to use.
fuchsia::web::FramePtr CreateFrameWithParams(
fuchsia::web::NavigationEventListener* listener,
fuchsia::web::CreateFrameParams params);
// Gets the client object for the Context service.
fuchsia::web::ContextPtr& context() { return context_; }
......@@ -42,6 +50,8 @@ class WebEngineBrowserTest : public content::BrowserTestBase {
return navigation_listener_bindings_;
}
void SetHeadlessInCommandLine(base::CommandLine* command_line);
void set_test_server_root(const base::FilePath& path) {
test_server_root_ = path;
}
......
......@@ -65,6 +65,8 @@ void WebComponent::StartComponent() {
// Create the underlying Frame and get its NavigationController.
fuchsia::web::CreateFrameParams create_params;
create_params.set_enable_remote_debugging(enable_remote_debugging_);
create_params.set_autoplay_policy(
fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
runner_->CreateFrameWithParams(std::move(create_params), frame_.NewRequest());
// If the Frame unexpectedly disconnects then tear-down this Component.
......
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