Skip to content

Commit

Permalink
Add AccessCodeCastEnabled & AccessCodeCastDeviceDuration policies
Browse files Browse the repository at this point in the history
These two policies will be used to control whether users can add cast
receivers to media router via entering access codes or scanning QR codes,
and how long such receivers will remain in the cast receiver list.

BUG=b:201472353,b:201472347

Change-Id: I2a1fd6e1ab4d58bb97e0506172384151071ee47a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3197939
Reviewed-by: Takumi Fujimoto <[email protected]>
Reviewed-by: Owen Min <[email protected]>
Commit-Queue: Brian Malcolm <[email protected]>
Cr-Commit-Position: refs/heads/main@{#928470}
  • Loading branch information
Brian Malcolm authored and Chromium LUCI CQ committed Oct 6, 2021
1 parent d82e9f5 commit bf99d99
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 3 deletions.
24 changes: 24 additions & 0 deletions chrome/browser/media/router/media_router_feature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ void RegisterProfilePrefs(PrefRegistrySimple* registry) {
// TODO(imcheng): Migrate existing Media Router prefs to here.
registry->RegisterStringPref(prefs::kMediaRouterReceiverIdHashToken, "",
PrefRegistry::PUBLIC);
registry->RegisterBooleanPref(prefs::kAccessCodeCastEnabled, false,
PrefRegistry::PUBLIC);
registry->RegisterIntegerPref(prefs::kAccessCodeCastDeviceDuration, 0,
PrefRegistry::PUBLIC);
}

bool GetCastAllowAllIPsPref(PrefService* pref_service) {
Expand Down Expand Up @@ -126,6 +130,26 @@ bool GlobalMediaControlsCastStartStopEnabled() {
return base::FeatureList::IsEnabled(kGlobalMediaControlsCastStartStop);
}

bool GetAccessCodeCastEnabledPref(PrefService* pref_service) {
#if !defined(OS_ANDROID)
return pref_service->GetBoolean(prefs::kAccessCodeCastEnabled);
#else
return false;
#endif // !defined(OS_ANDROID)
}

base::TimeDelta GetAccessCodeDeviceDurationPref(PrefService* pref_service) {
#if !defined(OS_ANDROID)
if (!GetAccessCodeCastEnabledPref(pref_service)) {
return base::TimeDelta::FromSeconds(0);
}
return base::TimeDelta::FromSeconds(
pref_service->GetInteger(prefs::kAccessCodeCastDeviceDuration));
#else
return base::TimeDelta::FromSeconds(0);
#endif // !defined(OS_ANDROID)
}

#endif // !defined(OS_ANDROID)

} // namespace media_router
17 changes: 17 additions & 0 deletions chrome/browser/media/router/media_router_feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_FEATURE_H_

#include "base/feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"

class PrefRegistrySimple;
Expand Down Expand Up @@ -52,6 +53,14 @@ constexpr char kMediaRouterCastAllowAllIPs[] =
// hash when externalizing MediaSink IDs.
constexpr char kMediaRouterReceiverIdHashToken[] =
"media_router.receiver_id_hash_token";
// Pref name that allows the AccessCode/QR code scanning dialog button to be
// shown.
constexpr char kAccessCodeCastEnabled[] =
"media_router.access_code_cast_enabled";
// Pref name for the pref that determines how long a scanned receiver remains in
// the receiver list.
constexpr char kAccessCodeCastDeviceDuration[] =
"media_router.access_code_cast_device_duration";
} // namespace prefs

// Registers |kMediaRouterCastAllowAllIPs| with local state pref |registry|.
Expand All @@ -74,6 +83,14 @@ bool DialMediaRouteProviderEnabled();

// Returns true if global media controls are used to start and stop casting.
bool GlobalMediaControlsCastStartStopEnabled();

// Returns true if this user is allowed to use Access Codes & QR codes to
// discover cast devices.
bool GetAccessCodeCastEnabledPref(PrefService* pref_service);

// Returns the duration that a scanned cast device is allowed to remain
// in the cast list.
base::TimeDelta GetAccessCodeDeviceDurationPref(PrefService* pref_service);
#endif // !defined(OS_ANDROID)

} // namespace media_router
Expand Down
51 changes: 51 additions & 0 deletions chrome/browser/media/router/media_router_feature_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,57 @@ TEST(MediaRouterFeatureTest, GetReceiverIdHashToken) {
EXPECT_EQ(token, GetReceiverIdHashToken(pref_service.get()));
}

TEST(MediaRouterFeatureTest, GetAccessCodeCastEnabledPref) {
auto pref_service = std::make_unique<TestingPrefServiceSimple>();
pref_service->registry()->RegisterBooleanPref(prefs::kAccessCodeCastEnabled,
false);
EXPECT_FALSE(GetAccessCodeCastEnabledPref(pref_service.get()));

// Setting the pref to true should now return true.
pref_service->SetManagedPref(prefs::kAccessCodeCastEnabled,
std::make_unique<base::Value>(true));
EXPECT_TRUE(GetAccessCodeCastEnabledPref(pref_service.get()));

// Removing the set value should now return the default value (false).
pref_service->RemoveManagedPref(prefs::kAccessCodeCastEnabled);
EXPECT_FALSE(GetAccessCodeCastEnabledPref(pref_service.get()));
}

