d62097e067
* chore: bump chromium in DEPS to 130.0.6673.0 * chore: bump chromium in DEPS to 130.0.6675.0 * chore: bump chromium in DEPS to 130.0.6677.2 * chore: bump chromium in DEPS to 130.0.6679.1 * chore: bump chromium in DEPS to 130.0.6681.1 * chore: bump chromium in DEPS to 130.0.6683.2 * chore: bump chromium in DEPS to 130.0.6685.0 * chore: bump chromium in DEPS to 130.0.6687.0 * chore: bump chromium in DEPS to 130.0.6689.0 * chore: bump chromium in DEPS to 130.0.6691.1 * chore: bump chromium in DEPS to 130.0.6693.1 * chore: bump chromium in DEPS to 130.0.6695.1 * chore: bump chromium in DEPS to 130.0.6697.1 * chore: bump chromium in DEPS to 130.0.6699.1 * chore: bump chromium in DEPS to 130.0.6701.1 * chore: bump chromium in DEPS to 130.0.6703.1 * chore: bump chromium in DEPS to 130.0.6705.1 * chore: bump chromium in DEPS to 130.0.6707.1 * chore: bump chromium in DEPS to 130.0.6709.1 * chore: bump chromium in DEPS to 130.0.6711.1 * chore: bump chromium in DEPS to 130.0.6713.1 * chore: bump chromium in DEPS to 130.0.6715.2 * chore: bump chromium in DEPS to 130.0.6717.0 * chore: bump chromium in DEPS to 130.0.6719.1 * chore: bump chromium in DEPS to 130.0.6720.1 * chore: bump chromium in DEPS to 130.0.6723.1 * chore: bump chromium in DEPS to 130.0.6723.4 * chore: bump chromium in DEPS to 130.0.6723.6 * chore: bump chromium to 130.0.6695.0 (main) (#43454) * chore: bump chromium in DEPS to 130.0.6673.0 * chore: bump chromium in DEPS to 130.0.6675.0 * chore: bump chromium in DEPS to 130.0.6677.2 * chore: bump chromium in DEPS to 130.0.6679.0 * 5802981: [Partitioned Popins] UKM https://chromium-review.googlesource.com/c/chromium/src/+/5802981 * 5799275: ash: Create //chrome/browser/ui/ash/web_view https://chromium-review.googlesource.com/c/chromium/src/+/5799275 * 5791853: [PWA] Allow WebContentsImpl::CreateNewWindow() to use new web contents for loading url https://chromium-review.googlesource.com/c/chromium/src/+/5791853 * 5805208: Move third_party/jacoco to a cipd/ subdirectory. https://chromium-review.googlesource.com/c/chromium/src/+/5805208 * chore: fixup patch indices * 5771091: Introduce InputManager class for handling input in Viz. https://chromium-review.googlesource.com/c/chromium/src/+/5771091 * 5498921: [Permission] Remove SubscribeToPermissionStatusChange from PermissionManager https://chromium-review.googlesource.com/c/chromium/src/+/5498921 * 5791853: [PWA] Allow WebContentsImpl::CreateNewWindow() to use new web contents for loading url https://chromium-review.googlesource.com/c/chromium/src/+/5791853 * 5801311: Don't use int for bindings https://chromium-review.googlesource.com/c/chromium/src/+/5801311 * 5548827: [Web Install] Define the web-app-installation PermissionPolicy https://chromium-review.googlesource.com/c/chromium/src/+/5548827 * 5786325: Add Infrastructure for Hand tracking permission https://chromium-review.googlesource.com/c/chromium/src/+/5786325 * chore: fixup patch indices * chore: bump chromium in DEPS to 130.0.6681.0 * [Views AX] Move BrowserAccessibility* to //ui/accessibility/platform Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5520052 * chore: e patches all * Don't have default arguments on virtual functions in render_frame_host.h https://chromium-review.googlesource.com/c/chromium/src/+/5809399 * test: log if loadURL fails in base url test * chore: bump chromium in DEPS to 130.0.6683.2 * chore: fix support_mixed_sandbox_with_zygote.patch content: restore old DisableJit behavior https://chromium-review.googlesource.com/c/chromium/src/+/5804255 * chore: update patch indices * chore: bump chromium in DEPS to 130.0.6685.0 * Parallel process launching Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5015584 * chore: update feat_expose_documentloader_setdefersloading_on_webdocumentloader.patch No manual changes; patch applied with fuzz 1 * chore: e patches all * chore: bump chromium in DEPS to 130.0.6687.0 * chore: bump chromium in DEPS to 130.0.6689.0 * chore: bump chromium in DEPS to 130.0.6691.0 * chore: bump chromium in DEPS to 130.0.6693.0 * chore: update patches * chore: bump chromium in DEPS to 130.0.6695.0 * chore: free up macos disk space as soon as possible * 5824143: Use checked in source lists for third_party/boringssl https://chromium-review.googlesource.com/c/chromium/src/+/5824143 * chore: update patches * 5824122: Extensions: Add a new view type enum for developer tools contexts https://chromium-review.googlesource.com/c/chromium/src/+/5824122 * 5806109: Option for JavaScriptExecuteRequestForTests() to ignore content settings https://chromium-review.googlesource.com/c/chromium/src/+/5806109 * build: free up disk space on gn check too * 5799369: [Refactoring] Make allow_http1_for_streaming_upload flags false. https://chromium-review.googlesource.com/c/chromium/src/+/5799369 * fixup! 5015584: Parallel process launching | https://chromium-review.googlesource.com/c/chromium/src/+/5015584 * Disable failing test for short-term See: https://github.com/electron/electron/issues/43730 * oops --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Keeley Hammond <khammond@slack-corp.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: clavin <clavin@electronjs.org> (cherry picked from commit4bcbc955dd
) * build: update appveyor image to latest version (#43772) Co-authored-by: electron-appveyor-updater[bot] <161660339+electron-appveyor-updater[bot]@users.noreply.github.com> (cherry picked from commit02fd8bbcc1
) * chore: fixup patches * chore: fixup patch * chore: bump chromium in DEPS to 130.0.6723.6 * chore: update filenames.libcxx.gni * test: re-enable disabled test * 5844369: controlledframe: Disable Web Bluetooth for <webview> & <controlledframe> https://chromium-review.googlesource.com/c/chromium/src/+/5844369 (cherry picked from commit 0ac4852763f0dce3468d30907620211f34c4104b) * (multiple CLs): Use an opaque type for FrameTreeNode IDs 5807683: Use an opaque type for FrameTreeNode IDs, part 1 | https://chromium-review.googlesource.com/c/chromium/src/+/5807683 5829746: Use an opaque type for FrameTreeNode IDs, part 2 | https://chromium-review.googlesource.com/c/chromium/src/+/5829746 5836903: Use an opaque type for FrameTreeNode IDs, part 7 | https://chromium-review.googlesource.com/c/chromium/src/+/5836903 5837249: Use an opaque type for FrameTreeNode IDs, part 8 | https://chromium-review.googlesource.com/c/chromium/src/+/5837249 5836564: Use an opaque type for FrameTreeNode IDs, part 12 | https://chromium-review.googlesource.com/c/chromium/src/+/5836564 5837180: Use an opaque type for FrameTreeNode IDs, part 15 | https://chromium-review.googlesource.com/c/chromium/src/+/5837180 (cherry picked from commit 1dd67f5241a3a24ea5a20f11efefdd2afc520a26) * 5822889: [task] Make GetForegroundTaskRunner non-virtual https://chromium-review.googlesource.com/c/v8/v8/+/5822889 (cherry picked from commit ae1e7232a35260cce8c4303d5f9809aa166bf19b) * 5833297: Remove unused inner WebContents attach params https://chromium-review.googlesource.com/c/chromium/src/+/5833297 (cherry picked from commit 390bb42e39eba66cd86221461d2822bbd85012df) * 5806403: Shift PowerMonitor to non static https://chromium-review.googlesource.com/c/chromium/src/+/5806403 (cherry picked from commit 128e3826e13768e89c7f5117de451ebd2b3ec9a4) * 5666874: [3/N] Remove old OnPowerChange in PowerObserver https://chromium-review.googlesource.com/c/chromium/src/+/5666874 (cherry picked from commit 6ea6ea14ae23ced6465b94fd753f8c80ced4b13b) * 5829085: [v8] Differentiate between UserVisible and BestEffort task runners https://chromium-review.googlesource.com/c/chromium/src/+/5829085 (cherry picked from commit 1db28ee4a51fb2593e4c11bcaa538a6524abe48c) * 5791112: [webrtc] Use `c/b/permissions/system` for system permissions https://chromium-review.googlesource.com/c/chromium/src/+/5791112 (cherry picked from commit 50d4a71923ca99653bf5733145bdc6e21f783b2d) * fixup! (multiple CLs): Use an opaque type for FrameTreeNode IDs (cherry picked from commit 9271130e160d479b3d286d295045b9d4acd2005c) * fixup! 5791112: [webrtc] Use `c/b/permissions/system` for system permissions https://chromium-review.googlesource.com/c/chromium/src/+/5791112 (cherry picked from commit db2c8c69d0c7bdee881997847dfc3c8abf32197c) * 5825636: [Extensions] Create WebContentsObservers with ExtensionsBrowserClient https://chromium-review.googlesource.com/c/chromium/src/+/5825636 (cherry picked from commit f2c3d09ccd141dbe2f4b62957b72f98bf8a010ae) * 5854811: Use kNotAllowedError instead of kSecurityError for Web MIDI https://chromium-review.googlesource.com/c/chromium/src/+/5854811 * test: fix should support base url for data urls test Caused by https://chromium-review.googlesource.com/c/chromium/src/+/5802682 * test: fixup extensions can cancel http requests * chore: document custom protocol handling on Windows change due to Non-Special Scheme URLs shipping https://chromium-review.googlesource.com/c/chromium/src/+/5802682 --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: electron-appveyor-updater[bot] <161660339+electron-appveyor-updater[bot]@users.noreply.github.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: clavin <clavin@electronjs.org>
605 lines
21 KiB
Text
605 lines
21 KiB
Text
// Copyright (c) 2016 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/browser/api/electron_api_system_preferences.h"
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
#import <Cocoa/Cocoa.h>
|
|
#import <LocalAuthentication/LocalAuthentication.h>
|
|
#import <Security/Security.h>
|
|
|
|
#include "base/apple/scoped_cftyperef.h"
|
|
#include "base/containers/flat_map.h"
|
|
#include "base/strings/sys_string_conversions.h"
|
|
#include "base/task/sequenced_task_runner.h"
|
|
#include "base/values.h"
|
|
#include "chrome/browser/media/webrtc/system_media_capture_permissions_mac.h"
|
|
#include "net/base/apple/url_conversions.h"
|
|
#include "shell/browser/mac/dict_util.h"
|
|
#include "shell/browser/mac/electron_application.h"
|
|
#include "shell/common/color_util.h"
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
|
#include "shell/common/gin_converters/value_converter.h"
|
|
#include "shell/common/gin_helper/promise.h"
|
|
#include "shell/common/node_includes.h"
|
|
#include "shell/common/process_util.h"
|
|
#include "skia/ext/skia_utils_mac.h"
|
|
|
|
namespace gin {
|
|
|
|
template <>
|
|
struct Converter<NSAppearance*> {
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
v8::Local<v8::Value> val,
|
|
NSAppearance* __strong* out) {
|
|
if (val->IsNull()) {
|
|
*out = nil;
|
|
return true;
|
|
}
|
|
|
|
std::string name;
|
|
if (!gin::ConvertFromV8(isolate, val, &name)) {
|
|
return false;
|
|
}
|
|
|
|
if (name == "light") {
|
|
*out = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
|
|
return true;
|
|
} else if (name == "dark") {
|
|
*out = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, NSAppearance* val) {
|
|
if (val == nil)
|
|
return v8::Null(isolate);
|
|
|
|
if ([val.name isEqualToString:NSAppearanceNameAqua]) {
|
|
return gin::ConvertToV8(isolate, "light");
|
|
} else if ([val.name isEqualToString:NSAppearanceNameDarkAqua]) {
|
|
return gin::ConvertToV8(isolate, "dark");
|
|
}
|
|
|
|
return gin::ConvertToV8(isolate, "unknown");
|
|
}
|
|
};
|
|
|
|
} // namespace gin
|
|
|
|
namespace electron::api {
|
|
|
|
namespace {
|
|
|
|
int g_next_id = 0;
|
|
|
|
// The map to convert |id| to |int|.
|
|
base::flat_map<int, id> g_id_map;
|
|
|
|
AVMediaType ParseMediaType(const std::string& media_type) {
|
|
if (media_type == "camera") {
|
|
return AVMediaTypeVideo;
|
|
} else if (media_type == "microphone") {
|
|
return AVMediaTypeAudio;
|
|
} else {
|
|
return nil;
|
|
}
|
|
}
|
|
|
|
std::string ConvertSystemPermission(
|
|
system_permission_settings::SystemPermission value) {
|
|
using SystemPermission = system_permission_settings::SystemPermission;
|
|
switch (value) {
|
|
case SystemPermission::kNotDetermined:
|
|
return "not-determined";
|
|
case SystemPermission::kRestricted:
|
|
return "restricted";
|
|
case SystemPermission::kDenied:
|
|
return "denied";
|
|
case SystemPermission::kAllowed:
|
|
return "granted";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
NSNotificationCenter* GetNotificationCenter(NotificationCenterKind kind) {
|
|
switch (kind) {
|
|
case NotificationCenterKind::kNSDistributedNotificationCenter:
|
|
return [NSDistributedNotificationCenter defaultCenter];
|
|
case NotificationCenterKind::kNSNotificationCenter:
|
|
return [NSNotificationCenter defaultCenter];
|
|
case NotificationCenterKind::kNSWorkspaceNotificationCenter:
|
|
return [[NSWorkspace sharedWorkspace] notificationCenter];
|
|
default:
|
|
return nil;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void SystemPreferences::PostNotification(const std::string& name,
|
|
base::Value::Dict user_info,
|
|
gin::Arguments* args) {
|
|
bool immediate = false;
|
|
args->GetNext(&immediate);
|
|
|
|
NSDistributedNotificationCenter* center =
|
|
[NSDistributedNotificationCenter defaultCenter];
|
|
[center
|
|
postNotificationName:base::SysUTF8ToNSString(name)
|
|
object:nil
|
|
userInfo:DictionaryValueToNSDictionary(std::move(user_info))
|
|
deliverImmediately:immediate];
|
|
}
|
|
|
|
int SystemPreferences::SubscribeNotification(
|
|
v8::Local<v8::Value> maybe_name,
|
|
const NotificationCallback& callback) {
|
|
return DoSubscribeNotification(
|
|
maybe_name, callback,
|
|
NotificationCenterKind::kNSDistributedNotificationCenter);
|
|
}
|
|
|
|
void SystemPreferences::UnsubscribeNotification(int request_id) {
|
|
DoUnsubscribeNotification(
|
|
request_id, NotificationCenterKind::kNSDistributedNotificationCenter);
|
|
}
|
|
|
|
void SystemPreferences::PostLocalNotification(const std::string& name,
|
|
base::Value::Dict user_info) {
|
|
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
|
|
[center
|
|
postNotificationName:base::SysUTF8ToNSString(name)
|
|
object:nil
|
|
userInfo:DictionaryValueToNSDictionary(std::move(user_info))];
|
|
}
|
|
|
|
int SystemPreferences::SubscribeLocalNotification(
|
|
v8::Local<v8::Value> maybe_name,
|
|
const NotificationCallback& callback) {
|
|
return DoSubscribeNotification(maybe_name, callback,
|
|
NotificationCenterKind::kNSNotificationCenter);
|
|
}
|
|
|
|
void SystemPreferences::UnsubscribeLocalNotification(int request_id) {
|
|
DoUnsubscribeNotification(request_id,
|
|
NotificationCenterKind::kNSNotificationCenter);
|
|
}
|
|
|
|
void SystemPreferences::PostWorkspaceNotification(const std::string& name,
|
|
base::Value::Dict user_info) {
|
|
NSNotificationCenter* center =
|
|
[[NSWorkspace sharedWorkspace] notificationCenter];
|
|
[center
|
|
postNotificationName:base::SysUTF8ToNSString(name)
|
|
object:nil
|
|
userInfo:DictionaryValueToNSDictionary(std::move(user_info))];
|
|
}
|
|
|
|
int SystemPreferences::SubscribeWorkspaceNotification(
|
|
v8::Local<v8::Value> maybe_name,
|
|
const NotificationCallback& callback) {
|
|
return DoSubscribeNotification(
|
|
maybe_name, callback,
|
|
NotificationCenterKind::kNSWorkspaceNotificationCenter);
|
|
}
|
|
|
|
void SystemPreferences::UnsubscribeWorkspaceNotification(int request_id) {
|
|
DoUnsubscribeNotification(
|
|
request_id, NotificationCenterKind::kNSWorkspaceNotificationCenter);
|
|
}
|
|
|
|
int SystemPreferences::DoSubscribeNotification(
|
|
v8::Local<v8::Value> maybe_name,
|
|
const NotificationCallback& callback,
|
|
NotificationCenterKind kind) {
|
|
int request_id = g_next_id++;
|
|
__block NotificationCallback copied_callback = callback;
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
std::string name_str;
|
|
if (!(maybe_name->IsNull() ||
|
|
gin::ConvertFromV8(isolate, maybe_name, &name_str))) {
|
|
isolate->ThrowException(v8::Exception::Error(
|
|
gin::StringToV8(isolate, "Must pass null or a string")));
|
|
return -1;
|
|
}
|
|
|
|
auto* name = maybe_name->IsNull() ? nil : base::SysUTF8ToNSString(name_str);
|
|
|
|
g_id_map[request_id] = [GetNotificationCenter(kind)
|
|
addObserverForName:name
|
|
object:nil
|
|
queue:nil
|
|
usingBlock:^(NSNotification* notification) {
|
|
std::string object = "";
|
|
if ([notification.object isKindOfClass:[NSString class]]) {
|
|
object = base::SysNSStringToUTF8(notification.object);
|
|
}
|
|
|
|
if (notification.userInfo) {
|
|
copied_callback.Run(
|
|
base::SysNSStringToUTF8(notification.name),
|
|
base::Value(NSDictionaryToValue(notification.userInfo)),
|
|
object);
|
|
} else {
|
|
copied_callback.Run(
|
|
base::SysNSStringToUTF8(notification.name),
|
|
base::Value(base::Value::Dict()), object);
|
|
}
|
|
}];
|
|
return request_id;
|
|
}
|
|
|
|
void SystemPreferences::DoUnsubscribeNotification(int request_id,
|
|
NotificationCenterKind kind) {
|
|
auto iter = g_id_map.find(request_id);
|
|
if (iter != g_id_map.end()) {
|
|
id observer = iter->second;
|
|
[GetNotificationCenter(kind) removeObserver:observer];
|
|
g_id_map.erase(iter);
|
|
}
|
|
}
|
|
|
|
v8::Local<v8::Value> SystemPreferences::GetUserDefault(
|
|
v8::Isolate* isolate,
|
|
const std::string& name,
|
|
const std::string& type) {
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
NSString* key = base::SysUTF8ToNSString(name);
|
|
if (type == "string") {
|
|
return gin::StringToV8(
|
|
isolate, base::SysNSStringToUTF8([defaults stringForKey:key]));
|
|
} else if (type == "boolean") {
|
|
return v8::Boolean::New(isolate, [defaults boolForKey:key]);
|
|
} else if (type == "float") {
|
|
return v8::Number::New(isolate, [defaults floatForKey:key]);
|
|
} else if (type == "integer") {
|
|
return v8::Integer::New(isolate, [defaults integerForKey:key]);
|
|
} else if (type == "double") {
|
|
return v8::Number::New(isolate, [defaults doubleForKey:key]);
|
|
} else if (type == "url") {
|
|
return gin::ConvertToV8(isolate,
|
|
net::GURLWithNSURL([defaults URLForKey:key]));
|
|
} else if (type == "array") {
|
|
return gin::ConvertToV8(
|
|
isolate, base::Value(NSArrayToValue([defaults arrayForKey:key])));
|
|
} else if (type == "dictionary") {
|
|
return gin::ConvertToV8(
|
|
isolate,
|
|
base::Value(NSDictionaryToValue([defaults dictionaryForKey:key])));
|
|
} else {
|
|
return v8::Undefined(isolate);
|
|
}
|
|
}
|
|
|
|
void SystemPreferences::RegisterDefaults(gin::Arguments* args) {
|
|
base::Value::Dict dict_value;
|
|
|
|
if (!args->GetNext(&dict_value)) {
|
|
args->ThrowError();
|
|
return;
|
|
}
|
|
@try {
|
|
NSDictionary* dict = DictionaryValueToNSDictionary(std::move(dict_value));
|
|
for (id key in dict) {
|
|
id value = [dict objectForKey:key];
|
|
if ([value isKindOfClass:[NSNull class]] || value == nil) {
|
|
args->ThrowError();
|
|
return;
|
|
}
|
|
}
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
|
|
} @catch (NSException* exception) {
|
|
args->ThrowError();
|
|
}
|
|
}
|
|
|
|
void SystemPreferences::SetUserDefault(const std::string& name,
|
|
const std::string& type,
|
|
gin::Arguments* args) {
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
NSString* key = base::SysUTF8ToNSString(name);
|
|
if (type == "string") {
|
|
std::string value;
|
|
if (args->GetNext(&value)) {
|
|
[defaults setObject:base::SysUTF8ToNSString(value) forKey:key];
|
|
return;
|
|
}
|
|
} else if (type == "boolean") {
|
|
bool value;
|
|
v8::Local<v8::Value> next = args->PeekNext();
|
|
if (!next.IsEmpty() && next->IsBoolean() && args->GetNext(&value)) {
|
|
[defaults setBool:value forKey:key];
|
|
return;
|
|
}
|
|
} else if (type == "float") {
|
|
float value;
|
|
if (args->GetNext(&value)) {
|
|
[defaults setFloat:value forKey:key];
|
|
return;
|
|
}
|
|
} else if (type == "integer") {
|
|
int value;
|
|
if (args->GetNext(&value)) {
|
|
[defaults setInteger:value forKey:key];
|
|
return;
|
|
}
|
|
} else if (type == "double") {
|
|
double value;
|
|
if (args->GetNext(&value)) {
|
|
[defaults setDouble:value forKey:key];
|
|
return;
|
|
}
|
|
} else if (type == "url") {
|
|
GURL value;
|
|
if (args->GetNext(&value)) {
|
|
if (NSURL* url = net::NSURLWithGURL(value)) {
|
|
[defaults setURL:url forKey:key];
|
|
return;
|
|
}
|
|
}
|
|
} else if (type == "array") {
|
|
base::Value value;
|
|
if (args->GetNext(&value) && value.is_list()) {
|
|
if (NSArray* array = ListValueToNSArray(value.GetList())) {
|
|
[defaults setObject:array forKey:key];
|
|
return;
|
|
}
|
|
}
|
|
} else if (type == "dictionary") {
|
|
base::Value value;
|
|
if (args->GetNext(&value) && value.is_dict()) {
|
|
if (NSDictionary* dict = DictionaryValueToNSDictionary(value.GetDict())) {
|
|
[defaults setObject:dict forKey:key];
|
|
return;
|
|
}
|
|
}
|
|
} else {
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
.ThrowTypeError("Invalid type: " + type);
|
|
return;
|
|
}
|
|
|
|
gin_helper::ErrorThrower(args->isolate())
|
|
.ThrowTypeError("Unable to convert value to: " + type);
|
|
}
|
|
|
|
std::string SystemPreferences::GetAccentColor() {
|
|
NSColor* sysColor = sysColor = [NSColor controlAccentColor];
|
|
return ToRGBAHex(skia::NSSystemColorToSkColor(sysColor),
|
|
false /* include_hash */);
|
|
}
|
|
|
|
std::string SystemPreferences::GetSystemColor(gin_helper::ErrorThrower thrower,
|
|
const std::string& color) {
|
|
NSColor* sysColor = nil;
|
|
if (color == "blue") {
|
|
sysColor = [NSColor systemBlueColor];
|
|
} else if (color == "brown") {
|
|
sysColor = [NSColor systemBrownColor];
|
|
} else if (color == "gray") {
|
|
sysColor = [NSColor systemGrayColor];
|
|
} else if (color == "green") {
|
|
sysColor = [NSColor systemGreenColor];
|
|
} else if (color == "orange") {
|
|
sysColor = [NSColor systemOrangeColor];
|
|
} else if (color == "pink") {
|
|
sysColor = [NSColor systemPinkColor];
|
|
} else if (color == "purple") {
|
|
sysColor = [NSColor systemPurpleColor];
|
|
} else if (color == "red") {
|
|
sysColor = [NSColor systemRedColor];
|
|
} else if (color == "yellow") {
|
|
sysColor = [NSColor systemYellowColor];
|
|
} else {
|
|
thrower.ThrowError("Unknown system color: " + color);
|
|
return "";
|
|
}
|
|
|
|
return ToRGBAHex(skia::NSSystemColorToSkColor(sysColor));
|
|
}
|
|
|
|
bool SystemPreferences::CanPromptTouchID() {
|
|
LAContext* context = [[LAContext alloc] init];
|
|
LAPolicy auth_policy = LAPolicyDeviceOwnerAuthenticationWithBiometricsOrWatch;
|
|
if (![context canEvaluatePolicy:auth_policy error:nil])
|
|
return false;
|
|
return [context biometryType] == LABiometryTypeTouchID;
|
|
}
|
|
|
|
v8::Local<v8::Promise> SystemPreferences::PromptTouchID(
|
|
v8::Isolate* isolate,
|
|
const std::string& reason) {
|
|
gin_helper::Promise<void> promise(isolate);
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
LAContext* context = [[LAContext alloc] init];
|
|
base::apple::ScopedCFTypeRef<SecAccessControlRef> access_control =
|
|
base::apple::ScopedCFTypeRef<SecAccessControlRef>(
|
|
SecAccessControlCreateWithFlags(
|
|
kCFAllocatorDefault, kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
kSecAccessControlPrivateKeyUsage | kSecAccessControlUserPresence,
|
|
nullptr));
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> runner =
|
|
base::SequencedTaskRunner::GetCurrentDefault();
|
|
|
|
__block gin_helper::Promise<void> p = std::move(promise);
|
|
[context
|
|
evaluateAccessControl:access_control.get()
|
|
operation:LAAccessControlOperationUseKeySign
|
|
localizedReason:[NSString stringWithUTF8String:reason.c_str()]
|
|
reply:^(BOOL success, NSError* error) {
|
|
// NOLINTBEGIN(bugprone-use-after-move)
|
|
if (!success) {
|
|
std::string err_msg = std::string(
|
|
[error.localizedDescription UTF8String]);
|
|
runner->PostTask(
|
|
FROM_HERE,
|
|
base::BindOnce(
|
|
gin_helper::Promise<void>::RejectPromise,
|
|
std::move(p), std::move(err_msg)));
|
|
} else {
|
|
runner->PostTask(
|
|
FROM_HERE,
|
|
base::BindOnce(
|
|
gin_helper::Promise<void>::ResolvePromise,
|
|
std::move(p)));
|
|
}
|
|
// NOLINTEND(bugprone-use-after-move)
|
|
}];
|
|
|
|
return handle;
|
|
}
|
|
|
|
// static
|
|
bool SystemPreferences::IsTrustedAccessibilityClient(bool prompt) {
|
|
NSDictionary* options =
|
|
@{(__bridge id)kAXTrustedCheckOptionPrompt : @(prompt)};
|
|
return AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
|
|
}
|
|
|
|
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
|
|
const std::string& color) {
|
|
NSColor* sysColor = nil;
|
|
if (color == "control-background") {
|
|
sysColor = [NSColor controlBackgroundColor];
|
|
} else if (color == "control") {
|
|
sysColor = [NSColor controlColor];
|
|
} else if (color == "control-text") {
|
|
sysColor = [NSColor controlTextColor];
|
|
} else if (color == "disabled-control-text") {
|
|
sysColor = [NSColor disabledControlTextColor];
|
|
} else if (color == "find-highlight") {
|
|
sysColor = [NSColor findHighlightColor];
|
|
} else if (color == "grid") {
|
|
sysColor = [NSColor gridColor];
|
|
} else if (color == "header-text") {
|
|
sysColor = [NSColor headerTextColor];
|
|
} else if (color == "highlight") {
|
|
sysColor = [NSColor highlightColor];
|
|
} else if (color == "keyboard-focus-indicator") {
|
|
sysColor = [NSColor keyboardFocusIndicatorColor];
|
|
} else if (color == "label") {
|
|
sysColor = [NSColor labelColor];
|
|
} else if (color == "link") {
|
|
sysColor = [NSColor linkColor];
|
|
} else if (color == "placeholder-text") {
|
|
sysColor = [NSColor placeholderTextColor];
|
|
} else if (color == "quaternary-label") {
|
|
sysColor = [NSColor quaternaryLabelColor];
|
|
} else if (color == "scrubber-textured-background") {
|
|
sysColor = [NSColor scrubberTexturedBackgroundColor];
|
|
} else if (color == "secondary-label") {
|
|
sysColor = [NSColor secondaryLabelColor];
|
|
} else if (color == "selected-content-background") {
|
|
sysColor = [NSColor selectedContentBackgroundColor];
|
|
} else if (color == "selected-control") {
|
|
sysColor = [NSColor selectedControlColor];
|
|
} else if (color == "selected-control-text") {
|
|
sysColor = [NSColor selectedControlTextColor];
|
|
} else if (color == "selected-menu-item-text") {
|
|
sysColor = [NSColor selectedMenuItemTextColor];
|
|
} else if (color == "selected-text-background") {
|
|
sysColor = [NSColor selectedTextBackgroundColor];
|
|
} else if (color == "selected-text") {
|
|
sysColor = [NSColor selectedTextColor];
|
|
} else if (color == "separator") {
|
|
sysColor = [NSColor separatorColor];
|
|
} else if (color == "shadow") {
|
|
sysColor = [NSColor shadowColor];
|
|
} else if (color == "tertiary-label") {
|
|
sysColor = [NSColor tertiaryLabelColor];
|
|
} else if (color == "text-background") {
|
|
sysColor = [NSColor textBackgroundColor];
|
|
} else if (color == "text") {
|
|
sysColor = [NSColor textColor];
|
|
} else if (color == "under-page-background") {
|
|
sysColor = [NSColor underPageBackgroundColor];
|
|
} else if (color == "unemphasized-selected-content-background") {
|
|
sysColor = [NSColor unemphasizedSelectedContentBackgroundColor];
|
|
} else if (color == "unemphasized-selected-text-background") {
|
|
sysColor = [NSColor unemphasizedSelectedTextBackgroundColor];
|
|
} else if (color == "unemphasized-selected-text") {
|
|
sysColor = [NSColor unemphasizedSelectedTextColor];
|
|
} else if (color == "window-background") {
|
|
sysColor = [NSColor windowBackgroundColor];
|
|
} else if (color == "window-frame-text") {
|
|
sysColor = [NSColor windowFrameTextColor];
|
|
} else {
|
|
thrower.ThrowError("Unknown color: " + color);
|
|
}
|
|
|
|
if (sysColor)
|
|
return ToRGBAHex(skia::NSSystemColorToSkColor(sysColor));
|
|
return "";
|
|
}
|
|
|
|
std::string SystemPreferences::GetMediaAccessStatus(
|
|
gin_helper::ErrorThrower thrower,
|
|
const std::string& media_type) {
|
|
if (media_type == "camera") {
|
|
return ConvertSystemPermission(
|
|
system_permission_settings::CheckSystemVideoCapturePermission());
|
|
} else if (media_type == "microphone") {
|
|
return ConvertSystemPermission(
|
|
system_permission_settings::CheckSystemAudioCapturePermission());
|
|
} else if (media_type == "screen") {
|
|
return ConvertSystemPermission(
|
|
system_permission_settings::CheckSystemScreenCapturePermission());
|
|
} else {
|
|
thrower.ThrowError("Invalid media type");
|
|
return std::string();
|
|
}
|
|
}
|
|
|
|
v8::Local<v8::Promise> SystemPreferences::AskForMediaAccess(
|
|
v8::Isolate* isolate,
|
|
const std::string& media_type) {
|
|
gin_helper::Promise<bool> promise(isolate);
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
if (auto type = ParseMediaType(media_type)) {
|
|
__block gin_helper::Promise<bool> p = std::move(promise);
|
|
[AVCaptureDevice requestAccessForMediaType:type
|
|
completionHandler:^(BOOL granted) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
p.Resolve(!!granted);
|
|
});
|
|
}];
|
|
} else {
|
|
promise.RejectWithErrorMessage("Invalid media type");
|
|
}
|
|
|
|
return handle;
|
|
}
|
|
|
|
void SystemPreferences::RemoveUserDefault(const std::string& name) {
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
[defaults removeObjectForKey:base::SysUTF8ToNSString(name)];
|
|
}
|
|
|
|
bool SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled() {
|
|
return [NSEvent isSwipeTrackingFromScrollEventsEnabled];
|
|
}
|
|
|
|
v8::Local<v8::Value> SystemPreferences::GetEffectiveAppearance(
|
|
v8::Isolate* isolate) {
|
|
return gin::ConvertToV8(
|
|
isolate, [NSApplication sharedApplication].effectiveAppearance);
|
|
}
|
|
|
|
bool SystemPreferences::AccessibilityDisplayShouldReduceTransparency() {
|
|
return [[NSWorkspace sharedWorkspace]
|
|
accessibilityDisplayShouldReduceTransparency];
|
|
}
|
|
|
|
} // namespace electron::api
|