[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / 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 #import <Cocoa/Cocoa.h>
8
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/mac/mac_logging.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "chrome/browser/platform_util_internal.h"
16 #include "content/public/browser/browser_task_traits.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ui/views/widget/widget.h"
19 #include "url/gurl.h"
20
21 namespace platform_util {
22
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:@""])
28     LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
29 }
30
31 void OpenFileOnMainThread(const base::FilePath& full_path) {
32   DCHECK([NSThread isMainThread]);
33   NSString* path_string = base::SysUTF8ToNSString(full_path.value());
34   if (!path_string)
35     return;
36
37   // On Mavericks or later, NSWorkspaceLaunchWithErrorPresentation will
38   // properly handle Finder activation for quarantined files
39   // (http://crbug.com/32921) and unassociated file types
40   // (http://crbug.com/50263).
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 }
53
54 namespace internal {
55
56 void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) {
57   switch (type) {
58     case OPEN_FILE:
59       content::GetUIThreadTaskRunner({})->PostTask(
60           FROM_HERE, base::BindOnce(&OpenFileOnMainThread, path));
61       return;
62     case OPEN_FOLDER:
63       NSString* path_string = base::SysUTF8ToNSString(path.value());
64       if (!path_string)
65         return;
66       // Note that there exists a TOCTOU race between the time that |path| was
67       // verified as being a directory and when NSWorkspace invokes Finder (or
68       // alternative) to open |path_string|.
69       [[NSWorkspace sharedWorkspace] openFile:path_string];
70       return;
71   }
72 }
73
74 }  // namespace internal
75
76 void OpenExternal(Profile* profile, const GURL& url) {
77   DCHECK([NSThread isMainThread]);
78   NSString* url_string = base::SysUTF8ToNSString(url.spec());
79   NSURL* ns_url = [NSURL URLWithString:url_string];
80   if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url])
81     LOG(WARNING) << "NSWorkspace failed to open URL " << url;
82 }
83
84 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
85   return gfx::NativeWindow([view.GetNativeNSView() window]);
86 }
87
88 gfx::NativeView GetViewForWindow(gfx::NativeWindow native_window) {
89   NSWindow* window = native_window.GetNativeNSWindow();
90   DCHECK(window);
91   DCHECK([window contentView]);
92   return gfx::NativeView([window contentView]);
93 }
94
95 gfx::NativeView GetParent(gfx::NativeView view) {
96   return gfx::NativeView(nil);
97 }
98
99 bool IsWindowActive(gfx::NativeWindow native_window) {
100   // If |window| is a doppelganger NSWindow being used to track an NSWindow that
101   // is being hosted in another process, then use the views::Widget interface to
102   // interact with it.
103   views::Widget* widget =
104       views::Widget::GetWidgetForNativeWindow(native_window);
105   if (widget)
106     return widget->IsActive();
107
108   NSWindow* window = native_window.GetNativeNSWindow();
109   return [window isKeyWindow] || [window isMainWindow];
110 }
111
112 void ActivateWindow(gfx::NativeWindow native_window) {
113   views::Widget* widget =
114       views::Widget::GetWidgetForNativeWindow(native_window);
115   if (widget)
116     return widget->Activate();
117
118   NSWindow* window = native_window.GetNativeNSWindow();
119   [window makeKeyAndOrderFront:nil];
120 }
121
122 bool IsVisible(gfx::NativeView native_view) {
123   views::Widget* widget = views::Widget::GetWidgetForNativeView(native_view);
124   if (widget)
125     return widget->IsVisible();
126
127   // A reasonable approximation of how you'd expect this to behave.
128   NSView* view = native_view.GetNativeNSView();
129   return (view &&
130           ![view isHiddenOrHasHiddenAncestor] &&
131           [view window] &&
132           [[view window] isVisible]);
133 }
134
135 bool IsSwipeTrackingFromScrollEventsEnabled() {
136   SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled);
137   return [NSEvent respondsToSelector:selector]
138       && [NSEvent performSelector:selector];
139 }
140
141 }  // namespace platform_util