Chromium Code Reviews| 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/dom_tracker.h" | 5 #include "chrome/test/chromedriver/dom_tracker.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/test/chromedriver/status.h" | 12 #include "chrome/test/chromedriver/status.h" |
| 13 | 13 |
| 14 DomTracker::DomTracker() {} | 14 DomTracker::DomTracker(DevToolsClient* client) |
| 15 : client_(client) { | |
|
kkania
2013/01/30 22:36:05
indent
| |
| 16 DCHECK(client_); | |
|
kkania
2013/01/30 22:36:05
indent
| |
| 17 } | |
| 15 | 18 |
| 16 DomTracker::~DomTracker() {} | 19 DomTracker::~DomTracker() {} |
| 17 | 20 |
| 21 Status DomTracker::Init() { | |
| 22 // Perform necessary configuration of the DevTools client. | |
| 23 // Fetch the root document node so that Inspector will push DOM node | |
| 24 // information to the client. | |
| 25 base::DictionaryValue params; | |
| 26 Status status = client_->SendCommand("DOM.getDocument", params); | |
| 27 if (status.IsError()) | |
| 28 return status; | |
| 29 // Enable page domain notifications to allow tracking navigation state. | |
| 30 status = client_->SendCommand("Page.enable", params); | |
|
kkania
2013/01/30 22:36:05
oops, i didn't mean to merge Page.enable and Runti
craigdh
2013/01/30 23:05:21
Done.
| |
| 31 if (status.IsError()) | |
| 32 return status; | |
| 33 // Enable runtime events to allow tracking execution context creation. | |
| 34 return client_->SendCommand("Runtime.enable", params); | |
| 35 } | |
| 36 | |
| 18 Status DomTracker::GetFrameIdForNode( | 37 Status DomTracker::GetFrameIdForNode( |
| 19 int node_id, std::string* frame_id) { | 38 int node_id, std::string* frame_id) { |
| 20 if (node_to_frame_map_.count(node_id) == 0) | 39 if (node_to_frame_map_.count(node_id) == 0) |
| 21 return Status(kUnknownError, "element is not a frame"); | 40 return Status(kUnknownError, "element is not a frame"); |
| 22 *frame_id = node_to_frame_map_[node_id]; | 41 *frame_id = node_to_frame_map_[node_id]; |
| 23 return Status(kOk); | 42 return Status(kOk); |
| 24 } | 43 } |
| 25 | 44 |
| 26 void DomTracker::OnEvent(const std::string& method, | 45 void DomTracker::OnEvent(const std::string& method, |
| 27 const base::DictionaryValue& params) { | 46 const base::DictionaryValue& params) { |
| 28 if (method == "DOM.setChildNodes") { | 47 if (method == "DOM.setChildNodes") { |
| 29 const base::Value* nodes; | 48 const base::Value* nodes; |
| 30 if (!params.Get("nodes", &nodes)) { | 49 if (!params.Get("nodes", &nodes)) { |
| 31 LOG(ERROR) << "DOM.setChildNodes missing 'nodes'"; | 50 LOG(ERROR) << "DOM.setChildNodes missing 'nodes'"; |
| 32 return; | 51 return; |
| 33 } | 52 } |
| 34 if (!ProcessNodeList(nodes)) { | 53 if (!ProcessNodeList(nodes)) { |
| 35 std::string json; | 54 std::string json; |
| 36 base::JSONWriter::Write(nodes, &json); | 55 base::JSONWriter::Write(nodes, &json); |
| 37 LOG(ERROR) << "DOM.setChildNodes has invalid 'nodes': " << json; | 56 LOG(ERROR) << "DOM.setChildNodes has invalid 'nodes': " << json; |
| 38 } | 57 } |
| 39 } else if (method == "DOM.documentUpdated") { | 58 } else if (method == "DOM.documentUpdated") { |
| 40 node_to_frame_map_.clear(); | 59 node_to_frame_map_.clear(); |
| 60 base::DictionaryValue params; | |
| 61 client_->SendCommand("DOM.getDocument", params); | |
| 41 } | 62 } |
| 42 } | 63 } |
| 43 | 64 |
| 44 bool DomTracker::ProcessNodeList(const base::Value* nodes) { | 65 bool DomTracker::ProcessNodeList(const base::Value* nodes) { |
| 45 const base::ListValue* nodes_list; | 66 const base::ListValue* nodes_list; |
| 46 if (!nodes->GetAsList(&nodes_list)) | 67 if (!nodes->GetAsList(&nodes_list)) |
| 47 return false; | 68 return false; |
| 48 for (size_t i = 0; i < nodes_list->GetSize(); ++i) { | 69 for (size_t i = 0; i < nodes_list->GetSize(); ++i) { |
| 49 const base::Value* node; | 70 const base::Value* node; |
| 50 if (!nodes_list->Get(i, &node)) | 71 if (!nodes_list->Get(i, &node)) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 64 return false; | 85 return false; |
| 65 std::string frame_id; | 86 std::string frame_id; |
| 66 if (dict->GetString("frameId", &frame_id)) | 87 if (dict->GetString("frameId", &frame_id)) |
| 67 node_to_frame_map_.insert(std::make_pair(node_id, frame_id)); | 88 node_to_frame_map_.insert(std::make_pair(node_id, frame_id)); |
| 68 | 89 |
| 69 const base::Value* children; | 90 const base::Value* children; |
| 70 if (dict->Get("children", &children)) | 91 if (dict->Get("children", &children)) |
| 71 return ProcessNodeList(children); | 92 return ProcessNodeList(children); |
| 72 return true; | 93 return true; |
| 73 } | 94 } |
| OLD | NEW |