- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / platform_util_mac.mm
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.
4
5 #include "chrome/browser/platform_util.h"
6
7 #include <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h>
9 #include <CoreServices/CoreServices.h>
10
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"
19 #include "url/gurl.h"
20
21 namespace platform_util {
22
23 void ShowItemInFolder(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();
29 }
30
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
37 // document.
38 void OpenItem(const base::FilePath& full_path) {
39   DCHECK([NSThread isMainThread]);
40   NSString* path_string = base::SysUTF8ToNSString(full_path.value());
41   if (!path_string)
42     return;
43
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";
53     return;
54   }
55
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
60                               address,  // target
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";
66     return;
67   }
68
69   // Create the list of files (only ever one) to open.
70   base::mac::ScopedAEDesc<AEDescList> fileList;
71   status = AECreateList(NULL,  // factoringPtr
72                         0,  // factoredSize
73                         false,  // isRecord
74                         fileList.OutPointer());  // resultList
75   if (status != noErr) {
76     OSSTATUS_LOG(WARNING, status) << "Could not create OpenItem() AE file list";
77     return;
78   }
79
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];
83   FSRef pathRef;
84   if (CFURLGetFSRef(pathURLRef, &pathRef)) {
85     status = AEPutPtr(fileList.OutPointer(),  // theAEDescList
86                       0,  // index
87                       typeFSRef,  // typeCode
88                       &pathRef,  // dataPtr
89                       sizeof(pathRef));  // dataSize
90     if (status != noErr) {
91       OSSTATUS_LOG(WARNING, status)
92           << "Could not add file path to AE list in OpenItem()";
93       return;
94     }
95   } else {
96     LOG(WARNING) << "Could not get FSRef for path URL in OpenItem()";
97     return;
98   }
99
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()";
107     return;
108   }
109
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
117                   NULL, // idleProc
118                   NULL);  // filterProc
119   if (status != noErr) {
120     OSSTATUS_LOG(WARNING, status)
121         << "Could not send AE to Finder in OpenItem()";
122   }
123 }
124
125 void OpenExternal(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;
131 }
132
133 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
134   return [view window];
135 }
136
137 gfx::NativeView GetParent(gfx::NativeView view) {
138   return nil;
139 }
140
141 bool IsWindowActive(gfx::NativeWindow window) {
142   return [window isKeyWindow] || [window isMainWindow];
143 }
144
145 void ActivateWindow(gfx::NativeWindow window) {
146   [window makeKeyAndOrderFront:nil];
147 }
148
149 bool IsVisible(gfx::NativeView view) {
150   // A reasonable approximation of how you'd expect this to behave.
151   return (view &&
152           ![view isHiddenOrHasHiddenAncestor] &&
153           [view window] &&
154           [[view window] isVisible]);
155 }
156
157 bool IsSwipeTrackingFromScrollEventsEnabled() {
158   SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled);
159   return [NSEvent respondsToSelector:selector]
160       && [NSEvent performSelector:selector];
161 }
162
163 }  // namespace platform_util