1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/dom_automation_controller.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/string_util.h"
13 #include "content/common/view_messages.h"
15 using webkit_glue::CppArgumentList
;
16 using webkit_glue::CppVariant
;
20 DomAutomationController::DomAutomationController()
22 routing_id_(MSG_ROUTING_NONE
),
23 automation_id_(MSG_ROUTING_NONE
) {
24 BindCallback("send", base::Bind(&DomAutomationController::Send
,
25 base::Unretained(this)));
26 BindCallback("setAutomationId",
27 base::Bind(&DomAutomationController::SetAutomationId
,
28 base::Unretained(this)));
29 BindCallback("sendJSON", base::Bind(&DomAutomationController::SendJSON
,
30 base::Unretained(this)));
31 BindCallback("sendWithId", base::Bind(&DomAutomationController::SendWithId
,
32 base::Unretained(this)));
33 BindCallback("getHistogram",
34 base::Bind(&DomAutomationController::GetHistogram
,
35 base::Unretained(this)));
38 void DomAutomationController::Send(const CppArgumentList
& args
,
40 if (args
.size() != 1) {
45 if (automation_id_
== MSG_ROUTING_NONE
) {
57 JSONStringValueSerializer
serializer(&json
);
58 scoped_ptr
<Value
> value
;
60 // Warning: note that JSON officially requires the root-level object to be
61 // an object (e.g. {foo:3}) or an array, while here we're serializing
62 // strings, bools, etc. to "JSON". This only works because (a) the JSON
63 // writer is lenient, and (b) on the receiving side we wrap the JSON string
64 // in square brackets, converting it to an array, then parsing it and
65 // grabbing the 0th element to get the value out.
66 switch (args
[0].type
) {
67 case NPVariantType_String
: {
68 value
.reset(Value::CreateStringValue(args
[0].ToString()));
71 case NPVariantType_Bool
: {
72 value
.reset(Value::CreateBooleanValue(args
[0].ToBoolean()));
75 case NPVariantType_Int32
: {
76 value
.reset(Value::CreateIntegerValue(args
[0].ToInt32()));
79 case NPVariantType_Double
: {
80 // The value that is sent back is an integer while it is treated
81 // as a double in this binding. The reason being that KJS treats
82 // any number value as a double. Refer for more details,
83 // chrome/third_party/webkit/src/JavaScriptCore/bindings/c/c_utility.cpp
84 value
.reset(Value::CreateIntegerValue(args
[0].ToInt32()));
93 if (!serializer
.Serialize(*value
)) {
98 bool succeeded
= sender_
->Send(
99 new ViewHostMsg_DomOperationResponse(routing_id_
, json
, automation_id_
));
100 result
->Set(succeeded
);
102 automation_id_
= MSG_ROUTING_NONE
;
106 void DomAutomationController::SendJSON(const CppArgumentList
& args
,
107 CppVariant
* result
) {
108 if (args
.size() != 1) {
113 if (automation_id_
== MSG_ROUTING_NONE
) {
124 if (args
[0].type
!= NPVariantType_String
) {
129 std::string json
= args
[0].ToString();
130 result
->Set(sender_
->Send(
131 new ViewHostMsg_DomOperationResponse(routing_id_
, json
, automation_id_
)));
133 automation_id_
= MSG_ROUTING_NONE
;
136 void DomAutomationController::SendWithId(const CppArgumentList
& args
,
137 CppVariant
* result
) {
138 if (args
.size() != 2) {
149 if (!args
[0].isNumber() || args
[1].type
!= NPVariantType_String
) {
154 result
->Set(sender_
->Send(
155 new ViewHostMsg_DomOperationResponse(routing_id_
, args
[1].ToString(),
156 args
[0].ToInt32())));
159 void DomAutomationController::SetAutomationId(
160 const CppArgumentList
& args
, CppVariant
* result
) {
161 if (args
.size() != 1) {
166 // The check here is for NumberType and not Int32 as
167 // KJS::JSType only defines a NumberType (no Int32)
168 if (!args
[0].isNumber()) {
173 automation_id_
= args
[0].ToInt32();
177 void DomAutomationController::GetHistogram(const CppArgumentList
& args
,
178 CppVariant
* result
) {
179 if (args
.size() != 1) {
183 base::Histogram
* histogram
=
184 base::StatisticsRecorder::FindHistogram(args
[0].ToString());
189 histogram
->WriteJSON(&output
);
194 } // namespace content