Convert arguments to V8 directly in EventEmitter::Emmit

This gets rid of the extra conversion between ListValue.
This commit is contained in:
Cheng Zhao 2015-01-14 17:51:54 -08:00
parent 65d2540807
commit 2d6dc9c527
10 changed files with 68 additions and 134 deletions

View file

@ -12,7 +12,6 @@
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
#include "base/values.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/environment.h" #include "base/environment.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
@ -124,15 +123,11 @@ void App::OnQuit() {
} }
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) { void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
base::ListValue args; *prevent_default = Emit("open-file", file_path);
args.AppendString(file_path);
*prevent_default = Emit("open-file", args);
} }
void App::OnOpenURL(const std::string& url) { void App::OnOpenURL(const std::string& url) {
base::ListValue args; Emit("open-url", url);
args.AppendString(url);
Emit("open-url", args);
} }
void App::OnActivateWithNoOpenWindows() { void App::OnActivateWithNoOpenWindows() {

View file

@ -5,7 +5,6 @@
#include "atom/browser/api/atom_api_auto_updater.h" #include "atom/browser/api/atom_api_auto_updater.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h"
#include "atom/browser/auto_updater.h" #include "atom/browser/auto_updater.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -26,9 +25,7 @@ AutoUpdater::~AutoUpdater() {
} }
void AutoUpdater::OnError(const std::string& error) { void AutoUpdater::OnError(const std::string& error) {
base::ListValue args; Emit("error", error);
args.AppendString(error);
Emit("error", args);
} }
void AutoUpdater::OnCheckingForUpdate() { void AutoUpdater::OnCheckingForUpdate() {
@ -49,13 +46,8 @@ void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
const std::string& update_url, const std::string& update_url,
const base::Closure& quit_and_install) { const base::Closure& quit_and_install) {
quit_and_install_ = quit_and_install; quit_and_install_ = quit_and_install;
Emit("update-downloaded-raw", release_notes, release_name,
base::ListValue args; release_date.ToJsTime(), update_url);
args.AppendString(release_notes);
args.AppendString(release_name);
args.AppendDouble(release_date.ToJsTime());
args.AppendString(update_url);
Emit("update-downloaded-raw", args);
} }
mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(

View file

@ -305,9 +305,7 @@ void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
void Protocol::EmitEventInUI(const std::string& event, void Protocol::EmitEventInUI(const std::string& event,
const std::string& parameter) { const std::string& parameter) {
base::ListValue args; Emit(event, parameter);
args.AppendString(parameter);
Emit(event, args);
} }
// static // static

View file

@ -88,12 +88,7 @@ bool WebContents::AddMessageToConsole(content::WebContents* source,
const base::string16& message, const base::string16& message,
int32 line_no, int32 line_no,
const base::string16& source_id) { const base::string16& source_id) {
base::ListValue args; Emit("console-message", level, message, line_no, source_id);
args.AppendInteger(level);
args.AppendString(message);
args.AppendInteger(line_no);
args.AppendString(source_id);
Emit("console-message", args);
return true; return true;
} }
@ -105,11 +100,10 @@ bool WebContents::ShouldCreateWebContents(
const GURL& target_url, const GURL& target_url,
const std::string& partition_id, const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) { content::SessionStorageNamespace* session_storage_namespace) {
base::ListValue args; Emit("-new-window",
args.AppendString(target_url.spec()); target_url,
args.AppendString(frame_name); frame_name,
args.AppendInteger(NEW_FOREGROUND_TAB); static_cast<int>(NEW_FOREGROUND_TAB));
Emit("-new-window", args);
return false; return false;
} }
@ -121,18 +115,12 @@ content::WebContents* WebContents::OpenURLFromTab(
content::WebContents* source, content::WebContents* source,
const content::OpenURLParams& params) { const content::OpenURLParams& params) {
if (params.disposition != CURRENT_TAB) { if (params.disposition != CURRENT_TAB) {
base::ListValue args; Emit("-new-window", params.url, "", static_cast<int>(params.disposition));
args.AppendString(params.url.spec());
args.AppendString("");
args.AppendInteger(params.disposition);
Emit("-new-window", args);
return nullptr; return nullptr;
} }
// Give user a chance to cancel navigation. // Give user a chance to cancel navigation.
base::ListValue args; if (Emit("will-navigate", params.url))
args.AppendString(params.url.spec());
if (Emit("will-navigate", args))
return nullptr; return nullptr;
content::NavigationController::LoadURLParams load_url_params(params.url); content::NavigationController::LoadURLParams load_url_params(params.url);
@ -184,10 +172,9 @@ void WebContents::HandleKeyboardEvent(
} }
void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) { void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
base::ListValue args; Emit("render-view-deleted",
args.AppendInteger(render_view_host->GetProcess()->GetID()); render_view_host->GetProcess()->GetID(),
args.AppendInteger(render_view_host->GetRoutingID()); render_view_host->GetRoutingID());
Emit("render-view-deleted", args);
} }
void WebContents::RenderProcessGone(base::TerminationStatus status) { void WebContents::RenderProcessGone(base::TerminationStatus status) {
@ -197,10 +184,7 @@ void WebContents::RenderProcessGone(base::TerminationStatus status) {
void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host, void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url) { const GURL& validated_url) {
bool is_main_frame = !render_frame_host->GetParent(); bool is_main_frame = !render_frame_host->GetParent();
Emit("did-frame-finish-load", is_main_frame);
base::ListValue args;
args.AppendBoolean(is_main_frame);
Emit("did-frame-finish-load", args);
if (is_main_frame) if (is_main_frame)
Emit("did-finish-load"); Emit("did-finish-load");
@ -210,10 +194,7 @@ void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url, const GURL& validated_url,
int error_code, int error_code,
const base::string16& error_description) { const base::string16& error_description) {
base::ListValue args; Emit("did-fail-load", error_code, error_description);
args.AppendInteger(error_code);
args.AppendString(error_description);
Emit("did-fail-load", args);
} }
void WebContents::DidStartLoading(content::RenderViewHost* render_view_host) { void WebContents::DidStartLoading(content::RenderViewHost* render_view_host) {
@ -227,12 +208,10 @@ void WebContents::DidStopLoading(content::RenderViewHost* render_view_host) {
void WebContents::DidGetRedirectForResourceRequest( void WebContents::DidGetRedirectForResourceRequest(
content::RenderViewHost* render_view_host, content::RenderViewHost* render_view_host,
const content::ResourceRedirectDetails& details) { const content::ResourceRedirectDetails& details) {
base::ListValue args; Emit("did-get-redirect-request",
args.AppendString(details.url.spec()); details.url,
args.AppendString(details.new_url.spec()); details.new_url,
args.AppendBoolean( (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME));
details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME);
Emit("did-get-redirect-request", args);
} }
void WebContents::DidNavigateMainFrame( void WebContents::DidNavigateMainFrame(
@ -526,17 +505,14 @@ void WebContents::OnRendererMessageSync(const base::string16& channel,
const base::ListValue& args, const base::ListValue& args,
IPC::Message* message) { IPC::Message* message) {
// webContents.emit(channel, new Event(sender, message), args...); // webContents.emit(channel, new Event(sender, message), args...);
Emit(base::UTF16ToUTF8(channel), args, web_contents(), message); EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
} }
void WebContents::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size, void WebContents::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
const gfx::Size& new_size) { const gfx::Size& new_size) {
base::ListValue args; Emit("size-changed",
args.AppendInteger(old_size.width()); old_size.width(), old_size.height(),
args.AppendInteger(old_size.height()); new_size.width(), new_size.height());
args.AppendInteger(new_size.width());
args.AppendInteger(new_size.height());
Emit("size-changed", args);
} }
// static // static

View file

@ -8,6 +8,8 @@
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "native_mate/callback.h" #include "native_mate/callback.h"
#include "native_mate/constructor.h" #include "native_mate/constructor.h"
@ -76,26 +78,18 @@ Window::~Window() {
void Window::OnPageTitleUpdated(bool* prevent_default, void Window::OnPageTitleUpdated(bool* prevent_default,
const std::string& title) { const std::string& title) {
base::ListValue args; *prevent_default = Emit("page-title-updated", title);
args.AppendString(title);
*prevent_default = Emit("page-title-updated", args);
} }
void Window::WillCreatePopupWindow(const base::string16& frame_name, void Window::WillCreatePopupWindow(const base::string16& frame_name,
const GURL& target_url, const GURL& target_url,
const std::string& partition_id, const std::string& partition_id,
WindowOpenDisposition disposition) { WindowOpenDisposition disposition) {
base::ListValue args; Emit("-new-window", target_url, frame_name, static_cast<int>(disposition));
args.AppendString(target_url.spec());
args.AppendString(frame_name);
args.AppendInteger(disposition);
Emit("-new-window", args);
} }
void Window::WillNavigate(bool* prevent_default, const GURL& url) { void Window::WillNavigate(bool* prevent_default, const GURL& url) {
base::ListValue args; *prevent_default = Emit("-will-navigate", url);
args.AppendString(url.spec());
*prevent_default = Emit("-will-navigate", args);
} }
void Window::WillCloseWindow(bool* prevent_default) { void Window::WillCloseWindow(bool* prevent_default) {

View file

@ -5,9 +5,6 @@
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/event_emitter.h"
#include "atom/browser/api/event.h" #include "atom/browser/api/event.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "native_mate/arguments.h" #include "native_mate/arguments.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
@ -41,43 +38,11 @@ v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate) {
EventEmitter::EventEmitter() { EventEmitter::EventEmitter() {
} }
bool EventEmitter::Emit(const base::StringPiece& name) { bool EventEmitter::CallEmit(v8::Isolate* isolate,
return Emit(name, base::ListValue()); const base::StringPiece& name,
} content::WebContents* sender,
IPC::Message* message,
bool EventEmitter::Emit(const base::StringPiece& name, ValueArray args) {
const base::ListValue& args) {
return Emit(name, args, NULL, NULL);
}
bool EventEmitter::Emit(const base::StringPiece& name,
const base::ListValue& args,
content::WebContents* sender,
IPC::Message* message) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
v8::Handle<v8::Context> context = isolate->GetCurrentContext();
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
// v8_args = [args...];
Arguments v8_args;
v8_args.reserve(args.GetSize());
for (size_t i = 0; i < args.GetSize(); i++) {
const base::Value* value(NULL);
if (args.Get(i, &value))
v8_args.push_back(converter->ToV8Value(value, context));
}
return Emit(isolate, name, v8_args, sender, message);
}
bool EventEmitter::Emit(v8::Isolate* isolate,
const base::StringPiece& name,
Arguments args,
content::WebContents* sender,
IPC::Message* message) {
v8::Handle<v8::Object> event; v8::Handle<v8::Object> event;
bool use_native_event = sender && message; bool use_native_event = sender && message;

View file

@ -9,10 +9,6 @@
#include "native_mate/wrappable.h" #include "native_mate/wrappable.h"
namespace base {
class ListValue;
}
namespace content { namespace content {
class WebContents; class WebContents;
} }
@ -26,29 +22,39 @@ namespace mate {
// Provide helperers to emit event in JavaScript. // Provide helperers to emit event in JavaScript.
class EventEmitter : public Wrappable { class EventEmitter : public Wrappable {
public: public:
typedef std::vector<v8::Handle<v8::Value>> Arguments; typedef std::vector<v8::Handle<v8::Value>> ValueArray;
protected: protected:
EventEmitter(); EventEmitter();
// this.emit(name, new Event());
bool Emit(const base::StringPiece& name);
// this.emit(name, new Event(), args...); // this.emit(name, new Event(), args...);
bool Emit(const base::StringPiece& name, const base::ListValue& args); template<typename... Args>
bool Emit(const base::StringPiece& name, const Args&... args) {
return EmitWithSender(name, nullptr, nullptr, args...);
}
// this.emit(name, new Event(sender, message), args...); // this.emit(name, new Event(sender, message), args...);
bool Emit(const base::StringPiece& name, const base::ListValue& args, template<typename... Args>
content::WebContents* sender, IPC::Message* message); bool EmitWithSender(const base::StringPiece& name,
content::WebContents* sender,
IPC::Message* message,
const Args&... args) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
// Lower level implementations. ValueArray converted = { ConvertToV8(isolate, args)... };
bool Emit(v8::Isolate* isolate, return CallEmit(isolate, name, sender, message, converted);
const base::StringPiece& name, }
Arguments args,
content::WebContents* sender = nullptr,
IPC::Message* message = nullptr);
private: private:
// Lower level implementations.
bool CallEmit(v8::Isolate* isolate,
const base::StringPiece& name,
content::WebContents* sender,
IPC::Message* message,
ValueArray args);
DISALLOW_COPY_AND_ASSIGN(EventEmitter); DISALLOW_COPY_AND_ASSIGN(EventEmitter);
}; };

View file

@ -37,4 +37,10 @@ bool Converter<base::ListValue>::FromV8(v8::Isolate* isolate,
} }
} }
v8::Handle<v8::Value> Converter<base::ListValue>::ToV8(
v8::Isolate* isolate,
const base::ListValue& val) {
return v8::Undefined(isolate);
}
} // namespace mate } // namespace mate

View file

@ -26,6 +26,8 @@ struct Converter<base::ListValue> {
static bool FromV8(v8::Isolate* isolate, static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val, v8::Handle<v8::Value> val,
base::ListValue* out); base::ListValue* out);
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
const base::ListValue& val);
}; };
} // namespace mate } // namespace mate

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit 753d5675dd58aab3d3c429a233803ad4598802bc Subproject commit 124b151614ea6358bc92d1c34c17577adb3a8071