electron/shell/common/platform_util_mac.mm
electron-roller[bot] 0dad99561b
chore: bump chromium to 123.0.6296.0 (main) (#41204)
* chore: bump chromium in DEPS to 123.0.6273.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6274.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6276.0

* chore: update patches

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* chore: bump chromium in DEPS to 123.0.6278.0

* chore: bump chromium in DEPS to 123.0.6280.0

* chore: update patches

* chore: use net::CanonicalCookie::SecureAttribute()

renamed from IsSecure()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5245913

* refactor: handle multiple requested device ids

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5132210

* refactor: trigger View layouts async with View::InvalidateLayout()

Upstream has introduced a PassKey to restrict who can call Layout()
directly. I've opted for calling `InvalidateLayout()` which is the
approach that upstream recommends.

If for some reason this approach doesn't work for us, we could use
`DeprecatedLayoutImmediately()` as a stopgap.

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5239586

Xref: https://chromium.googlesource.com/chromium/src/+/main/ui/views/view.h#809

Xref: https://chromium.googlesource.com/chromium/src/+/main/docs/ui/learn/bestpractices/layout.md?pli=1#don_t-invoke-layout_directly

* chore: bump chromium in DEPS to 123.0.6282.0

* chore: bump chromium in DEPS to 123.0.6284.0

* chore: update patches

* refactor: remove use of blink::MainThreadIsolate() pt 1/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* refactor: remove use of blink::MainThreadIsolate() pt 2/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* refactor: remove use of blink::MainThreadIsolate() pt 3/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* chore: update enum name to ui::AXMode::kPDFPrinting

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5270301

* chore: rebuild filenames.libcxx.gni

* chore: sync with upstream rename of PortProvider.TaskForHandle()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5259103

* chore: bump chromium in DEPS to 123.0.6286.0

* chore: bump chromium in DEPS to 123.0.6288.0

* WebPreferences: Initialize in declaration.

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5277099

* chore: update webview_fullscreen.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5053508

Simple update to fix patch shear

* chore: update feat_configure_launch_options_for_service_process.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5254861

Simple update to fix patch shear

* chore: add IWC::Delegate::RecordResize() stub to fix FTBFS

https://chromium-review.googlesource.com/c/chromium/src/+/5268963

* chore: add FormControlType::kButtonPopover to the FormControlType converter

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5230929

* chore: e patches all

* chore: node script/gen-libc++-filenames.js

* chore: bump chromium in DEPS to 123.0.6290.0

* chore: bump chromium in DEPS to 123.0.6291.0

* chore: bump chromium in DEPS to 123.0.6292.0

* chore: bump chromium in DEPS to 123.0.6294.0

* chore: update fix_aspect_ratio_with_max_size.patch

Xref: fix_aspect_ratio_with_max_size.patch

note: simple absl::optional -> std::optional conversion

* chore: update feat_filter_out_non-shareable_windows_in_the_current_application_in.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5272337

* chore: update add_maximized_parameter_to_linuxui_getwindowframeprovider.patch

No manual changes; just adjusting line patch offsets

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5258688

* chore: update feat_configure_launch_options_for_service_process.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5281322

* chore: update fix_select_The_first_menu_item_when_opened_via_keyboard.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5279376

note: simple absl::optional -> std::optional conversion

* chore: update feat_allow_code_cache_in_custom_schemes.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5268792

* chore: script/export_all_patches.py

* chore: bump chromium in DEPS to 123.0.6296.0

* chore: update patches

* fixup! chore: update feat_allow_code_cache_in_custom_schemes.patch

* fix: restore MessagePort close event

* spec: fix CORB testing

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5231506

* fix: use sync layout when content view changes

* fixup! chore: update feat_configure_launch_options_for_service_process.patch

* Add remote-cocoa support for context menus.

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5259806

* Rename //net/base/mac directory to //net/base/apple (1/n)

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5211389

* fixup! Add remote-cocoa support for context menus.

* [Clipboard] Don't add meta charset tag for async write() method on Mac.

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5187335

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
2024-02-14 12:33:32 -05:00

291 lines
10 KiB
Text

// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/platform_util.h"
#include <string>
#include <utility>
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#import <ServiceManagement/ServiceManagement.h>
#include "base/apple/foundation_util.h"
#include "base/apple/osstatus_logging.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/mac/scoped_aedesc.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "net/base/apple/url_conversions.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
namespace {
// This may be called from a global dispatch queue, the methods used here are
// thread safe, including LSGetApplicationForURL (> 10.2) and
// NSWorkspace#openURLs.
std::string OpenURL(NSURL* ns_url, bool activate) {
CFURLRef cf_url = (__bridge CFURLRef)(ns_url);
CFURLRef ref =
LSCopyDefaultApplicationURLForURL(cf_url, kLSRolesAll, nullptr);
// If no application could be found, nullptr is returned and outError
// (if not nullptr) is populated with kLSApplicationNotFoundErr.
if (ref == nullptr)
return "No application in the Launch Services database matches the input "
"criteria.";
NSUInteger launchOptions = NSWorkspaceLaunchDefault;
if (!activate)
launchOptions |= NSWorkspaceLaunchWithoutActivation;
bool opened = [[NSWorkspace sharedWorkspace] openURLs:@[ ns_url ]
withAppBundleIdentifier:nil
options:launchOptions
additionalEventParamDescriptor:nil
launchIdentifiers:nil];
if (!opened)
return "Failed to open URL";
return "";
}
NSString* GetLoginHelperBundleIdentifier() {
return [[[NSBundle mainBundle] bundleIdentifier]
stringByAppendingString:@".loginhelper"];
}
std::string OpenPathOnThread(const base::FilePath& full_path) {
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
NSURL* url = [NSURL fileURLWithPath:path_string];
if (!url)
return "Invalid path";
const NSWorkspaceLaunchOptions launch_options =
NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation;
BOOL success = [[NSWorkspace sharedWorkspace] openURLs:@[ url ]
withAppBundleIdentifier:nil
options:launch_options
additionalEventParamDescriptor:nil
launchIdentifiers:nil];
return success ? "" : "Failed to open path";
}
// https://developer.apple.com/documentation/servicemanagement/1561515-service_management_errors?language=objc
std::string GetLaunchStringForError(NSError* error) {
if (@available(macOS 13, *)) {
switch ([error code]) {
case kSMErrorAlreadyRegistered:
return "The application is already registered";
case kSMErrorAuthorizationFailure:
return "The authorization requested failed";
case kSMErrorLaunchDeniedByUser:
return "The user denied the app's launch request";
case kSMErrorInternalFailure:
return "An internal failure has occurred";
case kSMErrorInvalidPlist:
return "The app's property list is invalid";
case kSMErrorInvalidSignature:
return "The app's code signature doesn't meet the requirements to "
"perform the operation";
case kSMErrorJobMustBeEnabled:
return "The specified job is not enabled";
case kSMErrorJobNotFound:
return "The system can't find the specified job";
case kSMErrorJobPlistNotFound:
return "The app's property list cannot be found";
case kSMErrorServiceUnavailable:
return "The service necessary to perform this operation is unavailable "
"or is no longer accepting requests";
case kSMErrorToolNotValid:
return "The specified path doesn't exist or the helper tool at the "
"specified path isn't valid";
default:
return "Failed to register the login item";
}
}
return "";
}
SMAppService* GetServiceForType(const std::string& type,
const std::string& name)
API_AVAILABLE(macosx(13.0)) {
NSString* service_name = [NSString stringWithUTF8String:name.c_str()];
if (type == "mainAppService") {
return [SMAppService mainAppService];
} else if (type == "agentService") {
return [SMAppService agentServiceWithPlistName:service_name];
} else if (type == "daemonService") {
return [SMAppService daemonServiceWithPlistName:service_name];
} else if (type == "loginService") {
return [SMAppService loginItemServiceWithIdentifier:service_name];
} else {
LOG(ERROR) << "Unrecognized login item type";
return nullptr;
}
}
bool GetLoginItemEnabledDeprecated() {
BOOL enabled = NO;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// SMJobCopyDictionary does not work in sandbox (see rdar://13626319)
CFArrayRef jobs = SMCopyAllJobDictionaries(kSMDomainUserLaunchd);
#pragma clang diagnostic pop
NSArray* jobs_ = CFBridgingRelease(jobs);
NSString* identifier = GetLoginHelperBundleIdentifier();
if (jobs_ && [jobs_ count] > 0) {
for (NSDictionary* job in jobs_) {
if ([identifier isEqualToString:[job objectForKey:@"Label"]]) {
enabled = [[job objectForKey:@"OnDemand"] boolValue];
break;
}
}
}
return enabled;
}
} // namespace
namespace platform_util {
void ShowItemInFolder(const base::FilePath& path) {
// The API only takes absolute path.
base::FilePath full_path =
path.IsAbsolute() ? path : base::MakeAbsoluteFilePath(path);
DCHECK([NSThread isMainThread]);
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
inFileViewerRootedAtPath:@""]) {
LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
}
}
void OpenPath(const base::FilePath& full_path, OpenCallback callback) {
std::move(callback).Run(OpenPathOnThread(full_path));
}
void OpenExternal(const GURL& url,
const OpenExternalOptions& options,
OpenCallback callback) {
DCHECK([NSThread isMainThread]);
NSURL* ns_url = net::NSURLWithGURL(url);
if (!ns_url) {
std::move(callback).Run("Invalid URL");
return;
}
bool activate = options.activate;
__block OpenCallback c = std::move(callback);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
__block std::string error = OpenURL(ns_url, activate);
dispatch_async(dispatch_get_main_queue(), ^{
std::move(c).Run(error);
});
});
}
bool MoveItemToTrashWithError(const base::FilePath& full_path,
bool delete_on_fail,
std::string* error) {
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
if (!path_string) {
*error = "Invalid file path: " + full_path.value();
LOG(WARNING) << *error;
return false;
}
NSURL* url = [NSURL fileURLWithPath:path_string];
NSError* err = nil;
BOOL did_trash = [[NSFileManager defaultManager] trashItemAtURL:url
resultingItemURL:nil
error:&err];
if (delete_on_fail) {
// Some volumes may not support a Trash folder or it may be disabled
// so these methods will report failure by returning NO or nil and
// an NSError with NSFeatureUnsupportedError.
// Handle this by deleting the item as a fallback.
if (!did_trash && [err code] == NSFeatureUnsupportedError) {
did_trash = [[NSFileManager defaultManager] removeItemAtURL:url
error:&err];
}
}
if (!did_trash) {
*error = base::SysNSStringToUTF8([err localizedDescription]);
LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
<< " to trash: " << *error;
}
return did_trash;
}
namespace internal {
bool PlatformTrashItem(const base::FilePath& full_path, std::string* error) {
return MoveItemToTrashWithError(full_path, false, error);
}
} // namespace internal
void Beep() {
NSBeep();
}
std::string GetLoginItemEnabled(const std::string& type,
const std::string& service_name) {
bool enabled = GetLoginItemEnabledDeprecated();
if (@available(macOS 13, *)) {
SMAppService* service = GetServiceForType(type, service_name);
SMAppServiceStatus status = [service status];
if (status == SMAppServiceStatusNotRegistered)
return "not-registered";
else if (status == SMAppServiceStatusEnabled)
return "enabled";
else if (status == SMAppServiceStatusRequiresApproval)
return "requires-approval";
else if (status == SMAppServiceStatusNotFound) {
// If the login item was enabled with the old API, return that.
return enabled ? "enabled-deprecated" : "not-found";
}
}
return enabled ? "enabled" : "not-registered";
}
bool SetLoginItemEnabled(const std::string& type,
const std::string& service_name,
bool enabled) {
if (@available(macOS 13, *)) {
#if IS_MAS_BUILD()
// If the app was previously set as a LoginItem with the old API, remove it
// as a LoginItem via the old API before re-enabling with the new API.
if (GetLoginItemEnabledDeprecated() && enabled) {
NSString* identifier = GetLoginHelperBundleIdentifier();
SMLoginItemSetEnabled((__bridge CFStringRef)identifier, false);
}
#endif
SMAppService* service = GetServiceForType(type, service_name);
NSError* error = nil;
bool result = enabled ? [service registerAndReturnError:&error]
: [service unregisterAndReturnError:&error];
if (error != nil)
LOG(ERROR) << "Unable to set login item: "
<< GetLaunchStringForError(error);
return result;
} else {
NSString* identifier = GetLoginHelperBundleIdentifier();
return SMLoginItemSetEnabled((__bridge CFStringRef)identifier, enabled);
}
}
} // namespace platform_util