Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert arguments to V8 directly in EventEmitter::Emit #1009

Merged
merged 8 commits into from
Jan 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert arguments to V8 directly in EventEmitter::Emmit
This gets rid of the extra conversion between ListValue.
  • Loading branch information
zcbenz committed Jan 15, 2015
commit 2d6dc9c527d32678cd10a69ed9b13a02d4a9b35f
9 changes: 2 additions & 7 deletions atom/browser/api/atom_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 3 additions & 11 deletions atom/browser/api/atom_api_auto_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions atom/browser/api/atom_api_protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 20 additions & 44 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<int>(NEW_FOREGROUND_TAB));
return false;
}

Expand All @@ -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<int>(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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions atom/browser/api/atom_api_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<int>(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) {
Expand Down
45 changes: 5 additions & 40 deletions atom/browser/api/event_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -41,43 +38,11 @@ v8::Local<v8::Object> 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<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) {
bool EventEmitter::CallEmit(v8::Isolate* isolate,
const base::StringPiece& name,
content::WebContents* sender,
IPC::Message* message,
ValueArray args) {
v8::Handle<v8::Object> event;
bool use_native_event = sender && message;

Expand Down
40 changes: 23 additions & 17 deletions atom/browser/api/event_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

#include "native_mate/wrappable.h"

namespace base {
class ListValue;
}

namespace content {
class WebContents;
}
Expand All @@ -26,29 +22,39 @@ namespace mate {
// Provide helperers to emit event in JavaScript.
class EventEmitter : public Wrappable {
public:
typedef std::vector<v8::Handle<v8::Value>> Arguments;
typedef std::vector<v8::Handle<v8::Value>> 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<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...);
bool Emit(const base::StringPiece& name, const base::ListValue& args,
content::WebContents* sender, IPC::Message* message);
template<typename... Args>
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);

ValueArray converted = { ConvertToV8(isolate, args)... };
return CallEmit(isolate, name, sender, message, converted);
}

private:
// Lower level implementations.
bool Emit(v8::Isolate* isolate,
const base::StringPiece& name,
Arguments args,
content::WebContents* sender = nullptr,
IPC::Message* message = nullptr);
bool CallEmit(v8::Isolate* isolate,
const base::StringPiece& name,
content::WebContents* sender,
IPC::Message* message,
ValueArray args);

private:
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
};

Expand Down
6 changes: 6 additions & 0 deletions atom/common/native_mate_converters/value_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions atom/common/native_mate_converters/value_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct Converter<base::ListValue> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
base::ListValue* out);
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
const base::ListValue& val);
};

} // namespace mate
Expand Down
2 changes: 1 addition & 1 deletion vendor/native_mate