OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/frame_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 FrameTracker::FrameTracker(DevToolsClient* client) : client_(client) { | |
16 DCHECK(client_); | |
17 client_->AddListener(this); | |
18 } | |
19 | |
20 FrameTracker::~FrameTracker() {} | |
21 | |
22 Status FrameTracker::GetFrameForContextId( | |
23 int context_id, std::string* frame_id) { | |
24 if (context_to_frame_map_.count(context_id) == 0) | |
25 return Status(kUnknownError, "execution context does not have a frame"); | |
26 *frame_id = context_to_frame_map_[context_id]; | |
27 return Status(kOk); | |
28 } | |
29 | |
30 Status FrameTracker::GetContextIdForFrame( | |
31 const std::string& frame_id, int* context_id) { | |
32 if (frame_to_context_map_.count(frame_id) == 0) | |
33 return Status(kUnknownError, "frame does not have execution context"); | |
34 *context_id = frame_to_context_map_[frame_id]; | |
35 return Status(kOk); | |
36 } | |
37 | |
38 Status FrameTracker::OnConnected() { | |
39 frame_to_context_map_.clear(); | |
40 context_to_frame_map_.clear(); | |
41 // Enable runtime events to allow tracking execution context creation. | |
42 base::DictionaryValue params; | |
43 Status status = client_->SendCommand("Runtime.enable", params); | |
44 if (status.IsError()) | |
45 return status; | |
46 return client_->SendCommand("DOM.getDocument", params); | |
47 } | |
48 | |
49 void FrameTracker::OnEvent(const std::string& method, | |
50 const base::DictionaryValue& params) { | |
51 if (method == "Runtime.executionContextCreated") { | |
52 const base::DictionaryValue* context; | |
53 if (!params.GetDictionary("context", &context)) { | |
54 LOG(ERROR) << "Runtime.executionContextCreated missing dict 'context'"; | |
55 return; | |
56 } | |
57 int context_id; | |
58 std::string frame_id; | |
59 if (!context->GetInteger("id", &context_id) || | |
60 !context->GetString("frameId", &frame_id)) { | |
61 std::string json; | |
62 base::JSONWriter::Write(context, &json); | |
63 LOG(ERROR) << "Runtime.executionContextCreated has invalid 'context': " | |
64 << json; | |
65 return; | |
66 } | |
67 frame_to_context_map_.insert(std::make_pair(frame_id, context_id)); | |
68 context_to_frame_map_.insert(std::make_pair(context_id, frame_id)); | |
69 } else if (method == "DOM.documentUpdated") { | |
70 frame_to_context_map_.clear(); | |
71 context_to_frame_map_.clear(); | |
72 } | |
73 } | |
OLD | NEW |