Chromium Code Reviews| Index: content/renderer/dom_automation_controller.cc |
| diff --git a/content/renderer/dom_automation_controller.cc b/content/renderer/dom_automation_controller.cc |
| index b1f0d60b6a7d21df25f200a662c04831df594cfa..876424d83794e1a47283e8b7a6e342f94b8db42b 100644 |
| --- a/content/renderer/dom_automation_controller.cc |
| +++ b/content/renderer/dom_automation_controller.cc |
| @@ -4,52 +4,56 @@ |
| #include "content/renderer/dom_automation_controller.h" |
| -#include "base/bind.h" |
| -#include "base/bind_helpers.h" |
| #include "base/json/json_string_value_serializer.h" |
| -#include "base/metrics/histogram.h" |
| -#include "base/metrics/statistics_recorder.h" |
| #include "base/strings/string_util.h" |
| #include "content/common/child_process_messages.h" |
| #include "content/common/view_messages.h" |
| - |
| -using webkit_glue::CppArgumentList; |
| -using webkit_glue::CppVariant; |
| +#include "content/renderer/render_view_impl.h" |
| +#include "gin/handle.h" |
| +#include "gin/object_template_builder.h" |
| +#include "third_party/WebKit/public/web/WebFrame.h" |
| +#include "third_party/WebKit/public/web/WebKit.h" |
| namespace content { |
| -DomAutomationController::DomAutomationController() |
| - : sender_(NULL), |
| - routing_id_(MSG_ROUTING_NONE), |
| - automation_id_(MSG_ROUTING_NONE) { |
| - BindCallback("send", base::Bind(&DomAutomationController::Send, |
| - base::Unretained(this))); |
| - BindCallback("setAutomationId", |
| - base::Bind(&DomAutomationController::SetAutomationId, |
| - base::Unretained(this))); |
| - BindCallback("sendJSON", base::Bind(&DomAutomationController::SendJSON, |
| - base::Unretained(this))); |
| - BindCallback("sendWithId", base::Bind(&DomAutomationController::SendWithId, |
| - base::Unretained(this))); |
| -} |
| +gin::WrapperInfo DomAutomationController::kWrapperInfo = { |
| + gin::kEmbedderNativeGin}; |
| -void DomAutomationController::Send(const CppArgumentList& args, |
| - CppVariant* result) { |
| - if (args.size() != 1) { |
| - result->SetNull(); |
| - return; |
| - } |
| +DomAutomationController::DomAutomationController(RenderViewImpl* render_view) |
| + : render_view_(render_view), automation_id_(MSG_ROUTING_NONE) {} |
| - if (automation_id_ == MSG_ROUTING_NONE) { |
| - result->SetNull(); |
| +void DomAutomationController::Install(blink::WebFrame* frame) { |
| + v8::Isolate* isolate = blink::mainThreadIsolate(); |
| + v8::HandleScope handle_scope(isolate); |
| + v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| + if (context.IsEmpty()) |
| return; |
| - } |
| - if (!sender_) { |
| - NOTREACHED(); |
| - result->SetNull(); |
| - return; |
| - } |
| + v8::Context::Scope context_scope(context); |
| + |
| + gin::Handle<DomAutomationController> controller = |
| + gin::CreateHandle(isolate, this); |
| + v8::Handle<v8::Object> global = context->Global(); |
| + global->Set(gin::StringToV8(isolate, "domAutomationController"), |
| + controller.ToV8()); |
| +} |
| + |
| +DomAutomationController::~DomAutomationController() {} |
| + |
| +gin::ObjectTemplateBuilder DomAutomationController::GetObjectTemplateBuilder( |
| + v8::Isolate* isolate) { |
| + return gin::Wrappable<DomAutomationController>::GetObjectTemplateBuilder( |
| + isolate) |
| + .SetMethod("send", &DomAutomationController::Send) |
| + .SetMethod("setAutomationId", &DomAutomationController::SetAutomationId) |
| + .SetMethod("sendJSON", &DomAutomationController::SendJSON) |
| + .SetMethod("sendWithId", &DomAutomationController::SendWithId); |
| +} |
| + |
| +bool DomAutomationController::Send(gin::Arguments* args) { |
| + |
| + if (automation_id_ == MSG_ROUTING_NONE) |
| + return false; |
| std::string json; |
| JSONStringValueSerializer serializer(&json); |
| @@ -61,114 +65,51 @@ void DomAutomationController::Send(const CppArgumentList& args, |
| // writer is lenient, and (b) on the receiving side we wrap the JSON string |
| // in square brackets, converting it to an array, then parsing it and |
| // grabbing the 0th element to get the value out. |
| - switch (args[0].type) { |
| - case NPVariantType_String: { |
| - value.reset(new base::StringValue(args[0].ToString())); |
| - break; |
| - } |
| - case NPVariantType_Bool: { |
| - value.reset(new base::FundamentalValue(args[0].ToBoolean())); |
| - break; |
| - } |
| - case NPVariantType_Int32: { |
| - value.reset(new base::FundamentalValue(args[0].ToInt32())); |
| - break; |
| - } |
| - case NPVariantType_Double: { |
| - // The value that is sent back is an integer while it is treated |
| - // as a double in this binding. The reason being that KJS treats |
| - // any number value as a double. Refer for more details, |
| - // chrome/third_party/webkit/src/JavaScriptCore/bindings/c/c_utility.cpp |
| - value.reset(new base::FundamentalValue(args[0].ToInt32())); |
| - break; |
| - } |
| - default: { |
| - result->SetNull(); |
| - return; |
| - } |
| + if (args->PeekNext()->IsString()) { |
|
Aaron Boodman
2014/01/09 20:11:06
Weird API is weird. Why just these three JSON type
|
| + std::string str; |
|
Aaron Boodman
2014/01/09 20:11:06
You could shorten this a bit by using V8ValueConve
jochen (gone - plz use gerrit)
2014/01/09 20:30:38
The problem is that some tests rely on passing a d
Aaron Boodman
2014/01/09 20:41:25
Do you mean they pass a non-integral number, or th
|
| + args->GetNext(&str); |
| + value.reset(new base::StringValue(str)); |
| + } else if (args->PeekNext()->IsBoolean()) { |
| + bool val; |
| + args->GetNext(&val); |
| + value.reset(new base::FundamentalValue(val)); |
| + } else if (args->PeekNext()->IsNumber()) { |
| + double val; |
| + args->GetNext(&val); |
| + value.reset(new base::FundamentalValue(static_cast<int>(val))); |
| + } else { |
| + return false; |
| } |
| - if (!serializer.Serialize(*value)) { |
| - result->SetNull(); |
| - return; |
| - } |
| + if (!serializer.Serialize(*value)) |
| + return false; |
| - bool succeeded = sender_->Send( |
| - new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_)); |
| - result->Set(succeeded); |
| + bool succeeded = render_view_->Send(new ViewHostMsg_DomOperationResponse( |
| + render_view_->GetRoutingID(), json, automation_id_)); |
| automation_id_ = MSG_ROUTING_NONE; |
| + return succeeded; |
| } |
| -void DomAutomationController::SendJSON(const CppArgumentList& args, |
| - CppVariant* result) { |
| - if (args.size() != 1) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - if (automation_id_ == MSG_ROUTING_NONE) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - if (!sender_) { |
| - NOTREACHED(); |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - if (args[0].type != NPVariantType_String) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - std::string json = args[0].ToString(); |
| - result->Set(sender_->Send( |
| - new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_))); |
| +bool DomAutomationController::SendJSON(const std::string& json) { |
| + if (automation_id_ == MSG_ROUTING_NONE) |
| + return false; |
| + bool result = render_view_->Send(new ViewHostMsg_DomOperationResponse( |
| + render_view_->GetRoutingID(), json, automation_id_)); |
| automation_id_ = MSG_ROUTING_NONE; |
| + return result; |
| } |
| -void DomAutomationController::SendWithId(const CppArgumentList& args, |
| - CppVariant* result) { |
| - if (args.size() != 2) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - if (!sender_) { |
| - NOTREACHED(); |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - if (!args[0].isNumber() || args[1].type != NPVariantType_String) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - result->Set(sender_->Send( |
| - new ViewHostMsg_DomOperationResponse(routing_id_, args[1].ToString(), |
| - args[0].ToInt32()))); |
| +bool DomAutomationController::SendWithId(int automation_id, |
| + const std::string& str) { |
| + return render_view_->Send(new ViewHostMsg_DomOperationResponse( |
| + render_view_->GetRoutingID(), str, automation_id)); |
| } |
| -void DomAutomationController::SetAutomationId( |
| - const CppArgumentList& args, CppVariant* result) { |
| - if (args.size() != 1) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - // The check here is for NumberType and not Int32 as |
| - // KJS::JSType only defines a NumberType (no Int32) |
| - if (!args[0].isNumber()) { |
| - result->SetNull(); |
| - return; |
| - } |
| - |
| - automation_id_ = args[0].ToInt32(); |
| - result->Set(true); |
| +bool DomAutomationController::SetAutomationId(int automation_id) { |
| + automation_id_ = automation_id; |
| + return true; |
| } |
| } // namespace content |