OLD | NEW |
(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::GetProtocolTestURL(const GURL& test_url) { |
| 45 std::string spec = test_url.spec(); |
| 46 std::string dir = "/inspector-protocol/"; |
| 47 size_t pos = spec.find(dir); |
| 48 if (pos == std::string::npos) |
| 49 return GURL(); |
| 50 if (spec.rfind(".js") != spec.length() - 3) |
| 51 return GURL(); |
| 52 spec = spec.substr(0, pos + dir.length()) + |
| 53 "resources/inspector-protocol-test.html?" + spec; |
| 54 return GURL(spec); |
| 55 } |
| 56 |
| 57 void DevToolsProtocolTestBindings::ReadyToCommitNavigation( |
| 58 NavigationHandle* navigation_handle) { |
| 59 #if !defined(OS_ANDROID) |
| 60 content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost(); |
| 61 if (frame->GetParent()) |
| 62 return; |
| 63 frontend_host_.reset(DevToolsFrontendHost::Create( |
| 64 frame, base::Bind(&DevToolsProtocolTestBindings::HandleMessageFromTest, |
| 65 base::Unretained(this)))); |
| 66 #endif |
| 67 } |
| 68 |
| 69 void DevToolsProtocolTestBindings::WebContentsDestroyed() { |
| 70 if (agent_host_) { |
| 71 agent_host_->DetachClient(this); |
| 72 agent_host_ = nullptr; |
| 73 } |
| 74 } |
| 75 |
| 76 void DevToolsProtocolTestBindings::HandleMessageFromTest( |
| 77 const std::string& message) { |
| 78 std::string method; |
| 79 base::ListValue* params = nullptr; |
| 80 base::DictionaryValue* dict = nullptr; |
| 81 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); |
| 82 if (!parsed_message || !parsed_message->GetAsDictionary(&dict) || |
| 83 !dict->GetString("method", &method)) { |
| 84 return; |
| 85 } |
| 86 |
| 87 int request_id = 0; |
| 88 dict->GetInteger("id", &request_id); |
| 89 dict->GetList("params", ¶ms); |
| 90 |
| 91 if (method == "dispatchProtocolMessage" && params && params->GetSize() == 1) { |
| 92 std::string protocol_message; |
| 93 if (!params->GetString(0, &protocol_message)) |
| 94 return; |
| 95 if (agent_host_) |
| 96 agent_host_->DispatchProtocolMessage(this, protocol_message); |
| 97 return; |
| 98 } |
| 99 } |
| 100 |
| 101 void DevToolsProtocolTestBindings::DispatchProtocolMessage( |
| 102 DevToolsAgentHost* agent_host, |
| 103 const std::string& message) { |
| 104 if (message.length() < kMaxMessageChunkSize) { |
| 105 std::string param; |
| 106 base::EscapeJSONString(message, true, ¶m); |
| 107 std::string code = "DevToolsAPI.dispatchMessage(" + param + ");"; |
| 108 base::string16 javascript = base::UTF8ToUTF16(code); |
| 109 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); |
| 110 return; |
| 111 } |
| 112 |
| 113 size_t total_size = message.length(); |
| 114 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { |
| 115 std::string param; |
| 116 base::EscapeJSONString(message.substr(pos, kMaxMessageChunkSize), true, |
| 117 ¶m); |
| 118 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + "," + |
| 119 std::to_string(pos ? 0 : total_size) + ");"; |
| 120 base::string16 javascript = base::UTF8ToUTF16(code); |
| 121 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); |
| 122 } |
| 123 } |
| 124 |
| 125 void DevToolsProtocolTestBindings::AgentHostClosed( |
| 126 DevToolsAgentHost* agent_host, |
| 127 bool replaced) { |
| 128 agent_host_ = nullptr; |
| 129 } |
| 130 |
| 131 } // namespace content |
OLD | NEW |