[email protected] | 33b9820 | 2012-01-27 23:06:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 5 | #include "chrome/browser/platform_util.h" |
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 6 | |
[email protected] | 7bea1c5 | 2009-04-14 20:08:00 | [diff] [blame] | 7 | #import <Cocoa/Cocoa.h> |
8 | |||||
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 33b9820 | 2012-01-27 23:06:49 | [diff] [blame] | 13 | #include "base/mac/mac_logging.h" |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 14 | #import "base/mac/sdk_forward_declarations.h" |
[email protected] | 3268d7b7 | 2013-03-28 17:41:43 | [diff] [blame] | 15 | #include "base/strings/sys_string_conversions.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 16 | #include "chrome/browser/platform_util_internal.h" |
17 | #include "content/public/browser/browser_thread.h" | ||||
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 18 | #include "url/gurl.h" |
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 19 | |
20 | namespace platform_util { | ||||
21 | |||||
[email protected] | 4d225de1 | 2013-12-13 09:29:17 | [diff] [blame] | 22 | void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) { |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 23 | DCHECK([NSThread isMainThread]); |
[email protected] | 7bea1c5 | 2009-04-14 20:08:00 | [diff] [blame] | 24 | NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
[email protected] | 89d6e6e | 2009-12-03 23:06:51 | [diff] [blame] | 25 | if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string |
thakis | 134eaad | 2015-10-13 15:59:58 | [diff] [blame] | 26 | inFileViewerRootedAtPath:@""]) |
[email protected] | 89d6e6e | 2009-12-03 23:06:51 | [diff] [blame] | 27 | LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); |
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 28 | } |
29 | |||||
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 30 | void OpenFileOnMainThread(const base::FilePath& full_path) { |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 31 | DCHECK([NSThread isMainThread]); |
[email protected] | 1805b51 | 2009-06-10 16:12:48 | [diff] [blame] | 32 | NSString* path_string = base::SysUTF8ToNSString(full_path.value()); |
[email protected] | 824ff72 | 2010-08-13 15:24:53 | [diff] [blame] | 33 | if (!path_string) |
34 | return; | ||||
35 | |||||
Robert Sesek | b83907e | 2014-09-29 19:44:00 | [diff] [blame] | 36 | // On Mavericks or later, NSWorkspaceLaunchWithErrorPresentation will |
37 | // properly handle Finder activation for quarantined files | ||||
38 | // (http://crbug.com/32921) and unassociated file types | ||||
39 | // (http://crbug.com/50263). | ||||
mark | dd7e82fd | 2016-07-08 23:41:23 | [diff] [blame] | 40 | NSURL* url = [NSURL fileURLWithPath:path_string]; |
41 | if (!url) | ||||
Robert Sesek | b83907e | 2014-09-29 19:44:00 | [diff] [blame] | 42 | return; |
Robert Sesek | b83907e | 2014-09-29 19:44:00 | [diff] [blame] | 43 | |
mark | dd7e82fd | 2016-07-08 23:41:23 | [diff] [blame] | 44 | const NSWorkspaceLaunchOptions launch_options = |
45 | NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation; | ||||
46 | [[NSWorkspace sharedWorkspace] openURLs:@[ url ] | ||||
47 | withAppBundleIdentifier:nil | ||||
48 | options:launch_options | ||||
49 | additionalEventParamDescriptor:nil | ||||
50 | launchIdentifiers:NULL]; | ||||
[email protected] | de86a851 | 2009-05-28 20:29:40 | [diff] [blame] | 51 | } |
52 | |||||
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 53 | namespace internal { |
54 | |||||
55 | void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) { | ||||
56 | switch (type) { | ||||
57 | case OPEN_FILE: | ||||
58 | content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | ||||
59 | base::Bind(&OpenFileOnMainThread, path)); | ||||
60 | return; | ||||
61 | case OPEN_FOLDER: | ||||
62 | NSString* path_string = base::SysUTF8ToNSString(path.value()); | ||||
63 | if (!path_string) | ||||
64 | return; | ||||
65 | // Note that there exists a TOCTOU race between the time that |path| was | ||||
66 | // verified as being a directory and when NSWorkspace invokes Finder (or | ||||
67 | // alternative) to open |path_string|. | ||||
68 | [[NSWorkspace sharedWorkspace] openFile:path_string]; | ||||
69 | return; | ||||
70 | } | ||||
71 | } | ||||
72 | |||||
73 | } // namespace internal | ||||
74 | |||||
[email protected] | 7f0a3efa | 2013-12-12 17:16:12 | [diff] [blame] | 75 | void OpenExternal(Profile* profile, const GURL& url) { |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 76 | DCHECK([NSThread isMainThread]); |
[email protected] | 59b2e32 | 2009-09-01 22:32:26 | [diff] [blame] | 77 | NSString* url_string = base::SysUTF8ToNSString(url.spec()); |
78 | NSURL* ns_url = [NSURL URLWithString:url_string]; | ||||
[email protected] | 89d6e6e | 2009-12-03 23:06:51 | [diff] [blame] | 79 | if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url]) |
80 | LOG(WARNING) << "NSWorkspace failed to open URL " << url; | ||||
[email protected] | 59b2e32 | 2009-09-01 22:32:26 | [diff] [blame] | 81 | } |
82 | |||||
[email protected] | 076700e6 | 2009-04-01 18:41:23 | [diff] [blame] | 83 | gfx::NativeWindow GetTopLevel(gfx::NativeView view) { |
[email protected] | 7bea1c5 | 2009-04-14 20:08:00 | [diff] [blame] | 84 | return [view window]; |
[email protected] | 076700e6 | 2009-04-01 18:41:23 | [diff] [blame] | 85 | } |
86 | |||||
andresantoso | f8f9fd18 | 2014-11-15 01:14:39 | [diff] [blame] | 87 | gfx::NativeView GetViewForWindow(gfx::NativeWindow window) { |
88 | DCHECK(window); | ||||
89 | DCHECK([window contentView]); | ||||
90 | return [window contentView]; | ||||
91 | } | ||||
92 | |||||
[email protected] | 0583fe3 | 2010-11-01 21:21:15 | [diff] [blame] | 93 | gfx::NativeView GetParent(gfx::NativeView view) { |
94 | return nil; | ||||
95 | } | ||||
96 | |||||
[email protected] | d2cc6ed | 2009-04-24 00:26:17 | [diff] [blame] | 97 | bool IsWindowActive(gfx::NativeWindow window) { |
[email protected] | 9973ceb8 | 2009-10-15 15:39:24 | [diff] [blame] | 98 | return [window isKeyWindow] || [window isMainWindow]; |
[email protected] | d2cc6ed | 2009-04-24 00:26:17 | [diff] [blame] | 99 | } |
100 | |||||
[email protected] | 9fa8af6 | 2010-06-03 17:15:22 | [diff] [blame] | 101 | void ActivateWindow(gfx::NativeWindow window) { |
102 | [window makeKeyAndOrderFront:nil]; | ||||
103 | } | ||||
104 | |||||
[email protected] | bd1ad68 | 2009-05-15 22:19:17 | [diff] [blame] | 105 | bool IsVisible(gfx::NativeView view) { |
[email protected] | 9973ceb8 | 2009-10-15 15:39:24 | [diff] [blame] | 106 | // A reasonable approximation of how you'd expect this to behave. |
107 | return (view && | ||||
108 | ![view isHiddenOrHasHiddenAncestor] && | ||||
109 | [view window] && | ||||
110 | [[view window] isVisible]); | ||||
[email protected] | bd1ad68 | 2009-05-15 22:19:17 | [diff] [blame] | 111 | } |
112 | |||||
[email protected] | 895585e | 2012-04-19 18:24:07 | [diff] [blame] | 113 | bool IsSwipeTrackingFromScrollEventsEnabled() { |
114 | SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled); | ||||
115 | return [NSEvent respondsToSelector:selector] | ||||
116 | && [NSEvent performSelector:selector]; | ||||
117 | } | ||||
118 | |||||
[email protected] | 1a9e11dc | 2009-03-24 20:40:44 | [diff] [blame] | 119 | } // namespace platform_util |