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.
5 #include "chrome/test/chromedriver/chrome/frame_tracker.h"
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "chrome/test/chromedriver/chrome/devtools_client.h"
12 #include "chrome/test/chromedriver/chrome/status.h"
14 FrameTracker::FrameTracker(DevToolsClient
* client
) {
15 client
->AddListener(this);
18 FrameTracker::~FrameTracker() {}
20 Status
FrameTracker::GetContextIdForFrame(
21 const std::string
& frame_id
, int* context_id
) {
22 if (frame_to_context_map_
.count(frame_id
) == 0) {
23 return Status(kNoSuchExecutionContext
,
24 "frame does not have execution context");
26 *context_id
= frame_to_context_map_
[frame_id
];
30 Status
FrameTracker::OnConnected(DevToolsClient
* client
) {
31 frame_to_context_map_
.clear();
32 // Enable runtime events to allow tracking execution context creation.
33 base::DictionaryValue params
;
34 Status status
= client
->SendCommand("Runtime.enable", params
);
37 return client
->SendCommand("Page.enable", params
);
40 Status
FrameTracker::OnEvent(DevToolsClient
* client
,
41 const std::string
& method
,
42 const base::DictionaryValue
& params
) {
43 if (method
== "Runtime.executionContextCreated") {
44 const base::DictionaryValue
* context
;
45 if (!params
.GetDictionary("context", &context
)) {
46 return Status(kUnknownError
,
47 "Runtime.executionContextCreated missing dict 'context'");
52 if (!context
->GetInteger("id", &context_id
) ||
53 !context
->GetString("frameId", &frame_id
)) {
55 base::JSONWriter::Write(context
, &json
);
58 "Runtime.executionContextCreated has invalid 'context': " + json
);
60 frame_to_context_map_
[frame_id
] = context_id
;
61 } else if (method
== "Page.frameNavigated") {
62 const base::Value
* unused_value
;
63 if (!params
.Get("frame.parentId", &unused_value
))
64 frame_to_context_map_
.clear();