33// found in the LICENSE file.
44
55#include < dwmapi.h>
6+ #include < windows.devices.enumeration.h>
7+ #include < wrl/client.h>
68#include < iomanip>
79
810#include " shell/browser/api/electron_api_system_preferences.h"
911
12+ #include " base/win/core_winrt_util.h"
1013#include " base/win/wrapped_window_proc.h"
1114#include " shell/common/color_util.h"
1215#include " ui/base/win/shell.h"
@@ -20,6 +23,51 @@ namespace {
2023const wchar_t kSystemPreferencesWindowClass [] =
2124 L" Electron_SystemPreferencesHostWindow" ;
2225
26+ using ABI::Windows::Devices::Enumeration::DeviceAccessStatus;
27+ using ABI::Windows::Devices::Enumeration::DeviceClass;
28+ using ABI::Windows::Devices::Enumeration::IDeviceAccessInformation;
29+ using ABI::Windows::Devices::Enumeration::IDeviceAccessInformationStatics;
30+ using Microsoft::WRL::ComPtr;
31+
32+ DeviceAccessStatus GetDeviceAccessStatus (DeviceClass device_class) {
33+ ComPtr<IDeviceAccessInformationStatics> dev_access_info_statics;
34+ HRESULT hr = base::win::GetActivationFactory<
35+ IDeviceAccessInformationStatics,
36+ RuntimeClass_Windows_Devices_Enumeration_DeviceAccessInformation>(
37+ &dev_access_info_statics);
38+ if (FAILED (hr)) {
39+ VLOG (1 ) << " IDeviceAccessInformationStatics failed: " << hr;
40+ return DeviceAccessStatus::DeviceAccessStatus_Allowed;
41+ }
42+
43+ ComPtr<IDeviceAccessInformation> dev_access_info;
44+ hr = dev_access_info_statics->CreateFromDeviceClass (device_class,
45+ &dev_access_info);
46+ if (FAILED (hr)) {
47+ VLOG (1 ) << " IDeviceAccessInformation failed: " << hr;
48+ return DeviceAccessStatus::DeviceAccessStatus_Allowed;
49+ }
50+
51+ auto status = DeviceAccessStatus::DeviceAccessStatus_Unspecified;
52+ dev_access_info->get_CurrentStatus (&status);
53+ return status;
54+ }
55+
56+ std::string ConvertDeviceAccessStatus (DeviceAccessStatus value) {
57+ switch (value) {
58+ case DeviceAccessStatus::DeviceAccessStatus_Unspecified:
59+ return " not-determined" ;
60+ case DeviceAccessStatus::DeviceAccessStatus_Allowed:
61+ return " granted" ;
62+ case DeviceAccessStatus::DeviceAccessStatus_DeniedBySystem:
63+ return " restricted" ;
64+ case DeviceAccessStatus::DeviceAccessStatus_DeniedByUser:
65+ return " denied" ;
66+ default :
67+ return " unknown" ;
68+ }
69+ }
70+
2371} // namespace
2472
2573namespace api {
@@ -117,6 +165,24 @@ std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
117165 return ToRGBHex (color_utils::GetSysSkColor (id));
118166}
119167
168+ std::string SystemPreferences::GetMediaAccessStatus (
169+ const std::string& media_type,
170+ gin_helper::Arguments* args) {
171+ if (media_type == " camera" ) {
172+ return ConvertDeviceAccessStatus (
173+ GetDeviceAccessStatus (DeviceClass::DeviceClass_VideoCapture));
174+ } else if (media_type == " microphone" ) {
175+ return ConvertDeviceAccessStatus (
176+ GetDeviceAccessStatus (DeviceClass::DeviceClass_AudioCapture));
177+ } else if (media_type == " screen" ) {
178+ return ConvertDeviceAccessStatus (
179+ DeviceAccessStatus::DeviceAccessStatus_Allowed);
180+ } else {
181+ args->ThrowError (" Invalid media type" );
182+ return std::string ();
183+ }
184+ }
185+
120186void SystemPreferences::InitializeWindow () {
121187 invertered_color_scheme_ = IsInvertedColorScheme ();
122188 high_contrast_color_scheme_ = IsHighContrastColorScheme ();
0 commit comments