OLD | NEW |
1 // Copyright (c) 2011 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/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/mac_logging.h" |
13 #include "base/mac/scoped_aedesc.h" | 14 #include "base/mac/scoped_aedesc.h" |
14 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
19 | 20 |
20 namespace platform_util { | 21 namespace platform_util { |
21 | 22 |
22 void ShowItemInFolder(const FilePath& full_path) { | 23 void ShowItemInFolder(const FilePath& full_path) { |
(...skipping 10 matching lines...) Expand all Loading... |
33 // http://crbug.com/32921 | 34 // http://crbug.com/32921 |
34 // 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 |
35 // 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 |
36 // document. | 37 // document. |
37 void OpenItem(const FilePath& full_path) { | 38 void OpenItem(const FilePath& full_path) { |
38 DCHECK([NSThread isMainThread]); | 39 DCHECK([NSThread isMainThread]); |
39 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); | 40 NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
40 if (!path_string) | 41 if (!path_string) |
41 return; | 42 return; |
42 | 43 |
43 OSErr status; | |
44 | |
45 // Create the target of this AppleEvent, the Finder. | 44 // Create the target of this AppleEvent, the Finder. |
46 base::mac::ScopedAEDesc<AEAddressDesc> address; | 45 base::mac::ScopedAEDesc<AEAddressDesc> address; |
47 const OSType finderCreatorCode = 'MACS'; | 46 const OSType finderCreatorCode = 'MACS'; |
48 status = AECreateDesc(typeApplSignature, // type | 47 OSErr status = AECreateDesc(typeApplSignature, // type |
49 &finderCreatorCode, // data | 48 &finderCreatorCode, // data |
50 sizeof(finderCreatorCode), // dataSize | 49 sizeof(finderCreatorCode), // dataSize |
51 address.OutPointer()); // result | 50 address.OutPointer()); // result |
52 if (status != noErr) { | 51 if (status != noErr) { |
53 LOG(WARNING) << "Could not create OpenItem() AE target"; | 52 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target"; |
54 return; | 53 return; |
55 } | 54 } |
56 | 55 |
57 // Build the AppleEvent data structure that instructs Finder to open files. | 56 // Build the AppleEvent data structure that instructs Finder to open files. |
58 base::mac::ScopedAEDesc<AppleEvent> theEvent; | 57 base::mac::ScopedAEDesc<AppleEvent> theEvent; |
59 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass | 58 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass |
60 kAEOpenDocuments, // theAEEventID | 59 kAEOpenDocuments, // theAEEventID |
61 address, // target | 60 address, // target |
62 kAutoGenerateReturnID, // returnID | 61 kAutoGenerateReturnID, // returnID |
63 kAnyTransactionID, // transactionID | 62 kAnyTransactionID, // transactionID |
64 theEvent.OutPointer()); // result | 63 theEvent.OutPointer()); // result |
65 if (status != noErr) { | 64 if (status != noErr) { |
66 LOG(WARNING) << "Could not create OpenItem() AE event"; | 65 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE event"; |
67 return; | 66 return; |
68 } | 67 } |
69 | 68 |
70 // Create the list of files (only ever one) to open. | 69 // Create the list of files (only ever one) to open. |
71 base::mac::ScopedAEDesc<AEDescList> fileList; | 70 base::mac::ScopedAEDesc<AEDescList> fileList; |
72 status = AECreateList(NULL, // factoringPtr | 71 status = AECreateList(NULL, // factoringPtr |
73 0, // factoredSize | 72 0, // factoredSize |
74 false, // isRecord | 73 false, // isRecord |
75 fileList.OutPointer()); // resultList | 74 fileList.OutPointer()); // resultList |
76 if (status != noErr) { | 75 if (status != noErr) { |
77 LOG(WARNING) << "Could not create OpenItem() AE file list"; | 76 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list"; |
78 return; | 77 return; |
79 } | 78 } |
80 | 79 |
81 // 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 |
82 // 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. |
83 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string]; | 82 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string]; |
84 FSRef pathRef; | 83 FSRef pathRef; |
85 if (CFURLGetFSRef(pathURLRef, &pathRef)) { | 84 if (CFURLGetFSRef(pathURLRef, &pathRef)) { |
86 status = AEPutPtr(fileList.OutPointer(), // theAEDescList | 85 status = AEPutPtr(fileList.OutPointer(), // theAEDescList |
87 0, // index | 86 0, // index |
88 typeFSRef, // typeCode | 87 typeFSRef, // typeCode |
89 &pathRef, // dataPtr | 88 &pathRef, // dataPtr |
90 sizeof(pathRef)); // dataSize | 89 sizeof(pathRef)); // dataSize |
91 if (status != noErr) { | 90 if (status != noErr) { |
92 LOG(WARNING) << "Could not add file path to AE list in OpenItem()"; | 91 OSSTATUS_LOG(WARNING, status) |
| 92 << "Could not add file path to AE list in OpenItem()"; |
93 return; | 93 return; |
94 } | 94 } |
95 } else { | 95 } else { |
96 LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()"; | 96 LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()"; |
97 return; | 97 return; |
98 } | 98 } |
99 | 99 |
100 // Attach the file list to the AppleEvent. | 100 // Attach the file list to the AppleEvent. |
101 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent | 101 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent |
102 keyDirectObject, // theAEKeyword | 102 keyDirectObject, // theAEKeyword |
103 fileList); // theAEDesc | 103 fileList); // theAEDesc |
104 if (status != noErr) { | 104 if (status != noErr) { |
105 LOG(WARNING) << "Could not put the AE file list the path in OpenItem()"; | 105 OSSTATUS_LOG(WARNING, status) |
| 106 << "Could not put the AE file list the path in OpenItem()"; |
106 return; | 107 return; |
107 } | 108 } |
108 | 109 |
109 // Send the actual event. Do not care about the reply. | 110 // Send the actual event. Do not care about the reply. |
110 base::mac::ScopedAEDesc<AppleEvent> reply; | 111 base::mac::ScopedAEDesc<AppleEvent> reply; |
111 status = AESend(theEvent, // theAppleEvent | 112 status = AESend(theEvent, // theAppleEvent |
112 reply.OutPointer(), // reply | 113 reply.OutPointer(), // reply |
113 kAENoReply + kAEAlwaysInteract, // sendMode | 114 kAENoReply + kAEAlwaysInteract, // sendMode |
114 kAENormalPriority, // sendPriority | 115 kAENormalPriority, // sendPriority |
115 kAEDefaultTimeout, // timeOutInTicks | 116 kAEDefaultTimeout, // timeOutInTicks |
116 NULL, // idleProc | 117 NULL, // idleProc |
117 NULL); // filterProc | 118 NULL); // filterProc |
118 if (status != noErr) { | 119 if (status != noErr) { |
119 LOG(WARNING) << "Could not send AE to Finder in OpenItem()"; | 120 OSSTATUS_LOG(WARNING, status) |
| 121 << "Could not send AE to Finder in OpenItem()"; |
120 } | 122 } |
121 } | 123 } |
122 | 124 |
123 void OpenExternal(const GURL& url) { | 125 void OpenExternal(const GURL& url) { |
124 DCHECK([NSThread isMainThread]); | 126 DCHECK([NSThread isMainThread]); |
125 NSString* url_string = base::SysUTF8ToNSString(url.spec()); | 127 NSString* url_string = base::SysUTF8ToNSString(url.spec()); |
126 NSURL* ns_url = [NSURL URLWithString:url_string]; | 128 NSURL* ns_url = [NSURL URLWithString:url_string]; |
127 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url]) | 129 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url]) |
128 LOG(WARNING) << "NSWorkspace failed to open URL " << url; | 130 LOG(WARNING) << "NSWorkspace failed to open URL " << url; |
129 } | 131 } |
(...skipping 16 matching lines...) Expand all Loading... |
146 | 148 |
147 bool IsVisible(gfx::NativeView view) { | 149 bool IsVisible(gfx::NativeView view) { |
148 // A reasonable approximation of how you'd expect this to behave. | 150 // A reasonable approximation of how you'd expect this to behave. |
149 return (view && | 151 return (view && |
150 ![view isHiddenOrHasHiddenAncestor] && | 152 ![view isHiddenOrHasHiddenAncestor] && |
151 [view window] && | 153 [view window] && |
152 [[view window] isVisible]); | 154 [[view window] isVisible]); |
153 } | 155 } |
154 | 156 |
155 } // namespace platform_util | 157 } // namespace platform_util |
OLD | NEW |