2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-29 13:57:05 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2020-09-17 22:17:44 +00:00
|
|
|
#include "shell/common/gin_converters/guid_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
#include "shell/common/platform_util.h"
|
2013-04-29 13:57:05 +00:00
|
|
|
|
2016-07-27 07:16:21 +00:00
|
|
|
#if defined(OS_WIN)
|
2016-07-27 07:20:53 +00:00
|
|
|
#include "base/win/scoped_com_initializer.h"
|
2016-07-27 07:16:21 +00:00
|
|
|
#include "base/win/shortcut.h"
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
namespace gin {
|
2016-07-27 07:16:21 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2016-07-27 07:16:21 +00:00
|
|
|
struct Converter<base::win::ShortcutOperation> {
|
2018-04-18 01:55:30 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Value> val,
|
2016-07-27 07:16:21 +00:00
|
|
|
base::win::ShortcutOperation* out) {
|
|
|
|
std::string operation;
|
2018-04-18 01:55:30 +00:00
|
|
|
if (!ConvertFromV8(isolate, val, &operation))
|
2016-07-27 07:16:21 +00:00
|
|
|
return false;
|
|
|
|
if (operation.empty() || operation == "create")
|
|
|
|
*out = base::win::SHORTCUT_CREATE_ALWAYS;
|
|
|
|
else if (operation == "update")
|
|
|
|
*out = base::win::SHORTCUT_UPDATE_EXISTING;
|
|
|
|
else if (operation == "replace")
|
|
|
|
*out = base::win::SHORTCUT_REPLACE_EXISTING;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
} // namespace gin
|
2016-07-27 10:55:05 +00:00
|
|
|
#endif
|
2016-07-27 07:16:21 +00:00
|
|
|
|
2014-04-15 07:41:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-11-08 07:08:43 +00:00
|
|
|
void OnOpenFinished(gin_helper::Promise<void> promise,
|
|
|
|
const std::string& error) {
|
2016-11-17 02:22:09 +00:00
|
|
|
if (error.empty())
|
2019-02-21 12:32:44 +00:00
|
|
|
promise.Resolve();
|
2016-11-17 02:22:09 +00:00
|
|
|
else
|
2019-02-21 12:32:44 +00:00
|
|
|
promise.RejectWithErrorMessage(error.c_str());
|
2016-11-17 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
v8::Local<v8::Promise> OpenExternal(const GURL& url, gin::Arguments* args) {
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(args->isolate());
|
2019-05-03 20:53:45 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2019-01-15 04:35:21 +00:00
|
|
|
|
|
|
|
platform_util::OpenExternalOptions options;
|
|
|
|
if (args->Length() >= 2) {
|
2019-09-06 05:52:54 +00:00
|
|
|
gin::Dictionary obj(nullptr);
|
2019-01-15 04:35:21 +00:00
|
|
|
if (args->GetNext(&obj)) {
|
|
|
|
obj.Get("activate", &options.activate);
|
|
|
|
obj.Get("workingDirectory", &options.working_dir);
|
2016-10-13 20:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
platform_util::OpenExternal(
|
2019-11-08 07:08:43 +00:00
|
|
|
url, options, base::BindOnce(&OnOpenFinished, std::move(promise)));
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> OpenPath(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& full_path) {
|
|
|
|
gin_helper::Promise<const std::string&> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
platform_util::OpenPath(
|
|
|
|
full_path,
|
|
|
|
base::BindOnce(
|
|
|
|
[](gin_helper::Promise<const std::string&> promise,
|
|
|
|
const std::string& err_msg) { promise.Resolve(err_msg); },
|
|
|
|
std::move(promise)));
|
2019-02-21 12:32:44 +00:00
|
|
|
return handle;
|
2016-02-17 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 17:32:33 +00:00
|
|
|
v8::Local<v8::Promise> TrashItem(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path) {
|
|
|
|
gin_helper::Promise<void> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
platform_util::TrashItem(
|
|
|
|
path, base::BindOnce(
|
|
|
|
[](gin_helper::Promise<void> promise, bool success,
|
|
|
|
const std::string& error) {
|
|
|
|
if (success) {
|
|
|
|
promise.Resolve();
|
|
|
|
} else {
|
|
|
|
promise.RejectWithErrorMessage(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
std::move(promise)));
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-07-27 07:16:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
bool WriteShortcutLink(const base::FilePath& shortcut_path,
|
2019-10-18 00:31:29 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2016-07-27 07:16:21 +00:00
|
|
|
base::win::ShortcutOperation operation = base::win::SHORTCUT_CREATE_ALWAYS;
|
2019-10-18 00:31:29 +00:00
|
|
|
args->GetNext(&operation);
|
2019-09-06 05:52:54 +00:00
|
|
|
gin::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate());
|
2016-07-27 07:16:21 +00:00
|
|
|
if (!args->GetNext(&options)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::win::ShortcutProperties properties;
|
|
|
|
base::FilePath path;
|
2021-03-18 19:55:51 +00:00
|
|
|
std::wstring str;
|
2020-09-17 22:17:44 +00:00
|
|
|
UUID toastActivatorClsid;
|
2016-07-27 07:16:21 +00:00
|
|
|
int index;
|
|
|
|
if (options.Get("target", &path))
|
|
|
|
properties.set_target(path);
|
|
|
|
if (options.Get("cwd", &path))
|
|
|
|
properties.set_working_dir(path);
|
|
|
|
if (options.Get("args", &str))
|
|
|
|
properties.set_arguments(str);
|
|
|
|
if (options.Get("description", &str))
|
|
|
|
properties.set_description(str);
|
|
|
|
if (options.Get("icon", &path) && options.Get("iconIndex", &index))
|
|
|
|
properties.set_icon(path, index);
|
|
|
|
if (options.Get("appUserModelId", &str))
|
|
|
|
properties.set_app_id(str);
|
2020-09-17 22:17:44 +00:00
|
|
|
if (options.Get("toastActivatorClsid", &toastActivatorClsid))
|
|
|
|
properties.set_toast_activator_clsid(toastActivatorClsid);
|
2016-07-27 07:16:21 +00:00
|
|
|
|
2016-07-27 07:20:53 +00:00
|
|
|
base::win::ScopedCOMInitializer com_initializer;
|
2018-04-18 01:55:30 +00:00
|
|
|
return base::win::CreateOrUpdateShortcutLink(shortcut_path, properties,
|
|
|
|
operation);
|
2016-07-27 07:16:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
v8::Local<v8::Value> ReadShortcutLink(gin_helper::ErrorThrower thrower,
|
2016-07-27 07:32:32 +00:00
|
|
|
const base::FilePath& path) {
|
2016-07-27 07:16:21 +00:00
|
|
|
using base::win::ShortcutProperties;
|
2019-09-06 05:52:54 +00:00
|
|
|
gin::Dictionary options = gin::Dictionary::CreateEmpty(thrower.isolate());
|
2016-07-27 07:20:53 +00:00
|
|
|
base::win::ScopedCOMInitializer com_initializer;
|
2016-07-27 07:16:21 +00:00
|
|
|
base::win::ShortcutProperties properties;
|
2016-07-27 07:23:35 +00:00
|
|
|
if (!base::win::ResolveShortcutProperties(
|
|
|
|
path, ShortcutProperties::PROPERTIES_ALL, &properties)) {
|
2019-09-06 05:52:54 +00:00
|
|
|
thrower.ThrowError("Failed to read shortcut link");
|
|
|
|
return v8::Null(thrower.isolate());
|
2016-07-27 07:16:21 +00:00
|
|
|
}
|
2016-07-27 07:23:35 +00:00
|
|
|
options.Set("target", properties.target);
|
|
|
|
options.Set("cwd", properties.working_dir);
|
|
|
|
options.Set("args", properties.arguments);
|
|
|
|
options.Set("description", properties.description);
|
|
|
|
options.Set("icon", properties.icon);
|
|
|
|
options.Set("iconIndex", properties.icon_index);
|
|
|
|
options.Set("appUserModelId", properties.app_id);
|
2020-09-17 22:17:44 +00:00
|
|
|
options.Set("toastActivatorClsid", properties.toast_activator_clsid);
|
2019-09-06 05:52:54 +00:00
|
|
|
return gin::ConvertToV8(thrower.isolate(), options);
|
2016-07-27 07:16:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder);
|
2019-11-08 07:08:43 +00:00
|
|
|
dict.SetMethod("openPath", &OpenPath);
|
2016-02-17 17:05:21 +00:00
|
|
|
dict.SetMethod("openExternal", &OpenExternal);
|
2020-09-02 17:32:33 +00:00
|
|
|
dict.SetMethod("trashItem", &TrashItem);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("beep", &platform_util::Beep);
|
2016-07-27 07:16:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
dict.SetMethod("writeShortcutLink", &WriteShortcutLink);
|
|
|
|
dict.SetMethod("readShortcutLink", &ReadShortcutLink);
|
|
|
|
#endif
|
2013-04-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:41:26 +00:00
|
|
|
} // namespace
|
2013-04-29 13:57:05 +00:00
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_shell, Initialize)
|