Use string16 instead of std::string when sending IPC messages.

The underlying V8::String is represented in UTF18, by using string16 in
IPC messages we can avoid the overhead of encode conversion.
This commit is contained in:
Cheng Zhao 2013-09-20 22:55:42 +08:00
parent ef5a4b5fe0
commit ef4b36d621
17 changed files with 68 additions and 60 deletions

View file

@ -157,6 +157,7 @@
'common/platform_util.h',
'common/platform_util_mac.mm',
'common/platform_util_win.cc',
'common/string16_conversions.h',
'common/v8_value_converter_impl.cc',
'common/v8_value_converter_impl.h',
'renderer/api/atom_api_renderer_ipc.cc',

View file

@ -6,6 +6,7 @@
#include "base/values.h"
#include "common/api/api_messages.h"
#include "common/string16_conversions.h"
#include "common/v8_value_converter_impl.h"
#include "content/public/browser/render_view_host.h"
#include "vendor/node/src/node.h"
@ -25,7 +26,7 @@ v8::Handle<v8::Value> BrowserIPC::Send(const v8::Arguments &args) {
if (!args[0]->IsString() || !args[1]->IsNumber() || !args[2]->IsNumber())
return node::ThrowTypeError("Bad argument");
std::string channel(*v8::String::Utf8Value(args[0]));
string16 channel(V8ValueToUTF16(args[0]));
int process_id = args[1]->IntegerValue();
int routing_id = args[2]->IntegerValue();

View file

@ -4,6 +4,8 @@
#include "browser/api/atom_api_event.h"
#include "common/string16_conversions.h"
using node::node_isolate;
namespace atom {
@ -40,10 +42,10 @@ v8::Handle<v8::Object> Event::CreateV8Object() {
}
// static
std::string Event::GetReturnValue(v8::Handle<v8::Object> event) {
string16 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);
return V8ValueToUTF16(json);
}
v8::Handle<v8::Value> Event::New(const v8::Arguments &args) {

View file

@ -5,9 +5,8 @@
#ifndef ATOM_BROWSER_ATOM_API_EVENT_H_
#define ATOM_BROWSER_ATOM_API_EVENT_H_
#include <string>
#include "base/basictypes.h"
#include "base/string16.h"
#include "vendor/node/src/node_object_wrap.h"
namespace atom {
@ -22,7 +21,7 @@ class Event : public node::ObjectWrap {
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);
static string16 GetReturnValue(v8::Handle<v8::Object> event);
// Accessor to return handle_, this follows Google C++ Style.
v8::Persistent<v8::Object>& handle() { return handle_; }

View file

@ -6,6 +6,7 @@
#include "browser/api/atom_api_window.h"
#include "browser/ui/accelerator_util.h"
#include "common/string16_conversions.h"
#define UNWRAP_MEMNU_AND_CHECK \
Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); \
@ -18,17 +19,6 @@ namespace api {
namespace {
// Converts a V8 value to a string16.
string16 V8ValueToUTF16(v8::Handle<v8::Value> value) {
v8::String::Value s(value);
return string16(reinterpret_cast<const char16*>(*s), s.length());
}
// Converts string16 to V8 String.
v8::Handle<v8::Value> UTF16ToV8Value(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
// Call method of delegate object.
v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
v8::Handle<v8::Object> menu,

View file

@ -6,6 +6,7 @@
#include "base/values.h"
#include "browser/native_window.h"
#include "common/string16_conversions.h"
#include "common/v8_value_converter_impl.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
@ -27,15 +28,6 @@ namespace atom {
namespace api {
namespace {
// Converts string16 to V8 String.
v8::Handle<v8::String> UTF16ToV8String(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
} // namespace
Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
: EventEmitter(wrapper),
window_(NativeWindow::Create(options)) {
@ -473,7 +465,7 @@ v8::Handle<v8::Value> Window::GetPageTitle(const v8::Arguments &args) {
string16 title = self->window_->GetWebContents()->GetTitle();
return UTF16ToV8String(title);
return UTF16ToV8Value(title);
}
// static

View file

@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/values.h"
#include "browser/api/atom_api_event.h"
#include "common/string16_conversions.h"
#include "common/v8_value_converter_impl.h"
#include "content/public/browser/browser_thread.h"
#include "vendor/node/src/node.h"
@ -43,7 +44,7 @@ void AtomBrowserBindings::AfterLoad() {
void AtomBrowserBindings::OnRendererMessage(int process_id,
int routing_id,
const std::string& channel,
const string16& channel,
const base::ListValue& args) {
v8::HandleScope scope;
@ -54,7 +55,7 @@ void AtomBrowserBindings::OnRendererMessage(int process_id,
// process.emit(channel, 'message', process_id, routing_id);
std::vector<v8::Handle<v8::Value>> arguments;
arguments.reserve(3 + args.GetSize());
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
arguments.push_back(UTF16ToV8Value(channel));
const base::Value* value;
if (args.Get(0, &value))
arguments.push_back(converter->ToV8Value(value, context));
@ -73,9 +74,9 @@ void AtomBrowserBindings::OnRendererMessage(int process_id,
void AtomBrowserBindings::OnRendererMessageSync(
int process_id,
int routing_id,
const std::string& channel,
const string16& channel,
const base::ListValue& args,
std::string* result) {
string16* result) {
v8::HandleScope scope;
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
@ -87,7 +88,7 @@ void AtomBrowserBindings::OnRendererMessageSync(
// process.emit(channel, 'sync-message', event, process_id, routing_id);
std::vector<v8::Handle<v8::Value>> arguments;
arguments.reserve(3 + args.GetSize());
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
arguments.push_back(UTF16ToV8Value(channel));
const base::Value* value;
if (args.Get(0, &value))
arguments.push_back(converter->ToV8Value(value, context));

View file

@ -5,8 +5,7 @@
#ifndef ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_
#define ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_
#include <string>
#include "base/string16.h"
#include "common/api/atom_bindings.h"
namespace base {
@ -26,15 +25,15 @@ class AtomBrowserBindings : public AtomBindings {
// Called when received a message from renderer.
void OnRendererMessage(int process_id,
int routing_id,
const std::string& channel,
const string16& channel,
const base::ListValue& args);
// Called when received a synchronous message from renderer.
void OnRendererMessageSync(int process_id,
int routing_id,
const std::string& channel,
const string16& channel,
const base::ListValue& args,
std::string* result);
string16* result);
// The require('atom').browserMainParts object.
v8::Handle<v8::Object> browser_main_parts() {

View file

@ -341,7 +341,7 @@ void NativeWindow::Observe(int type,
}
}
void NativeWindow::OnRendererMessage(const std::string& channel,
void NativeWindow::OnRendererMessage(const string16& channel,
const base::ListValue& args) {
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
GetWebContents()->GetRenderProcessHost()->GetID(),
@ -350,10 +350,10 @@ void NativeWindow::OnRendererMessage(const std::string& channel,
args);
}
void NativeWindow::OnRendererMessageSync(const std::string& channel,
void NativeWindow::OnRendererMessageSync(const string16& channel,
const base::ListValue& args,
IPC::Message* reply_msg) {
std::string json;
string16 json;
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessageSync(
GetWebContents()->GetRenderProcessHost()->GetID(),
GetWebContents()->GetRoutingID(),

View file

@ -177,10 +177,10 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
private:
void RendererUnresponsiveDelayed();
void OnRendererMessage(const std::string& channel,
void OnRendererMessage(const string16& channel,
const base::ListValue& args);
void OnRendererMessageSync(const std::string& channel,
void OnRendererMessageSync(const string16& channel,
const base::ListValue& args,
IPC::Message* reply_msg);

View file

@ -4,8 +4,7 @@
// Multiply-included file, no traditional include guard.
#include <string>
#include "base/string16.h"
#include "base/values.h"
#include "common/draggable_region.h"
#include "content/public/common/common_param_traits.h"
@ -22,16 +21,16 @@ IPC_STRUCT_TRAITS_BEGIN(atom::DraggableRegion)
IPC_STRUCT_TRAITS_END()
IPC_MESSAGE_ROUTED2(AtomViewHostMsg_Message,
std::string /* channel */,
string16 /* channel */,
ListValue /* arguments */)
IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync,
std::string /* channel */,
string16 /* channel */,
ListValue /* arguments */,
std::string /* result (in JSON) */)
string16 /* result (in JSON) */)
IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
std::string /* channel */,
string16 /* channel */,
ListValue /* arguments */)
// Sent by the renderer when the draggable regions are updated.

View file

@ -0,0 +1,23 @@
// 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 COMMON_STRING16_CONVERSIONS_H_
#define COMMON_STRING16_CONVERSIONS_H_
#include "v8/include/v8.h"
class string16;
// Converts a V8 value to a string16.
inline string16 V8ValueToUTF16(v8::Handle<v8::Value> value) {
v8::String::Value s(value);
return string16(reinterpret_cast<const char16*>(*s), s.length());
}
// Converts string16 to V8 String.
inline v8::Handle<v8::Value> UTF16ToV8Value(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
#endif // COMMON_STRING16_CONVERSIONS_H_

View file

@ -6,6 +6,7 @@
#include "base/values.h"
#include "common/api/api_messages.h"
#include "common/string16_conversions.h"
#include "content/public/renderer/render_view.h"
#include "content/public/renderer/v8_value_converter.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
@ -47,8 +48,7 @@ v8::Handle<v8::Value> RendererIPC::Send(const v8::Arguments &args) {
if (!args[0]->IsString())
return node::ThrowTypeError("Bad argument");
std::string channel(*v8::String::Utf8Value(args[0]));
string16 channel(V8ValueToUTF16(args[0]));
RenderView* render_view = GetCurrentRenderView();
// Convert Arguments to Array, so we can use V8ValueConverter to convert it
@ -82,7 +82,7 @@ v8::Handle<v8::Value> RendererIPC::SendSync(const v8::Arguments &args) {
return node::ThrowTypeError("Bad argument");
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
std::string channel(*v8::String::Utf8Value(args[0]));
string16 channel(V8ValueToUTF16(args[0]));
// Convert Arguments to Array, so we can use V8ValueConverter to convert it
// to ListValue.
@ -97,7 +97,7 @@ v8::Handle<v8::Value> RendererIPC::SendSync(const v8::Arguments &args) {
RenderView* render_view = GetCurrentRenderView();
std::string json;
string16 json;
IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync(
render_view->GetRoutingID(),
channel,
@ -110,7 +110,7 @@ v8::Handle<v8::Value> RendererIPC::SendSync(const v8::Arguments &args) {
if (!success)
return node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
return scope.Close(v8::String::New(json.data(), json.size()));
return scope.Close(UTF16ToV8Value(json));
}
// static

View file

@ -8,6 +8,7 @@
#include "base/logging.h"
#include "base/values.h"
#include "common/string16_conversions.h"
#include "content/public/renderer/render_view.h"
#include "content/public/renderer/v8_value_converter.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
@ -51,7 +52,7 @@ void AtomRendererBindings::BindToFrame(WebFrame* frame) {
AtomBindings::BindTo(GetProcessObject(context));
}
void AtomRendererBindings::OnBrowserMessage(const std::string& channel,
void AtomRendererBindings::OnBrowserMessage(const string16& channel,
const base::ListValue& args) {
if (!render_view_->GetWebView())
return;
@ -70,7 +71,7 @@ void AtomRendererBindings::OnBrowserMessage(const std::string& channel,
std::vector<v8::Handle<v8::Value>> arguments;
arguments.reserve(1 + args.GetSize());
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
arguments.push_back(UTF16ToV8Value(channel));
for (size_t i = 0; i < args.GetSize(); i++) {
const base::Value* value;

View file

@ -5,10 +5,10 @@
#ifndef ATOM_RENDERER_API_ATOM_RENDERER_BINDINGS_H_
#define ATOM_RENDERER_API_ATOM_RENDERER_BINDINGS_H_
#include <string>
#include "common/api/atom_bindings.h"
#include "base/string16.h"
namespace base {
class ListValue;
}
@ -32,7 +32,7 @@ class AtomRendererBindings : public AtomBindings {
void BindToFrame(WebKit::WebFrame* frame);
// Dispatch messages from browser.
void OnBrowserMessage(const std::string& channel,
void OnBrowserMessage(const string16& channel,
const base::ListValue& args);
private:

View file

@ -101,7 +101,7 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
return handled;
}
void AtomRenderViewObserver::OnBrowserMessage(const std::string& channel,
void AtomRenderViewObserver::OnBrowserMessage(const string16& channel,
const base::ListValue& args) {
atom_bindings()->OnBrowserMessage(channel, args);
}

View file

@ -35,7 +35,7 @@ class AtomRenderViewObserver : content::RenderViewObserver {
virtual void DraggableRegionsChanged(WebKit::WebFrame* frame) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
void OnBrowserMessage(const std::string& channel,
void OnBrowserMessage(const string16& channel,
const base::ListValue& args);
scoped_ptr<AtomRendererBindings> atom_bindings_;