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

Side by Side Diff: content/shell/browser/layout_test/devtools_protocol_test_bindings.cc

Issue 2950713002: [DevTools] New harness for inspector-protocol layout tests (Closed)
Patch Set: addressed comments, 4 tests Created 3 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2017 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 "content/shell/browser/layout_test/devtools_protocol_test_bindings.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/string_escape.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "content/public/browser/devtools_agent_host.h"
13 #include "content/public/browser/navigation_handle.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16
17 #if !defined(OS_ANDROID)
18 #include "content/public/browser/devtools_frontend_host.h"
19 #endif
20
21 namespace content {
22
23 namespace {
24 // This constant should be in sync with
25 // the constant at devtools_ui_bindings.cc.
26 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;
27 } // namespace
28
29 DevToolsProtocolTestBindings::DevToolsProtocolTestBindings(
30 WebContents* devtools)
31 : WebContentsObserver(devtools),
32 agent_host_(DevToolsAgentHost::CreateForDiscovery()) {
33 agent_host_->AttachClient(this);
34 }
35
36 DevToolsProtocolTestBindings::~DevToolsProtocolTestBindings() {
37 if (agent_host_) {
38 agent_host_->DetachClient(this);
39 agent_host_ = nullptr;
40 }
41 }
42
43 // static
44 GURL DevToolsProtocolTestBindings::MapTestURLIfNeeded(const GURL& test_url,
45 bool* is_protocol_test) {
46 *is_protocol_test = false;
47 std::string spec = test_url.spec();
48 std::string dir = "/inspector-protocol/";
49 size_t pos = spec.find(dir);
50 if (pos == std::string::npos)
51 return test_url;
52 if (spec.rfind(".js") != spec.length() - 3)
53 return test_url;
54 spec = spec.substr(0, pos + dir.length()) +
55 "resources/inspector-protocol-test.html?" + spec;
56 *is_protocol_test = true;
57 return GURL(spec);
58 }
59
60 void DevToolsProtocolTestBindings::ReadyToCommitNavigation(
61 NavigationHandle* navigation_handle) {
62 #if !defined(OS_ANDROID)
63 content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost();
64 if (frame->GetParent())
65 return;
66 frontend_host_.reset(DevToolsFrontendHost::Create(
67 frame, base::Bind(&DevToolsProtocolTestBindings::HandleMessageFromTest,
68 base::Unretained(this))));
69 #endif
70 }
71
72 void DevToolsProtocolTestBindings::WebContentsDestroyed() {
73 if (agent_host_) {
74 agent_host_->DetachClient(this);
75 agent_host_ = nullptr;
76 }
77 }
78
79 void DevToolsProtocolTestBindings::HandleMessageFromTest(
80 const std::string& message) {
81 std::string method;
82 base::ListValue* params = nullptr;
83 base::DictionaryValue* dict = nullptr;
84 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message);
85 if (!parsed_message || !parsed_message->GetAsDictionary(&dict) ||
86 !dict->GetString("method", &method)) {
87 return;
88 }
89
90 int request_id = 0;
91 dict->GetInteger("id", &request_id);
92 dict->GetList("params", &params);
93
94 if (method == "dispatchProtocolMessage" && params && params->GetSize() == 1) {
95 std::string protocol_message;
96 if (!params->GetString(0, &protocol_message))
97 return;
98 if (agent_host_)
99 agent_host_->DispatchProtocolMessage(this, protocol_message);
100 return;
101 }
102 }
103
104 void DevToolsProtocolTestBindings::DispatchProtocolMessage(
105 DevToolsAgentHost* agent_host,
106 const std::string& message) {
107 if (message.length() < kMaxMessageChunkSize) {
108 std::string param;
109 base::EscapeJSONString(message, true, &param);
110 std::string code = "DevToolsAPI.dispatchMessage(" + param + ");";
111 base::string16 javascript = base::UTF8ToUTF16(code);
112 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript);
113 return;
114 }
115
116 size_t total_size = message.length();
117 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) {
118 std::string param;
119 base::EscapeJSONString(message.substr(pos, kMaxMessageChunkSize), true,
120 &param);
121 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + "," +
122 std::to_string(pos ? 0 : total_size) + ");";
123 base::string16 javascript = base::UTF8ToUTF16(code);
124 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript);
125 }
126 }
127
128 void DevToolsProtocolTestBindings::AgentHostClosed(
129 DevToolsAgentHost* agent_host,
130 bool replaced) {
131 agent_host_ = nullptr;
132 }
133
134 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698