2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-29 12:41:11 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/platform_util.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-11-17 02:22:09 +00:00
|
|
|
#import <Carbon/Carbon.h>
|
2013-04-29 12:41:11 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2017-10-17 07:28:29 +00:00
|
|
|
#import <ServiceManagement/ServiceManagement.h>
|
2013-04-29 12:41:11 +00:00
|
|
|
|
chore: bump chromium to 118.0.5975.0 (main) (#39531)
* chore: bump chromium in DEPS to 118.0.5951.0
* chore: update printing.patch
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4727894
No logic changes, but patch needed to be manually re-applied due to upstream code shear
* chore: update port_autofill_colors_to_the_color_pipeline.patch
No manual changes; patch applied with fuzz
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5953.0
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5955.0
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5957.0
* chore: update patches
* chore: include path of native_web_keyboard_event.h
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4758689
* chore: remove reference to eextensions/browser/notification-types.h
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4771627
* chore: update references to renamed upstream field NativeWebKeyboardEvent.skip_if_unhandled (formerly known as skip_in_browser
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4758689
Need a second pair of eyes on this commit. In particular the reference in content_converter.cc, skipInBrowser, seems to not be set or documented anywhere? Is this unused/vestigal code?
* chore: sync signature of ElectronExtensionsBrowserClient::IsValidContext() to upstream change
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4784198
* chore: add auto_pip_setting_helper.[cc,h] to chromium_src build
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4688277
Exiting upstream code used by chromium_src now depends on this new upstream class
* chore: bump chromium in DEPS to 118.0.5959.0
* chore: update add_maximized_parameter_to_linuxui_getwindowframeprovider.patch
Xref: add_maximized_parameter_to_linuxui_getwindowframeprovider.patch
manually adjust patch to minor upstream chagnes
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5961.0
* chore: bump chromium in DEPS to 118.0.5963.0
* chore: update patches
* 4780994: Rename various base files to "apple" since iOS uses them too
https://chromium-review.googlesource.com/c/chromium/src/+/4780994
* Many files moved from `mac` -> `apple`
This commit follows a handful of CLs that simply rename files/symbols to change `mac`
to `apple`
to signify their use across both macOS and iOS:
- 4784010: Move scoped_nsautorelease_pool to base/apple, leave a forwarding header
- 4790744: Move foundation_util to base/apple, leave a forwarding header
- 4790741: Move scoped_cftypreref to base/apple, leave a forwarding header
- 4787627: Move and rename macOS+iOS base/ files in PA to "apple"
- 4780399: Move OSStatus logging to base/apple
- 4787387: Remove forwarding headers
- 4781113: Rename message_pump_mac to "apple" because iOS uses it too
* fixup minor patch update error
A function param got dropped from this patch somewhere earlier
* chore: bump chromium in DEPS to 118.0.5965.2
* chore: update patches
* 4799213: Move ScopedTypeRef and ScopedCFTypeRef into base::apple::
https://chromium-review.googlesource.com/c/chromium/src/+/4799213
* Fix removed include to BrowserContext
In crrev.com/c/4767962 an include to BrowserContext was removed,
which was necessary for compilation. This broke only for us because
"chrome/browser/profiles/profile.h" includes that class, but we remove
all references to profiles.
* chore: bump chromium in DEPS to 118.0.5967.0
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5969.0
* chore: update patches
* chore: bump chromium in DEPS to 118.0.5971.0
* chore: bump chromium in DEPS to 118.0.5973.0
* chore: update patches
* 4772121: [OOPIF PDF] Replace PDFWebContentsHelper with PDFDocumentHelper
https://chromium-review.googlesource.com/c/chromium/src/+/4772121
* 4811164: [Extensions] Do some cleanup in ChromeManagementAPIDelegate.
https://chromium-review.googlesource.com/c/chromium/src/+/4811164
* 4809488: Remove duplicate dnd functionality between Web and Renderer prefs
https://chromium-review.googlesource.com/c/chromium/src/+/4809488
Given that this is no longer an option of web preferences, we should
consider deprecating this option and then removing it.
* chore: bump chromium in DEPS to 118.0.5975.0
* chore: update patches
* fixup! chore: add auto_pip_settings_helper.{cc|h} to chromium_src build
* Reland "[windows] Remove RegKey::DeleteEmptyKey"
Refs https://chromium-review.googlesource.com/c/chromium/src/+/4813255
* Ensure StrCat means StrCat
Refs https://chromium-review.googlesource.com/c/chromium/src/+/1117180
* fixup! Remove RegKey::DeleteEmptyKey
* Consistently reject large p and large q in DH
Refs https://boringssl-review.googlesource.com/c/boringssl/+/62226
---------
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
2023-09-01 06:54:59 +00:00
|
|
|
#include "base/apple/foundation_util.h"
|
|
|
|
#include "base/apple/osstatus_logging.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
#include "base/files/file_path.h"
|
2016-06-20 12:14:17 +00:00
|
|
|
#include "base/files/file_util.h"
|
2024-04-19 13:43:01 +00:00
|
|
|
#include "base/functional/bind.h"
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/callback.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/mac/scoped_aedesc.h"
|
2016-11-17 02:22:09 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2024-04-19 13:43:01 +00:00
|
|
|
#include "base/task/thread_pool.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2024-02-14 17:33:32 +00:00
|
|
|
#include "net/base/apple/url_conversions.h"
|
2021-09-15 00:40:36 +00:00
|
|
|
#include "ui/views/widget/widget.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "url/gurl.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
|
2016-10-25 22:19:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-11-17 02:22:09 +00:00
|
|
|
// 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) {
|
2023-08-04 08:47:29 +00:00
|
|
|
CFURLRef cf_url = (__bridge CFURLRef)(ns_url);
|
2023-07-16 14:14:43 +00:00
|
|
|
CFURLRef ref =
|
|
|
|
LSCopyDefaultApplicationURLForURL(cf_url, kLSRolesAll, nullptr);
|
2016-11-17 02:22:09 +00:00
|
|
|
|
2023-10-03 19:26:35 +00:00
|
|
|
// If no application could be found, nullptr is returned and outError
|
|
|
|
// (if not nullptr) is populated with kLSApplicationNotFoundErr.
|
|
|
|
if (ref == nullptr)
|
2019-01-02 23:20:32 +00:00
|
|
|
return "No application in the Launch Services database matches the input "
|
|
|
|
"criteria.";
|
2016-10-25 22:19:34 +00:00
|
|
|
|
|
|
|
NSUInteger launchOptions = NSWorkspaceLaunchDefault;
|
|
|
|
if (!activate)
|
|
|
|
launchOptions |= NSWorkspaceLaunchWithoutActivation;
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
bool opened = [[NSWorkspace sharedWorkspace] openURLs:@[ ns_url ]
|
|
|
|
withAppBundleIdentifier:nil
|
|
|
|
options:launchOptions
|
|
|
|
additionalEventParamDescriptor:nil
|
|
|
|
launchIdentifiers:nil];
|
2016-11-17 02:22:09 +00:00
|
|
|
if (!opened)
|
|
|
|
return "Failed to open URL";
|
2016-11-02 23:20:26 +00:00
|
|
|
|
2016-11-17 02:22:09 +00:00
|
|
|
return "";
|
2016-10-25 22:19:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 07:28:29 +00:00
|
|
|
NSString* GetLoginHelperBundleIdentifier() {
|
2018-04-20 18:47:04 +00:00
|
|
|
return [[[NSBundle mainBundle] bundleIdentifier]
|
|
|
|
stringByAppendingString:@".loginhelper"];
|
2017-10-17 07:28:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 07:08:43 +00:00
|
|
|
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
|
2023-10-03 19:26:35 +00:00
|
|
|
launchIdentifiers:nil];
|
2019-11-08 07:08:43 +00:00
|
|
|
|
|
|
|
return success ? "" : "Failed to open path";
|
|
|
|
}
|
|
|
|
|
2023-10-16 16:25:11 +00:00
|
|
|
// 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:
|
2024-06-18 08:51:37 +00:00
|
|
|
return base::SysNSStringToUTF8([error localizedDescription]);
|
2023-10-16 16:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
2024-06-07 09:49:00 +00:00
|
|
|
} else if (type == "loginItemService") {
|
2023-10-16 16:25:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:19:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-04-29 12:41:11 +00:00
|
|
|
namespace platform_util {
|
|
|
|
|
2019-02-27 12:58:23 +00:00
|
|
|
void ShowItemInFolder(const base::FilePath& path) {
|
2016-06-20 12:14:17 +00:00
|
|
|
// The API only takes absolute path.
|
|
|
|
base::FilePath full_path =
|
|
|
|
path.IsAbsolute() ? path : base::MakeAbsoluteFilePath(path);
|
|
|
|
|
2013-04-29 12:41:11 +00:00
|
|
|
DCHECK([NSThread isMainThread]);
|
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
|
|
|
if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
|
2016-09-12 21:22:29 +00:00
|
|
|
inFileViewerRootedAtPath:@""]) {
|
2013-04-29 12:41:11 +00:00
|
|
|
LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
|
2016-09-12 21:22:29 +00:00
|
|
|
}
|
2013-04-29 12:41:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 07:08:43 +00:00
|
|
|
void OpenPath(const base::FilePath& full_path, OpenCallback callback) {
|
|
|
|
std::move(callback).Run(OpenPathOnThread(full_path));
|
2013-04-29 12:41:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
void OpenExternal(const GURL& url,
|
2018-10-10 20:46:54 +00:00
|
|
|
const OpenExternalOptions& options,
|
2019-11-08 07:08:43 +00:00
|
|
|
OpenCallback callback) {
|
2019-05-03 20:53:45 +00:00
|
|
|
DCHECK([NSThread isMainThread]);
|
2016-11-02 23:20:26 +00:00
|
|
|
NSURL* ns_url = net::NSURLWithGURL(url);
|
|
|
|
if (!ns_url) {
|
2019-02-21 12:32:44 +00:00
|
|
|
std::move(callback).Run("Invalid URL");
|
2016-11-02 23:20:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-19 13:43:01 +00:00
|
|
|
base::ThreadPool::PostTaskAndReplyWithResult(
|
|
|
|
FROM_HERE,
|
|
|
|
{base::MayBlock(), base::WithBaseSyncPrimitives(),
|
|
|
|
base::TaskPriority::USER_BLOCKING,
|
|
|
|
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
|
|
|
|
base::BindOnce(&OpenURL, ns_url, options.activate), std::move(callback));
|
2013-04-29 12:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 17:32:33 +00:00
|
|
|
bool MoveItemToTrashWithError(const base::FilePath& full_path,
|
|
|
|
bool delete_on_fail,
|
|
|
|
std::string* error) {
|
2013-04-29 13:57:05 +00:00
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
2019-08-13 19:31:53 +00:00
|
|
|
if (!path_string) {
|
2020-09-02 17:32:33 +00:00
|
|
|
*error = "Invalid file path: " + full_path.value();
|
|
|
|
LOG(WARNING) << *error;
|
2019-08-13 19:31:53 +00:00
|
|
|
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
|
2020-05-18 02:18:34 +00:00
|
|
|
error:&err];
|
2019-08-13 19:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 02:18:34 +00:00
|
|
|
if (!did_trash) {
|
2020-09-02 17:32:33 +00:00
|
|
|
*error = base::SysNSStringToUTF8([err localizedDescription]);
|
2013-04-29 13:57:05 +00:00
|
|
|
LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
|
2020-09-02 17:32:33 +00:00
|
|
|
<< " to trash: " << *error;
|
2020-05-18 02:18:34 +00:00
|
|
|
}
|
2019-08-13 19:31:53 +00:00
|
|
|
|
|
|
|
return did_trash;
|
2013-04-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 17:32:33 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
bool PlatformTrashItem(const base::FilePath& full_path, std::string* error) {
|
|
|
|
return MoveItemToTrashWithError(full_path, false, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2013-04-29 14:10:03 +00:00
|
|
|
void Beep() {
|
|
|
|
NSBeep();
|
|
|
|
}
|
|
|
|
|
2023-10-16 16:25:11 +00:00
|
|
|
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";
|
2017-10-17 07:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-16 16:25:11 +00:00
|
|
|
return enabled ? "enabled" : "not-registered";
|
2017-10-17 07:28:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 16:25:11 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-10-17 07:28:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 12:41:11 +00:00
|
|
|
} // namespace platform_util
|