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
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/mac/mac_logging.h"
|
|
|
|
#include "base/mac/scoped_aedesc.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "url/gurl.h"
|
2013-04-29 12:41:11 +00:00
|
|
|
|
|
|
|
namespace platform_util {
|
|
|
|
|
|
|
|
void ShowItemInFolder(const base::FilePath& full_path) {
|
|
|
|
DCHECK([NSThread isMainThread]);
|
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
|
|
|
if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
|
2015-10-08 18:25:05 +00:00
|
|
|
inFileViewerRootedAtPath:@""])
|
2013-04-29 12:41:11 +00:00
|
|
|
LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function opens a file. This doesn't use LaunchServices or NSWorkspace
|
|
|
|
// because of two bugs:
|
|
|
|
// 1. Incorrect app activation with com.apple.quarantine:
|
|
|
|
// http://crbug.com/32921
|
|
|
|
// 2. Silent no-op for unassociated file types: http://crbug.com/50263
|
|
|
|
// Instead, an AppleEvent is constructed to tell the Finder to open the
|
|
|
|
// document.
|
|
|
|
void OpenItem(const base::FilePath& full_path) {
|
|
|
|
DCHECK([NSThread isMainThread]);
|
|
|
|
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
|
|
|
|
if (!path_string)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create the target of this AppleEvent, the Finder.
|
|
|
|
base::mac::ScopedAEDesc<AEAddressDesc> address;
|
|
|
|
const OSType finderCreatorCode = 'MACS';
|
|
|
|
OSErr status = AECreateDesc(typeApplSignature, // type
|
|
|
|
&finderCreatorCode, // data
|
|
|
|
sizeof(finderCreatorCode), // dataSize
|
|
|
|
address.OutPointer()); // result
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the AppleEvent data structure that instructs Finder to open files.
|
|
|
|
base::mac::ScopedAEDesc<AppleEvent> theEvent;
|
|
|
|
status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass
|
|
|
|
kAEOpenDocuments, // theAEEventID
|
|
|
|
address, // target
|
|
|
|
kAutoGenerateReturnID, // returnID
|
|
|
|
kAnyTransactionID, // transactionID
|
|
|
|
theEvent.OutPointer()); // result
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE event";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the list of files (only ever one) to open.
|
|
|
|
base::mac::ScopedAEDesc<AEDescList> fileList;
|
|
|
|
status = AECreateList(NULL, // factoringPtr
|
|
|
|
0, // factoredSize
|
|
|
|
false, // isRecord
|
|
|
|
fileList.OutPointer()); // resultList
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the single path to the file list. C-style cast to avoid both a
|
|
|
|
// static_cast and a const_cast to get across the toll-free bridge.
|
|
|
|
CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string];
|
|
|
|
FSRef pathRef;
|
|
|
|
if (CFURLGetFSRef(pathURLRef, &pathRef)) {
|
|
|
|
status = AEPutPtr(fileList.OutPointer(), // theAEDescList
|
|
|
|
0, // index
|
|
|
|
typeFSRef, // typeCode
|
|
|
|
&pathRef, // dataPtr
|
|
|
|
sizeof(pathRef)); // dataSize
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status)
|
|
|
|
<< "Could not add file path to AE list in OpenItem()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the file list to the AppleEvent.
|
|
|
|
status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent
|
|
|
|
keyDirectObject, // theAEKeyword
|
|
|
|
fileList); // theAEDesc
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status)
|
|
|
|
<< "Could not put the AE file list the path in OpenItem()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the actual event. Do not care about the reply.
|
|
|
|
base::mac::ScopedAEDesc<AppleEvent> reply;
|
|
|
|
status = AESend(theEvent, // theAppleEvent
|
|
|
|
reply.OutPointer(), // reply
|
|
|
|
kAENoReply + kAEAlwaysInteract, // sendMode
|
|
|
|
kAENormalPriority, // sendPriority
|
|
|
|
kAEDefaultTimeout, // timeOutInTicks
|
|
|
|
NULL, // idleProc
|
|
|
|
NULL); // filterProc
|
|
|
|
if (status != noErr) {
|
|
|
|
OSSTATUS_LOG(WARNING, status)
|
|
|
|
<< "Could not send AE to Finder in OpenItem()";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:06:22 +00:00
|
|
|
bool OpenExternal(const GURL& url) {
|
2013-04-29 12:41:11 +00:00
|
|
|
DCHECK([NSThread isMainThread]);
|
|
|
|
NSString* url_string = base::SysUTF8ToNSString(url.spec());
|
2015-12-17 15:20:33 +00:00
|
|
|
NSString* url_escaped_string = [url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSURL* ns_url = [NSURL URLWithString:url_escaped_string];
|
2015-06-12 05:54:42 +00:00
|
|
|
if (!ns_url) {
|
2015-06-10 15:06:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-12 05:54:42 +00:00
|
|
|
|
|
|
|
CFURLRef openingApp = NULL;
|
|
|
|
OSStatus status = LSGetApplicationForURL((CFURLRef)ns_url,
|
|
|
|
kLSRolesAll,
|
|
|
|
NULL,
|
|
|
|
&openingApp);
|
|
|
|
if (status != noErr) {
|
|
|
|
return false;
|
2015-06-10 15:06:22 +00:00
|
|
|
}
|
2015-06-12 05:54:42 +00:00
|
|
|
CFRelease(openingApp); // NOT A BUG; LSGetApplicationForURL retains for us
|
|
|
|
|
|
|
|
return [[NSWorkspace sharedWorkspace] openURL:ns_url];
|
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]
|
|
|
|
trashItemAtURL:[NSURL fileURLWithPath:path_string]
|
|
|
|
resultingItemURL:nil
|
|
|
|
error:nil];
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-04-29 12:41:11 +00:00
|
|
|
} // namespace platform_util
|