update shell.OpenItem to use NSWorkspace

This commit is contained in:
Shelley Vohr 2017-09-19 18:15:37 -04:00
parent fa444dd029
commit 27fd2dad59
No known key found for this signature in database
GPG key ID: F13993A75599653C

View file

@ -117,100 +117,23 @@ bool ShowItemInFolder(const base::FilePath& path) {
return true; return true;
} }
// 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.
bool OpenItem(const base::FilePath& full_path) { bool OpenItem(const base::FilePath& full_path) {
DCHECK([NSThread isMainThread]); DCHECK([NSThread isMainThread]);
NSString* path_string = base::SysUTF8ToNSString(full_path.value()); NSString* path_string = base::SysUTF8ToNSString(full_path.value());
if (!path_string) if (!path_string)
return false; return false;
// Create the target of this AppleEvent, the Finder. NSURL* url = [NSURL fileURLWithPath:path_string];
base::mac::ScopedAEDesc<AEAddressDesc> address; if (!url)
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 false; return false;
}
// Build the AppleEvent data structure that instructs Finder to open files. const NSWorkspaceLaunchOptions launch_options =
base::mac::ScopedAEDesc<AppleEvent> theEvent; NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation;
status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass return [[NSWorkspace sharedWorkspace] openURLs:@[ url ]
kAEOpenDocuments, // theAEEventID withAppBundleIdentifier:nil
address, // target options:launch_options
kAutoGenerateReturnID, // returnID additionalEventParamDescriptor:nil
kAnyTransactionID, // transactionID launchIdentifiers:NULL];
theEvent.OutPointer()); // result
if (status != noErr) {
OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE event";
return false;
}
// Create the list of files (only ever one) to open.
base::mac::ScopedAEDesc<AEDescList> fileList;
status = AECreateList(nullptr, // factoringPtr
0, // factoredSize
false, // isRecord
fileList.OutPointer()); // resultList
if (status != noErr) {
OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list";
return false;
}
// 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 = base::mac::NSToCFCast(
[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 false;
}
} else {
LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()";
return false;
}
// 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 false;
}
// 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
nullptr, // idleProc
nullptr); // filterProc
if (status != noErr) {
OSSTATUS_LOG(WARNING, status)
<< "Could not send AE to Finder in OpenItem()";
}
return status == noErr;
} }
bool OpenExternal(const GURL& url, bool activate) { bool OpenExternal(const GURL& url, bool activate) {