Skip to content

Commit aca56ae

Browse files
Add new desktop policy to control audio/video autoplay.
The "media.autoplay_allowed" preference can be set to allow autoplay as a Chrome Policy. It is meant to be used for internal websites that may be broken by the change described at chromium.org/audio-video/autoplay. Bug: 779087, 819234 Change-Id: I6a2b5a13e80adfc72056536bc9cf425e30f50e6a Reviewed-on: https://chromium-review.googlesource.com/941956 Commit-Queue: François Beaufort <beaufort.francois@gmail.com> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org> Reviewed-by: Mounir Lamouri <mlamouri@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#540805}(cherry picked from commit 4f8107d) Reviewed-on: https://chromium-review.googlesource.com/951943 Reviewed-by: François Beaufort <beaufort.francois@gmail.com> Cr-Commit-Position: refs/branch-heads/3359@{#68} Cr-Branched-From: 66afc5e-refs/heads/master@{#540276}
1 parent 5dd5b5f commit aca56ae

File tree

9 files changed

+87
-2
lines changed

9 files changed

+87
-2
lines changed

chrome/browser/chrome_content_browser_client.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,9 @@ void ChromeContentBrowserClient::RegisterProfilePrefs(
891891
// used for mapping the command-line flags).
892892
registry->RegisterStringPref(prefs::kIsolateOrigins, std::string());
893893
registry->RegisterBooleanPref(prefs::kSitePerProcess, false);
894+
#if !defined(OS_ANDROID)
895+
registry->RegisterBooleanPref(prefs::kAutoplayAllowed, false);
896+
#endif
894897
}
895898

896899
// static
@@ -2750,6 +2753,14 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
27502753
}
27512754
}
27522755

2756+
#if !defined(OS_ANDROID)
2757+
if (prefs->GetBoolean(prefs::kAutoplayAllowed) &&
2758+
prefs->IsManagedPreference(prefs::kAutoplayAllowed)) {
2759+
web_prefs->autoplay_policy =
2760+
content::AutoplayPolicy::kNoUserGestureRequired;
2761+
}
2762+
#endif // !defined(OS_ANDROID)
2763+
27532764
for (size_t i = 0; i < extra_parts_.size(); ++i)
27542765
extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs);
27552766
}

chrome/browser/policy/configuration_policy_handler_list_factory.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,12 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = {
774774
prefs::kRelaunchNotificationPeriod,
775775
base::Value::Type::INTEGER },
776776
#endif // !defined(OS_ANDROID)
777+
778+
#if !defined(OS_ANDROID)
779+
{ key::kAutoplayAllowed,
780+
prefs::kAutoplayAllowed,
781+
base::Value::Type::BOOLEAN },
782+
#endif // !defined(OS_ANDROID)
777783
};
778784
// clang-format on
779785

chrome/browser/policy/policy_browsertest.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
#include "content/public/common/content_switches.h"
159159
#include "content/public/common/result_codes.h"
160160
#include "content/public/common/url_constants.h"
161+
#include "content/public/common/web_preferences.h"
161162
#include "content/public/test/browser_test_utils.h"
162163
#include "content/public/test/download_test_observer.h"
163164
#include "content/public/test/mock_notification_observer.h"
@@ -5045,4 +5046,31 @@ IN_PROC_BROWSER_TEST_F(NoteTakingOnLockScreenPolicyTest,
50455046

50465047
#endif // defined(OS_CHROMEOS)
50475048

5049+
#if !defined(OS_ANDROID)
5050+
5051+
IN_PROC_BROWSER_TEST_F(PolicyTest, AutoplayAllowedByPolicy) {
5052+
ASSERT_TRUE(embedded_test_server()->Start());
5053+
content::WebContents* web_contents =
5054+
browser()->tab_strip_model()->GetActiveWebContents();
5055+
5056+
// Check that autoplay was not allowed.
5057+
EXPECT_EQ(
5058+
web_contents->GetRenderViewHost()->GetWebkitPreferences().autoplay_policy,
5059+
content::AutoplayPolicy::kDocumentUserActivationRequired);
5060+
5061+
// Update policy to allow autoplay.
5062+
PolicyMap policies;
5063+
SetPolicy(&policies, key::kAutoplayAllowed,
5064+
std::make_unique<base::Value>(true));
5065+
UpdateProviderPolicy(policies);
5066+
5067+
// Check that autoplay was allowed by policy.
5068+
web_contents->GetRenderViewHost()->OnWebkitPreferencesChanged();
5069+
EXPECT_EQ(
5070+
web_contents->GetRenderViewHost()->GetWebkitPreferences().autoplay_policy,
5071+
content::AutoplayPolicy::kNoUserGestureRequired);
5072+
}
5073+
5074+
#endif // !defined(OS_ANDROID)
5075+
50485076
} // namespace policy

