Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: chrome/test/chromedriver/dom_tracker.cc

Issue 12848005: [chromedriver] Separate stuff of chrome from chromedriver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments and fix compile error on mac. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « chrome/test/chromedriver/dom_tracker.h ('k') | chrome/test/chromedriver/dom_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698