2525#include " ui/views/widget/widget.h"
2626#include " url/gurl.h"
2727
28- // platform_util_mac.mm uses a lot of deprecated API.
29- // https://github.com/electron/electron/issues/43126
30- #pragma clang diagnostic push
31- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
32-
3328namespace {
3429
35- // This may be called from a global dispatch queue, the methods used here are
36- // thread safe, including LSGetApplicationForURL (> 10.2) and
37- // NSWorkspace#openURLs.
38- std::string OpenURL (NSURL * ns_url, bool activate) {
39- CFURLRef cf_url = (__bridge CFURLRef)(ns_url);
40- CFURLRef ref =
41- LSCopyDefaultApplicationURLForURL (cf_url, kLSRolesAll , nullptr );
42-
43- // If no application could be found, nullptr is returned and outError
44- // (if not nullptr) is populated with kLSApplicationNotFoundErr.
45- if (ref == nullptr )
46- return " No application in the Launch Services database matches the input "
47- " criteria." ;
48-
49- NSUInteger launchOptions = NSWorkspaceLaunchDefault ;
50- if (!activate)
51- launchOptions |= NSWorkspaceLaunchWithoutActivation ;
52-
53- bool opened = [[NSWorkspace sharedWorkspace ] openURLs: @[ ns_url ]
54- withAppBundleIdentifier: nil
55- options: launchOptions
56- additionalEventParamDescriptor: nil
57- launchIdentifiers: nil ];
58- if (!opened)
59- return " Failed to open URL" ;
60-
61- return " " ;
62- }
63-
6430NSString * GetLoginHelperBundleIdentifier () {
6531 return [[[NSBundle mainBundle ] bundleIdentifier ]
6632 stringByAppendingString: @" .loginhelper" ];
6733}
6834
69- std::string OpenPathOnThread (const base::FilePath& full_path) {
70- NSString * path_string = base::SysUTF8ToNSString (full_path.value ());
71- NSURL * url = [NSURL fileURLWithPath: path_string];
72- if (!url)
73- return " Invalid path" ;
74-
75- const NSWorkspaceLaunchOptions launch_options =
76- NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithErrorPresentation ;
77- BOOL success = [[NSWorkspace sharedWorkspace ] openURLs: @[ url ]
78- withAppBundleIdentifier: nil
79- options: launch_options
80- additionalEventParamDescriptor: nil
81- launchIdentifiers: nil ];
82-
83- return success ? " " : " Failed to open path" ;
84- }
85-
86- // -Wdeprecated-declarations
87- #pragma clang diagnostic pop
88-
8935// https://developer.apple.com/documentation/servicemanagement/1561515-service_management_errors?language=objc
9036std::string GetLaunchStringForError (NSError * error) {
9137 if (@available (macOS 13 , *)) {
@@ -179,7 +125,15 @@ void ShowItemInFolder(const base::FilePath& path) {
179125}
180126
181127void OpenPath (const base::FilePath& full_path, OpenCallback callback) {
182- std::move (callback).Run (OpenPathOnThread (full_path));
128+ DCHECK ([NSThread isMainThread ]);
129+ NSURL * ns_url = base::apple::FilePathToNSURL (full_path);
130+ if (!ns_url) {
131+ std::move (callback).Run (" Invalid path" );
132+ return ;
133+ }
134+
135+ bool success = [[NSWorkspace sharedWorkspace ] openURL: ns_url];
136+ std::move (callback).Run (success ? " " : " Failed to open path" );
183137}
184138
185139void OpenExternal (const GURL& url,
@@ -192,25 +146,23 @@ void OpenExternal(const GURL& url,
192146 return ;
193147 }
194148
195- base::ThreadPool::PostTaskAndReplyWithResult (
196- FROM_HERE,
197- {base::MayBlock (), base::WithBaseSyncPrimitives (),
198- base::TaskPriority::USER_BLOCKING,
199- base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
200- base::BindOnce (&OpenURL, ns_url, options.activate ), std::move (callback));
149+ bool success = [[NSWorkspace sharedWorkspace ] openURL: ns_url];
150+ if (success && options.activate )
151+ [NSApp activateIgnoringOtherApps: YES ];
152+
153+ std::move (callback).Run (success ? " " : " Failed to open URL" );
201154}
202155
203156bool MoveItemToTrashWithError (const base::FilePath& full_path,
204157 bool delete_on_fail,
205158 std::string* error) {
206- NSString * path_string = base::SysUTF8ToNSString (full_path. value () );
207- if (!path_string ) {
159+ NSURL * url = base::apple::FilePathToNSURL (full_path);
160+ if (!url ) {
208161 *error = " Invalid file path: " + full_path.value ();
209162 LOG (WARNING) << *error;
210163 return false ;
211164 }
212165
213- NSURL * url = [NSURL fileURLWithPath: path_string];
214166 NSError * err = nil ;
215167 BOOL did_trash = [[NSFileManager defaultManager ] trashItemAtURL: url
216168 resultingItemURL: nil
0 commit comments