blob: cb0c3096ddb7cad41bf734498f21136748322875 [file] [log] [blame]
[email protected]33b98202012-01-27 23:06:491// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1a9e11dc2009-03-24 20:40:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]14a000d2010-04-29 21:44:245#include "chrome/browser/platform_util.h"
[email protected]1a9e11dc2009-03-24 20:40:446
[email protected]7bea1c52009-04-14 20:08:007#import <Cocoa/Cocoa.h>
8
asanka655d1112015-03-07 05:33:419#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
tapted574f09c2015-05-19 13:08:0811#include "base/files/file_util.h"
[email protected]1a9e11dc2009-03-24 20:40:4412#include "base/logging.h"
[email protected]33b98202012-01-27 23:06:4913#include "base/mac/mac_logging.h"
tapted574f09c2015-05-19 13:08:0814#import "base/mac/sdk_forward_declarations.h"
[email protected]3268d7b72013-03-28 17:41:4315#include "base/strings/sys_string_conversions.h"
asanka655d1112015-03-07 05:33:4116#include "chrome/browser/platform_util_internal.h"
17#include "content/public/browser/browser_thread.h"
[email protected]761fa4702013-07-02 15:25:1518#include "url/gurl.h"
[email protected]1a9e11dc2009-03-24 20:40:4419
20namespace platform_util {
21
[email protected]4d225de12013-12-13 09:29:1722void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
[email protected]7c3228a2011-11-11 21:35:2223 DCHECK([NSThread isMainThread]);
[email protected]7bea1c52009-04-14 20:08:0024 NSString* path_string = base::SysUTF8ToNSString(full_path.value());
[email protected]89d6e6e2009-12-03 23:06:5125 if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
thakis134eaad2015-10-13 15:59:5826 inFileViewerRootedAtPath:@""])
[email protected]89d6e6e2009-12-03 23:06:5127 LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
[email protected]1a9e11dc2009-03-24 20:40:4428}
29
asanka655d1112015-03-07 05:33:4130void OpenFileOnMainThread(const base::FilePath& full_path) {
[email protected]7c3228a2011-11-11 21:35:2231 DCHECK([NSThread isMainThread]);
[email protected]1805b512009-06-10 16:12:4832 NSString* path_string = base::SysUTF8ToNSString(full_path.value());
[email protected]824ff722010-08-13 15:24:5333 if (!path_string)
34 return;
35
Robert Sesekb83907e2014-09-29 19:44:0036 // 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).
markdd7e82fd2016-07-08 23:41:2340 NSURL* url = [NSURL fileURLWithPath:path_string];
41 if (!url)
Robert Sesekb83907e2014-09-29 19:44:0042 return;
Robert Sesekb83907e2014-09-29 19:44:0043
markdd7e82fd2016-07-08 23:41:2344 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]de86a8512009-05-28 20:29:4051}
52
asanka655d1112015-03-07 05:33:4153namespace internal {
54
55void 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]7f0a3efa2013-12-12 17:16:1275void OpenExternal(Profile* profile, const GURL& url) {
[email protected]7c3228a2011-11-11 21:35:2276 DCHECK([NSThread isMainThread]);
[email protected]59b2e322009-09-01 22:32:2677 NSString* url_string = base::SysUTF8ToNSString(url.spec());
78 NSURL* ns_url = [NSURL URLWithString:url_string];
[email protected]89d6e6e2009-12-03 23:06:5179 if (!ns_url || ![[NSWorkspace sharedWorkspace] openURL:ns_url])
80 LOG(WARNING) << "NSWorkspace failed to open URL " << url;
[email protected]59b2e322009-09-01 22:32:2681}
82
[email protected]076700e62009-04-01 18:41:2383gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
[email protected]7bea1c52009-04-14 20:08:0084 return [view window];
[email protected]076700e62009-04-01 18:41:2385}
86
andresantosof8f9fd182014-11-15 01:14:3987gfx::NativeView GetViewForWindow(gfx::NativeWindow window) {
88 DCHECK(window);
89 DCHECK([window contentView]);
90 return [window contentView];
91}
92
[email protected]0583fe32010-11-01 21:21:1593gfx::NativeView GetParent(gfx::NativeView view) {
94 return nil;
95}
96
[email protected]d2cc6ed2009-04-24 00:26:1797bool IsWindowActive(gfx::NativeWindow window) {
[email protected]9973ceb82009-10-15 15:39:2498 return [window isKeyWindow] || [window isMainWindow];
[email protected]d2cc6ed2009-04-24 00:26:1799}
100
[email protected]9fa8af62010-06-03 17:15:22101void ActivateWindow(gfx::NativeWindow window) {
102 [window makeKeyAndOrderFront:nil];
103}
104
[email protected]bd1ad682009-05-15 22:19:17105bool IsVisible(gfx::NativeView view) {
[email protected]9973ceb82009-10-15 15:39:24106 // 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]bd1ad682009-05-15 22:19:17111}
112
[email protected]895585e2012-04-19 18:24:07113bool IsSwipeTrackingFromScrollEventsEnabled() {
114 SEL selector = @selector(isSwipeTrackingFromScrollEventsEnabled);
115 return [NSEvent respondsToSelector:selector]
116 && [NSEvent performSelector:selector];
117}
118
[email protected]1a9e11dc2009-03-24 20:40:44119} // namespace platform_util