| 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/bind.h" |
| 12 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/mac/mac_logging.h" | 15 #include "base/mac/mac_logging.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" |
| 18 #include "content/public/browser/browser_thread.h" |
| 16 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 17 | 20 |
| 18 namespace platform_util { | 21 namespace platform_util { |
| 19 | 22 |
| 20 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { | 23 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { |
| 21 DCHECK([NSThread isMainThread]); | 24 DCHECK([NSThread isMainThread]); |
| 22 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 25 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
| 23 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string | 26 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string |
| 24 inFileViewerRootedAtPath:nil]) | 27 inFileViewerRootedAtPath:nil]) |
| 25 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); | 28 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); |
| 26 } | 29 } |
| 27 | 30 |
| 28 // This function opens a file. This doesn't use LaunchServices or NSWorkspace | 31 // This function opens a file. This doesn't use LaunchServices or NSWorkspace |
| 29 // because of two bugs: | 32 // because of two bugs: |
| 30 // 1. Incorrect app activation with com.apple.quarantine: | 33 // 1. Incorrect app activation with com.apple.quarantine: |
| 31 // http://crbug.com/32921 | 34 // http://crbug.com/32921 |
| 32 // 2. Silent no-op for unassociated file types: http://crbug.com/50263 | 35 // 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 | 36 // Instead, an AppleEvent is constructed to tell the Finder to open the |
| 34 // document. | 37 // document. |
| 35 void OpenItem(Profile* profile, const base::FilePath& full_path) { | 38 void OpenFile(Profile* profile, const base::FilePath& full_path) { |
| 36 DCHECK([NSThread isMainThread]); | 39 DCHECK([NSThread isMainThread]); |
| 37 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 40 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
| 38 if (!path_string) | 41 if (!path_string) |
| 39 return; | 42 return; |
| 40 | 43 |
| 41 // Create the target of this AppleEvent, the Finder. | 44 // Create the target of this AppleEvent, the Finder. |
| 42 base::mac::ScopedAEDesc<AEAddressDesc> address; | 45 base::mac::ScopedAEDesc<AEAddressDesc> address; |
| 43 const OSType finderCreatorCode = 'MACS'; | 46 const OSType finderCreatorCode = 'MACS'; |
| 44 OSErr status = AECreateDesc(typeApplSignature, // type | 47 OSErr status = AECreateDesc(typeApplSignature, // type |
| 45 &finderCreatorCode, // data | 48 &finderCreatorCode, // data |
| 46 sizeof(finderCreatorCode), // dataSize | 49 sizeof(finderCreatorCode), // dataSize |
| 47 address.OutPointer()); // result | 50 address.OutPointer()); // result |
| 48 if (status != noErr) { | 51 if (status != noErr) { |
| 49 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target"; | 52 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE target"; |
| 50 return; | 53 return; |
| 51 } | 54 } |
| 52 | 55 |
| 53 // Build the AppleEvent data structure that instructs Finder to open files. | 56 // Build the AppleEvent data structure that instructs Finder to open files. |
| 54 base::mac::ScopedAEDesc<AppleEvent> theEvent; | 57 base::mac::ScopedAEDesc<AppleEvent> theEvent; |
| 55 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass | 58 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass |
| 56 kAEOpenDocuments, // theAEEventID | 59 kAEOpenDocuments, // theAEEventID |
| 57 address, // target | 60 address, // target |
| 58 kAutoGenerateReturnID, // returnID | 61 kAutoGenerateReturnID, // returnID |
| 59 kAnyTransactionID, // transactionID | 62 kAnyTransactionID, // transactionID |
| 60 theEvent.OutPointer()); // result | 63 theEvent.OutPointer()); // result |
| 61 if (status != noErr) { | 64 if (status != noErr) { |
| 62 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE event"; | 65 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE event"; |
| 63 return; | 66 return; |
| 64 } | 67 } |
| 65 | 68 |
| 66 // Create the list of files (only ever one) to open. | 69 // Create the list of files (only ever one) to open. |
| 67 base::mac::ScopedAEDesc<AEDescList> fileList; | 70 base::mac::ScopedAEDesc<AEDescList> fileList; |
| 68 status = AECreateList(NULL, // factoringPtr | 71 status = AECreateList(NULL, // factoringPtr |
| 69 0, // factoredSize | 72 0, // factoredSize |
| 70 false, // isRecord | 73 false, // isRecord |
| 71 fileList.OutPointer()); // resultList | 74 fileList.OutPointer()); // resultList |
| 72 if (status != noErr) { | 75 if (status != noErr) { |
| 73 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list"; | 76 OSSTATUS_LOG(WARNING, status) << "Could not create OpenFile() AE file list"; |
| 74 return; | 77 return; |
| 75 } | 78 } |
| 76 | 79 |
| 77 // Add the single path to the file list. C-style cast to avoid both a | 80 // Add the single path to the file list. C-style cast to avoid both a |
| 78 // static_cast and a const_cast to get across the toll-free bridge. | 81 // static_cast and a const_cast to get across the toll-free bridge. |
| 79 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string]; | 82 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string]; |
| 80 FSRef pathRef; | 83 FSRef pathRef; |
| 81 if (CFURLGetFSRef(pathURLRef, &pathRef)) { | 84 if (CFURLGetFSRef(pathURLRef, &pathRef)) { |
| 82 status = AEPutPtr(fileList.OutPointer(), // theAEDescList | 85 status = AEPutPtr(fileList.OutPointer(), // theAEDescList |
| 83 0, // index | 86 0, // index |
| 84 typeFSRef, // typeCode | 87 typeFSRef, // typeCode |
| 85 &pathRef, // dataPtr | 88 &pathRef, // dataPtr |
| 86 sizeof(pathRef)); // dataSize | 89 sizeof(pathRef)); // dataSize |
| 87 if (status != noErr) { | 90 if (status != noErr) { |
| 88 OSSTATUS_LOG(WARNING, status) | 91 OSSTATUS_LOG(WARNING, status) |
| 89 << "Could not add file path to AE list in OpenItem()"; | 92 << "Could not add file path to AE list in OpenFile()"; |
| 90 return; | 93 return; |
| 91 } | 94 } |
| 92 } else { | 95 } else { |
| 93 LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()"; | 96 LOG(WARNING) << "Could not get FSRef for path URL in OpenFile()"; |
| 94 return; | 97 return; |
| 95 } | 98 } |
| 96 | 99 |
| 97 // Attach the file list to the AppleEvent. | 100 // Attach the file list to the AppleEvent. |
| 98 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent | 101 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent |
| 99 keyDirectObject, // theAEKeyword | 102 keyDirectObject, // theAEKeyword |
| 100 fileList); // theAEDesc | 103 fileList); // theAEDesc |
| 101 if (status != noErr) { | 104 if (status != noErr) { |
| 102 OSSTATUS_LOG(WARNING, status) | 105 OSSTATUS_LOG(WARNING, status) |
| 103 << "Could not put the AE file list the path in OpenItem()"; | 106 << "Could not put the AE file list the path in OpenFile()"; |
| 104 return; | 107 return; |
| 105 } | 108 } |
| 106 | 109 |
| 107 // Send the actual event. Do not care about the reply. | 110 // Send the actual event. Do not care about the reply. |
| 108 base::mac::ScopedAEDesc<AppleEvent> reply; | 111 base::mac::ScopedAEDesc<AppleEvent> reply; |
| 109 status = AESend(theEvent, // theAppleEvent | 112 status = AESend(theEvent, // theAppleEvent |
| 110 reply.OutPointer(), // reply | 113 reply.OutPointer(), // reply |
| 111 kAENoReply + kAEAlwaysInteract, // sendMode | 114 kAENoReply + kAEAlwaysInteract, // sendMode |
| 112 kAENormalPriority, // sendPriority | 115 kAENormalPriority, // sendPriority |
| 113 kAEDefaultTimeout, // timeOutInTicks | 116 kAEDefaultTimeout, // timeOutInTicks |
| 114 NULL, // idleProc | 117 NULL, // idleProc |
| 115 NULL); // filterProc | 118 NULL); // filterProc |
| 116 if (status != noErr) { | 119 if (status != noErr) { |
| 117 OSSTATUS_LOG(WARNING, status) | 120 OSSTATUS_LOG(WARNING, status) |
| 118 << "Could not send AE to Finder in OpenItem()"; | 121 << "Could not send AE to Finder in OpenFile()"; |
| 119 } | 122 } |
| 120 } | 123 } |
| 121 | 124 |
| 125 namespace { |
| 126 |
| 127 void OpenFolderOnBlockingThread(const base::FilePath& full_path) { |
| 128 if (!base::DirectoryExists(full_path)) |
| 129 return; |
| 130 NSString* path = base::SysUTF8ToNSString(full_path.value()); |
| 131 if (!path) |
| 132 return; |
| 133 [[NSWorkspace sharedWorkspace] openFile:path withApplication:@"Finder"]; |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 |
| 138 void OpenFolder(Profile* profile, const base::FilePath& full_path) { |
| 139 content::BrowserThread::PostBlockingPoolTask( |
| 140 FROM_HERE, base::Bind(&OpenFolderOnBlockingThread, full_path)); |
| 141 } |
| 142 |
| 122 void OpenExternal(Profile* profile, const GURL& url) { | 143 void OpenExternal(Profile* profile, const GURL& url) { |
| 123 DCHECK([NSThread isMainThread]); | 144 DCHECK([NSThread isMainThread]); |
| 124 NSString* url_string = base::SysUTF8ToNSString(url.spec()); | 145 NSString* url_string = base::SysUTF8ToNSString(url.spec()); |
| 125 NSURL* ns_url = [NSURL URLWithString:url_string]; | 146 NSURL* ns_url = [NSURL URLWithString:url_string]; |
| 126 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url]) | 147 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url]) |
| 127 LOG(WARNING) << "NSWorkspace failed to open URL " << url; | 148 LOG(WARNING) << "NSWorkspace failed to open URL " << url; |
| 128 } | 149 } |
| 129 | 150 |
| 130 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { | 151 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { |
| 131 return [view window]; | 152 return [view window]; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 [[view window] isVisible]); | 172 [[view window] isVisible]); |
| 152 } | 173 } |
| 153 | 174 |
| 154 bool IsSwipeTrackingFromScrollEventsEnabled() { | 175 bool IsSwipeTrackingFromScrollEventsEnabled() { |
| 155 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); | 176 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); |
| 156 return [NSEvent respondsToSelector:selector] | 177 return [NSEvent respondsToSelector:selector] |
| 157 && [NSEvent performSelector:selector]; | 178 && [NSEvent performSelector:selector]; |
| 158 } | 179 } |
| 159 | 180 |
| 160 } // namespace platform_util | 181 } // namespace platform_util |
| OLD | NEW |