From 2d6dc9c527d32678cd10a69ed9b13a02d4a9b35f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 14 Jan 2015 17:51:54 -0800 Subject: [PATCH] Convert arguments to V8 directly in EventEmitter::Emmit This gets rid of the extra conversion between ListValue. --- atom/browser/api/atom_api_app.cc | 9 +-- atom/browser/api/atom_api_auto_updater.cc | 14 +--- atom/browser/api/atom_api_protocol.cc | 4 +- atom/browser/api/atom_api_web_contents.cc | 64 ++++++------------- atom/browser/api/atom_api_window.cc | 16 ++--- atom/browser/api/event_emitter.cc | 45 ++----------- atom/browser/api/event_emitter.h | 40 +++++++----- .../native_mate_converters/value_converter.cc | 6 ++ .../native_mate_converters/value_converter.h | 2 + vendor/native_mate | 2 +- 10 files changed, 68 insertions(+), 134 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 6b0d7e478ec..98d46f895a7 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -12,7 +12,6 @@ #include "atom/browser/browser.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" -#include "base/values.h" #include "base/command_line.h" #include "base/environment.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) { - base::ListValue args; - args.AppendString(file_path); - *prevent_default = Emit("open-file", args); + *prevent_default = Emit("open-file", file_path); } void App::OnOpenURL(const std::string& url) { - base::ListValue args; - args.AppendString(url); - Emit("open-url", args); + Emit("open-url", url); } void App::OnActivateWithNoOpenWindows() { diff --git a/atom/browser/api/atom_api_auto_updater.cc b/atom/browser/api/atom_api_auto_updater.cc index 6a6a574b735..bf52c310b72 100644 --- a/atom/browser/api/atom_api_auto_updater.cc +++ b/atom/browser/api/atom_api_auto_updater.cc @@ -5,7 +5,6 @@ #include "atom/browser/api/atom_api_auto_updater.h" #include "base/time/time.h" -#include "base/values.h" #include "atom/browser/auto_updater.h" #include "atom/browser/browser.h" #include "native_mate/dictionary.h" @@ -26,9 +25,7 @@ AutoUpdater::~AutoUpdater() { } void AutoUpdater::OnError(const std::string& error) { - base::ListValue args; - args.AppendString(error); - Emit("error", args); + Emit("error", error); } void AutoUpdater::OnCheckingForUpdate() { @@ -49,13 +46,8 @@ void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes, const std::string& update_url, const base::Closure& quit_and_install) { quit_and_install_ = quit_and_install; - - base::ListValue args; - args.AppendString(release_notes); - args.AppendString(release_name); - args.AppendDouble(release_date.ToJsTime()); - args.AppendString(update_url); - Emit("update-downloaded-raw", args); + Emit("update-downloaded-raw", release_notes, release_name, + release_date.ToJsTime(), update_url); } mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 4ba8bb41718..446a4040467 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -305,9 +305,7 @@ void Protocol::UninterceptProtocolInIO(const std::string& scheme) { void Protocol::EmitEventInUI(const std::string& event, const std::string& parameter) { - base::ListValue args; - args.AppendString(parameter); - Emit(event, args); + Emit(event, parameter); } // static diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 398535b2dbe..dd2799a30e9 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -88,12 +88,7 @@ bool WebContents::AddMessageToConsole(content::WebContents* source, const base::string16& message, int32 line_no, const base::string16& source_id) { - base::ListValue args; - args.AppendInteger(level); - args.AppendString(message); - args.AppendInteger(line_no); - args.AppendString(source_id); - Emit("console-message", args); + Emit("console-message", level, message, line_no, source_id); return true; } @@ -105,11 +100,10 @@ bool WebContents::ShouldCreateWebContents( const GURL& target_url, const std::string& partition_id, content::SessionStorageNamespace* session_storage_namespace) { - base::ListValue args; - args.AppendString(target_url.spec()); - args.AppendString(frame_name); - args.AppendInteger(NEW_FOREGROUND_TAB); - Emit("-new-window", args); + Emit("-new-window", + target_url, + frame_name, + static_cast(NEW_FOREGROUND_TAB)); return false; } @@ -121,18 +115,12 @@ content::WebContents* WebContents::OpenURLFromTab( content::WebContents* source, const content::OpenURLParams& params) { if (params.disposition != CURRENT_TAB) { - base::ListValue args; - args.AppendString(params.url.spec()); - args.AppendString(""); - args.AppendInteger(params.disposition); - Emit("-new-window", args); + Emit("-new-window", params.url, "", static_cast(params.disposition)); return nullptr; } // Give user a chance to cancel navigation. - base::ListValue args; - args.AppendString(params.url.spec()); - if (Emit("will-navigate", args)) + if (Emit("will-navigate", params.url)) return nullptr; content::NavigationController::LoadURLParams load_url_params(params.url); @@ -184,10 +172,9 @@ void WebContents::HandleKeyboardEvent( } void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) { - base::ListValue args; - args.AppendInteger(render_view_host->GetProcess()->GetID()); - args.AppendInteger(render_view_host->GetRoutingID()); - Emit("render-view-deleted", args); + Emit("render-view-deleted", + render_view_host->GetProcess()->GetID(), + render_view_host->GetRoutingID()); } void WebContents::RenderProcessGone(base::TerminationStatus status) { @@ -197,10 +184,7 @@ void WebContents::RenderProcessGone(base::TerminationStatus status) { void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url) { bool is_main_frame = !render_frame_host->GetParent(); - - base::ListValue args; - args.AppendBoolean(is_main_frame); - Emit("did-frame-finish-load", args); + Emit("did-frame-finish-load", is_main_frame); if (is_main_frame) Emit("did-finish-load"); @@ -210,10 +194,7 @@ void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url, int error_code, const base::string16& error_description) { - base::ListValue args; - args.AppendInteger(error_code); - args.AppendString(error_description); - Emit("did-fail-load", args); + Emit("did-fail-load", error_code, error_description); } void WebContents::DidStartLoading(content::RenderViewHost* render_view_host) { @@ -227,12 +208,10 @@ void WebContents::DidStopLoading(content::RenderViewHost* render_view_host) { void WebContents::DidGetRedirectForResourceRequest( content::RenderViewHost* render_view_host, const content::ResourceRedirectDetails& details) { - base::ListValue args; - args.AppendString(details.url.spec()); - args.AppendString(details.new_url.spec()); - args.AppendBoolean( - details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME); - Emit("did-get-redirect-request", args); + Emit("did-get-redirect-request", + details.url, + details.new_url, + (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME)); } void WebContents::DidNavigateMainFrame( @@ -526,17 +505,14 @@ void WebContents::OnRendererMessageSync(const base::string16& channel, const base::ListValue& args, IPC::Message* message) { // 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, const gfx::Size& new_size) { - base::ListValue args; - args.AppendInteger(old_size.width()); - args.AppendInteger(old_size.height()); - args.AppendInteger(new_size.width()); - args.AppendInteger(new_size.height()); - Emit("size-changed", args); + Emit("size-changed", + old_size.width(), old_size.height(), + new_size.width(), new_size.height()); } // static diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 33bea26b8d1..30e386540b5 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -8,6 +8,8 @@ #include "atom/browser/browser.h" #include "atom/browser/native_window.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 "native_mate/callback.h" #include "native_mate/constructor.h" @@ -76,26 +78,18 @@ Window::~Window() { void Window::OnPageTitleUpdated(bool* prevent_default, const std::string& title) { - base::ListValue args; - args.AppendString(title); - *prevent_default = Emit("page-title-updated", args); + *prevent_default = Emit("page-title-updated", title); } void Window::WillCreatePopupWindow(const base::string16& frame_name, const GURL& target_url, const std::string& partition_id, WindowOpenDisposition disposition) { - base::ListValue args; - args.AppendString(target_url.spec()); - args.AppendString(frame_name); - args.AppendInteger(disposition); - Emit("-new-window", args); + Emit("-new-window", target_url, frame_name, static_cast(disposition)); } void Window::WillNavigate(bool* prevent_default, const GURL& url) { - base::ListValue args; - args.AppendString(url.spec()); - *prevent_default = Emit("-will-navigate", args); + *prevent_default = Emit("-will-navigate", url); } void Window::WillCloseWindow(bool* prevent_default) { diff --git a/atom/browser/api/event_emitter.cc b/atom/browser/api/event_emitter.cc index f594a2ad758..87ab69a16f8 100644 --- a/atom/browser/api/event_emitter.cc +++ b/atom/browser/api/event_emitter.cc @@ -5,9 +5,6 @@ #include "atom/browser/api/event_emitter.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/object_template_builder.h" @@ -41,43 +38,11 @@ v8::Local CreateEventObject(v8::Isolate* isolate) { EventEmitter::EventEmitter() { } -bool EventEmitter::Emit(const base::StringPiece& name) { - return Emit(name, base::ListValue()); -} - -bool EventEmitter::Emit(const base::StringPiece& name, - 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 context = isolate->GetCurrentContext(); - scoped_ptr 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) { +bool EventEmitter::CallEmit(v8::Isolate* isolate, + const base::StringPiece& name, + content::WebContents* sender, + IPC::Message* message, + ValueArray args) { v8::Handle event; bool use_native_event = sender && message; diff --git a/atom/browser/api/event_emitter.h b/atom/browser/api/event_emitter.h index 51d6c57c971..854076044bd 100644 --- a/atom/browser/api/event_emitter.h +++ b/atom/browser/api/event_emitter.h @@ -9,10 +9,6 @@ #include "native_mate/wrappable.h" -namespace base { -class ListValue; -} - namespace content { class WebContents; } @@ -26,29 +22,39 @@ namespace mate { // Provide helperers to emit event in JavaScript. class EventEmitter : public Wrappable { public: - typedef std::vector> Arguments; + typedef std::vector> ValueArray; protected: EventEmitter(); - // this.emit(name, new Event()); - bool Emit(const base::StringPiece& name); - // this.emit(name, new Event(), args...); - bool Emit(const base::StringPiece& name, const base::ListValue& args); + template + bool Emit(const base::StringPiece& name, const Args&... args) { + return EmitWithSender(name, nullptr, nullptr, args...); + } // this.emit(name, new Event(sender, message), args...); - bool Emit(const base::StringPiece& name, const base::ListValue& args, - content::WebContents* sender, IPC::Message* message); + template + 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. - bool Emit(v8::Isolate* isolate, - const base::StringPiece& name, - Arguments args, - content::WebContents* sender = nullptr, - IPC::Message* message = nullptr); + ValueArray converted = { ConvertToV8(isolate, args)... }; + return CallEmit(isolate, name, sender, message, converted); + } 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); }; diff --git a/atom/common/native_mate_converters/value_converter.cc b/atom/common/native_mate_converters/value_converter.cc index 67f7194a715..252c4c2abcb 100644 --- a/atom/common/native_mate_converters/value_converter.cc +++ b/atom/common/native_mate_converters/value_converter.cc @@ -37,4 +37,10 @@ bool Converter::FromV8(v8::Isolate* isolate, } } +v8::Handle Converter::ToV8( + v8::Isolate* isolate, + const base::ListValue& val) { + return v8::Undefined(isolate); +} + } // namespace mate diff --git a/atom/common/native_mate_converters/value_converter.h b/atom/common/native_mate_converters/value_converter.h index f487c34e24c..4810e6aa26f 100644 --- a/atom/common/native_mate_converters/value_converter.h +++ b/atom/common/native_mate_converters/value_converter.h @@ -26,6 +26,8 @@ struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Handle val, base::ListValue* out); + static v8::Handle ToV8(v8::Isolate* isolate, + const base::ListValue& val); }; } // namespace mate diff --git a/vendor/native_mate b/vendor/native_mate index 753d5675dd5..124b151614e 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 753d5675dd58aab3d3c429a233803ad4598802bc +Subproject commit 124b151614ea6358bc92d1c34c17577adb3a8071