Skip to content

Commit

Permalink
Add Widevine toggle option to settings
Browse files Browse the repository at this point in the history
This toggle option register/unregister widevine cdm components.

Resolves brave/brave-browser#2791
  • Loading branch information
simonhong committed Jan 19, 2021
1 parent 09d9bed commit f47d8d6
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 20 deletions.
6 changes: 6 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
<message name="IDS_WIDEVINE_DONT_ASK_AGAIN_CHECKBOX" desc="Checkbox for prevent showing widevine install prompt">
Don't ask again
</message>
<message name="IDS_SETTINGS_ENABLE_WIDEVINE_TITLE" desc="Text fragment for enabling widevine component. 'Widevine' is the name of a plugin and should not be translated.">
Enable Widevine
</message>
<message name="IDS_SETTINGS_ENABLE_WIDEVINE_DESC" desc="Text fragment for enabling widevine component. 'Widevine' is the name of a plugin and should not be translated.">
Uses Widevine digital rights management (DRM) component to view streaming video and other content in the browser.
</message>
<message name="IDS_SETTINGS_ASK_WIDEVINE_INSTALL_DESC" desc="Text fragment for asking widevine install or not">
Ask when a site wants to install Widevine on your computer.
</message>
Expand Down
10 changes: 9 additions & 1 deletion browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,15 @@ source_set("browser_process") {
"brave_drm_tab_helper.cc",
"brave_drm_tab_helper.h",
]
deps += [ "//brave/browser/widevine" ]
deps += [
"//base",
"//brave/browser/widevine",
"//brave/common:pref_names",
"//brave/components/brave_drm",
"//components/component_updater",
"//components/prefs",
"//content/public/browser",
]
}

