From 6e2bf824f0e9fae7308b0e5dd90f7f6d1394e2cf Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 16 Apr 2014 12:29:16 +0800 Subject: [PATCH] Use native_mate to simplify renderer_ipc api. --- atom.gyp | 1 - atom/renderer/api/atom_api_renderer_ipc.cc | 95 ++++++++++++---------- atom/renderer/api/atom_api_renderer_ipc.h | 30 ------- 3 files changed, 53 insertions(+), 73 deletions(-) delete mode 100644 atom/renderer/api/atom_api_renderer_ipc.h diff --git a/atom.gyp b/atom.gyp index d40e956efc83..73429f2248e2 100644 --- a/atom.gyp +++ b/atom.gyp @@ -196,7 +196,6 @@ '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', diff --git a/atom/renderer/api/atom_api_renderer_ipc.cc b/atom/renderer/api/atom_api_renderer_ipc.cc index d95acf83f298..30a489f5854b 100644 --- a/atom/renderer/api/atom_api_renderer_ipc.cc +++ b/atom/renderer/api/atom_api_renderer_ipc.cc @@ -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 mate { -namespace api { +template<> +struct Converter { + static v8::Handle ToV8(v8::Isolate* isolate, + const string16& val) { + return v8::String::New(reinterpret_cast(val.data()), + val.size()); + } + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + string16* out) { + v8::String::Value s(val); + *out = string16(reinterpret_cast(*s), s.length()); + return true; + } +}; + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + base::ListValue* out) { + scoped_ptr converter(new atom::V8ValueConverter); + scoped_ptr value(converter->FromV8Value( + val, v8::Context::GetCurrent())); + if (value->IsType(base::Value::TYPE_LIST)) { + out->Swap(static_cast(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& args) { - string16 channel; - scoped_ptr 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(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& args) { - string16 channel; - scoped_ptr 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(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 target) { - v8::HandleScope handle_scope(node_isolate); - - NODE_SET_METHOD(target, "send", Send); - NODE_SET_METHOD(target, "sendSync", SendSync); +void Initialize(v8::Handle exports) { + mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); + builder.SetMethod("send", &Send) + .SetMethod("sendSync", &SendSync); + exports->SetPrototype(builder.Build()->NewInstance()); } -} // namespace api +} // namespace -} // namespace atom - -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 index 915859501a62..000000000000 --- a/atom/renderer/api/atom_api_renderer_ipc.h +++ /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 target); - - private: - static void Send(const v8::FunctionCallbackInfo& args); - static void SendSync(const v8::FunctionCallbackInfo& args); - - DISALLOW_IMPLICIT_CONSTRUCTORS(RendererIPC); -}; - -} // namespace api - -} // namespace atom - -#endif // ATOM_RENDERER_API_ATOM_API_RENDERER_IPC_H_