| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/platform_util.h" | 5 #include "chrome/browser/platform_util.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | |
| 8 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 9 #include <CoreServices/CoreServices.h> | |
| 10 | 8 |
| 11 #include "base/bind.h" | 9 #include "base/bind.h" |
| 12 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 14 #include "base/logging.h" | 12 #include "base/logging.h" |
| 15 #include "base/mac/mac_logging.h" | 13 #include "base/mac/mac_logging.h" |
| 16 #import "base/mac/mac_util.h" | |
| 17 #include "base/mac/scoped_aedesc.h" | |
| 18 #import "base/mac/sdk_forward_declarations.h" | 14 #import "base/mac/sdk_forward_declarations.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 20 #include "chrome/browser/platform_util_internal.h" | 16 #include "chrome/browser/platform_util_internal.h" |
| 21 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 22 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 23 | 19 |
| 24 namespace platform_util { | 20 namespace platform_util { |
| 25 | 21 |
| 26 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { | 22 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { |
| 27 DCHECK([NSThread isMainThread]); | 23 DCHECK([NSThread isMainThread]); |
| 28 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 24 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
| 29 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string | 25 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string |
| 30 inFileViewerRootedAtPath:@""]) | 26 inFileViewerRootedAtPath:@""]) |
| 31 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); | 27 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); |
| 32 } | 28 } |
| 33 | 29 |
| 34 void OpenFileOnMainThread(const base::FilePath& full_path) { | 30 void OpenFileOnMainThread(const base::FilePath& full_path) { |
| 35 DCHECK([NSThread isMainThread]); | 31 DCHECK([NSThread isMainThread]); |
| 36 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 32 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
| 37 if (!path_string) | 33 if (!path_string) |
| 38 return; | 34 return; |
| 39 | 35 |
| 40 // On Mavericks or later, NSWorkspaceLaunchWithErrorPresentation will | 36 // On Mavericks or later, NSWorkspaceLaunchWithErrorPresentation will |
| 41 // properly handle Finder activation for quarantined files | 37 // properly handle Finder activation for quarantined files |
| 42 // (http://crbug.com/32921) and unassociated file types | 38 // (http://crbug.com/32921) and unassociated file types |
| 43 // (http://crbug.com/50263). | 39 // (http://crbug.com/50263). |
| 44 if (base::mac::IsOSMavericksOrLater()) { | 40 NSURL* url = [NSURL fileURLWithPath:path_string]; |
| 45 NSURL* url = [NSURL fileURLWithPath:path_string]; | 41 if (!url) |
| 46 if (!url) | 42 return; |
| 47 return; | |
| 48 | 43 |
| 49 const NSWorkspaceLaunchOptions launch_options = | 44 const NSWorkspaceLaunchOptions launch_options = |
| 50 NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation; | 45 NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation; |
| 51 [[NSWorkspace sharedWorkspace] openURLs:@[ url ] | 46 [[NSWorkspace sharedWorkspace] openURLs:@[ url ] |
| 52 withAppBundleIdentifier:nil | 47 withAppBundleIdentifier:nil |
| 53 options:launch_options | 48 options:launch_options |
| 54 additionalEventParamDescriptor:nil | 49 additionalEventParamDescriptor:nil |
| 55 launchIdentifiers:NULL]; | 50 launchIdentifiers:NULL]; |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 // On older OSes, both LaunchServices and NSWorkspace will fail silently for | |
| 60 // the two cases described above. On those platforms, use an AppleEvent to | |
| 61 // instruct the Finder to open the file. | |
| 62 | |
| 63 // Create the target of this AppleEvent, the Finder. | |
| 64 base::mac::ScopedAEDesc<AEAddressDesc> address; | |
| 65 const OSType finderCreatorCode = 'MACS'; | |
| 66 OSErr status = AECreateDesc(typeApplSignature, // type | |
| 67 &finderCreatorCode, // data | |
| 68 sizeof(finderCreatorCode), // dataSize | |
| 69 address.OutPointer()); // result | |
| 70 if (status != noErr) { | |
| 71 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE target"; | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 // Build the AppleEvent data structure that instructs Finder to open files. | |
| 76 base::mac::ScopedAEDesc<AppleEvent> theEvent; | |
| 77 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass | |
| 78 kAEOpenDocuments, // theAEEventID | |
| 79 address, // target | |
| 80 kAutoGenerateReturnID, // returnID | |
| 81 kAnyTransactionID, // transactionID | |
| 82 theEvent.OutPointer()); // result | |
| 83 if (status != noErr) { | |
| 84 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE event"; | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 // Create the list of files (only ever one) to open. | |
| 89 base::mac::ScopedAEDesc<AEDescList> fileList; | |
| 90 status = AECreateList(NULL, // factoringPtr | |
| 91 0, // factoredSize | |
| 92 false, // isRecord | |
| 93 fileList.OutPointer()); // resultList | |
| 94 if (status != noErr) { | |
| 95 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE file list"; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 // Add the single path to the file list. C-style cast to avoid both a | |
| 100 // static_cast and a const_cast to get across the toll-free bridge. | |
| 101 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string]; | |
| 102 FSRef pathRef; | |
| 103 if (CFURLGetFSRef(pathURLRef, &pathRef)) { | |
| 104 status = AEPutPtr(fileList.OutPointer(), // theAEDescList | |
| 105 0, // index | |
| 106 typeFSRef, // typeCode | |
| 107 &pathRef, // dataPtr | |
| 108 sizeof(pathRef)); // dataSize | |
| 109 if (status != noErr) { | |
| 110 OSSTATUS_LOG(WARNING, status) | |
| 111 << "Could not add file path to AE list in OpenFile()"; | |
| 112 return; | |
| 113 } | |
| 114 } else { | |
| 115 LOG(WARNING) << "Could not get FSRef for path URL in OpenFile()"; | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 // Attach the file list to the AppleEvent. | |
| 120 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent | |
| 121 keyDirectObject, // theAEKeyword | |
| 122 fileList); // theAEDesc | |
| 123 if (status != noErr) { | |
| 124 OSSTATUS_LOG(WARNING, status) | |
| 125 << "Could not put the AE file list the path in OpenFile()"; | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 // Send the actual event. Do not care about the reply. | |
| 130 base::mac::ScopedAEDesc<AppleEvent> reply; | |
| 131 status = AESend(theEvent, // theAppleEvent | |
| 132 reply.OutPointer(), // reply | |
| 133 kAENoReply + kAEAlwaysInteract, // sendMode | |
| 134 kAENormalPriority, // sendPriority | |
| 135 kAEDefaultTimeout, // timeOutInTicks | |
| 136 NULL, // idleProc | |
| 137 NULL); // filterProc | |
| 138 if (status != noErr) { | |
| 139 OSSTATUS_LOG(WARNING, status) | |
| 140 << "Could not send AE to Finder in OpenFile()"; | |
| 141 } | |
| 142 } | 51 } |
| 143 | 52 |
| 144 namespace internal { | 53 namespace internal { |
| 145 | 54 |
| 146 void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) { | 55 void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) { |
| 147 switch (type) { | 56 switch (type) { |
| 148 case OPEN_FILE: | 57 case OPEN_FILE: |
| 149 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 58 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 150 base::Bind(&OpenFileOnMainThread, path)); | 59 base::Bind(&OpenFileOnMainThread, path)); |
| 151 return; | 60 return; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 [[view window] isVisible]); | 110 [[view window] isVisible]); |
| 202 } | 111 } |
| 203 | 112 |
| 204 bool IsSwipeTrackingFromScrollEventsEnabled() { | 113 bool IsSwipeTrackingFromScrollEventsEnabled() { |
| 205 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); | 114 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); |
| 206 return [NSEvent respondsToSelector:selector] | 115 return [NSEvent respondsToSelector:selector] |
| 207 && [NSEvent performSelector:selector]; | 116 && [NSEvent performSelector:selector]; |
| 208 } | 117 } |
| 209 | 118 |
| 210 } // namespace platform_util | 119 } // namespace platform_util |
| OLD | NEW |