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

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

Issue 2942573003: [DevTools] New harness for inspector-protocol layout tests (Closed)
Patch Set: 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 }
28
29 DevToolsProtocolTestBindings::DevToolsProtocolTestBindings(
30 WebContents* devtools)
31 : WebContentsObserver(devtools),
32 agent_host_(DevToolsAgentHost::CreateForDiscovery()) {
33 #if !defined(OS_ANDROID)
34 frontend_host_.reset(DevToolsFrontendHost::Create(
35 devtools->GetMainFrame(),
36 base::Bind(&DevToolsProtocolTestBindings::HandleMessageFromTest,
37 base::Unretained(this))));
38 #endif
39 agent_host_->AttachClient(this);
40 }
41
42 DevToolsProtocolTestBindings::~DevToolsProtocolTestBindings() {
43 if (agent_host_)
44 agent_host_->DetachClient(this);
45 }
46
47 // static
48 GURL DevToolsProtocolTestBindings::GetProtocolTestURL(const GURL& test_url) {
49 std::string spec = test_url.spec();
50 std::string dir = "/inspector-protocol-2/";
chenwilliam 2017/06/14 22:06:41 let's make this directory name more descriptive? (
dgozman 2017/06/19 17:48:59 This was a temporary name :)
51 size_t pos = spec.find(dir);
52 if (pos == std::string::npos)
53 return GURL();
54 spec = spec.substr(0, pos + dir.length()) +
55 "resources/inspector-protocol-test.html?" + spec;
56 return GURL(spec);
57 }
58
59 void DevToolsProtocolTestBindings::ReadyToCommitNavigation(
60 NavigationHandle* navigation_handle) {
61 #if !defined(OS_ANDROID)
62 content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost();
63 if (frame->GetParent())
64 return;
65 frontend_host_.reset(DevToolsFrontendHost::Create(
66 frame, base::Bind(&DevToolsProtocolTestBindings::HandleMessageFromTest,
67 base::Unretained(this))));
68 #endif
69 }
70
71 void DevToolsProtocolTestBindings::WebContentsDestroyed() {
72 if (agent_host_) {
73 agent_host_->DetachClient(this);
74 agent_host_ = nullptr;
75 }
76 }
77
78 void DevToolsProtocolTestBindings::HandleMessageFromTest(
79 const std::string& message) {
80 if (!agent_host_)
81 return;
82 std::string method;
83 base::ListValue* params = NULL;
84 base::DictionaryValue* dict = NULL;
chenwilliam 2017/06/14 22:06:41 nullptr? :)
dgozman 2017/06/19 17:48:59 Done.
85 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message);
86 if (!parsed_message || !parsed_message->GetAsDictionary(&dict) ||
87 !dict->GetString("method", &method)) {
88 return;
89 }
90
91 int request_id = 0;
92 dict->GetInteger("id", &request_id);
93 dict->GetList("params", &params);
94
95 if (method == "dispatchProtocolMessage" && params && params->GetSize() == 1) {
96 if (!agent_host_ || !agent_host_->IsAttached())
97 return;
98 std::string protocol_message;
99 if (!params->GetString(0, &protocol_message))
100 return;
101 agent_host_->DispatchProtocolMessage(this, protocol_message);
102 if (request_id) {
103 std::string code = "DevToolsAPI.embedderMessageAck(" +
104 base::IntToString(request_id) + ");";
105 base::string16 javascript = base::UTF8ToUTF16(code);
106 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript);
107 }
108 }
109 }
110
111 void DevToolsProtocolTestBindings::DispatchProtocolMessage(
112 DevToolsAgentHost* agent_host,
113 const std::string& message) {
114 if (message.length() < kMaxMessageChunkSize) {
115 std::string param;
116 base::EscapeJSONString(message, true, &param);
117 std::string code = "DevToolsAPI.dispatchMessage(" + param + ");";
118 base::string16 javascript = base::UTF8ToUTF16(code);
119 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript);
120 return;
121 }
122
123 size_t total_size = message.length();
124 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) {
125 std::string param;
126 base::EscapeJSONString(message.substr(pos, kMaxMessageChunkSize), true,
127 &param);
128 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + "," +
129 std::to_string(pos ? 0 : total_size) + ");";
130 base::string16 javascript = base::UTF8ToUTF16(code);
131 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript);
132 }
133 }
134
135 void DevToolsProtocolTestBindings::AgentHostClosed(
136 DevToolsAgentHost* agent_host,
137 bool replaced) {
138 agent_host_ = nullptr;
139 }
140
141 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698