New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement systemPreferences.getMediaAccessStatus() on Windows #24275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ | |
// found in the LICENSE file. | ||
|
||
#include <dwmapi.h> | ||
#include <windows.devices.enumeration.h> | ||
#include <wrl/client.h> | ||
#include <iomanip> | ||
|
||
#include "shell/browser/api/electron_api_system_preferences.h" | ||
|
||
#include "base/win/core_winrt_util.h" | ||
#include "base/win/wrapped_window_proc.h" | ||
#include "shell/common/color_util.h" | ||
#include "ui/base/win/shell.h" | ||
|
@@ -20,6 +23,51 @@ namespace { | |
const wchar_t kSystemPreferencesWindowClass[] = | ||
L"Electron_SystemPreferencesHostWindow"; | ||
|
||
using ABI::Windows::Devices::Enumeration::DeviceAccessStatus; | ||
using ABI::Windows::Devices::Enumeration::DeviceClass; | ||
using ABI::Windows::Devices::Enumeration::IDeviceAccessInformation; | ||
using ABI::Windows::Devices::Enumeration::IDeviceAccessInformationStatics; | ||
using Microsoft::WRL::ComPtr; | ||
|
||
DeviceAccessStatus GetDeviceAccessStatus(DeviceClass device_class) { | ||
ComPtr<IDeviceAccessInformationStatics> dev_access_info_statics; | ||
HRESULT hr = base::win::GetActivationFactory< | ||
IDeviceAccessInformationStatics, | ||
RuntimeClass_Windows_Devices_Enumeration_DeviceAccessInformation>( | ||
&dev_access_info_statics); | ||
if (FAILED(hr)) { | ||
VLOG(1) << "IDeviceAccessInformationStatics failed: " << hr; | ||
return DeviceAccessStatus::DeviceAccessStatus_Allowed; | ||
} | ||
|
||
ComPtr<IDeviceAccessInformation> dev_access_info; | ||
hr = dev_access_info_statics->CreateFromDeviceClass(device_class, | ||
&dev_access_info); | ||
if (FAILED(hr)) { | ||
VLOG(1) << "IDeviceAccessInformation failed: " << hr; | ||
return DeviceAccessStatus::DeviceAccessStatus_Allowed; | ||
} | ||
|
||
auto status = DeviceAccessStatus::DeviceAccessStatus_Unspecified; | ||
dev_access_info->get_CurrentStatus(&status); | ||
return status; | ||
} | ||
|
||
std::string ConvertDeviceAccessStatus(DeviceAccessStatus value) { | ||
switch (value) { | ||
case DeviceAccessStatus::DeviceAccessStatus_Unspecified: | ||
return "not-determined"; | ||
case DeviceAccessStatus::DeviceAccessStatus_Allowed: | ||
return "granted"; | ||
case DeviceAccessStatus::DeviceAccessStatus_DeniedBySystem: | ||
return "restricted"; | ||
case DeviceAccessStatus::DeviceAccessStatus_DeniedByUser: | ||
return "denied"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
namespace api { | ||
|
@@ -117,6 +165,24 @@ std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower, | |
return ToRGBHex(color_utils::GetSysSkColor(id)); | ||
} | ||
|
||
std::string SystemPreferences::GetMediaAccessStatus( | ||
const std::string& media_type, | ||
gin_helper::Arguments* args) { | ||
if (media_type == "camera") { | ||
return ConvertDeviceAccessStatus( | ||
GetDeviceAccessStatus(DeviceClass::DeviceClass_VideoCapture)); | ||
} else if (media_type == "microphone") { | ||
return ConvertDeviceAccessStatus( | ||
GetDeviceAccessStatus(DeviceClass::DeviceClass_AudioCapture)); | ||
} else if (media_type == "screen") { | ||
return ConvertDeviceAccessStatus( | ||
DeviceAccessStatus::DeviceAccessStatus_Allowed); | ||
miniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
args->ThrowError("Invalid media type"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not originating from this PR but when we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @deepak1556 The return type can have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For an unknown media type why can't the return type by unknown, the current system calls both on windows and macOS never return an unknown state, the value is being unused. I wouldn't mind if this state wasn't documented but it is :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to bring this up with api-WG There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can address it in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
return std::string(); | ||
} | ||
} | ||
|
||
void SystemPreferences::InitializeWindow() { | ||
invertered_color_scheme_ = IsInvertedColorScheme(); | ||
high_contrast_color_scheme_ = IsHighContrastColorScheme(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is specific for win32 applications should we safety check if for APPX / Window Store builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The system API returns the global value, it should work fine. Electron Window Store app is still Win32.