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
3 // found in the LICENSE file.
5 #include "chrome/browser/platform_util.h"
7 #include <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h>
9 #include <CoreServices/CoreServices.h>
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/mac/mac_logging.h"
14 #include "base/mac/scoped_aedesc.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/l10n/l10n_util_mac.h"
21 namespace platform_util {
23 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
24 DCHECK([NSThread isMainThread]);
25 NSString* path_string = base::SysUTF8ToNSString(full_path.value());
26 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
27 inFileViewerRootedAtPath:nil])
28 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
31 // This function opens a file. This doesn't use LaunchServices or NSWorkspace
32 // because of two bugs:
33 // 1. Incorrect app activation with com.apple.quarantine:
34 // http://crbug.com/32921
35 // 2. Silent no-op for unassociated file types: http://crbug.com/50263
36 // Instead, an AppleEvent is constructed to tell the Finder to open the
38 void OpenItem(Profile* profile, const base::FilePath& full_path) {
39 DCHECK([NSThread isMainThread]);
40 NSString* path_string = base::SysUTF8ToNSString(full_path.value());
44 // Create the target of this AppleEvent, the Finder.
45 base::mac::ScopedAEDesc<AEAddressDesc> address;
46 const OSType finderCreatorCode = 'MACS';
47 OSErr status = AECreateDesc(typeApplSignature, // type
48 &finderCreatorCode, // data
49 sizeof(finderCreatorCode), // dataSize
50 address.OutPointer()); // result
51 if (status != noErr) {
52 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE target";
56 // Build the AppleEvent data structure that instructs Finder to open files.
57 base::mac::ScopedAEDesc<AppleEvent> theEvent;
58 status = AECreateAppleEvent(kCoreEventClass, // theAEEventClass
59 kAEOpenDocuments, // theAEEventID
61 kAutoGenerateReturnID, // returnID
62 kAnyTransactionID, // transactionID
63 theEvent.OutPointer()); // result
64 if (status != noErr) {
65 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE event";
69 // Create the list of files (only ever one) to open.
70 base::mac::ScopedAEDesc<AEDescList> fileList;
71 status = AECreateList(NULL, // factoringPtr
74 fileList.OutPointer()); // resultList
75 if (status != noErr) {
76 OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list";
80 // Add the single path to the file list. C-style cast to avoid both a
81 // static_cast and a const_cast to get across the toll-free bridge.
82 CFURLRef pathURLRef = (CFURLRef)[NSURL fileURLWithPath:path_string];
84 if (CFURLGetFSRef(pathURLRef, &pathRef)) {
85 status = AEPutPtr(fileList.OutPointer(), // theAEDescList
87 typeFSRef, // typeCode
89 sizeof(pathRef)); // dataSize
90 if (status != noErr) {
91 OSSTATUS_LOG(WARNING, status)
92 << "Could not add file path to AE list in OpenItem()";
96 LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()";
100 // Attach the file list to the AppleEvent.
101 status = AEPutParamDesc(theEvent.OutPointer(), // theAppleEvent
102 keyDirectObject, // theAEKeyword
103 fileList); // theAEDesc
104 if (status != noErr) {
105 OSSTATUS_LOG(WARNING, status)
106 << "Could not put the AE file list the path in OpenItem()";
110 // Send the actual event. Do not care about the reply.
111 base::mac::ScopedAEDesc<AppleEvent> reply;
112 status = AESend(theEvent, // theAppleEvent
113 reply.OutPointer(), // reply
114 kAENoReply + kAEAlwaysInteract, // sendMode
115 kAENormalPriority, // sendPriority
116 kAEDefaultTimeout, // timeOutInTicks
119 if (status != noErr) {
120 OSSTATUS_LOG(WARNING, status)
121 << "Could not send AE to Finder in OpenItem()";
125 void OpenExternal(Profile* profile, const GURL& url) {
126 DCHECK([NSThread isMainThread]);
127 NSString* url_string = base::SysUTF8ToNSString(url.spec());
128 NSURL* ns_url = [NSURL URLWithString:url_string];
129 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url])
130 LOG(WARNING) << "NSWorkspace failed to open URL " << url;
133 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
134 return [view window];
137 gfx::NativeView GetParent(gfx::NativeView view) {
141 bool IsWindowActive(gfx::NativeWindow window) {
142 return [window isKeyWindow] || [window isMainWindow];
145 void ActivateWindow(gfx::NativeWindow window) {
146 [window makeKeyAndOrderFront:nil];
149 bool IsVisible(gfx::NativeView view) {
150 // A reasonable approximation of how you'd expect this to behave.
152 ![view isHiddenOrHasHiddenAncestor] &&
154 [[view window] isVisible]);
157 bool IsSwipeTrackingFromScrollEventsEnabled() {
158 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled);
159 return [NSEvent respondsToSelector:selector]
160 && [NSEvent performSelector:selector];
163 } // namespace platform_util