Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / content / renderer / dom_automation_controller.cc
blob6f4b27a6fadbc14d7ad39bdb7dd855501e2336d9
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"
7 #include "base/bind.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;
18 namespace content {
20 DomAutomationController::DomAutomationController()
21 : sender_(NULL),
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,
39 CppVariant* result) {
40 if (args.size() != 1) {
41 result->SetNull();
42 return;
45 if (automation_id_ == MSG_ROUTING_NONE) {
46 result->SetNull();
47 return;
50 if (!sender_) {
51 NOTREACHED();
52 result->SetNull();
53 return;
56 std::string json;
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()));
69 break;
71 case NPVariantType_Bool: {
72 value.reset(Value::CreateBooleanValue(args[0].ToBoolean()));
73 break;
75 case NPVariantType_Int32: {
76 value.reset(Value::CreateIntegerValue(args[0].ToInt32()));
77 break;
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()));
85 break;
87 default: {
88 result->SetNull();
89 return;
93 if (!serializer.Serialize(*value)) {
94 result->SetNull();
95 return;
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) {
109 result->SetNull();
110 return;
113 if (automation_id_ == MSG_ROUTING_NONE) {
114 result->SetNull();
115 return;
118 if (!sender_) {
119 NOTREACHED();
120 result->SetNull();
121 return;
124 if (args[0].type != NPVariantType_String) {
125 result->SetNull();
126 return;
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) {
139 result->SetNull();
140 return;
143 if (!sender_) {
144 NOTREACHED();
145 result->SetNull();
146 return;
149 if (!args[0].isNumber() || args[1].type != NPVariantType_String) {
150 result->SetNull();
151 return;
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) {
162 result->SetNull();
163 return;
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()) {
169 result->SetNull();
170 return;
173 automation_id_ = args[0].ToInt32();
174 result->Set(true);
177 void DomAutomationController::GetHistogram(const CppArgumentList& args,
178 CppVariant* result) {
179 if (args.size() != 1) {
180 result->SetNull();
181 return;
183 base::Histogram* histogram =
184 base::StatisticsRecorder::FindHistogram(args[0].ToString());
185 std::string output;
186 if (!histogram) {
187 output = "{}";
188 } else {
189 histogram->WriteJSON(&output);
191 result->Set(output);
194 } // namespace content