diff --git a/atom.gyp b/atom.gyp index bd2ce11b234..47896d76bb9 100644 --- a/atom.gyp +++ b/atom.gyp @@ -157,7 +157,6 @@ 'common/platform_util.h', 'common/platform_util_mac.mm', 'common/platform_util_win.cc', - 'common/string16_conversions.h', 'common/v8_conversions.h', 'common/v8_value_converter_impl.cc', 'common/v8_value_converter_impl.h', diff --git a/browser/api/atom_api_app.cc b/browser/api/atom_api_app.cc index 51a1362b4b6..909bff3950c 100644 --- a/browser/api/atom_api_app.cc +++ b/browser/api/atom_api_app.cc @@ -7,6 +7,7 @@ #include "base/values.h" #include "base/command_line.h" #include "browser/browser.h" +#include "common/v8_conversions.h" #include "vendor/node/src/node.h" namespace atom { @@ -111,14 +112,14 @@ v8::Handle App::GetVersion(const v8::Arguments &args) { v8::Handle App::AppendSwitch(const v8::Arguments &args) { v8::HandleScope scope; - if (!args[0]->IsString()) + std::string switch_string; + if (!FromV8Arguments(args, &switch_string)) return node::ThrowError("Bad argument"); - std::string switch_string(*v8::String::Utf8Value(args[0])); if (args.Length() == 1) { CommandLine::ForCurrentProcess()->AppendSwitch(switch_string); } else { - std::string value(*v8::String::Utf8Value(args[1])); + std::string value = FromV8Value(args[1]); CommandLine::ForCurrentProcess()->AppendSwitchASCII( switch_string, value); } @@ -130,10 +131,10 @@ v8::Handle App::AppendSwitch(const v8::Arguments &args) { v8::Handle App::AppendArgument(const v8::Arguments &args) { v8::HandleScope scope; - if (!args[0]->IsString()) + std::string value; + if (!FromV8Arguments(args, &value)) return node::ThrowError("Bad argument"); - std::string value(*v8::String::Utf8Value(args[0])); CommandLine::ForCurrentProcess()->AppendArg(value); return v8::Undefined(); @@ -143,7 +144,7 @@ v8::Handle App::AppendArgument(const v8::Arguments &args) { // static v8::Handle App::DockBounce(const v8::Arguments& args) { - std::string type(*v8::String::Utf8Value(args[0])); + std::string type = FromV8Value(args[0]); int request_id = -1; if (type == "critical") @@ -158,21 +159,20 @@ v8::Handle App::DockBounce(const v8::Arguments& args) { // static v8::Handle App::DockCancelBounce(const v8::Arguments& args) { - Browser::Get()->DockCancelBounce(args[0]->IntegerValue()); + Browser::Get()->DockCancelBounce(FromV8Value(args[0])); return v8::Undefined(); } // static v8::Handle App::DockSetBadgeText(const v8::Arguments& args) { - std::string label(*v8::String::Utf8Value(args[0])); - Browser::Get()->DockSetBadgeText(label); + Browser::Get()->DockSetBadgeText(FromV8Value(args[0])); return v8::Undefined(); } // static v8::Handle App::DockGetBadgeText(const v8::Arguments& args) { std::string text(Browser::Get()->DockGetBadgeText()); - return v8::String::New(text.data(), text.size()); + return ToV8Value(text); } #endif // defined(OS_MACOSX) diff --git a/browser/api/atom_api_auto_updater.cc b/browser/api/atom_api_auto_updater.cc index e5e2013b6ad..29bdacc056d 100644 --- a/browser/api/atom_api_auto_updater.cc +++ b/browser/api/atom_api_auto_updater.cc @@ -6,6 +6,7 @@ #include "base/values.h" #include "browser/auto_updater.h" +#include "common/v8_conversions.h" namespace atom { @@ -56,7 +57,7 @@ v8::Handle AutoUpdater::New(const v8::Arguments &args) { // static v8::Handle AutoUpdater::SetFeedURL(const v8::Arguments &args) { - auto_updater::AutoUpdater::SetFeedURL(*v8::String::Utf8Value(args[0])); + auto_updater::AutoUpdater::SetFeedURL(FromV8Value(args[0])); return v8::Undefined(); } diff --git a/browser/api/atom_api_browser_ipc.cc b/browser/api/atom_api_browser_ipc.cc index 763a3dfba70..7e43e1edb66 100644 --- a/browser/api/atom_api_browser_ipc.cc +++ b/browser/api/atom_api_browser_ipc.cc @@ -6,7 +6,7 @@ #include "base/values.h" #include "common/api/api_messages.h" -#include "common/string16_conversions.h" +#include "common/v8_conversions.h" #include "common/v8_value_converter_impl.h" #include "content/public/browser/render_view_host.h" #include "vendor/node/src/node.h" @@ -23,13 +23,11 @@ namespace api { v8::Handle BrowserIPC::Send(const v8::Arguments &args) { v8::HandleScope scope; - if (!args[0]->IsString() || !args[1]->IsNumber() || !args[2]->IsNumber()) + string16 channel; + int process_id, routing_id; + if (!FromV8Arguments(args, &channel, &process_id, &routing_id)) return node::ThrowTypeError("Bad argument"); - string16 channel(FromV8Value(args[0])); - int process_id = args[1]->IntegerValue(); - int routing_id = args[2]->IntegerValue(); - RenderViewHost* render_view_host(RenderViewHost::FromID( process_id, routing_id)); if (!render_view_host) diff --git a/browser/api/atom_api_crash_reporter.cc b/browser/api/atom_api_crash_reporter.cc index 2b23db1d1a6..1c5d85de28e 100644 --- a/browser/api/atom_api_crash_reporter.cc +++ b/browser/api/atom_api_crash_reporter.cc @@ -5,6 +5,7 @@ #include "browser/api/atom_api_crash_reporter.h" #include "browser/crash_reporter.h" +#include "common/v8_conversions.h" #include "vendor/node/src/node.h" #include "vendor/node/src/node_internals.h" @@ -14,22 +15,20 @@ namespace api { // static v8::Handle CrashReporter::SetCompanyName(const v8::Arguments &args) { - std::string name(*v8::String::Utf8Value(args[0])); - crash_reporter::CrashReporter::SetCompanyName(name); + crash_reporter::CrashReporter::SetCompanyName(FromV8Value(args[0])); return v8::Undefined(); } // static v8::Handle CrashReporter::SetSubmissionURL( const v8::Arguments &args) { - std::string url(*v8::String::Utf8Value(args[0])); - crash_reporter::CrashReporter::SetSubmissionURL(url); + crash_reporter::CrashReporter::SetSubmissionURL(FromV8Value(args[0])); return v8::Undefined(); } // static v8::Handle CrashReporter::SetAutoSubmit(const v8::Arguments &args) { - crash_reporter::CrashReporter::SetAutoSubmit(args[0]->BooleanValue()); + crash_reporter::CrashReporter::SetAutoSubmit(FromV8Value(args[0])); return v8::Undefined(); } diff --git a/browser/api/atom_api_event.cc b/browser/api/atom_api_event.cc index 7ab14467042..26f8981b1fc 100644 --- a/browser/api/atom_api_event.cc +++ b/browser/api/atom_api_event.cc @@ -6,7 +6,7 @@ #include "browser/native_window.h" #include "common/api/api_messages.h" -#include "common/string16_conversions.h" +#include "common/v8_conversions.h" using node::node_isolate; diff --git a/browser/api/atom_api_menu.cc b/browser/api/atom_api_menu.cc index afb7e157757..a057f307eb9 100644 --- a/browser/api/atom_api_menu.cc +++ b/browser/api/atom_api_menu.cc @@ -5,7 +5,6 @@ #include "browser/api/atom_api_menu.h" #include "browser/ui/accelerator_util.h" -#include "common/string16_conversions.h" #include "common/v8_conversions.h" #define UNWRAP_MEMNU_AND_CHECK \ @@ -83,7 +82,7 @@ bool Menu::GetAcceleratorForCommandId(int command_id, "getAcceleratorForCommandId", command_id); if (shortcut->IsString()) { - std::string shortcut_str(*v8::String::Utf8Value(shortcut)); + std::string shortcut_str = FromV8Value(shortcut); return accelerator_util::StringToAccelerator(shortcut_str, accelerator); } @@ -135,16 +134,15 @@ v8::Handle Menu::New(const v8::Arguments &args) { v8::Handle Menu::InsertItem(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) + int index, command_id; + string16 label; + if (!FromV8Arguments(args, &index, &command_id, &label)) return node::ThrowTypeError("Bad argument"); - int index = args[0]->IntegerValue(); - if (index < 0) - self->model_->AddItem(args[1]->IntegerValue(), FromV8Value(args[2])); + self->model_->AddItem(command_id, label); else - self->model_->InsertItemAt( - index, args[1]->IntegerValue(), FromV8Value(args[2])); + self->model_->InsertItemAt(index, command_id, label); return v8::Undefined(); } @@ -153,16 +151,15 @@ v8::Handle Menu::InsertItem(const v8::Arguments &args) { v8::Handle Menu::InsertCheckItem(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) + int index, command_id; + string16 label; + if (!FromV8Arguments(args, &index, &command_id, &label)) return node::ThrowTypeError("Bad argument"); - int index = args[0]->IntegerValue(); - int command_id = args[1]->IntegerValue(); - if (index < 0) - self->model_->AddCheckItem(command_id, FromV8Value(args[2])); + self->model_->AddCheckItem(command_id, label); else - self->model_->InsertCheckItemAt(index, command_id, FromV8Value(args[2])); + self->model_->InsertCheckItemAt(index, command_id, label); return v8::Undefined(); } @@ -171,21 +168,15 @@ v8::Handle Menu::InsertCheckItem(const v8::Arguments &args) { v8::Handle Menu::InsertRadioItem(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || - !args[1]->IsNumber() || - !args[2]->IsString() || - !args[3]->IsNumber()) + int index, command_id, group_id; + string16 label; + if (!FromV8Arguments(args, &index, &command_id, &label, &group_id)) return node::ThrowTypeError("Bad argument"); - int index = args[0]->IntegerValue(); - int command_id = args[1]->IntegerValue(); - int group_id = args[3]->IntegerValue(); - if (index < 0) - self->model_->AddRadioItem(command_id, FromV8Value(args[2]), group_id); + self->model_->AddRadioItem(command_id, label, group_id); else - self->model_->InsertRadioItemAt( - index, command_id, FromV8Value(args[2]), group_id); + self->model_->InsertRadioItemAt(index, command_id, label, group_id); return v8::Undefined(); } @@ -194,11 +185,10 @@ v8::Handle Menu::InsertRadioItem(const v8::Arguments &args) { v8::Handle Menu::InsertSeparator(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber()) + int index; + if (!FromV8Arguments(args, &index)) return node::ThrowTypeError("Bad argument"); - int index = args[0]->IntegerValue(); - if (index < 0) self->model_->AddSeparator(ui::NORMAL_SEPARATOR); else @@ -211,25 +201,20 @@ v8::Handle Menu::InsertSeparator(const v8::Arguments &args) { v8::Handle Menu::InsertSubMenu(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || - !args[1]->IsNumber() || - !args[2]->IsString() || - !args[3]->IsObject()) + int index, command_id; + string16 label; + if (!FromV8Arguments(args, &index, &command_id, &label)) return node::ThrowTypeError("Bad argument"); Menu* submenu = ObjectWrap::Unwrap(args[3]->ToObject()); if (!submenu) return node::ThrowTypeError("The submenu is already destroyed"); - int index = args[0]->IntegerValue(); - int command_id = args[1]->IntegerValue(); - if (index < 0) - self->model_->AddSubMenu( - command_id, FromV8Value(args[2]), submenu->model_.get()); + self->model_->AddSubMenu(command_id, label, submenu->model_.get()); else self->model_->InsertSubMenuAt( - index, command_id, FromV8Value(args[2]), submenu->model_.get()); + index, command_id, label, submenu->model_.get()); return v8::Undefined(); } @@ -238,7 +223,9 @@ v8::Handle Menu::InsertSubMenu(const v8::Arguments &args) { v8::Handle Menu::SetIcon(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || !args[1]->IsString()) + int index; + base::FilePath path; + if (!FromV8Arguments(args, &index, &path)) return node::ThrowTypeError("Bad argument"); // FIXME use webkit_glue's image decoder here. @@ -250,10 +237,12 @@ v8::Handle Menu::SetIcon(const v8::Arguments &args) { v8::Handle Menu::SetSublabel(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - if (!args[0]->IsNumber() || !args[1]->IsString()) + int index; + string16 label; + if (!FromV8Arguments(args, &index, &label)) return node::ThrowTypeError("Bad argument"); - self->model_->SetSublabel(args[0]->IntegerValue(), FromV8Value(args[1])); + self->model_->SetSublabel(index, label); return v8::Undefined(); } @@ -324,11 +313,11 @@ v8::Handle Menu::IsVisibleAt(const v8::Arguments &args) { v8::Handle Menu::Popup(const v8::Arguments &args) { UNWRAP_MEMNU_AND_CHECK; - Window* window = Window::Unwrap(args[0]->ToObject()); - if (!window) - return node::ThrowTypeError("Invalid window"); + atom::NativeWindow* window; + if (!FromV8Arguments(args, &window)) + return node::ThrowTypeError("Bad argument"); - self->Popup(window->window()); + self->Popup(window); return v8::Undefined(); } diff --git a/browser/api/atom_api_menu_mac.mm b/browser/api/atom_api_menu_mac.mm index acf4849435e..89270190baa 100644 --- a/browser/api/atom_api_menu_mac.mm +++ b/browser/api/atom_api_menu_mac.mm @@ -8,6 +8,7 @@ #include "base/mac/scoped_sending_event.h" #include "base/strings/sys_string_conversions.h" #include "browser/native_window.h" +#include "common/v8_conversions.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" @@ -95,10 +96,10 @@ v8::Handle Menu::SendActionToFirstResponder( const v8::Arguments &args) { v8::HandleScope scope; - if (!args[0]->IsString()) + std::string action; + if (!FromV8Arguments(args, &action)) return node::ThrowTypeError("Bad argument"); - std::string action(*v8::String::Utf8Value(args[0])); MenuMac::SendActionToFirstResponder(action); return v8::Undefined(); diff --git a/browser/api/atom_api_protocol.cc b/browser/api/atom_api_protocol.cc index 6f0af3dfeb7..a1ae29223e4 100644 --- a/browser/api/atom_api_protocol.cc +++ b/browser/api/atom_api_protocol.cc @@ -9,6 +9,7 @@ #include "browser/net/adapter_request_job.h" #include "browser/net/atom_url_request_context_getter.h" #include "browser/net/atom_url_request_job_factory.h" +#include "common/v8_conversions.h" #include "content/public/browser/browser_thread.h" #include "net/url_request/url_request_context.h" #include "vendor/node/src/node.h" @@ -36,9 +37,9 @@ static const char* kEarlyUseProtocolError = "This method can only be used" void EmitEventInUI(const std::string& event, const std::string& parameter) { v8::HandleScope scope; - v8::Local argv[] = { - v8::String::New(event.data(), event.size()), - v8::String::New(parameter.data(), parameter.size()), + v8::Handle argv[] = { + ToV8Value(event), + ToV8Value(parameter), }; node::MakeCallback(g_protocol_object, "emit", arraysize(argv), argv); } @@ -83,7 +84,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob { // Determine the type of the job we are going to create. if (result->IsString()) { - std::string data = *v8::String::Utf8Value(result); + std::string data = FromV8Value(result); content::BrowserThread::PostTask( content::BrowserThread::IO, FROM_HERE, @@ -95,14 +96,12 @@ class CustomProtocolRequestJob : public AdapterRequestJob { return; } else if (result->IsObject()) { v8::Handle obj = result->ToObject(); - std::string name = *v8::String::Utf8Value(obj->GetConstructorName()); + std::string name = FromV8Value(obj->GetConstructorName()); if (name == "RequestStringJob") { - std::string mime_type = *v8::String::Utf8Value(obj->Get( + std::string mime_type = FromV8Value(obj->Get( v8::String::New("mimeType"))); - std::string charset = *v8::String::Utf8Value(obj->Get( - v8::String::New("charset"))); - std::string data = *v8::String::Utf8Value(obj->Get( - v8::String::New("data"))); + std::string charset = FromV8Value(obj->Get(v8::String::New("charset"))); + std::string data = FromV8Value(obj->Get(v8::String::New("data"))); content::BrowserThread::PostTask( content::BrowserThread::IO, @@ -114,8 +113,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob { data)); return; } else if (name == "RequestFileJob") { - base::FilePath path = base::FilePath::FromUTF8Unsafe( - *v8::String::Utf8Value(obj->Get(v8::String::New("path")))); + base::FilePath path = FromV8Value(obj->Get(v8::String::New("path"))); content::BrowserThread::PostTask( content::BrowserThread::IO, @@ -183,7 +181,11 @@ class CustomProtocolHandler : public ProtocolHandler { // static v8::Handle Protocol::RegisterProtocol(const v8::Arguments& args) { - std::string scheme(*v8::String::Utf8Value(args[0])); + std::string scheme; + v8::Persistent callback; + if (!FromV8Arguments(args, &scheme, &callback)) + return node::ThrowTypeError("Bad argument"); + if (g_handlers.find(scheme) != g_handlers.end() || net::URLRequest::IsHandledProtocol(scheme)) return node::ThrowError("The scheme is already registered"); @@ -192,10 +194,7 @@ v8::Handle Protocol::RegisterProtocol(const v8::Arguments& args) { return node::ThrowError(kEarlyUseProtocolError); // Store the handler in a map. - if (!args[1]->IsFunction()) - return node::ThrowError("Handler must be a function"); - g_handlers[scheme] = v8::Persistent::New( - node::node_isolate, v8::Handle::Cast(args[1])); + g_handlers[scheme] = callback; content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, @@ -206,7 +205,9 @@ v8::Handle Protocol::RegisterProtocol(const v8::Arguments& args) { // static v8::Handle Protocol::UnregisterProtocol(const v8::Arguments& args) { - std::string scheme(*v8::String::Utf8Value(args[0])); + std::string scheme; + if (!FromV8Arguments(args, &scheme)) + return node::ThrowTypeError("Bad argument"); if (AtomBrowserContext::Get()->url_request_context_getter() == NULL) return node::ThrowError(kEarlyUseProtocolError); @@ -226,13 +227,16 @@ v8::Handle Protocol::UnregisterProtocol(const v8::Arguments& args) { // static v8::Handle Protocol::IsHandledProtocol(const v8::Arguments& args) { - return v8::Boolean::New(net::URLRequest::IsHandledProtocol( - *v8::String::Utf8Value(args[0]))); + return ToV8Value(net::URLRequest::IsHandledProtocol(FromV8Value(args[0]))); } // static v8::Handle Protocol::InterceptProtocol(const v8::Arguments& args) { - std::string scheme(*v8::String::Utf8Value(args[0])); + std::string scheme; + v8::Persistent callback; + if (!FromV8Arguments(args, &scheme, &callback)) + return node::ThrowTypeError("Bad argument"); + if (!GetRequestJobFactory()->HasProtocolHandler(scheme)) return node::ThrowError("Cannot intercept procotol"); @@ -243,10 +247,7 @@ v8::Handle Protocol::InterceptProtocol(const v8::Arguments& args) { return node::ThrowError(kEarlyUseProtocolError); // Store the handler in a map. - if (!args[1]->IsFunction()) - return node::ThrowError("Handler must be a function"); - g_handlers[scheme] = v8::Persistent::New( - node::node_isolate, v8::Handle::Cast(args[1])); + g_handlers[scheme] = callback; content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, @@ -256,7 +257,9 @@ v8::Handle Protocol::InterceptProtocol(const v8::Arguments& args) { // static v8::Handle Protocol::UninterceptProtocol(const v8::Arguments& args) { - std::string scheme(*v8::String::Utf8Value(args[0])); + std::string scheme; + if (!FromV8Arguments(args, &scheme)) + return node::ThrowTypeError("Bad argument"); if (AtomBrowserContext::Get()->url_request_context_getter() == NULL) return node::ThrowError(kEarlyUseProtocolError); diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index be582d87794..64ee74b9f9d 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -6,7 +6,7 @@ #include "base/values.h" #include "browser/native_window.h" -#include "common/string16_conversions.h" +#include "common/v8_conversions.h" #include "common/v8_value_converter_impl.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/web_contents.h" @@ -133,7 +133,7 @@ v8::Handle Window::Focus(const v8::Arguments &args) { // static v8::Handle Window::IsFocused(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsFocused()); + return ToV8Value(self->window_->IsFocused()); } // static @@ -194,10 +194,11 @@ v8::Handle Window::Restore(const v8::Arguments &args) { v8::Handle Window::SetFullscreen(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsBoolean()) + bool fs; + if (!FromV8Arguments(args, &fs)) return node::ThrowTypeError("Bad argument"); - self->window_->SetFullscreen(args[0]->BooleanValue()); + self->window_->SetFullscreen(fs); return v8::Undefined(); } @@ -205,18 +206,18 @@ v8::Handle Window::SetFullscreen(const v8::Arguments &args) { v8::Handle Window::IsFullscreen(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsFullscreen()); + return ToV8Value(self->window_->IsFullscreen()); } // static v8::Handle Window::SetSize(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 2) + int width, height; + if (!FromV8Arguments(args, &width, &height)) return node::ThrowTypeError("Bad argument"); - self->window_->SetSize( - gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + self->window_->SetSize(gfx::Size(width, height)); return v8::Undefined(); } @@ -226,8 +227,8 @@ v8::Handle Window::GetSize(const v8::Arguments &args) { gfx::Size size = self->window_->GetSize(); v8::Handle ret = v8::Array::New(2); - ret->Set(0, v8::Integer::New(size.width())); - ret->Set(1, v8::Integer::New(size.height())); + ret->Set(0, ToV8Value(size.width())); + ret->Set(1, ToV8Value(size.height())); return ret; } @@ -236,11 +237,11 @@ v8::Handle Window::GetSize(const v8::Arguments &args) { v8::Handle Window::SetMinimumSize(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 2) + int width, height; + if (!FromV8Arguments(args, &width, &height)) return node::ThrowTypeError("Bad argument"); - self->window_->SetMinimumSize( - gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + self->window_->SetMinimumSize(gfx::Size(width, height)); return v8::Undefined(); } @@ -250,8 +251,8 @@ v8::Handle Window::GetMinimumSize(const v8::Arguments &args) { gfx::Size size = self->window_->GetMinimumSize(); v8::Handle ret = v8::Array::New(2); - ret->Set(0, v8::Integer::New(size.width())); - ret->Set(1, v8::Integer::New(size.height())); + ret->Set(0, ToV8Value(size.width())); + ret->Set(1, ToV8Value(size.height())); return ret; } @@ -260,11 +261,12 @@ v8::Handle Window::GetMinimumSize(const v8::Arguments &args) { v8::Handle Window::SetMaximumSize(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 2) - return node::ThrowTypeError("Bad argument"); - self->window_->SetMaximumSize( - gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + int width, height; + if (!FromV8Arguments(args, &width, &height)) + return node::ThrowTypeError("Bad argument"); + + self->window_->SetMaximumSize(gfx::Size(width, height)); return v8::Undefined(); } @@ -274,8 +276,8 @@ v8::Handle Window::GetMaximumSize(const v8::Arguments &args) { gfx::Size size = self->window_->GetMaximumSize(); v8::Handle ret = v8::Array::New(2); - ret->Set(0, v8::Integer::New(size.width())); - ret->Set(1, v8::Integer::New(size.height())); + ret->Set(0, ToV8Value(size.width())); + ret->Set(1, ToV8Value(size.height())); return ret; } @@ -284,10 +286,11 @@ v8::Handle Window::GetMaximumSize(const v8::Arguments &args) { v8::Handle Window::SetResizable(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsBoolean()) + bool resizable; + if (!FromV8Arguments(args, &resizable)) return node::ThrowTypeError("Bad argument"); - self->window_->SetResizable(args[0]->BooleanValue()); + self->window_->SetResizable(resizable); return v8::Undefined(); } @@ -295,17 +298,18 @@ v8::Handle Window::SetResizable(const v8::Arguments &args) { v8::Handle Window::IsResizable(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsResizable()); + return ToV8Value(self->window_->IsResizable()); } // static v8::Handle Window::SetAlwaysOnTop(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsBoolean()) + bool top; + if (!FromV8Arguments(args, &top)) return node::ThrowTypeError("Bad argument"); - self->window_->SetAlwaysOnTop(args[0]->BooleanValue()); + self->window_->SetAlwaysOnTop(top); return v8::Undefined(); } @@ -313,7 +317,7 @@ v8::Handle Window::SetAlwaysOnTop(const v8::Arguments &args) { v8::Handle Window::IsAlwaysOnTop(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsAlwaysOnTop()); + return ToV8Value(self->window_->IsAlwaysOnTop()); } // static @@ -329,11 +333,11 @@ v8::Handle Window::Center(const v8::Arguments &args) { v8::Handle Window::SetPosition(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 2) + int x, y; + if (!FromV8Arguments(args, &x, &y)) return node::ThrowTypeError("Bad argument"); - self->window_->SetPosition( - gfx::Point(args[0]->IntegerValue(), args[1]->IntegerValue())); + self->window_->SetPosition(gfx::Point(x, y)); return v8::Undefined(); } @@ -343,8 +347,8 @@ v8::Handle Window::GetPosition(const v8::Arguments &args) { gfx::Point pos = self->window_->GetPosition(); v8::Handle ret = v8::Array::New(2); - ret->Set(0, v8::Integer::New(pos.x())); - ret->Set(1, v8::Integer::New(pos.y())); + ret->Set(0, ToV8Value(pos.x())); + ret->Set(1, ToV8Value(pos.y())); return ret; } @@ -353,20 +357,18 @@ v8::Handle Window::GetPosition(const v8::Arguments &args) { v8::Handle Window::SetTitle(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsString()) + std::string title; + if (!FromV8Arguments(args, &title)) return node::ThrowTypeError("Bad argument"); - self->window_->SetTitle(*v8::String::Utf8Value(args[0])); + self->window_->SetTitle(title); return v8::Undefined(); } // static v8::Handle Window::GetTitle(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - - std::string title = self->window_->GetTitle(); - - return v8::String::New(title.c_str(), title.size()); + return ToV8Value(self->window_->GetTitle()); } // static @@ -383,10 +385,11 @@ v8::Handle Window::FlashFrame(const v8::Arguments &args) { v8::Handle Window::SetKiosk(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsBoolean()) + bool kiosk; + if (!FromV8Arguments(args, &kiosk)) return node::ThrowTypeError("Bad argument"); - self->window_->SetKiosk(args[0]->BooleanValue()); + self->window_->SetKiosk(kiosk); return v8::Undefined(); } @@ -394,7 +397,7 @@ v8::Handle Window::SetKiosk(const v8::Arguments &args) { v8::Handle Window::IsKiosk(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsKiosk()); + return ToV8Value(self->window_->IsKiosk()); } // static @@ -419,9 +422,11 @@ v8::Handle Window::CloseDevTools(const v8::Arguments &args) { v8::Handle Window::InspectElement(const v8::Arguments& args) { UNWRAP_WINDOW_AND_CHECK; - self->window_->InspectElement(args[0]->IntegerValue(), - args[1]->IntegerValue()); + int x, y; + if (!FromV8Arguments(args, &x, &y)) + return node::ThrowTypeError("Bad argument"); + self->window_->InspectElement(x, y); return v8::Undefined(); } @@ -446,7 +451,7 @@ v8::Handle Window::BlurWebView(const v8::Arguments &args) { // static v8::Handle Window::IsWebViewFocused(const v8::Arguments& args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->IsWebViewFocused()); + return ToV8Value(self->window_->IsWebViewFocused()); } // static @@ -463,24 +468,21 @@ v8::Handle Window::RestartHangMonitorTimeout( v8::Handle Window::GetPageTitle(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - string16 title = self->window_->GetWebContents()->GetTitle(); - - return ToV8Value(title); + return ToV8Value(self->window_->GetWebContents()->GetTitle()); } // static v8::Handle Window::IsLoading(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->GetWebContents()->IsLoading()); + return ToV8Value(self->window_->GetWebContents()->IsLoading()); } // static v8::Handle Window::IsWaitingForResponse(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New( - self->window_->GetWebContents()->IsWaitingForResponse()); + return ToV8Value(self->window_->GetWebContents()->IsWaitingForResponse()); } // static @@ -496,14 +498,14 @@ v8::Handle Window::Stop(const v8::Arguments &args) { v8::Handle Window::GetRoutingID(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Integer::New(self->window_->GetWebContents()->GetRoutingID()); + return ToV8Value(self->window_->GetWebContents()->GetRoutingID()); } // static v8::Handle Window::GetProcessID(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Integer::New( + return ToV8Value( self->window_->GetWebContents()->GetRenderProcessHost()->GetID()); } @@ -511,19 +513,20 @@ v8::Handle Window::GetProcessID(const v8::Arguments &args) { v8::Handle Window::IsCrashed(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - return v8::Boolean::New(self->window_->GetWebContents()->IsCrashed()); + return ToV8Value(self->window_->GetWebContents()->IsCrashed()); } // static v8::Handle Window::LoadURL(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1 || !args[0]->IsString()) + std::string url; + if (!FromV8Arguments(args, &url)) return node::ThrowTypeError("Bad argument"); NavigationController& controller = self->window_->GetWebContents()->GetController(); - controller.LoadURL(GURL(*v8::String::Utf8Value(args[0])), + controller.LoadURL(GURL(url), content::Referrer(), content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string()); @@ -541,7 +544,7 @@ v8::Handle Window::GetURL(const v8::Arguments &args) { if (controller.GetActiveEntry()) url = controller.GetActiveEntry()->GetVirtualURL().spec(); - return v8::String::New(url.c_str(), url.size()); + return ToV8Value(url); } // static @@ -551,7 +554,7 @@ v8::Handle Window::CanGoBack(const v8::Arguments &args) { NavigationController& controller = self->window_->GetWebContents()->GetController(); - return v8::Boolean::New(controller.CanGoBack()); + return ToV8Value(controller.CanGoBack()); } // static @@ -561,21 +564,21 @@ v8::Handle Window::CanGoForward(const v8::Arguments &args) { NavigationController& controller = self->window_->GetWebContents()->GetController(); - return v8::Boolean::New(controller.CanGoForward()); + return ToV8Value(controller.CanGoForward()); } // static v8::Handle Window::CanGoToOffset(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1) + int offset; + if (!FromV8Arguments(args, &offset)) return node::ThrowTypeError("Bad argument"); NavigationController& controller = self->window_->GetWebContents()->GetController(); - int offset = args[0]->IntegerValue(); - return v8::Boolean::New(controller.CanGoToOffset(offset)); + return ToV8Value(controller.CanGoToOffset(offset)); } // static @@ -604,12 +607,13 @@ v8::Handle Window::GoForward(const v8::Arguments &args) { v8::Handle Window::GoToIndex(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1) + int index; + if (!FromV8Arguments(args, &index)) return node::ThrowTypeError("Bad argument"); NavigationController& controller = self->window_->GetWebContents()->GetController(); - controller.GoToIndex(args[0]->IntegerValue()); + controller.GoToIndex(index); return v8::Undefined(); } @@ -618,12 +622,13 @@ v8::Handle Window::GoToIndex(const v8::Arguments &args) { v8::Handle Window::GoToOffset(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; - if (args.Length() < 1) + int offset; + if (!FromV8Arguments(args, &offset)) return node::ThrowTypeError("Bad argument"); NavigationController& controller = self->window_->GetWebContents()->GetController(); - controller.GoToOffset(args[0]->IntegerValue()); + controller.GoToOffset(offset); return v8::Undefined(); } diff --git a/browser/api/atom_browser_bindings.cc b/browser/api/atom_browser_bindings.cc index 9c2189460f6..c2dc9e78cc0 100644 --- a/browser/api/atom_browser_bindings.cc +++ b/browser/api/atom_browser_bindings.cc @@ -9,7 +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_conversions.h" #include "common/v8_value_converter_impl.h" #include "content/public/browser/browser_thread.h" #include "vendor/node/src/node.h" diff --git a/common/string16_conversions.h b/common/string16_conversions.h deleted file mode 100644 index db2d5da0bff..00000000000 --- a/common/string16_conversions.h +++ /dev/null @@ -1,22 +0,0 @@ -// 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 "base/string16.h" -#include "v8/include/v8.h" - -// Converts a V8 value to a string16. -inline string16 FromV8Value(v8::Handle value) { - v8::String::Value s(value); - return string16(reinterpret_cast(*s), s.length()); -} - -// Converts string16 to V8 String. -inline v8::Handle ToV8Value(const string16& s) { - return v8::String::New(reinterpret_cast(s.data()), s.size()); -} - -#endif // COMMON_STRING16_CONVERSIONS_H_ diff --git a/common/v8_conversions.h b/common/v8_conversions.h index a82e5978c99..26cb0bc7271 100644 --- a/common/v8_conversions.h +++ b/common/v8_conversions.h @@ -9,6 +9,7 @@ #include #include "base/files/file_path.h" +#include "base/string16.h" #include "browser/api/atom_api_window.h" #include "v8/include/v8.h" @@ -16,12 +17,21 @@ struct FromV8Value { explicit FromV8Value(v8::Handle value) : value_(value) {} + operator int() { + return value_->IntegerValue(); + } + + operator bool() { + return value_->BooleanValue(); + } + operator std::string() { return *v8::String::Utf8Value(value_); } - operator int() { - return value_->IntegerValue(); + operator string16() { + v8::String::Value s(value_); + return string16(reinterpret_cast(*s), s.length()); } operator base::FilePath() { @@ -59,6 +69,22 @@ struct FromV8Value { }; // Convert arbitrary supported native type to V8 value. +inline v8::Handle ToV8Value(int i) { + return v8::Integer::New(i); +} + +inline v8::Handle ToV8Value(bool b) { + return v8::Boolean::New(b); +} + +inline v8::Handle ToV8Value(const std::string& s) { + return v8::String::New(s.data(), s.size()); +} + +inline v8::Handle ToV8Value(const string16& s) { + return v8::String::New(reinterpret_cast(s.data()), s.size()); +} + inline v8::Handle ToV8Value(const base::FilePath& path) { std::string path_string(path.AsUTF8Unsafe()); return v8::String::New(path_string.data(), path_string.size()); @@ -68,10 +94,6 @@ inline v8::Handle ToV8Value(void* whatever) { return v8::Undefined(); } -inline v8::Handle ToV8Value(int code) { - return v8::Integer::New(code); -} - inline v8::Handle ToV8Value(const std::vector& paths) { v8::Handle result = v8::Array::New(paths.size()); @@ -91,11 +113,21 @@ bool V8ValueCanBeConvertedTo(v8::Handle value) { return value->IsNumber(); } +template<> inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return value->IsBoolean(); +} + template<> inline bool V8ValueCanBeConvertedTo(v8::Handle value) { return value->IsString(); } +template<> inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return V8ValueCanBeConvertedTo(value); +} + template<> inline bool V8ValueCanBeConvertedTo(v8::Handle value) { return V8ValueCanBeConvertedTo(value); diff --git a/renderer/api/atom_api_renderer_ipc.cc b/renderer/api/atom_api_renderer_ipc.cc index 16ded068712..d1263479722 100644 --- a/renderer/api/atom_api_renderer_ipc.cc +++ b/renderer/api/atom_api_renderer_ipc.cc @@ -6,7 +6,7 @@ #include "base/values.h" #include "common/api/api_messages.h" -#include "common/string16_conversions.h" +#include "common/v8_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" diff --git a/renderer/api/atom_renderer_bindings.cc b/renderer/api/atom_renderer_bindings.cc index 61d90878569..6609d04eb66 100644 --- a/renderer/api/atom_renderer_bindings.cc +++ b/renderer/api/atom_renderer_bindings.cc @@ -8,7 +8,7 @@ #include "base/logging.h" #include "base/values.h" -#include "common/string16_conversions.h" +#include "common/v8_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"