diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-13 12:07:58 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-13 12:07:58 +0000 |
commit | caa8dc0445093d10ca22412f2f5c35e50db463ca (patch) | |
tree | 09fbc9d668d449b6f4e27821512df13ba4306b8a /content | |
parent | 5f32610d70c88d43dcb1e9321320f57d2a300849 (diff) | |
download | chromium_src-caa8dc0445093d10ca22412f2f5c35e50db463ca.zip chromium_src-caa8dc0445093d10ca22412f2f5c35e50db463ca.tar.gz chromium_src-caa8dc0445093d10ca22412f2f5c35e50db463ca.tar.bz2 |
Reland 244394 - Convert DomAutomationController from CppBoundClass to...
The difference to the original CL is that the DAC is only installed into the main world.
Original Review URL: https://codereview.chromium.org/131573003
BUG=297480
R=mkwst@chromium.org
Review URL: https://codereview.chromium.org/136533002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244488 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/renderer/dom_automation_controller.cc | 198 | ||||
-rw-r--r-- | content/renderer/dom_automation_controller.h | 75 | ||||
-rw-r--r-- | content/renderer/render_view_impl.cc | 17 | ||||
-rw-r--r-- | content/renderer/render_view_impl.h | 5 |
4 files changed, 107 insertions, 188 deletions
diff --git a/content/renderer/dom_automation_controller.cc b/content/renderer/dom_automation_controller.cc index b1f0d60..805426c 100644 --- a/content/renderer/dom_automation_controller.cc +++ b/content/renderer/dom_automation_controller.cc @@ -4,52 +4,57 @@ #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 "content/renderer/v8_value_converter_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(); +// static +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 (automation_id_ == MSG_ROUTING_NONE) { - result->SetNull(); - return; - } + v8::Context::Scope context_scope(context); - if (!sender_) { - NOTREACHED(); - result->SetNull(); - return; - } + gin::Handle<DomAutomationController> controller = + gin::CreateHandle(isolate, new DomAutomationController(frame)); + v8::Handle<v8::Object> global = context->Global(); + global->Set(gin::StringToV8(isolate, "domAutomationController"), + controller.ToV8()); +} + +DomAutomationController::DomAutomationController(blink::WebFrame* frame) + : frame_(frame), automation_id_(MSG_ROUTING_NONE) {} + +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(const gin::Arguments& args) { + if (automation_id_ == MSG_ROUTING_NONE) + return false; std::string json; JSONStringValueSerializer serializer(&json); @@ -61,114 +66,47 @@ 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() || args.PeekNext()->IsBoolean() || + args.PeekNext()->IsNumber()) { + V8ValueConverterImpl conv; + value.reset( + conv.FromV8Value(args.PeekNext(), args.isolate()->GetCurrentContext())); + } 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); + RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); + 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; + RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); + 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) { + RenderViewImpl* render_view = RenderViewImpl::FromWebView(frame_->view()); + 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 diff --git a/content/renderer/dom_automation_controller.h b/content/renderer/dom_automation_controller.h index 0f6a74a..8098b29 100644 --- a/content/renderer/dom_automation_controller.h +++ b/content/renderer/dom_automation_controller.h @@ -5,8 +5,8 @@ #ifndef CONTENT_RENDERER_DOM_AUTOMATION_CONTROLLER_H_ #define CONTENT_RENDERER_DOM_AUTOMATION_CONTROLLER_H_ -#include "ipc/ipc_sender.h" -#include "webkit/renderer/cpp_bound_class.h" +#include "base/basictypes.h" +#include "gin/wrappable.h" /* DomAutomationController class: Bound to Javascript window.domAutomationController object. @@ -50,7 +50,7 @@ (0) Initialization step where DAController is bound to the renderer and the view_id of the renderer is supplied to the DAController for - routing message in (5). (routing_id_) + routing message in (5). (1) A 'javascript:' url is sent from the test process to master as an IPC message. A unique routing id is generated at this stage (automation_id_) (2) The automation_id_ of step (1) is supplied to DAController by calling @@ -61,8 +61,8 @@ (4) A callback is generated as a result of domAutomationController.send() into Cpp. The supplied value is received as a result of this callback. (5) The value received in (4) is sent to the master along with the - stored automation_id_ as an IPC message. routing_id_ is used to route - the message. (IPC messages, ViewHostMsg_*DomAutomation* ) + stored automation_id_ as an IPC message. The frame_'s RenderFrameImpl is + used to route the message. (IPC messages, ViewHostMsg_*DomAutomation* ) (6) The value and the automation_id_ is extracted out of the message received in (5). This value is relayed to AProxy using another IPC message. automation_id_ is used to route the message. @@ -70,57 +70,50 @@ */ +namespace blink { +class WebFrame; +} + +namespace gin { +class Arguments; +} + namespace content { -// TODO(vibhor): Add another method-pair like sendLater() and sendNow() -// sendLater() should keep building a json serializer -// sendNow() should send the above serializer as a string. -class DomAutomationController : public webkit_glue::CppBoundClass { +class DomAutomationController : public gin::Wrappable<DomAutomationController> { public: - DomAutomationController(); + static gin::WrapperInfo kWrapperInfo; + + static void Install(blink::WebFrame* frame); // Makes the renderer send a javascript value to the app. - // The value to be sent can be either of type NPString, - // Number (double casted to int32) or boolean. - // The function returns true/false based on the result of actual send over - // IPC. It sets the return value to null on unexpected errors or arguments. - void Send(const webkit_glue::CppArgumentList& args, - webkit_glue::CppVariant* result); + // The value to be sent can be either of type String, + // Number (double casted to int32) or Boolean. Any other type or no + // argument at all is ignored. + bool Send(const gin::Arguments& args); // Makes the renderer send a javascript value to the app. - // The value must be a NPString and should be properly formed JSON. - // This function does not modify/escape the returned string in any way. - void SendJSON(const webkit_glue::CppArgumentList& args, - webkit_glue::CppVariant* result); + // The value should be properly formed JSON. + bool SendJSON(const std::string& json); // Sends a string with a provided Automation Id. - // Expects two javascript values; the first must be a number type and will be - // used as the Automation Id, the second must be of type NPString. - // The function returns true/false to the javascript based on the success - // of the send over IPC. It sets the javascript return value to null on - // unexpected errors or arguments. - void SendWithId(const webkit_glue::CppArgumentList& args, - webkit_glue::CppVariant* result); - - void SetAutomationId(const webkit_glue::CppArgumentList& args, - webkit_glue::CppVariant* result); + bool SendWithId(int automation_id, const std::string& str); - // TODO(vibhor): Implement later - // static CppBindingObjectMethod sendLater; - // static CppBindingObjectMethod sendNow; + bool SetAutomationId(int automation_id); - void set_routing_id(int routing_id) { routing_id_ = routing_id; } + private: + explicit DomAutomationController(blink::WebFrame* frame); + virtual ~DomAutomationController(); - void set_message_sender(IPC::Sender* sender) { - sender_ = sender; - } + // gin::WrappableBase + virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) OVERRIDE; - private: - IPC::Sender* sender_; + blink::WebFrame* frame_; - // Refer to the comments at the top of the file for more details. - int routing_id_; // routing id to be used by first channel. int automation_id_; // routing id to be used by the next channel. + + DISALLOW_COPY_AND_ASSIGN(DomAutomationController); }; } // namespace content diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc index 0294c71..718f9c2 100644 --- a/content/renderer/render_view_impl.cc +++ b/content/renderer/render_view_impl.cc @@ -3654,18 +3654,11 @@ void RenderViewImpl::didClearWindowObject(WebFrame* frame, int world_id) { FOR_EACH_OBSERVER(RenderViewObserver, observers_, DidClearWindowObject(frame, world_id)); - if (enabled_bindings_ & BINDINGS_POLICY_DOM_AUTOMATION) { - if (!dom_automation_controller_) - dom_automation_controller_.reset(new DomAutomationController()); - dom_automation_controller_->set_message_sender( - static_cast<RenderView*>(this)); - dom_automation_controller_->set_routing_id(routing_id()); - dom_automation_controller_->BindToJavascript(frame, - "domAutomationController"); - } - - if (enabled_bindings_ & BINDINGS_POLICY_STATS_COLLECTION) - StatsCollectionController::Install(frame); + if ((enabled_bindings_ & BINDINGS_POLICY_DOM_AUTOMATION) && (world_id == 0)) + DomAutomationController::Install(frame); + + if (enabled_bindings_ & BINDINGS_POLICY_STATS_COLLECTION) + StatsCollectionController::Install(frame); } void RenderViewImpl::didCreateDocumentElement(WebFrame* frame) { diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h index 756046e..b47380a 100644 --- a/content/renderer/render_view_impl.h +++ b/content/renderer/render_view_impl.h @@ -133,7 +133,6 @@ class BrowserPluginManager; class DeviceOrientationDispatcher; class DevToolsAgent; class DocumentState; -class DomAutomationController; class ExternalPopupMenu; class FaviconHelper; class GeolocationDispatcher; @@ -1497,10 +1496,6 @@ class CONTENT_EXPORT RenderViewImpl // compositor. bool allow_partial_swap_; - // Allows JS to access DOM automation. The JS object is only exposed when the - // DOM automation bindings are enabled. - scoped_ptr<DomAutomationController> dom_automation_controller_; - // This field stores drag/drop related info for the event that is currently // being handled. If the current event results in starting a drag/drop // session, this info is sent to the browser along with other drag/drop info. |