Use native_mate to simplify renderer_ipc api.

This commit is contained in:
Cheng Zhao 2014-04-16 12:29:16 +08:00
parent 1ae30328d6
commit 6e2bf824f0
3 changed files with 53 additions and 73 deletions

View file

@ -196,7 +196,6 @@
'atom/common/v8/v8_value_converter.cc', 'atom/common/v8/v8_value_converter.cc',
'atom/common/v8/v8_value_converter.h', 'atom/common/v8/v8_value_converter.h',
'atom/renderer/api/atom_api_renderer_ipc.cc', '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.cc',
'atom/renderer/api/atom_renderer_bindings.h', 'atom/renderer/api/atom_renderer_bindings.h',
'atom/renderer/atom_render_view_observer.cc', 'atom/renderer/atom_render_view_observer.cc',

View file

@ -2,11 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "atom/renderer/api/atom_api_renderer_ipc.h"
#include "atom/common/api/api_messages.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 "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/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h" #include "third_party/WebKit/public/web/WebView.h"
@ -16,9 +15,42 @@ using content::RenderView;
using WebKit::WebFrame; using WebKit::WebFrame;
using WebKit::WebView; using WebKit::WebView;
namespace atom { namespace mate {
namespace api { 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 { namespace {
@ -34,65 +66,44 @@ RenderView* GetCurrentRenderView() {
return RenderView::FromWebView(view); return RenderView::FromWebView(view);
} }
} // namespace void Send(const string16& channel, const base::ListValue& arguments) {
// 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");
RenderView* render_view = GetCurrentRenderView(); RenderView* render_view = GetCurrentRenderView();
if (render_view == NULL) if (render_view == NULL)
return; return;
bool success = render_view->Send(new AtomViewHostMsg_Message( bool success = render_view->Send(new AtomViewHostMsg_Message(
render_view->GetRoutingID(), render_view->GetRoutingID(), channel, arguments));
channel,
*static_cast<base::ListValue*>(arguments.get())));
if (!success) if (!success)
return node::ThrowError("Unable to send AtomViewHostMsg_Message"); node::ThrowError("Unable to send AtomViewHostMsg_Message");
} }
// static string16 SendSync(const string16& channel, const base::ListValue& arguments) {
void RendererIPC::SendSync(const v8::FunctionCallbackInfo<v8::Value>& args) { string16 json;
string16 channel;
scoped_ptr<base::Value> arguments;
if (!FromV8Arguments(args, &channel, &arguments))
return node::ThrowTypeError("Bad argument");
RenderView* render_view = GetCurrentRenderView(); RenderView* render_view = GetCurrentRenderView();
if (render_view == NULL) if (render_view == NULL)
return; return json;
string16 json;
IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync( IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync(
render_view->GetRoutingID(), render_view->GetRoutingID(), channel, arguments, &json);
channel,
*static_cast<base::ListValue*>(arguments.get()),
&json);
// Enable the UI thread in browser to receive messages. // Enable the UI thread in browser to receive messages.
message->EnableMessagePumping(); message->EnableMessagePumping();
bool success = render_view->Send(message); bool success = render_view->Send(message);
if (!success) 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 Initialize(v8::Handle<v8::Object> exports) {
void RendererIPC::Initialize(v8::Handle<v8::Object> target) { mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent());
v8::HandleScope handle_scope(node_isolate); builder.SetMethod("send", &Send)
.SetMethod("sendSync", &SendSync);
NODE_SET_METHOD(target, "send", Send); exports->SetPrototype(builder.Build()->NewInstance());
NODE_SET_METHOD(target, "sendSync", SendSync);
} }
} // namespace api } // namespace
} // namespace atom NODE_MODULE(atom_renderer_ipc, Initialize)
NODE_MODULE(atom_renderer_ipc, atom::api::RendererIPC::Initialize)

View file

@ -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_