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 11, 2021
1 parent 69dcac2 commit 7d76713
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 38 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.">
Enable Widevine description...
</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
11 changes: 10 additions & 1 deletion browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,16 @@ 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",
"//extensions/common",
]
}

if (enable_brave_perf_predictor) {
Expand Down
28 changes: 12 additions & 16 deletions browser/brave_drm_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
#include "components/prefs/pref_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_handle.h"
#include "extensions/common/constants.h"

using component_updater::ComponentUpdateService;

namespace {
bool IsAlreadyRegistered(ComponentUpdateService* cus) {
std::vector<std::string> component_ids;
component_ids = cus->GetComponentIDs();
return std::find(component_ids.begin(),
component_ids.end(),
BraveDrmTabHelper::kWidevineComponentId) !=
component_ids.end();
return std::find(component_ids.begin(), component_ids.end(),
widevine_extension_id) != component_ids.end();
}
#if !defined(OS_LINUX)
content::WebContents* GetActiveWebContents() {
Expand All @@ -44,19 +43,10 @@ void ReloadIfActive(content::WebContents* web_contents) {

} // namespace

// static
const char BraveDrmTabHelper::kWidevineComponentId[] =
"oimompecagnajdejgnnjijobebaeigek";

BraveDrmTabHelper::BraveDrmTabHelper(content::WebContents* contents)
: WebContentsObserver(contents),
receivers_(contents, this),
observer_(this) {
auto* updater = g_browser_process->component_updater();
// We don't need to observe if widevine is already registered.
if (!IsAlreadyRegistered(updater))
observer_.Add(updater);
}
observer_(this) {}

BraveDrmTabHelper::~BraveDrmTabHelper() {}

Expand Down Expand Up @@ -84,6 +74,11 @@ void BraveDrmTabHelper::DidStartNavigation(
void BraveDrmTabHelper::OnWidevineKeySystemAccessRequest() {
is_widevine_requested_ = true;

auto* updater = g_browser_process->component_updater();
// We don't need to observe if widevine is already registered.
if (!observer_.IsObserving(updater) && !IsAlreadyRegistered(updater))
observer_.Add(updater);

if (ShouldShowWidevineOptIn() && !is_permission_requested_) {
is_permission_requested_ = true;
RequestWidevinePermission(web_contents(), false /* for_restart */);
Expand All @@ -92,11 +87,12 @@ void BraveDrmTabHelper::OnWidevineKeySystemAccessRequest() {

void BraveDrmTabHelper::OnEvent(Events event, const std::string& id) {
if (event == ComponentUpdateService::Observer::Events::COMPONENT_UPDATED &&
id == kWidevineComponentId) {
id == widevine_extension_id) {
#if defined(OS_LINUX)
// Ask restart instead of reloading. Widevine is only usable after
// restarting on linux.
RequestWidevinePermission(web_contents(), true /* for_restart*/);
if (is_widevine_requested_)
RequestWidevinePermission(web_contents(), true /* for_restart*/);
#else
// When widevine is ready to use, only active tab that requests widevine is
// reloaded automatically.
Expand Down
4 changes: 0 additions & 4 deletions browser/brave_drm_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ class BraveDrmTabHelper final
public brave_drm::mojom::BraveDRM,
public component_updater::ComponentUpdateService::Observer {
public:
// Copied from widevine_cdm_component_installer.cc.
// There is no shared constant value.
static const char kWidevineComponentId[];

explicit BraveDrmTabHelper(content::WebContents* contents);
~BraveDrmTabHelper() override;

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
6 changes: 5 additions & 1 deletion browser/widevine/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ 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",
"//content/public/common",
"//extensions/common",
"//third_party/widevine/cdm:buildflags",
"//third_party/widevine/cdm:headers",
"//ui/base",
Expand Down Expand Up @@ -64,6 +67,7 @@ source_set("browser_tests") {
"//components/prefs",
"//content/public/browser",
"//content/test:test_support",
"//extensions/common",
"//testing/gtest",
"//third_party/widevine/cdm:buildflags",
"//ui/views",
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
Loading

0 comments on commit 7d76713

Please sign in to comment.