Use native_mate to simplify shell API.
This commit is contained in:
Родитель
16af53237c
Коммит
e42433cf9a
1
atom.gyp
1
atom.gyp
|
@ -153,7 +153,6 @@
|
|||
'atom/common/api/atom_api_screen.cc',
|
||||
'atom/common/api/atom_api_screen.h',
|
||||
'atom/common/api/atom_api_shell.cc',
|
||||
'atom/common/api/atom_api_shell.h',
|
||||
'atom/common/api/atom_api_v8_util.cc',
|
||||
'atom/common/api/atom_bindings.cc',
|
||||
'atom/common/api/atom_bindings.h',
|
||||
|
|
|
@ -7,68 +7,66 @@
|
|||
#include <string>
|
||||
|
||||
#include "atom/common/platform_util.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
|
||||
namespace atom {
|
||||
namespace mate {
|
||||
|
||||
namespace api {
|
||||
template<>
|
||||
struct Converter<GURL> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const GURL& val) {
|
||||
return Converter<base::StringPiece>::ToV8(isolate, val.spec());
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
GURL* out) {
|
||||
std::string url;
|
||||
if (Converter<std::string>::FromV8(isolate, val, &url)) {
|
||||
*out = GURL(url);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// static
|
||||
void Shell::ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
template<>
|
||||
struct Converter<base::FilePath> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::FilePath& val) {
|
||||
return Converter<base::StringPiece>::ToV8(isolate, val.AsUTF8Unsafe());
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::FilePath* out) {
|
||||
std::string path;
|
||||
if (Converter<std::string>::FromV8(isolate, val, &path)) {
|
||||
*out = base::FilePath::FromUTF8Unsafe(path);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
platform_util::ShowItemInFolder(file_path);
|
||||
} // namespace mate
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent());
|
||||
builder.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder)
|
||||
.SetMethod("openItem", &platform_util::OpenItem)
|
||||
.SetMethod("openExternal", &platform_util::OpenExternal)
|
||||
.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash)
|
||||
.SetMethod("beep", &platform_util::Beep);
|
||||
exports->SetPrototype(builder.Build()->NewInstance());
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
} // namespace
|
||||
|
||||
platform_util::OpenItem(file_path);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
GURL url;
|
||||
if (!FromV8Arguments(args, &url))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
platform_util::OpenExternal(url);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
platform_util::MoveItemToTrash(file_path);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::Beep(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
platform_util::Beep();
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "showItemInFolder", ShowItemInFolder);
|
||||
NODE_SET_METHOD(target, "openItem", OpenItem);
|
||||
NODE_SET_METHOD(target, "openExternal", OpenExternal);
|
||||
NODE_SET_METHOD(target, "moveItemToTrash", MoveItemToTrash);
|
||||
NODE_SET_METHOD(target, "beep", Beep);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_shell, atom::api::Shell::Initialize)
|
||||
NODE_MODULE(atom_common_shell, Initialize)
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_API_ATOM_API_SHELL_H_
|
||||
#define ATOM_COMMON_API_ATOM_API_SHELL_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class Shell {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Beep(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Shell);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_SHELL_H_
|
|
@ -1 +1 @@
|
|||
Subproject commit 4cada31f808994b297439dff429638debc4a4ee9
|
||||
Subproject commit 78ab726ec7b14ecea6240a684002c6910561ef5c
|
Загрузка…
Ссылка в новой задаче