TEST(MediaRouterFeatureTest, GetAccessCodeDeviceDurationPref) {
const int non_default = 10;
auto pref_service = std::make_unique<TestingPrefServiceSimple>();
pref_service->registry()->RegisterBooleanPref(prefs::kAccessCodeCastEnabled,
false);
pref_service->registry()->RegisterIntegerPref(
prefs::kAccessCodeCastDeviceDuration, 0);

pref_service->SetManagedPref(prefs::kAccessCodeCastEnabled,
std::make_unique<base::Value>(true));

// Defaults to 0.
EXPECT_EQ(base::TimeDelta::FromSeconds(0),
GetAccessCodeDeviceDurationPref(pref_service.get()));

// Setting to a non-zero value should cause the return value to match.
pref_service->SetManagedPref(prefs::kAccessCodeCastDeviceDuration,
std::make_unique<base::Value>(non_default));
EXPECT_EQ(base::TimeDelta::FromSeconds(non_default),
GetAccessCodeDeviceDurationPref(pref_service.get()));

// Disabling the feature overall in policy now makes this return 0.
pref_service->SetManagedPref(prefs::kAccessCodeCastEnabled,
std::make_unique<base::Value>(false));
EXPECT_EQ(base::TimeDelta::FromSeconds(0),
GetAccessCodeDeviceDurationPref(pref_service.get()));

pref_service->SetManagedPref(prefs::kAccessCodeCastEnabled,
std::make_unique<base::Value>(true));
// Removing the set value should return the default.
pref_service->RemoveManagedPref(prefs::kAccessCodeCastDeviceDuration);
EXPECT_EQ(base::TimeDelta::FromSeconds(0),
GetAccessCodeDeviceDurationPref(pref_service.get()));
}