if (enable_brave_perf_predictor) {
Expand Down
5 changes: 2 additions & 3 deletions browser/brave_drm_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ namespace {
bool IsAlreadyRegistered(ComponentUpdateService* cus) {
std::vector<std::string> component_ids;
component_ids = cus->GetComponentIDs();
return std::find(component_ids.begin(),
component_ids.end(),
return std::find(component_ids.begin(), component_ids.end(),
BraveDrmTabHelper::kWidevineComponentId) !=
component_ids.end();
component_ids.end();
}
#if !defined(OS_LINUX)
content::WebContents* GetActiveWebContents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ cr.define('settings', function () {
setTorEnabled (value) {}
isTorEnabled () {}
isTorManaged () {}
setWidevineEnabled() {}
isWidevineEnabled() {}
getRestartNeeded () {}
getWeb3ProviderList () {}
wasSignInEnabledAtStartup () {}
Expand Down Expand Up @@ -63,6 +65,14 @@ cr.define('settings', function () {
return cr.sendWithPromise('isTorManaged')
}

setWidevineEnabled (value) {
chrome.send('setWidevineEnabled', [value])
}

isWidevineEnabled () {
return cr.sendWithPromise('isWidevineEnabled')
}

getRestartNeeded () {
return cr.sendWithPromise('getRestartNeeded')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@
label="Widevine"
sub-label="$i18n{appearanceSettingsAskWidevineInstallDesc}">
</settings-toggle-button>
<settings-toggle-button
id="widevineEnabled"
class="cr-row"
pref=""
label="$i18n{widevineEnabledLabel}"
sub-label="$i18n{widevineEnabledDesc}"
on-settings-boolean-control-change="onWidevineEnabledChange_"
checked="[[widevineEnabled_]]">
</settings-toggle-button>
<div class="settings-row" id="manageExtensionsRow">
<cr-link-row icon-class="icon-external"
label="$i18n{manageExtensionsLabel}" on-click="openExtensionsPage_">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Polymer({
properties: {
showRestartToast_: Boolean,
torEnabled_: Boolean,
widevineEnabled_: Boolean,
disableTorOption_: Boolean,
ipfsEnabled_: Boolean,
showChangeIPFSGatewayDialog_: Boolean,
Expand All @@ -42,15 +43,18 @@ Polymer({
this.onIPFSCompanionEnabledChange_ = this.onIPFSCompanionEnabledChange_.bind(this)
this.openExtensionsPage_ = this.openExtensionsPage_.bind(this)
this.openKeyboardShortcutsPage_ = this.openKeyboardShortcutsPage_.bind(this)
this.onWidevineEnabledChange_ = this.onWidevineEnabledChange_.bind(this)
this.restartBrowser_ = this.restartBrowser_.bind(this)
this.onTorEnabledChange_ = this.onTorEnabledChange_.bind(this)

this.addWebUIListener('brave-needs-restart-changed', (needsRestart) => {
this.showRestartToast_ = needsRestart
})
this.addWebUIListener('tor-enabled-changed', (enabled) => {
this.torEnabled_ = enabled
})
this.addWebUIListener('widevine-enabled-changed', (enabled) => {
this.widevineEnabled_ = enabled
})

this.browserProxy_.getRestartNeeded().then(show => {
this.showRestartToast_ = show;
Expand All @@ -61,6 +65,9 @@ Polymer({
this.browserProxy_.isTorManaged().then(managed => {
this.disableTorOption_ = managed
})
this.browserProxy_.isWidevineEnabled().then(enabled => {
this.widevineEnabled_ = enabled
})
this.browserProxy_.getWeb3ProviderList().then(list => {
this.braveWeb3Providers_ = JSON.parse(list)
});
Expand Down Expand Up @@ -101,6 +108,10 @@ Polymer({
this.browserProxy_.setTorEnabled(this.$.torEnabled.checked);
},

onWidevineEnabledChange_: function() {
this.browserProxy_.setWidevineEnabled(this.$.widevineEnabled.checked);
},

openExtensionsPage_: function() {
window.open("chrome://extensions", "_self");
},
Expand Down
7 changes: 7 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import("//brave/components/tor/buildflags/buildflags.gni")
import("//build/config/features.gni")
import("//chrome/common/features.gni")
import("//components/gcm_driver/config.gni")
import("//third_party/widevine/cdm/widevine.gni")

source_set("favicon_source") {
# Remove when https://github.com/brave/brave-browser/issues/10614 is resolved
Expand Down Expand Up @@ -323,6 +324,12 @@ source_set("ui") {
"webui/settings/brave_default_extensions_handler.cc",
"webui/settings/brave_default_extensions_handler.h",
]

deps += [ "//third_party/widevine/cdm:buildflags" ]

if (enable_widevine) {
deps += [ "//brave/browser/widevine" ]
}
}

if (brave_rewards_enabled) {
Expand Down
73 changes: 70 additions & 3 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@
#include "brave/components/brave_wallet/brave_wallet_constants.h"
#endif

#if BUILDFLAG(ENABLE_WIDEVINE)
#include "brave/browser/widevine/widevine_utils.h"
#endif

BraveDefaultExtensionsHandler::BraveDefaultExtensionsHandler()
: weak_ptr_factory_(this) {}
: weak_ptr_factory_(this) {
#if BUILDFLAG(ENABLE_WIDEVINE)
was_widevine_enabled_ = IsWidevineOptedIn();
#endif
}

BraveDefaultExtensionsHandler::~BraveDefaultExtensionsHandler() {}

Expand Down Expand Up @@ -92,6 +100,14 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"isTorManaged",
base::BindRepeating(&BraveDefaultExtensionsHandler::IsTorManaged,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setWidevineEnabled",
base::BindRepeating(&BraveDefaultExtensionsHandler::SetWidevineEnabled,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"isWidevineEnabled",
base::BindRepeating(&BraveDefaultExtensionsHandler::IsWidevineEnabled,
base::Unretained(this)));

// Can't call this in ctor because it needs to access web_ui().
InitializePrefCallbacks();
Expand All @@ -108,13 +124,20 @@ void BraveDefaultExtensionsHandler::InitializePrefCallbacks() {
prefs::kEnableMediaRouter,
base::Bind(&BraveDefaultExtensionsHandler::OnMediaRouterEnabledChanged,
base::Unretained(this)));
#if BUILDFLAG(ENABLE_TOR)
local_state_change_registrar_.Init(g_brave_browser_process->local_state());
#if BUILDFLAG(ENABLE_TOR)
local_state_change_registrar_.Add(
tor::prefs::kTorDisabled,
base::Bind(&BraveDefaultExtensionsHandler::OnTorEnabledChanged,
base::Unretained(this)));
#endif

#if BUILDFLAG(ENABLE_WIDEVINE)
local_state_change_registrar_.Add(
kWidevineOptedIn,
base::Bind(&BraveDefaultExtensionsHandler::OnWidevineEnabledChanged,
base::Unretained(this)));
#endif
}

void BraveDefaultExtensionsHandler::OnMediaRouterEnabledChanged() {
Expand All @@ -126,7 +149,16 @@ bool BraveDefaultExtensionsHandler::IsRestartNeeded() {
profile_->GetPrefs()->GetBoolean(prefs::kEnableMediaRouter);
bool media_router_new_pref =
profile_->GetPrefs()->GetBoolean(kBraveEnabledMediaRouter);
return media_router_current_pref != media_router_new_pref;

if (media_router_current_pref != media_router_new_pref)
return true;

#if BUILDFLAG(ENABLE_WIDEVINE)
if (was_widevine_enabled_ != IsWidevineOptedIn())
return true;
#endif

return false;
}

void BraveDefaultExtensionsHandler::GetRestartNeeded(
Expand Down Expand Up @@ -274,6 +306,41 @@ void BraveDefaultExtensionsHandler::IsTorManaged(const base::ListValue* args) {
ResolveJavascriptCallback(args->GetList()[0], base::Value(is_managed));
}

void BraveDefaultExtensionsHandler::SetWidevineEnabled(
const base::ListValue* args) {
#if BUILDFLAG(ENABLE_WIDEVINE)
CHECK_EQ(args->GetSize(), 1U);
bool enabled;
args->GetBoolean(0, &enabled);
enabled ? EnableWidevineCdmComponent() : DisableWidevineCdmComponent();
AllowJavascript();
#endif
}

void BraveDefaultExtensionsHandler::IsWidevineEnabled(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
AllowJavascript();
ResolveJavascriptCallback(args->GetList()[0],
#if BUILDFLAG(ENABLE_WIDEVINE)
base::Value(IsWidevineOptedIn()));
#else
base::Value(false));
#endif
}

void BraveDefaultExtensionsHandler::OnWidevineEnabledChanged() {
if (IsJavascriptAllowed()) {
FireWebUIListener("widevine-enabled-changed",
#if BUILDFLAG(ENABLE_WIDEVINE)
base::Value(IsWidevineOptedIn()));
#else
base::Value(false));
#endif
OnRestartNeededChanged();
}
}

void BraveDefaultExtensionsHandler::SetIPFSCompanionEnabled(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
Expand Down
7 changes: 7 additions & 0 deletions browser/ui/webui/settings/brave_default_extensions_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
#include "chrome/common/extensions/webstore_install_result.h"
#include "components/prefs/pref_change_registrar.h"
#include "third_party/widevine/cdm/buildflags.h"

class Profile;

Expand All @@ -40,6 +41,9 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler {
void IsTorEnabled(const base::ListValue* args);
void OnTorEnabledChanged();
void IsTorManaged(const base::ListValue* args);
void SetWidevineEnabled(const base::ListValue* args);
void IsWidevineEnabled(const base::ListValue* args);
void OnWidevineEnabledChanged();

void InitializePrefCallbacks();

Expand All @@ -53,6 +57,9 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler {
void OnMediaRouterEnabledChanged();
bool IsRestartNeeded();

#if BUILDFLAG(ENABLE_WIDEVINE)
bool was_widevine_enabled_ = false;
#endif
Profile* profile_ = nullptr;
PrefChangeRegistrar pref_change_registrar_;
#if BUILDFLAG(ENABLE_TOR)
Expand Down
4 changes: 3 additions & 1 deletion browser/widevine/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ source_set("widevine") {
"//base",
"//brave/app:brave_generated_resources_grit",
"//brave/common:pref_names",
"//chrome/browser/ui",
"//chrome/common",
"//components/component_updater/",
"//components/content_settings/core/common",
"//components/permissions",
"//components/pref_registry",
"//components/prefs",
"//components/subresource_filter/content/browser",
"//components/vector_icons",
"//content/public/browser",
Expand Down
2 changes: 1 addition & 1 deletion browser/widevine/widevine_permission_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void WidevinePermissionRequest::PermissionGranted(bool is_one_time) {
}
#endif
if (!for_restart_)
EnableWidevineCdmComponent(web_contents_);
EnableWidevineCdmComponent();
}

void WidevinePermissionRequest::PermissionDenied() {
Expand Down
17 changes: 13 additions & 4 deletions browser/widevine/widevine_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/brave_drm_tab_helper.h"
#include "brave/browser/widevine/widevine_permission_request.h"
#include "brave/common/pref_names.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/component_updater/widevine_cdm_component_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_updater_service.h"
#include "components/permissions/permission_request_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/widevine/cdm/widevine_cdm_common.h"

Expand Down Expand Up @@ -72,16 +74,23 @@ void ClearWidevinePrefs(Profile* profile) {

} // namespace

void EnableWidevineCdmComponent(content::WebContents* web_contents) {
DCHECK(web_contents);

void EnableWidevineCdmComponent() {
if (IsWidevineOptedIn())
return;

SetWidevineOptedIn(true);
RegisterWidevineCdmComponent(g_brave_browser_process->component_updater());
}

void DisableWidevineCdmComponent() {
if (!IsWidevineOptedIn())
return;

SetWidevineOptedIn(false);
g_brave_browser_process->component_updater()->UnregisterComponent(
BraveDrmTabHelper::kWidevineComponentId);
}

int GetWidevinePermissionRequestTextFrangmentResourceId(bool for_restart) {
#if defined(OS_LINUX)
return for_restart
Expand Down
3 changes: 2 additions & 1 deletion browser/widevine/widevine_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class PrefRegistrySimple;
class PrefService;
class Profile;

void EnableWidevineCdmComponent(content::WebContents* web_contents);
void EnableWidevineCdmComponent();
void DisableWidevineCdmComponent();
void RegisterWidevineProfilePrefsForMigration(
user_prefs::PrefRegistrySyncable* registry);
int GetWidevinePermissionRequestTextFrangmentResourceId(bool for_restart);
Expand Down
Loading

0 comments on commit f47d8d6

Please sign in to comment.