Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / test / chromedriver / chrome / frame_tracker.cc
blobc18b948e3482eaed4d7898a198ea64e9ebfdc626
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"
7 #include <utility>
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];
27 return Status(kOk);
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);
35 if (status.IsError())
36 return status;
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'");
50 int context_id;
51 std::string frame_id;
52 if (!context->GetInteger("id", &context_id) ||
53 !context->GetString("frameId", &frame_id)) {
54 std::string json;
55 base::JSONWriter::Write(context, &json);
56 return Status(
57 kUnknownError,
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();
66 return Status(kOk);