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