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> | 7 #include <Carbon/Carbon.h> |
8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
9 #include <CoreServices/CoreServices.h> | 9 #include <CoreServices/CoreServices.h> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/mac/mac_logging.h" | 13 #include "base/mac/mac_logging.h" |
| 14 #import "base/mac/mac_util.h" |
| 15 #import "base/mac/sdk_forward_declarations.h" |
14 #include "base/mac/scoped_aedesc.h" | 16 #include "base/mac/scoped_aedesc.h" |
15 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 namespace platform_util { | 20 namespace platform_util { |
19 | 21 |
20 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { | 22 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { |
21 DCHECK([NSThread isMainThread]); | 23 DCHECK([NSThread isMainThread]); |
22 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 24 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
23 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string | 25 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string |
24 inFileViewerRootedAtPath:nil]) | 26 inFileViewerRootedAtPath:nil]) |
25 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); | 27 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); |
26 } | 28 } |
27 | 29 |
28 // This function opens a file. This doesn't use LaunchServices or NSWorkspace | |
29 // because of two bugs: | |
30 // 1. Incorrect app activation with com.apple.quarantine: | |
31 // http://crbug.com/32921 | |
32 // 2. Silent no-op for unassociated file types: http://crbug.com/50263 | |
33 // Instead, an AppleEvent is constructed to tell the Finder to open the | |
34 // document. | |
35 void OpenItem(Profile* profile, const base::FilePath& full_path) { | 30 void OpenItem(Profile* profile, const base::FilePath& full_path) { |
36 DCHECK([NSThread isMainThread]); | 31 DCHECK([NSThread isMainThread]); |
37 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 32 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
38 if (!path_string) | 33 if (!path_string) |
39 return; | 34 return; |
40 | 35 |
| 36 // On Mavericks or later, NSWorkspaceLaunchWithErrorPresentation will |
| 37 // properly handle Finder activation for quarantined files |
| 38 // (http://crbug.com/32921) and unassociated file types |
| 39 // (http://crbug.com/50263). |
| 40 if (base::mac::IsOSMavericksOrLater()) { |
| 41 NSURL* url = [NSURL fileURLWithPath:path_string]; |
| 42 if (!url) |
| 43 return; |
| 44 |
| 45 const NSWorkspaceLaunchOptions launch_options = |
| 46 NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation; |
| 47 [[NSWorkspace sharedWorkspace] openURLs:@[ url ] |
| 48 withAppBundleIdentifier:nil |
| 49 options:launch_options |
| 50 additionalEventParamDescriptor:nil |
| 51 launchIdentifiers:NULL]; |
| 52 return; |
| 53 } |
| 54 |
| 55 // On older OSes, both LaunchServices and NSWorkspace will fail silently for |
| 56 // the two cases described above. On those platforms, use an AppleEvent to |
| 57 // instruct the Finder to open the file. |
| 58 |
41 // Create the target of this AppleEvent, the Finder. | 59 // Create the target of this AppleEvent, the Finder. |
42 base::mac::ScopedAEDesc<AEAddressDesc> address; | 60 base::mac::ScopedAEDesc<AEAddressDesc> address; |
43 const OSType finderCreatorCode = 'MACS'; | 61 const OSType finderCreatorCode = 'MACS'; |
44 OSErr status = AECreateDesc(typeApplSignature, // type | 62 OSErr status = AECreateDesc(typeApplSignature, // type |
45 &finderCreatorCode, // data | 63 &finderCreatorCode, // data |
46 sizeof(finderCreatorCode), // dataSize | 64 sizeof(finderCreatorCode), // dataSize |
47 address.OutPointer()); // result | 65 address.OutPointer()); // result |
48 if (status != noErr) { | 66 if (status != noErr) { |
49 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target"; | 67 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target"; |
50 return; | 68 return; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 [[view window] isVisible]); | 169 [[view window] isVisible]); |
152 } | 170 } |
153 | 171 |
154 bool IsSwipeTrackingFromScrollEventsEnabled() { | 172 bool IsSwipeTrackingFromScrollEventsEnabled() { |
155 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); | 173 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); |
156 return [NSEvent respondsToSelector:selector] | 174 return [NSEvent respondsToSelector:selector] |
157 && [NSEvent performSelector:selector]; | 175 && [NSEvent performSelector:selector]; |
158 } | 176 } |
159 | 177 |
160 } // namespace platform_util | 178 } // namespace platform_util |
OLD | NEW |