Use native_mate to simplify renderer_ipc api.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 16 Apr 2014 04:29:16 +0000 (12:29 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 16 Apr 2014 04:29:16 +0000 (12:29 +0800)
atom.gyp
atom/renderer/api/atom_api_renderer_ipc.cc
atom/renderer/api/atom_api_renderer_ipc.h [deleted file]

index d40e956..73429f2 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       'atom/common/v8/v8_value_converter.cc',
       'atom/common/v8/v8_value_converter.h',
       'atom/renderer/api/atom_api_renderer_ipc.cc',
-      'atom/renderer/api/atom_api_renderer_ipc.h',
       'atom/renderer/api/atom_renderer_bindings.cc',
       'atom/renderer/api/atom_renderer_bindings.h',
       'atom/renderer/atom_render_view_observer.cc',
index d95acf8..30a489f 100644 (file)
@@ -2,11 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "atom/renderer/api/atom_api_renderer_ipc.h"
-
 #include "atom/common/api/api_messages.h"
-#include "atom/common/v8/native_type_conversions.h"
+#include "atom/common/v8/v8_value_converter.h"
 #include "content/public/renderer/render_view.h"
+#include "native_mate/object_template_builder.h"
 #include "third_party/WebKit/public/web/WebFrame.h"
 #include "third_party/WebKit/public/web/WebView.h"
 
@@ -16,9 +15,42 @@ using content::RenderView;
 using WebKit::WebFrame;
 using WebKit::WebView;
 
-namespace atom {
-
-namespace api {
+namespace mate {
+
+template<>
+struct Converter<string16> {
+  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+                                    const string16& val) {
+    return v8::String::New(reinterpret_cast<const uint16_t*>(val.data()),
+                           val.size());
+  }
+  static bool FromV8(v8::Isolate* isolate,
+                     v8::Handle<v8::Value> val,
+                     string16* out) {
+    v8::String::Value s(val);
+    *out = string16(reinterpret_cast<const char16*>(*s), s.length());
+    return true;
+  }
+};
+
+template<>
+struct Converter<base::ListValue> {
+  static bool FromV8(v8::Isolate* isolate,
+                     v8::Handle<v8::Value> val,
+                     base::ListValue* out) {
+    scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
+    scoped_ptr<base::Value> value(converter->FromV8Value(
+        val, v8::Context::GetCurrent()));
+    if (value->IsType(base::Value::TYPE_LIST)) {
+      out->Swap(static_cast<ListValue*>(value.get()));
+      return true;
+    } else {
+      return false;
+    }
+  }
+};
+
+}  // namespace mate
 
 namespace {
 
@@ -34,65 +66,44 @@ RenderView* GetCurrentRenderView() {
   return RenderView::FromWebView(view);
 }
 
-}  // namespace
-
-// static
-void RendererIPC::Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
-  string16 channel;
-  scoped_ptr<base::Value> arguments;
-  if (!FromV8Arguments(args, &channel, &arguments))
-    return node::ThrowTypeError("Bad argument");
-
+void Send(const string16& channel, const base::ListValue& arguments) {
   RenderView* render_view = GetCurrentRenderView();
   if (render_view == NULL)
     return;
 
   bool success = render_view->Send(new AtomViewHostMsg_Message(
-      render_view->GetRoutingID(),
-      channel,
-      *static_cast<base::ListValue*>(arguments.get())));
+      render_view->GetRoutingID(), channel, arguments));
 
   if (!success)
-    return node::ThrowError("Unable to send AtomViewHostMsg_Message");
+    node::ThrowError("Unable to send AtomViewHostMsg_Message");
 }
 
-// static
-void RendererIPC::SendSync(const v8::FunctionCallbackInfo<v8::Value>& args) {
-  string16 channel;
-  scoped_ptr<base::Value> arguments;
-  if (!FromV8Arguments(args, &channel, &arguments))
-    return node::ThrowTypeError("Bad argument");
+string16 SendSync(const string16& channel, const base::ListValue& arguments) {
+  string16 json;
 
   RenderView* render_view = GetCurrentRenderView();
   if (render_view == NULL)
-    return;
+    return json;
 
-  string16 json;
   IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync(
-      render_view->GetRoutingID(),
-      channel,
-      *static_cast<base::ListValue*>(arguments.get()),
-      &json);
+      render_view->GetRoutingID(), channel, arguments, &json);
   // Enable the UI thread in browser to receive messages.
   message->EnableMessagePumping();
   bool success = render_view->Send(message);
 
   if (!success)
-    return node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
+    node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
 
-  args.GetReturnValue().Set(ToV8Value(json));
+  return json;
 }
 
-// static
-void RendererIPC::Initialize(v8::Handle<v8::Object> target) {
-  v8::HandleScope handle_scope(node_isolate);
-
-  NODE_SET_METHOD(target, "send", Send);
-  NODE_SET_METHOD(target, "sendSync", SendSync);
+void Initialize(v8::Handle<v8::Object> exports) {
+  mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent());
+  builder.SetMethod("send", &Send)
+         .SetMethod("sendSync", &SendSync);
+  exports->SetPrototype(builder.Build()->NewInstance());
 }
 
-}  // namespace api
-
-}  // namespace atom
+}  // namespace
 
-NODE_MODULE(atom_renderer_ipc, atom::api::RendererIPC::Initialize)
+NODE_MODULE(atom_renderer_ipc, Initialize)
diff --git a/atom/renderer/api/atom_api_renderer_ipc.h b/atom/renderer/api/atom_api_renderer_ipc.h
deleted file mode 100644 (file)
index 9158595..0000000
+++ /dev/null
@@ -1,30 +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_RENDERER_API_ATOM_API_RENDERER_IPC_H_
-#define ATOM_RENDERER_API_ATOM_API_RENDERER_IPC_H_
-
-#include "base/basictypes.h"
-#include "v8/include/v8.h"
-
-namespace atom {
-
-namespace api {
-
-class RendererIPC {
- public:
-  static void Initialize(v8::Handle<v8::Object> target);
-
- private:
-  static void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
-  static void SendSync(const v8::FunctionCallbackInfo<v8::Value>& args);
-
-  DISALLOW_IMPLICIT_CONSTRUCTORS(RendererIPC);
-};
-
-}  // namespace api
-
-}  // namespace atom
-
-#endif  // ATOM_RENDERER_API_ATOM_API_RENDERER_IPC_H_