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
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
2016-06-20 12:14:17 +00:00
|
|
|
#include "base/files/file_util.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"
|
2017-03-30 20:33:09 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
#include "base/mac/mac_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"
|
2015-12-24 04:43:07 +00:00
|
|
|
#include "net/base/mac/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-07-16 14:14:43 +00:00
|
|
|
CFURLRef cf_url = reinterpret_cast<CFURLRef>(ns_url);
|
|
|
|
CFURLRef ref =
|
|
|
|
LSCopyDefaultApplicationURLForURL(cf_url, kLSRolesAll, nullptr);
|
2016-11-17 02:22:09 +00:00
|
|
|
|
2019-01-02 23:20:32 +00:00
|
|
|
// If no application could be found, NULL is returned and outError
|
|
|
|
// (if not NULL) is populated with kLSApplicationNotFoundErr.
|
|
|
|
if (ref == NULL)
|
|
|
|
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
|
|
|
|
launchIdentifiers:NULL];
|
|
|
|
|
|
|
|
return success ? "" : "Failed to open path";
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:40:05 +00:00
|
|
|
bool activate = options.activate;
|
2019-11-08 07:08:43 +00:00
|
|
|
__block OpenCallback c = std::move(callback);
|
2019-06-10 14:40:05 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-17 07:28:29 +00:00
|
|
|
bool GetLoginItemEnabled() {
|
|
|
|
BOOL enabled = NO;
|
2019-02-04 17:07:51 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2017-10-17 07:28:29 +00:00
|
|
|
// SMJobCopyDictionary does not work in sandbox (see rdar://13626319)
|
|
|
|
CFArrayRef jobs = SMCopyAllJobDictionaries(kSMDomainUserLaunchd);
|
2019-02-04 17:07:51 +00:00
|
|
|
#pragma clang diagnostic pop
|
2017-10-17 07:28:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-28 04:16:53 +00:00
|
|
|
bool SetLoginItemEnabled(bool enabled) {
|
2017-10-17 07:28:29 +00:00
|
|
|
NSString* identifier = GetLoginHelperBundleIdentifier();
|
2018-11-28 04:16:53 +00:00
|
|
|
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
|