OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/chromedriver/chrome_impl.h" | 5 #include "chrome/test/chromedriver/chrome_impl.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 const std::string& function, | 122 const std::string& function, |
123 const base::ListValue& args, | 123 const base::ListValue& args, |
124 scoped_ptr<base::Value>* result) { | 124 scoped_ptr<base::Value>* result) { |
125 std::string json; | 125 std::string json; |
126 base::JSONWriter::Write(&args, &json); | 126 base::JSONWriter::Write(&args, &json); |
127 std::string expression = base::StringPrintf( | 127 std::string expression = base::StringPrintf( |
128 "(%s).apply(null, [%s, %s])", | 128 "(%s).apply(null, [%s, %s])", |
129 kCallFunctionScript, | 129 kCallFunctionScript, |
130 function.c_str(), | 130 function.c_str(), |
131 json.c_str()); | 131 json.c_str()); |
132 return EvaluateScript(frame, expression, result); | 132 scoped_ptr<base::Value> temp_result; |
| 133 Status status = EvaluateScript(frame, expression, &temp_result); |
| 134 if (status.IsError()) |
| 135 return status; |
| 136 |
| 137 return internal::ParseCallFunctionResult(*temp_result, result); |
133 } | 138 } |
134 | 139 |
135 Status ChromeImpl::GetFrameByFunction(const std::string& frame, | 140 Status ChromeImpl::GetFrameByFunction(const std::string& frame, |
136 const std::string& function, | 141 const std::string& function, |
137 const base::ListValue& args, | 142 const base::ListValue& args, |
138 std::string* out_frame) { | 143 std::string* out_frame) { |
139 int context_id; | 144 int context_id; |
140 Status status = GetContextIdForFrame(dom_tracker_.get(), frame, &context_id); | 145 Status status = GetContextIdForFrame(dom_tracker_.get(), frame, &context_id); |
141 if (status.IsError()) | 146 if (status.IsError()) |
142 return status; | 147 return status; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 if (status.IsError()) | 241 if (status.IsError()) |
237 return status; | 242 return status; |
238 | 243 |
239 std::string type; | 244 std::string type; |
240 if (!temp_result->GetString("type", &type)) | 245 if (!temp_result->GetString("type", &type)) |
241 return Status(kUnknownError, "Runtime.evaluate missing string 'type'"); | 246 return Status(kUnknownError, "Runtime.evaluate missing string 'type'"); |
242 | 247 |
243 if (type == "undefined") { | 248 if (type == "undefined") { |
244 result->reset(base::Value::CreateNullValue()); | 249 result->reset(base::Value::CreateNullValue()); |
245 } else { | 250 } else { |
246 int status_code; | 251 base::Value* value; |
247 if (!temp_result->GetInteger("value.status", &status_code)) { | 252 if (!temp_result->Get("value", &value)) |
248 return Status(kUnknownError, | 253 return Status(kUnknownError, "Runtime.evaluate missing 'value'"); |
249 "Runtime.evaluate missing int 'value.status'"); | 254 result->reset(value->DeepCopy()); |
250 } | |
251 if (status_code != kOk) | |
252 return Status(static_cast<StatusCode>(status_code)); | |
253 base::Value* unscoped_value; | |
254 if (!temp_result->Get("value.value", &unscoped_value)) { | |
255 return Status(kUnknownError, | |
256 "Runtime.evaluate missing 'value.value'"); | |
257 } | |
258 result->reset(unscoped_value->DeepCopy()); | |
259 } | 255 } |
260 return Status(kOk); | 256 return Status(kOk); |
261 } | 257 } |
262 | 258 |
| 259 Status ParseCallFunctionResult(const base::Value& temp_result, |
| 260 scoped_ptr<base::Value>* result) { |
| 261 const base::DictionaryValue* dict; |
| 262 if (!temp_result.GetAsDictionary(&dict)) |
| 263 return Status(kUnknownError, "call function result must be a dictionary"); |
| 264 int status_code; |
| 265 if (!dict->GetInteger("status", &status_code)) { |
| 266 return Status(kUnknownError, |
| 267 "call function result missing int 'status'"); |
| 268 } |
| 269 if (status_code != kOk) |
| 270 return Status(static_cast<StatusCode>(status_code)); |
| 271 const base::Value* unscoped_value; |
| 272 if (!dict->Get("value", &unscoped_value)) { |
| 273 return Status(kUnknownError, |
| 274 "call function result missing 'value'"); |
| 275 } |
| 276 result->reset(unscoped_value->DeepCopy()); |
| 277 return Status(kOk); |
| 278 } |
| 279 |
263 Status GetNodeIdFromFunction(DevToolsClient* client, | 280 Status GetNodeIdFromFunction(DevToolsClient* client, |
264 int context_id, | 281 int context_id, |
265 const std::string& function, | 282 const std::string& function, |
266 const base::ListValue& args, | 283 const base::ListValue& args, |
267 int* node_id) { | 284 int* node_id) { |
268 std::string json; | 285 std::string json; |
269 base::JSONWriter::Write(&args, &json); | 286 base::JSONWriter::Write(&args, &json); |
270 std::string expression = base::StringPrintf( | 287 std::string expression = base::StringPrintf( |
271 "(%s).apply(null, [%s, %s, true])", | 288 "(%s).apply(null, [%s, %s, true])", |
272 kCallFunctionScript, | 289 kCallFunctionScript, |
(...skipping 26 matching lines...) Expand all Loading... |
299 } | 316 } |
300 if (status.IsError()) | 317 if (status.IsError()) |
301 return status; | 318 return status; |
302 | 319 |
303 if (!cmd_result->GetInteger("nodeId", node_id)) | 320 if (!cmd_result->GetInteger("nodeId", node_id)) |
304 return Status(kUnknownError, "DOM.requestNode missing int 'nodeId'"); | 321 return Status(kUnknownError, "DOM.requestNode missing int 'nodeId'"); |
305 return Status(kOk); | 322 return Status(kOk); |
306 } | 323 } |
307 | 324 |
308 } // namespace internal | 325 } // namespace internal |
OLD | NEW |