chrome/common/pref_names.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,4 +2568,9 @@ const char kSitePerProcess[] = "site_isolation.site_per_process";
25682568
const char kWebDriverOverridesIncompatiblePolicies[] =
25692569
"webdriver.override_incompatible_policy";
25702570

2571+
#if !defined(OS_ANDROID)
2572+
// Boolean that specifies whether media (audio/video) autoplay is allowed.
2573+
const char kAutoplayAllowed[] = "media.autoplay_allowed";
2574+
#endif // !defined(OS_ANDROID)
2575+
25712576
} // namespace prefs

chrome/common/pref_names.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,10 @@ extern const char kIsolateOrigins[];
919919
extern const char kSitePerProcess[];
920920
extern const char kWebDriverOverridesIncompatiblePolicies[];
921921

922+
#if !defined(OS_ANDROID)
923+
extern const char kAutoplayAllowed[];
924+
#endif
925+
922926
} // namespace prefs
923927

924928
#endif // CHROME_COMMON_PREF_NAMES_H_

chrome/test/data/media/unified_autoplay.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<meta charset='utf-8'>
4-
<title>Simple autopaly test page</title>
4+
<title>Simple autoplay test page</title>
55
<video src='bigbuck.webm'></video>
66
<script>
77
function attemptPlay() {

chrome/test/data/policy/policy_test_cases.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,6 +3623,14 @@
36233623
]
36243624
},
36253625

3626+
"AutoplayAllowed": {
3627+
"os": ["win", "linux", "mac", "chromeos"],
3628+
"test_policy": { "AutoplayAllowed": false },
3629+
"pref_mappings": [
3630+
{ "pref": "media.autoplay_allowed" }
3631+
]
3632+
},
3633+
36263634
"----- Chrome Frame policies -------------------------------------------": {},
36273635

36283636
"ChromeFrameRendererSettings": {

components/policy/resources/policy_templates.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
# persistent IDs for all fields (but not for groups!) are needed. These are
147147
# specified by the 'id' keys of each policy. NEVER CHANGE EXISTING IDs,
148148
# because doing so would break the deployed wire format!
149-
# For your editing convenience: highest ID currently used: 429
149+
# For your editing convenience: highest ID currently used: 430
150150
# And don't forget to also update the EnterprisePolicies enum of
151151
# histograms.xml (run 'python tools/metrics/histograms/update_policies.py').
152152
#
@@ -11481,6 +11481,28 @@
1148111481
If this policy is set, <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> will try to register itself and apply associated cloud policy for all profiles.
1148211482

1148311483
The value of this policy is an Enrollment token that can be retrieved from the Google Admin console.''',
11484+
},
11485+
{
11486+
'name': 'AutoplayAllowed',
11487+
'type': 'main',
11488+
'schema': { 'type': 'boolean' },
11489+
'supported_on': ['chrome.win:66-', 'chrome.linux:66-', 'chrome.mac:66-', 'chrome_os:66-'],
11490+
'features': {
11491+
'dynamic_refresh': True,
11492+
'per_profile': True,
11493+
},
11494+
'example_value': True,
11495+
'id': 430,
11496+
'caption': '''Allows media autoplay''',
11497+
'tags': [],
11498+
'desc': '''Allows you to control if videos can play automatically (without user consent) with audio content in <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>.
11499+
11500+
If the policy is set to True, <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> is allowed to autoplay media.
11501+
If the policy is set to False, <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> is not allowed to autoplay media.
11502+
By default, <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> is not allowed to autoplay media.
11503+
11504+
Note that if <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> is running and this policy changes, it will be applied only to new opened tabs. Therefore some tabs might still observe the previous behavior.
11505+
''',
1148411506
}
1148511507
],
1148611508

tools/metrics/histograms/enums.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12617,6 +12617,7 @@ Called by update_net_error_codes.py.-->
1261712617
<int value="427" label="DeviceRollbackToTargetVersion"/>
1261812618
<int value="428" label="MachineLevelUserCloudPolicyEnrollmentToken"/>
1261912619
<int value="429" label="SafeBrowsingExtendedReportingEnabled"/>
12620+
<int value="430" label="AutoplayAllowed"/>
1262012621
</enum>
1262112622

1262212623
<enum name="EnterprisePolicyInvalidations">

0 commit comments

Comments
 (0)