Callback uses (platform specific) error, with message
This commit is contained in:
parent
5e8059e0fa
commit
99a5258999
5 changed files with 83 additions and 35 deletions
|
@ -62,7 +62,8 @@ bool OpenExternal(
|
||||||
platform_util::OpenExternalCallback callback;
|
platform_util::OpenExternalCallback callback;
|
||||||
if (mate::Converter<platform_util::OpenExternalCallback>::FromV8(
|
if (mate::Converter<platform_util::OpenExternalCallback>::FromV8(
|
||||||
args->isolate(), peek, &callback)) {
|
args->isolate(), peek, &callback)) {
|
||||||
return platform_util::OpenExternal(url, activate, callback);
|
platform_util::OpenExternal(url, activate, callback);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "base/callback_forward.h"
|
#include "base/callback_forward.h"
|
||||||
#include "build/build_config.h"
|
#include "build/build_config.h"
|
||||||
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
#include "base/strings/string16.h"
|
#include "base/strings/string16.h"
|
||||||
|
@ -28,7 +29,7 @@ bool ShowItemInFolder(const base::FilePath& full_path);
|
||||||
// Must be called from the UI thread.
|
// Must be called from the UI thread.
|
||||||
bool OpenItem(const base::FilePath& full_path);
|
bool OpenItem(const base::FilePath& full_path);
|
||||||
|
|
||||||
typedef base::Callback<void(bool opened)> OpenExternalCallback;
|
typedef base::Callback<void(v8::Local<v8::Value> error)> OpenExternalCallback;
|
||||||
|
|
||||||
// Open the given external protocol URL in the desktop's default manner.
|
// Open the given external protocol URL in the desktop's default manner.
|
||||||
// (For example, mailto: URLs in the default mail user agent.)
|
// (For example, mailto: URLs in the default mail user agent.)
|
||||||
|
@ -40,7 +41,7 @@ bool OpenExternal(
|
||||||
#endif
|
#endif
|
||||||
bool activate);
|
bool activate);
|
||||||
|
|
||||||
bool OpenExternal(
|
void OpenExternal(
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
const base::string16& url,
|
const base::string16& url,
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -89,12 +89,16 @@ bool OpenExternal(const GURL& url, bool activate) {
|
||||||
return XDGOpen(url.spec(), false);
|
return XDGOpen(url.spec(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenExternal(const GURL& url, bool activate,
|
void OpenExternal(const GURL& url, bool activate,
|
||||||
const OpenExternalCallback& callback) {
|
const OpenExternalCallback& callback) {
|
||||||
// TODO(gabriel): Implement async open if callback is specified
|
// TODO(gabriel): Implement async open if callback is specified
|
||||||
bool opened = OpenExternal(url, activate);
|
bool opened = OpenExternal(url, activate);
|
||||||
callback.Run(opened);
|
if (!opened) {
|
||||||
return opened;
|
callback.Run(v8::Exception::Error(
|
||||||
|
v8::String::NewFromUtf8(isolate, @"Failed to open")));
|
||||||
|
} else {
|
||||||
|
callback.Run(v8::Null(isolate);)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MoveItemToTrash(const base::FilePath& full_path) {
|
bool MoveItemToTrash(const base::FilePath& full_path) {
|
||||||
|
|
|
@ -19,30 +19,43 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
bool OpenURLInWorkspace(NSURL* ns_url, NSUInteger launchOptions) {
|
NSError *PlatformError(NSString* message) {
|
||||||
return [[NSWorkspace sharedWorkspace] openURLs: @[ns_url]
|
return [NSError errorWithDomain:@"Electron"
|
||||||
withAppBundleIdentifier: nil
|
code:0
|
||||||
options: launchOptions
|
userInfo:@{NSLocalizedDescriptionKey: message}];
|
||||||
additionalEventParamDescriptor: NULL
|
|
||||||
launchIdentifiers: NULL];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef bool(^OpenExternalBlock)(NSURL* ns_url, NSUInteger launchOptions);
|
NSString *MessageForOSStatus(OSStatus status) {
|
||||||
|
switch (status) {
|
||||||
bool OpenExternalWithBlock(const GURL& url, bool activate, OpenExternalBlock open) {
|
case kLSAppInTrashErr: return @"The application cannot be run because it is inside a Trash folder.";
|
||||||
DCHECK([NSThread isMainThread]);
|
case kLSUnknownErr: return @"An unknown error has occurred.";
|
||||||
NSURL* ns_url = net::NSURLWithGURL(url);
|
case kLSNotAnApplicationErr: return @"The item to be registered is not an application.";
|
||||||
if (!ns_url) {
|
case kLSNotInitializedErr: return @"Formerly returned by LSInit on initialization failure; no longer used.";
|
||||||
return false;
|
case kLSDataUnavailableErr: return @"Data of the desired type is not available (for example, there is no kind string).";
|
||||||
|
case kLSApplicationNotFoundErr: return @"No application in the Launch Services database matches the input criteria.";
|
||||||
|
case kLSDataErr: return @"Data is structured improperly (for example, an item’s information property list is malformed). Not used in macOS 10.4.";
|
||||||
|
case kLSLaunchInProgressErr: return @"A launch of the application is already in progress.";
|
||||||
|
case kLSServerCommunicationErr: return @"There is a problem communicating with the server process that maintains the Launch Services database.";
|
||||||
|
case kLSCannotSetInfoErr: return @"The filename extension to be hidden cannot be hidden.";
|
||||||
|
case kLSIncompatibleSystemVersionErr: return @"The application to be launched cannot run on the current Mac OS version.";
|
||||||
|
case kLSNoLaunchPermissionErr: return @"The user does not have permission to launch the application (on a managed network).";
|
||||||
|
case kLSNoExecutableErr: return @"The executable file is missing or has an unusable format.";
|
||||||
|
case kLSNoClassicEnvironmentErr: return @"The Classic emulation environment was required but is not available.";
|
||||||
|
case kLSMultipleSessionsNotSupportedErr: return @"The application to be launched cannot run simultaneously in two different user sessions.";
|
||||||
|
default: return [NSString stringWithFormat:@"Failed to open (%@)", @(status)];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This may be called from a global dispatch queue, the methods used here are thread safe,
|
||||||
|
// including LSGetApplicationForURL (> 10.2) and NSWorkspace#openURLs.
|
||||||
|
NSError* OpenURL(NSURL* ns_url, bool activate) {
|
||||||
CFURLRef openingApp = NULL;
|
CFURLRef openingApp = NULL;
|
||||||
OSStatus status = LSGetApplicationForURL((CFURLRef)ns_url,
|
OSStatus status = LSGetApplicationForURL((CFURLRef)ns_url,
|
||||||
kLSRolesAll,
|
kLSRolesAll,
|
||||||
NULL,
|
NULL,
|
||||||
&openingApp);
|
&openingApp);
|
||||||
if (status != noErr) {
|
if (status != noErr) {
|
||||||
return false;
|
return PlatformError(MessageForOSStatus(status));
|
||||||
}
|
}
|
||||||
CFRelease(openingApp); // NOT A BUG; LSGetApplicationForURL retains for us
|
CFRelease(openingApp); // NOT A BUG; LSGetApplicationForURL retains for us
|
||||||
|
|
||||||
|
@ -50,7 +63,23 @@ bool OpenExternalWithBlock(const GURL& url, bool activate, OpenExternalBlock ope
|
||||||
if (!activate)
|
if (!activate)
|
||||||
launchOptions |= NSWorkspaceLaunchWithoutActivation;
|
launchOptions |= NSWorkspaceLaunchWithoutActivation;
|
||||||
|
|
||||||
return open(ns_url, launchOptions);
|
bool opened = [[NSWorkspace sharedWorkspace] openURLs: @[ns_url]
|
||||||
|
withAppBundleIdentifier: nil
|
||||||
|
options: launchOptions
|
||||||
|
additionalEventParamDescriptor: nil
|
||||||
|
launchIdentifiers: nil];
|
||||||
|
if (!opened) return PlatformError(@"Failed to open URL");
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> ConvertNSError(v8::Isolate* isolate, NSError* platformError) {
|
||||||
|
if (!platformError) {
|
||||||
|
return v8::Null(isolate);
|
||||||
|
} else {
|
||||||
|
v8::Local<v8::String> error_message =
|
||||||
|
v8::String::NewFromUtf8(isolate, platformError.localizedDescription.UTF8String);
|
||||||
|
return v8::Exception::Error(error_message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -168,21 +197,30 @@ bool OpenItem(const base::FilePath& full_path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenExternal(const GURL& url, bool activate) {
|
bool OpenExternal(const GURL& url, bool activate) {
|
||||||
return OpenExternalWithBlock(url, activate, ^bool(NSURL* ns_url, NSUInteger launchOptions) {
|
DCHECK([NSThread isMainThread]);
|
||||||
return OpenURLInWorkspace(ns_url, launchOptions);
|
NSURL* ns_url = net::NSURLWithGURL(url);
|
||||||
});
|
if (!ns_url) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
NSError *error = OpenURL(ns_url, activate);
|
||||||
|
return error ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenExternal(const GURL& url, bool activate, const OpenExternalCallback& c) {
|
void OpenExternal(const GURL& url, bool activate, const OpenExternalCallback& c) {
|
||||||
|
NSURL* ns_url = net::NSURLWithGURL(url);
|
||||||
|
if (!ns_url) {
|
||||||
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
|
c.Run(ConvertNSError(isolate, PlatformError(@"Invalid URL")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
__block OpenExternalCallback callback = c;
|
__block OpenExternalCallback callback = c;
|
||||||
return OpenExternalWithBlock(url, activate, ^bool(NSURL* ns_url, NSUInteger launchOptions) {
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
NSError *openError = OpenURL(ns_url, activate);
|
||||||
bool opened = OpenURLInWorkspace(ns_url, launchOptions);
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
callback.Run(opened);
|
callback.Run(ConvertNSError(isolate, openError));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return YES;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -316,12 +316,16 @@ bool OpenExternal(const base::string16& url, bool activate) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenExternal(const base::string16& url, bool activate,
|
void OpenExternal(const base::string16& url, bool activate,
|
||||||
const OpenExternalCallback& callback) {
|
const OpenExternalCallback& callback) {
|
||||||
// TODO(gabriel): Implement async open if callback is specified
|
// TODO(gabriel): Implement async open if callback is specified
|
||||||
bool opened = OpenExternal(url, activate);
|
bool opened = OpenExternal(url, activate);
|
||||||
callback.Run(opened);
|
if (!opened) {
|
||||||
return opened;
|
callback.Run(v8::Exception::Error(
|
||||||
|
v8::String::NewFromUtf8(isolate, @"Failed to open")));
|
||||||
|
} else {
|
||||||
|
callback.Run(v8::Null(isolate);)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MoveItemToTrash(const base::FilePath& path) {
|
bool MoveItemToTrash(const base::FilePath& path) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue