summaryrefslogtreecommitdiffstats
path: root/content/renderer/dom_automation_controller.cc
diff options
context:
space:
mode:
authorjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-03 23:39:15 +0000
committerjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-03 23:39:15 +0000
commit766a7086b0ac46dd7a7bd676da47813e5a1790bf (patch)
treeeedca5078840d057ba5a0738ca827b7379a22cf1 /content/renderer/dom_automation_controller.cc
parent74368115e55ca8ae0b0799fd2bedcd7e8688882d (diff)
downloadchromium_src-766a7086b0ac46dd7a7bd676da47813e5a1790bf.zip
chromium_src-766a7086b0ac46dd7a7bd676da47813e5a1790bf.tar.gz
chromium_src-766a7086b0ac46dd7a7bd676da47813e5a1790bf.tar.bz2
Move DOMAutomationController code from chrome to content. This is needed by interstitial pages which are in content, and also browser tests in content.
In followup changes, I'll make interstitial pages not have to derive from a concrete class, and instead implement some sort of delegate interface like we did with WebUI. BUG=98716 Review URL: https://chromiumcodereview.appspot.com/9310094 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@120441 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/renderer/dom_automation_controller.cc')
-rw-r--r--content/renderer/dom_automation_controller.cc140
1 files changed, 140 insertions, 0 deletions
diff --git a/content/renderer/dom_automation_controller.cc b/content/renderer/dom_automation_controller.cc
new file mode 100644
index 0000000..b1c4fee
--- /dev/null
+++ b/content/renderer/dom_automation_controller.cc
@@ -0,0 +1,140 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/dom_automation_controller.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/json/json_value_serializer.h"
+#include "base/string_util.h"
+#include "content/common/view_messages.h"
+
+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)));
+}
+
+void DomAutomationController::Send(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;
+ }
+
+ std::string json;
+ JSONStringValueSerializer serializer(&json);
+ scoped_ptr<Value> value;
+
+ // Warning: note that JSON officially requires the root-level object to be
+ // an object (e.g. {foo:3}) or an array, while here we're serializing
+ // strings, bools, etc. to "JSON". This only works because (a) the JSON
+ // 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(Value::CreateStringValue(args[0].ToString()));
+ break;
+ }
+ case NPVariantType_Bool: {
+ value.reset(Value::CreateBooleanValue(args[0].ToBoolean()));
+ break;
+ }
+ case NPVariantType_Int32: {
+ value.reset(Value::CreateIntegerValue(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(Value::CreateIntegerValue(args[0].ToInt32()));
+ break;
+ }
+ default: {
+ result->SetNull();
+ return;
+ }
+ }
+
+ if (!serializer.Serialize(*value)) {
+ result->SetNull();
+ return;
+ }
+
+ bool succeeded = sender_->Send(
+ new ViewHostMsg_DomOperationResponse(routing_id_, json, automation_id_));
+ result->Set(succeeded);
+
+ automation_id_ = MSG_ROUTING_NONE;
+
+}
+
+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_)));
+
+ automation_id_ = MSG_ROUTING_NONE;
+}
+
+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);
+}