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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/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
|
|
|
|
2016-11-17 02:22:09 +00:00
|
|
|
#include "base/callback.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"
|
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"
|
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) {
|
2019-02-25 17:21:57 +00:00
|
|
|
CFURLRef ref = LSCopyDefaultApplicationURLForURL(
|
|
|
|
base::mac::NSToCFCast(ns_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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-12 21:29:06 +00:00
|
|
|
bool OpenItem(const base::FilePath& full_path) {
|
2013-04-29 12:41:11 +00:00
|
|
|
DCHECK([NSThread isMainThread]);
|
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
|
|
|
if (!path_string)
|
2016-09-12 21:29:06 +00:00
|
|
|
return false;
|
2013-04-29 12:41:11 +00:00
|
|
|
|
2017-09-19 22:15:37 +00:00
|
|
|
NSURL* url = [NSURL fileURLWithPath:path_string];
|
|
|
|
if (!url)
|
2016-09-12 21:29:06 +00:00
|
|
|
return false;
|
2013-04-29 12:41:11 +00:00
|
|
|
|
2017-09-19 22:15:37 +00:00
|
|
|
const NSWorkspaceLaunchOptions launch_options =
|
|
|
|
NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation;
|
|
|
|
return [[NSWorkspace sharedWorkspace] openURLs:@[ url ]
|
2018-04-20 18:47:04 +00:00
|
|
|
withAppBundleIdentifier:nil
|
|
|
|
options:launch_options
|
|
|
|
additionalEventParamDescriptor:nil
|
|
|
|
launchIdentifiers:NULL];
|
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-02-21 12:32:44 +00:00
|
|
|
OpenExternalCallback 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-02-21 12:32:44 +00:00
|
|
|
__block OpenExternalCallback 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
|
|
|
}
|
|
|
|
|
2015-03-27 13:01:57 +00:00
|
|
|
bool MoveItemToTrash(const base::FilePath& full_path) {
|
2013-04-29 13:57:05 +00:00
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
2015-06-18 05:09:02 +00:00
|
|
|
BOOL status = [[NSFileManager defaultManager]
|
2018-04-20 18:47:04 +00:00
|
|
|
trashItemAtURL:[NSURL fileURLWithPath:path_string]
|
|
|
|
resultingItemURL:nil
|
|
|
|
error:nil];
|
2015-06-18 05:09:02 +00:00
|
|
|
if (!path_string || !status)
|
2013-04-29 13:57:05 +00:00
|
|
|
LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
|
|
|
|
<< " to trash";
|
2015-04-08 14:01:46 +00:00
|
|
|
return status;
|
2013-04-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
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
|