]> khamidou — GitHub mirrors - electron.git/commitdiff
Use native_mate to simplify shell API.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 15 Apr 2014 07:41:26 +0000 (15:41 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 15 Apr 2014 07:41:26 +0000 (15:41 +0800)
atom.gyp
atom/common/api/atom_api_shell.cc
atom/common/api/atom_api_shell.h [deleted file]
vendor/native_mate

index e637a4d691dbbc619726e77961259140a8d5f7b0..b4caea84d89c95c1b0ba0751934280d7769a8533 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       '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',
index bae608982c878fddf7537a075ff936affced3f5b..222e086c833584e70012520db62b418d7a1aff26 100644 (file)
@@ -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 api {
-
-// static
-void Shell::ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args) {
-  base::FilePath file_path;
-  if (!FromV8Arguments(args, &file_path))
-    return node::ThrowTypeError("Bad argument");
-
-  platform_util::ShowItemInFolder(file_path);
-}
-
-// static
-void Shell::OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
-  base::FilePath file_path;
-  if (!FromV8Arguments(args, &file_path))
-    return node::ThrowTypeError("Bad argument");
-
-  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);
+namespace mate {
+
+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;
+    }
+  }
+};
+
+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;
+    }
+  }
+};
+
+}  // 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::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
+}  // namespace
 
-NODE_MODULE(atom_common_shell, atom::api::Shell::Initialize)
+NODE_MODULE(atom_common_shell, Initialize)
diff --git a/atom/common/api/atom_api_shell.h b/atom/common/api/atom_api_shell.h
deleted file mode 100644 (file)
index 7a35bb2..0000000
+++ /dev/null
@@ -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_
index 4cada31f808994b297439dff429638debc4a4ee9..78ab726ec7b14ecea6240a684002c6910561ef5c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 4cada31f808994b297439dff429638debc4a4ee9
+Subproject commit 78ab726ec7b14ecea6240a684002c6910561ef5c