2013-04-23 04:18:07 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/renderer/api/atom_api_renderer_ipc.h"
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/api/api_messages.h"
|
|
|
|
#include "atom/common/v8/native_type_conversions.h"
|
2013-04-23 04:18:07 +00:00
|
|
|
#include "content/public/renderer/render_view.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebFrame.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebView.h"
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/v8/node_common.h"
|
2013-04-23 04:18:07 +00:00
|
|
|
|
|
|
|
using content::RenderView;
|
|
|
|
using WebKit::WebFrame;
|
|
|
|
using WebKit::WebView;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
RenderView* GetCurrentRenderView() {
|
|
|
|
WebFrame* frame = WebFrame::frameForCurrentContext();
|
|
|
|
if (!frame)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
WebView* view = frame->view();
|
|
|
|
if (!view)
|
|
|
|
return NULL; // can happen during closing.
|
|
|
|
|
2013-12-05 15:34:43 +00:00
|
|
|
return RenderView::FromWebView(view);
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void RendererIPC::Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-05 14:12:27 +00:00
|
|
|
string16 channel;
|
2013-12-05 15:34:43 +00:00
|
|
|
scoped_ptr<base::Value> arguments;
|
|
|
|
if (!FromV8Arguments(args, &channel, &arguments))
|
2013-04-23 04:18:07 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
RenderView* render_view = GetCurrentRenderView();
|
2013-12-05 15:34:43 +00:00
|
|
|
if (render_view == NULL)
|
2013-12-11 07:48:19 +00:00
|
|
|
return;
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
bool success = render_view->Send(new AtomViewHostMsg_Message(
|
2013-04-23 04:18:07 +00:00
|
|
|
render_view->GetRoutingID(),
|
|
|
|
channel,
|
|
|
|
*static_cast<base::ListValue*>(arguments.get())));
|
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
if (!success)
|
|
|
|
return node::ThrowError("Unable to send AtomViewHostMsg_Message");
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void RendererIPC::SendSync(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-05 14:12:27 +00:00
|
|
|
string16 channel;
|
2013-12-05 15:34:43 +00:00
|
|
|
scoped_ptr<base::Value> arguments;
|
|
|
|
if (!FromV8Arguments(args, &channel, &arguments))
|
2013-04-23 13:52:19 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
RenderView* render_view = GetCurrentRenderView();
|
2013-12-05 15:34:43 +00:00
|
|
|
if (render_view == NULL)
|
2013-12-11 07:48:19 +00:00
|
|
|
return;
|
2013-04-23 13:52:19 +00:00
|
|
|
|
2013-09-20 14:55:42 +00:00
|
|
|
string16 json;
|
2013-07-10 04:36:02 +00:00
|
|
|
IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync(
|
2013-04-23 13:52:19 +00:00
|
|
|
render_view->GetRoutingID(),
|
|
|
|
channel,
|
|
|
|
*static_cast<base::ListValue*>(arguments.get()),
|
2013-09-20 14:32:59 +00:00
|
|
|
&json);
|
2013-07-10 04:36:02 +00:00
|
|
|
// Enable the UI thread in browser to receive messages.
|
|
|
|
message->EnableMessagePumping();
|
|
|
|
bool success = render_view->Send(message);
|
2013-04-23 13:52:19 +00:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
return node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(ToV8Value(json));
|
2013-04-23 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
// static
|
|
|
|
void RendererIPC::Initialize(v8::Handle<v8::Object> target) {
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
|
|
|
|
|
|
|
NODE_SET_METHOD(target, "send", Send);
|
|
|
|
NODE_SET_METHOD(target, "sendSync", SendSync);
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
NODE_MODULE(atom_renderer_ipc, atom::api::RendererIPC::Initialize)
|