Skip to content

Commit ca51b02

Browse files
committed
Add App.enableComponent and App.updateComponent for Widevine support
Ref nwjs#6425
1 parent c04739f commit ca51b02

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/api/nw_app.idl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
// nw APP API
66
[implemented_in="content/nw/src/api/nw_app_api.h"]
77
namespace nw.App {
8+
enum ComponentExtensions {
9+
WIDEVINE
10+
};
811
callback EventCallback = void();
12+
callback ErrorCallback = void(boolean result);
13+
callback ComponentCallback = void(DOMString version);
914
interface Functions {
1015
// crash the renderer for testing
1116
[nocompile] static void crashRenderer();
@@ -15,6 +20,8 @@ namespace nw.App {
1520
[nocompile] static void removeListener(DOMString event,
1621
EventCallback callback);
1722
[nocompile] static void removeAllListeners(DOMString event);
23+
static void enableComponent(ComponentExtensions extension_id, ComponentCallback callback);
24+
static void updateComponent(ComponentExtensions extension_id, ErrorCallback callback);
1825
static void quit();
1926
static void closeAllWindows();
2027
static void clearCache();

src/api/nw_app_api.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "content/public/browser/browser_task_traits.h"
1212
#include "components/browsing_data/content/appcache_helper.h"
1313
#include "components/browsing_data/content/browsing_data_helper.h"
14+
#include "components/component_updater/component_updater_service.h"
15+
#include "chrome/browser/browser_process.h"
16+
#include "chrome/browser/component_updater/widevine_cdm_component_installer.h"
1417
#include "chrome/browser/devtools/devtools_window.h"
1518
#include "chrome/browser/extensions/devtools_util.h"
1619
#include "chrome/browser/extensions/extension_service.h"
@@ -99,6 +102,49 @@ NwAppCloseAllWindowsFunction::Run() {
99102
return RespondNow(NoArguments());
100103
}
101104

105+
ExtensionFunction::ResponseAction
106+
NwAppEnableComponentFunction::Run() {
107+
component_updater::RegisterWidevineCdmComponent(g_browser_process->component_updater(),
108+
base::BindOnce(&NwAppEnableComponentFunction::OnRegistered,
109+
this));
110+
return RespondLater();
111+
}
112+
113+
void NwAppEnableComponentFunction::OnRegistered() {
114+
std::string ret;
115+
const std::vector<component_updater::ComponentInfo> component_list =
116+
g_browser_process->component_updater()->GetComponents();
117+
118+
for (const auto& component : component_list) {
119+
if (component.id == "oimompecagnajdejgnnjijobebaeigek") {
120+
ret = component.version.GetString();
121+
}
122+
}
123+
auto result_value = std::make_unique<base::Value>(ret);
124+
125+
Respond(OneArgument(std::move(result_value)));
126+
}
127+
128+
ExtensionFunction::ResponseAction
129+
NwAppUpdateComponentFunction::Run() {
130+
g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
131+
"oimompecagnajdejgnnjijobebaeigek", component_updater::OnDemandUpdater::Priority::FOREGROUND,
132+
base::BindOnce(&NwAppUpdateComponentFunction::OnUpdated, this));
133+
134+
return RespondLater();
135+
}
136+
137+
void NwAppUpdateComponentFunction::OnUpdated(update_client::Error error) {
138+
bool ret = (error == update_client::Error::NONE);
139+
auto result_value = std::make_unique<base::Value>(ret);
140+
if (ret) {
141+
Respond(OneArgument(std::move(result_value)));
142+
} else {
143+
VLOG(1) << "update component error: " << (int)error;
144+
Respond(OneArgument(std::move(result_value)));
145+
}
146+
}
147+
102148
NwAppGetArgvSyncFunction::NwAppGetArgvSyncFunction() {
103149
}
104150

src/api/nw_app_api.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "base/run_loop.h"
77
#include "content/public/browser/browsing_data_remover.h"
88
#include "extensions/browser/extension_function.h"
9+
#include "components/update_client/update_client_errors.h"
910

1011
namespace extensions {
1112
class AppWindowRegistry;
@@ -39,6 +40,32 @@ class NwAppCloseAllWindowsFunction : public ExtensionFunction {
3940
DECLARE_EXTENSION_FUNCTION("nw.App.closeAllWindows", UNKNOWN)
4041
};
4142

43+
class NwAppEnableComponentFunction : public ExtensionFunction {
44+
public:
45+
NwAppEnableComponentFunction() {}
46+
47+
protected:
48+
~NwAppEnableComponentFunction() override {}
49+
50+
void OnRegistered();
51+
// ExtensionFunction:
52+
ResponseAction Run() override;
53+
DECLARE_EXTENSION_FUNCTION("nw.App.enableComponent", UNKNOWN)
54+
};
55+
56+
class NwAppUpdateComponentFunction : public ExtensionFunction {
57+
public:
58+
NwAppUpdateComponentFunction() {}
59+
60+
protected:
61+
~NwAppUpdateComponentFunction() override {}
62+
63+
void OnUpdated(update_client::Error error);
64+
// ExtensionFunction:
65+
ResponseAction Run() override;
66+
DECLARE_EXTENSION_FUNCTION("nw.App.updateComponent", UNKNOWN)
67+
};
68+
4269
class NwAppGetArgvSyncFunction : public NWSyncExtensionFunction {
4370
public:
4471
NwAppGetArgvSyncFunction();

0 commit comments

Comments
 (0)