| 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/devtools_client.h" | |
| 13 #include "chrome/test/chromedriver/status.h" | |
| 14 | |
| 15 DomTracker::DomTracker(DevToolsClient* client) : client_(client) { | |
| 16 DCHECK(client_); | |
| 17 client_->AddListener(this); | |
| 18 } | |
| 19 | |
| 20 DomTracker::~DomTracker() {} | |
| 21 | |
| 22 Status DomTracker::GetFrameIdForNode( | |
| 23 int node_id, std::string* frame_id) { | |
| 24 if (node_to_frame_map_.count(node_id) == 0) | |
| 25 return Status(kUnknownError, "element is not a frame"); | |
| 26 *frame_id = node_to_frame_map_[node_id]; | |
| 27 return Status(kOk); | |
| 28 } | |
| 29 | |
| 30 Status DomTracker::OnConnected() { | |
| 31 node_to_frame_map_.clear(); | |
| 32 // Fetch the root document node so that Inspector will push DOM node | |
| 33 // information to the client. | |
| 34 base::DictionaryValue params; | |
| 35 return client_->SendCommand("DOM.getDocument", params); | |
| 36 } | |
| 37 | |
| 38 void DomTracker::OnEvent(const std::string& method, | |
| 39 const base::DictionaryValue& params) { | |
| 40 if (method == "DOM.setChildNodes") { | |
| 41 const base::Value* nodes; | |
| 42 if (!params.Get("nodes", &nodes)) { | |
| 43 LOG(ERROR) << "DOM.setChildNodes missing 'nodes'"; | |
| 44 return; | |
| 45 } | |
| 46 if (!ProcessNodeList(nodes)) { | |
| 47 std::string json; | |
| 48 base::JSONWriter::Write(nodes, &json); | |
| 49 LOG(ERROR) << "DOM.setChildNodes has invalid 'nodes': " << json; | |
| 50 } | |
| 51 } else if (method == "DOM.documentUpdated") { | |
| 52 node_to_frame_map_.clear(); | |
| 53 base::DictionaryValue params; | |
| 54 client_->SendCommand("DOM.getDocument", params); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 bool DomTracker::ProcessNodeList(const base::Value* nodes) { | |
| 59 const base::ListValue* nodes_list; | |
| 60 if (!nodes->GetAsList(&nodes_list)) | |
| 61 return false; | |
| 62 for (size_t i = 0; i < nodes_list->GetSize(); ++i) { | |
| 63 const base::Value* node; | |
| 64 if (!nodes_list->Get(i, &node)) | |
| 65 return false; | |
| 66 if (!ProcessNode(node)) | |
| 67 return false; | |
| 68 } | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 bool DomTracker::ProcessNode(const base::Value* node) { | |
| 73 const base::DictionaryValue* dict; | |
| 74 if (!node->GetAsDictionary(&dict)) | |
| 75 return false; | |
| 76 int node_id; | |
| 77 if (!dict->GetInteger("nodeId", &node_id)) | |
| 78 return false; | |
| 79 std::string frame_id; | |
| 80 if (dict->GetString("frameId", &frame_id)) | |
| 81 node_to_frame_map_.insert(std::make_pair(node_id, frame_id)); | |
| 82 | |
| 83 const base::Value* children; | |
| 84 if (dict->Get("children", &children)) | |
| 85 return ProcessNodeList(children); | |
| 86 return true; | |
| 87 } | |
| OLD | NEW |