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>
|
|
|
|
|
2016-10-13 20:28:11 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2014-04-22 15:07:21 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2016-07-11 08:11:05 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "atom/common/platform_util.h"
|
2015-09-07 08:29:54 +00:00
|
|
|
#include "native_mate/dictionary.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"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Converter<base::win::ShortcutOperation> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
|
|
|
base::win::ShortcutOperation* out) {
|
|
|
|
std::string operation;
|
|
|
|
if (!ConvertFromV8(isolate, val, & operation))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
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 {
|
|
|
|
|
2016-07-11 08:11:05 +00:00
|
|
|
bool OpenExternal(
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
const base::string16& url,
|
|
|
|
#else
|
|
|
|
const GURL& url,
|
|
|
|
#endif
|
|
|
|
mate::Arguments* args) {
|
2016-02-17 17:05:21 +00:00
|
|
|
bool activate = true;
|
2016-10-13 20:28:11 +00:00
|
|
|
if (args->Length() >= 2) {
|
2016-02-17 17:05:21 +00:00
|
|
|
mate::Dictionary options;
|
|
|
|
if (args->GetNext(&options)) {
|
|
|
|
options.Get("activate", &activate);
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 20:28:11 +00:00
|
|
|
|
|
|
|
if (args->Length() >= 3) {
|
|
|
|
platform_util::OpenExternalCallback callback;
|
2016-11-17 01:36:16 +00:00
|
|
|
if (args->GetNext(&callback)) {
|
2016-11-02 23:20:26 +00:00
|
|
|
platform_util::OpenExternal(url, activate, callback);
|
|
|
|
return true;
|
2016-10-13 20:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:05:21 +00:00
|
|
|
return platform_util::OpenExternal(url, activate);
|
|
|
|
}
|
|
|
|
|
2016-07-27 07:16:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
bool WriteShortcutLink(const base::FilePath& shortcut_path,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
base::win::ShortcutOperation operation = base::win::SHORTCUT_CREATE_ALWAYS;
|
|
|
|
args->GetNext(&operation);
|
|
|
|
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
|
|
|
|
if (!args->GetNext(&options)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::win::ShortcutProperties properties;
|
|
|
|
base::FilePath path;
|
|
|
|
base::string16 str;
|
|
|
|
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);
|
|
|
|
|
2016-07-27 07:20:53 +00:00
|
|
|
base::win::ScopedCOMInitializer com_initializer;
|
2016-07-27 07:16:21 +00:00
|
|
|
return base::win::CreateOrUpdateShortcutLink(
|
|
|
|
shortcut_path, properties, operation);
|
|
|
|
}
|
|
|
|
|
2016-07-27 07:32:32 +00:00
|
|
|
v8::Local<v8::Value> ReadShortcutLink(mate::Arguments* args,
|
|
|
|
const base::FilePath& path) {
|
2016-07-27 07:16:21 +00:00
|
|
|
using base::win::ShortcutProperties;
|
2016-07-27 07:32:32 +00:00
|
|
|
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->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)) {
|
2016-07-27 07:32:32 +00:00
|
|
|
args->ThrowError("Failed to read shortcut link");
|
|
|
|
return v8::Null(args->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);
|
2016-07-27 07:32:32 +00:00
|
|
|
return options.GetHandle();
|
2016-07-27 07:16:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder);
|
|
|
|
dict.SetMethod("openItem", &platform_util::OpenItem);
|
2016-02-17 17:05:21 +00:00
|
|
|
dict.SetMethod("openExternal", &OpenExternal);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash);
|
|
|
|
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
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_shell, Initialize)
|