OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/test/chromedriver/dom_tracker.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/test/chromedriver/status.h" |
| 13 |
| 14 DomTracker::DomTracker() {} |
| 15 |
| 16 DomTracker::~DomTracker() {} |
| 17 |
| 18 Status DomTracker::GetFrameIdForNode( |
| 19 int node_id, std::string* frame_id) { |
| 20 if (node_to_frame_map_.count(node_id) == 0) |
| 21 return Status(kUnknownError, "element is not a frame"); |
| 22 *frame_id = node_to_frame_map_[node_id]; |
| 23 return Status(kOk); |
| 24 } |
| 25 |
| 26 Status DomTracker::GetContextIdForFrame( |
| 27 const std::string& frame_id, int* context_id) { |
| 28 if (frame_to_context_map_.count(frame_id) == 0) |
| 29 return Status(kUnknownError, "frame does not have execution context"); |
| 30 *context_id = frame_to_context_map_[frame_id]; |
| 31 return Status(kOk); |
| 32 } |
| 33 |
| 34 void DomTracker::OnEvent(const std::string& method, |
| 35 const base::DictionaryValue& params) { |
| 36 if (method == "DOM.setChildNodes") { |
| 37 const base::Value* nodes; |
| 38 if (!params.Get("nodes", &nodes)) { |
| 39 LOG(ERROR) << "DOM.setChildNodes missing 'nodes'"; |
| 40 return; |
| 41 } |
| 42 if (!ProcessNodeList(nodes)) { |
| 43 std::string json; |
| 44 base::JSONWriter::Write(nodes, &json); |
| 45 LOG(ERROR) << "DOM.setChildNodes has invalid 'nodes': " << json; |
| 46 } |
| 47 } else if (method == "Runtime.executionContextCreated") { |
| 48 const base::DictionaryValue* context; |
| 49 if (!params.GetDictionary("context", &context)) { |
| 50 LOG(ERROR) << "Runtime.executionContextCreated missing dict 'context'"; |
| 51 return; |
| 52 } |
| 53 int context_id; |
| 54 std::string frame_id; |
| 55 if (!context->GetInteger("id", &context_id) || |
| 56 !context->GetString("frameId", &frame_id)) { |
| 57 std::string json; |
| 58 base::JSONWriter::Write(context, &json); |
| 59 LOG(ERROR) << "Runtime.executionContextCreated has invalid 'context': " |
| 60 << json; |
| 61 return; |
| 62 } |
| 63 frame_to_context_map_.insert(std::make_pair(frame_id, context_id)); |
| 64 } else if (method == "DOM.documentUpdated") { |
| 65 node_to_frame_map_.clear(); |
| 66 frame_to_context_map_.clear(); |
| 67 } |
| 68 } |
| 69 |
| 70 bool DomTracker::ProcessNodeList(const base::Value* nodes) { |
| 71 const base::ListValue* nodes_list; |
| 72 if (!nodes->GetAsList(&nodes_list)) |
| 73 return false; |
| 74 for (size_t i = 0; i < nodes_list->GetSize(); ++i) { |
| 75 const base::Value* node; |
| 76 if (!nodes_list->Get(i, &node)) |
| 77 return false; |
| 78 if (!ProcessNode(node)) |
| 79 return false; |
| 80 } |
| 81 return true; |
| 82 } |
| 83 |
| 84 bool DomTracker::ProcessNode(const base::Value* node) { |
| 85 const base::DictionaryValue* dict; |
| 86 if (!node->GetAsDictionary(&dict)) |
| 87 return false; |
| 88 int node_id; |
| 89 if (!dict->GetInteger("nodeId", &node_id)) |
| 90 return false; |
| 91 std::string frame_id; |
| 92 if (dict->GetString("frameId", &frame_id)) |
| 93 node_to_frame_map_.insert(std::make_pair(node_id, frame_id)); |
| 94 |
| 95 const base::Value* children; |
| 96 if (dict->Get("children", &children)) |
| 97 return ProcessNodeList(children); |
| 98 return true; |
| 99 } |
OLD | NEW |