Pass synchronous messages by JSON string.

We are going to use IPC_MESSAGE_HANDLER_DELAY_REPLY to handle
synchronous messages but DictionaryValue is not copyable, so we pass the
JSON string instead.
This commit is contained in:
Cheng Zhao 2013-09-20 22:32:59 +08:00
parent 07b5039c64
commit ef5a4b5fe0
10 changed files with 44 additions and 20 deletions

View file

@ -39,6 +39,13 @@ v8::Handle<v8::Object> Event::CreateV8Object() {
return scope.Close(v8_event);
}
// static
std::string Event::GetReturnValue(v8::Handle<v8::Object> event) {
v8::HandleScope scope;
v8::Local<v8::Value> json = event->Get(v8::String::New("returnValue"));
return *v8::String::Utf8Value(json);
}
v8::Handle<v8::Value> Event::New(const v8::Arguments &args) {
Event* event = new Event;
event->Wrap(args.This());

View file

@ -5,6 +5,8 @@
#ifndef ATOM_BROWSER_ATOM_API_EVENT_H_
#define ATOM_BROWSER_ATOM_API_EVENT_H_
#include <string>
#include "base/basictypes.h"
#include "vendor/node/src/node_object_wrap.h"
@ -19,6 +21,9 @@ class Event : public node::ObjectWrap {
// Create a V8 Event object.
static v8::Handle<v8::Object> CreateV8Object();
// Get JSON string of the event.returnValue from a Event object.
static std::string GetReturnValue(v8::Handle<v8::Object> event);
// Accessor to return handle_, this follows Google C++ Style.
v8::Persistent<v8::Object>& handle() { return handle_; }

View file

@ -8,6 +8,7 @@
#include "base/logging.h"
#include "base/values.h"
#include "browser/api/atom_api_event.h"
#include "common/v8_value_converter_impl.h"
#include "content/public/browser/browser_thread.h"
#include "vendor/node/src/node.h"
@ -74,7 +75,7 @@ void AtomBrowserBindings::OnRendererMessageSync(
int routing_id,
const std::string& channel,
const base::ListValue& args,
base::DictionaryValue* result) {
std::string* result) {
v8::HandleScope scope;
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
@ -101,11 +102,7 @@ void AtomBrowserBindings::OnRendererMessageSync(
}
node::MakeCallback(node::process, "emit", arguments.size(), &arguments[0]);
scoped_ptr<base::Value> base_event(converter->FromV8Value(event, context));
DCHECK(base_event && base_event->IsType(base::Value::TYPE_DICTIONARY));
result->Swap(static_cast<base::DictionaryValue*>(base_event.get()));
*result = api::Event::GetReturnValue(event);
}
} // namespace atom

View file

@ -10,7 +10,6 @@
#include "common/api/atom_bindings.h"
namespace base {
class DictionaryValue;
class ListValue;
}
@ -35,7 +34,7 @@ class AtomBrowserBindings : public AtomBindings {
int routing_id,
const std::string& channel,
const base::ListValue& args,
base::DictionaryValue* result);
std::string* result);
// The require('atom').browserMainParts object.
v8::Handle<v8::Object> browser_main_parts() {

View file

@ -14,8 +14,15 @@ class Ipc extends EventEmitter
constructor: ->
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
@emit(args...)
process.on 'ATOM_INTERNAL_MESSAGE_SYNC', (args...) =>
@emit(args...)
process.on 'ATOM_INTERNAL_MESSAGE_SYNC', (channel, event, args...) =>
returnValue = 'null'
get = -> returnValue
set = (value) -> returnValue = JSON.stringify(value)
Object.defineProperty event, 'returnValue', {get, set}
Object.defineProperty event, 'result', {get, set}
@emit(channel, event, args...)
send: (processId, routingId, args...) ->
@sendChannel(processId, routingId, 'message', args...)