class MediaRouterEnabledTest : public ::testing::Test {
public:
MediaRouterEnabledTest() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = {
{ key::kMediaRouterCastAllowAllIPs,
media_router::prefs::kMediaRouterCastAllowAllIPs,
base::Value::Type::BOOLEAN },
{ key::kAccessCodeCastEnabled,
media_router::prefs::kAccessCodeCastEnabled,
base::Value::Type::BOOLEAN },
{ key::kAccessCodeCastDeviceDuration,
media_router::prefs::kAccessCodeCastDeviceDuration,
base::Value::Type::INTEGER },
{ key::kWebRtcLocalIpsAllowedUrls,
prefs::kWebRtcLocalIpsAllowedUrls,
base::Value::Type::LIST },
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/ui_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const base::Feature kChromeWhatsNewInMainMenuNewBadge{
"ChromeWhatsNewInMainMenuNewBadge", base::FEATURE_DISABLED_BY_DEFAULT};
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
#if !defined(ANDROID)
// Enables "Enterprise Casting" UI.
const base::Feature kEnterpriseCastingUI{"EnterpriseCastingUI",
base::FEATURE_DISABLED_BY_DEFAULT};
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/ui_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern const base::Feature kChromeWhatsNewInMainMenuNewBadge;

extern const base::Feature kCommander;

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
#if !defined(ANDROID)
extern const base::Feature kEnterpriseCastingUI;
#endif

Expand Down
52 changes: 52 additions & 0 deletions chrome/test/data/policy/policy_test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -15941,5 +15941,57 @@
}
}
]
},
"AccessCodeCastEnabled": {
"os": ["win", "linux", "mac", "chromeos_ash", "chromeos_lacros"],
"policy_pref_mapping_tests": [
{
"policies": {},
"prefs": {
"media_router.access_code_cast_enabled": {
"default_value": false
}
}
},
{
"policies": { "AccessCodeCastEnabled": false },
"prefs": {
"media_router.access_code_cast_enabled": {
"value": false
}
}
},
{
"policies": { "AccessCodeCastEnabled": true },
"prefs": {
"media_router.access_code_cast_enabled": {
"value": true
}
}
}
]
},
"AccessCodeCastDeviceDuration": {
"os": ["win", "linux", "mac", "chromeos_ash", "chromeos_lacros"],
"policy_pref_mapping_tests": [
{
"policies": {
"AccessCodeCastDeviceDuration": 10
},
"prefs": {
"media_router.access_code_cast_device_duration": {
"value": 10
}
}
},
{
"policies": {},
"prefs": {
"media_router.access_code_cast_device_duration": {
"default_value": 0
}
}
}
]
}
}
55 changes: 54 additions & 1 deletion components/policy/resources/policy_templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -28155,6 +28155,59 @@ When this policy is set, <ph name="PRODUCT_OS_NAME">$2<ex>Google Chrome OS</ex><
If this policy is set to Enabled, the user is prompted to select a client certificate whenever the auto-selection policy matches multiple certificates.
If this policy is set to Disabled or not set, the user may only be prompted when no certificate matches the auto-selection.''',
},
{
'name': 'AccessCodeCastEnabled',
'owners': ['[email protected]', '[email protected]'],
'type': 'main',
'schema': { 'type': 'boolean' },
'future_on': ['chrome.*', 'chrome_os'],
'features': {
'dynamic_refresh': True,
'per_profile': True,
},
'items': [
{
'value': True,
'caption': 'User will be given the option in the <ph name="PRODUCT_NAME">Google Cast</ph> menu to select cast devices by using an access code or by scanning a QR code.',
},
{
'value': False,
'caption': 'User will not be given the option in the <ph name="PRODUCT_NAME">Google Cast</ph> menu to select cast devices by using an access code or by scanning a QR code.',
},
],
'default': False,
'example_value': True,
'id': 907,
'tags': [],
'caption': '''Allow users to select cast devices with an access code or QR code from within the <ph name="PRODUCT_NAME">Google Cast</ph> menu.''',
'desc': '''This policy controls whether a user will be presented with an option, within the <ph name="PRODUCT_NAME">Google Cast</ph> menu which allows them to cast to cast devices that do not appear in the <ph name="PRODUCT_NAME">Google Cast</ph> menu, using either the access code or QR code displayed on the cast devices's screen.
By default, a user must reenter the access code or rescan the QR code in order to initiate a subsequent casting session, but if the <ph name="ACCESS_CODE_CAST_DEVICE_DURATION_POLICY_NAME">AccessCodeCastDeviceDuration</ph> policy has been set to a non-zero value (the default is zero), then the cast device will remain in the list of available cast devices until the specified period of time has expired.
When this policy is set to Enabled, users will be presented with the option to select cast devices by using an access code or by scanning a QR code.
When this policy is set to Disabled or not set, users will not be given the option to select cast devices by using an access code or by scanning a QR code.''',
},
{
'name': 'AccessCodeCastDeviceDuration',
'owners': ['[email protected]', '[email protected]'],
'type': 'int',
'schema': { 'type': 'integer', 'minimum': 0 },
'future_on': ['chrome.*', 'chrome_os'],
'features': {
'dynamic_refresh': True,
'per_profile': True,
},
'default': 0,
'example_value': 60,
'id': 908,
'tags': [],
'caption': '''Specifies how long (in seconds) a cast device selected with an access code or QR code stays in the <ph name="PRODUCT_NAME">Google Cast</ph> menu's list of cast devices.''',
'desc': '''This policy specifies how long (in seconds) a cast device that was previously selected via an access code or QR code can be seen within the <ph name="PRODUCT_NAME">Google Cast</ph> menu of cast devices.
The lifetime of an entry starts at the time the access code was first entered or the QR code was first scanned.
During this period the cast device will appear in the <ph name="PRODUCT_NAME">Google Cast</ph> menu's list of cast devices.
After this period, in order to use the cast device again the access code must be reentered or the QR code must be rescanned.
By default, the period is zero seconds, so cast devices will not stay in the <ph name="PRODUCT_NAME">Google Cast</ph> menu, and so the access code must be reentered, or the QR code rescanned, in order to initiate a new casting session.
Note that this policy only affects how long a cast devices appears in the <ph name="PRODUCT_NAME">Google Cast</ph> menu, and has no effect on any ongoing cast session which will continue even if the period expires.
This policy has no effect unless the <ph name="ACCESS_CODE_CAST_ENABLED_POLICY_NAME">AccessCodeCastEnabled</ph> policy is Enabled.''',
},
],
'messages': {
# Messages that are not associated to any policies.
Expand Down Expand Up @@ -29119,6 +29172,6 @@ The recommended way to configure policy on Windows is via GPO, although provisio
'placeholders': [],
'deleted_policy_ids': [114, 115, 204, 205, 206, 412, 476, 544, 546, 562, 569, 578, 583, 585, 586, 587, 588, 589, 590, 591, 600, 668, 669, 872],
'deleted_atomic_policy_group_ids': [19],
'highest_id_currently_used': 906,
'highest_id_currently_used': 908,
'highest_atomic_group_id_currently_used': 41
}
2 changes: 2 additions & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26472,6 +26472,8 @@ Called by update_document_policy_enum.py.-->
label="DeviceLoginScreenPromptOnMultipleMatchingCertificates"/>
<int value="905" label="PromptOnMultipleMatchingCertificates"/>
<int value="906" label="SideSearchEnabled"/>
<int value="907" label="AccessCodeCastEnabled"/>
<int value="908" label="AccessCodeCastDeviceDuration"/>
</enum>

<enum name="EnterprisePolicyDeviceIdValidity">
Expand Down

0 comments on commit bf99d99

Please sign in to comment.