| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "headless/lib/browser/headless_devtools_client_impl.h" | 5 #include "headless/lib/browser/headless_devtools_client_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 agent_host_ = nullptr; | 76 agent_host_ = nullptr; |
| 77 pending_messages_.clear(); | 77 pending_messages_.clear(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void HeadlessDevToolsClientImpl::DispatchProtocolMessage( | 80 void HeadlessDevToolsClientImpl::DispatchProtocolMessage( |
| 81 content::DevToolsAgentHost* agent_host, | 81 content::DevToolsAgentHost* agent_host, |
| 82 const std::string& json_message) { | 82 const std::string& json_message) { |
| 83 DCHECK_EQ(agent_host_, agent_host); | 83 DCHECK_EQ(agent_host_, agent_host); |
| 84 std::unique_ptr<base::Value> message = | 84 std::unique_ptr<base::Value> message = |
| 85 base::JSONReader::Read(json_message, base::JSON_PARSE_RFC); | 85 base::JSONReader::Read(json_message, base::JSON_PARSE_RFC); |
| 86 base::DictionaryValue* message_dict; | 86 const base::DictionaryValue* message_dict; |
| 87 if (!message || !message->GetAsDictionary(&message_dict)) { | 87 if (!message || !message->GetAsDictionary(&message_dict)) { |
| 88 NOTREACHED() << "Badly formed reply"; | 88 NOTREACHED() << "Badly formed reply"; |
| 89 return; | 89 return; |
| 90 } | 90 } |
| 91 if (!DispatchMessageReply(*message_dict) && !DispatchEvent(*message_dict)) |
| 92 DLOG(ERROR) << "Unhandled protocol message: " << json_message; |
| 93 } |
| 94 |
| 95 bool HeadlessDevToolsClientImpl::DispatchMessageReply( |
| 96 const base::DictionaryValue& message_dict) { |
| 91 int id = 0; | 97 int id = 0; |
| 92 DCHECK(message_dict->GetInteger("id", &id)); | 98 if (!message_dict.GetInteger("id", &id)) |
| 99 return false; |
| 93 auto it = pending_messages_.find(id); | 100 auto it = pending_messages_.find(id); |
| 94 if (it == pending_messages_.end()) { | 101 if (it == pending_messages_.end()) { |
| 95 NOTREACHED() << "Unexpected reply"; | 102 NOTREACHED() << "Unexpected reply"; |
| 96 return; | 103 return false; |
| 97 } | 104 } |
| 98 if (!it->second.callback_with_result.is_null()) { | 105 if (!it->second.callback_with_result.is_null()) { |
| 99 base::DictionaryValue* result_dict; | 106 const base::DictionaryValue* result_dict; |
| 100 if (!message_dict->GetDictionary("result", &result_dict)) { | 107 if (!message_dict.GetDictionary("result", &result_dict)) { |
| 101 NOTREACHED() << "Badly formed reply result"; | 108 NOTREACHED() << "Badly formed reply result"; |
| 102 return; | 109 return false; |
| 103 } | 110 } |
| 104 it->second.callback_with_result.Run(*result_dict); | 111 it->second.callback_with_result.Run(*result_dict); |
| 105 } else if (!it->second.callback.is_null()) { | 112 } else if (!it->second.callback.is_null()) { |
| 106 it->second.callback.Run(); | 113 it->second.callback.Run(); |
| 107 } | 114 } |
| 108 pending_messages_.erase(it); | 115 pending_messages_.erase(it); |
| 116 return true; |
| 117 } |
| 118 |
| 119 bool HeadlessDevToolsClientImpl::DispatchEvent( |
| 120 const base::DictionaryValue& message_dict) { |
| 121 std::string method; |
| 122 if (!message_dict.GetString("method", &method)) |
| 123 return false; |
| 124 auto it = event_handlers_.find(method); |
| 125 if (it == event_handlers_.end()) { |
| 126 NOTREACHED() << "Unknown event: " << method; |
| 127 return false; |
| 128 } |
| 129 if (!it->second.is_null()) { |
| 130 const base::DictionaryValue* result_dict; |
| 131 if (!message_dict.GetDictionary("params", &result_dict)) { |
| 132 NOTREACHED() << "Badly formed event parameters"; |
| 133 return false; |
| 134 } |
| 135 it->second.Run(*result_dict); |
| 136 } |
| 137 return true; |
| 109 } | 138 } |
| 110 | 139 |
| 111 void HeadlessDevToolsClientImpl::AgentHostClosed( | 140 void HeadlessDevToolsClientImpl::AgentHostClosed( |
| 112 content::DevToolsAgentHost* agent_host, | 141 content::DevToolsAgentHost* agent_host, |
| 113 bool replaced_with_another_client) { | 142 bool replaced_with_another_client) { |
| 114 DCHECK_EQ(agent_host_, agent_host); | 143 DCHECK_EQ(agent_host_, agent_host); |
| 115 agent_host = nullptr; | 144 agent_host = nullptr; |
| 116 pending_messages_.clear(); | 145 pending_messages_.clear(); |
| 117 } | 146 } |
| 118 | 147 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 263 |
| 235 template <typename CallbackType> | 264 template <typename CallbackType> |
| 236 void HeadlessDevToolsClientImpl::FinalizeAndSendMessage( | 265 void HeadlessDevToolsClientImpl::FinalizeAndSendMessage( |
| 237 base::DictionaryValue* message, | 266 base::DictionaryValue* message, |
| 238 CallbackType callback) { | 267 CallbackType callback) { |
| 239 DCHECK(agent_host_); | 268 DCHECK(agent_host_); |
| 240 int id = next_message_id_++; | 269 int id = next_message_id_++; |
| 241 message->SetInteger("id", id); | 270 message->SetInteger("id", id); |
| 242 std::string json_message; | 271 std::string json_message; |
| 243 base::JSONWriter::Write(*message, &json_message); | 272 base::JSONWriter::Write(*message, &json_message); |
| 244 pending_messages_[id] = PendingMessage(callback); | 273 pending_messages_[id] = Callback(callback); |
| 245 agent_host_->DispatchProtocolMessage(json_message); | 274 agent_host_->DispatchProtocolMessage(json_message); |
| 246 } | 275 } |
| 247 | 276 |
| 248 template <typename CallbackType> | 277 template <typename CallbackType> |
| 249 void HeadlessDevToolsClientImpl::SendMessageWithParams( | 278 void HeadlessDevToolsClientImpl::SendMessageWithParams( |
| 250 const char* method, | 279 const char* method, |
| 251 std::unique_ptr<base::Value> params, | 280 std::unique_ptr<base::Value> params, |
| 252 CallbackType callback) { | 281 CallbackType callback) { |
| 253 base::DictionaryValue message; | 282 base::DictionaryValue message; |
| 254 message.SetString("method", method); | 283 message.SetString("method", method); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 283 const char* method, | 312 const char* method, |
| 284 base::Callback<void(const base::Value&)> callback) { | 313 base::Callback<void(const base::Value&)> callback) { |
| 285 SendMessageWithoutParams(method, std::move(callback)); | 314 SendMessageWithoutParams(method, std::move(callback)); |
| 286 } | 315 } |
| 287 | 316 |
| 288 void HeadlessDevToolsClientImpl::SendMessage(const char* method, | 317 void HeadlessDevToolsClientImpl::SendMessage(const char* method, |
| 289 base::Callback<void()> callback) { | 318 base::Callback<void()> callback) { |
| 290 SendMessageWithoutParams(method, std::move(callback)); | 319 SendMessageWithoutParams(method, std::move(callback)); |
| 291 } | 320 } |
| 292 | 321 |
| 293 HeadlessDevToolsClientImpl::PendingMessage::PendingMessage() {} | 322 void HeadlessDevToolsClientImpl::RegisterEventHandler( |
| 323 const char* method, |
| 324 base::Callback<void(const base::Value&)> callback) { |
| 325 DCHECK(event_handlers_.find(method) == event_handlers_.end()); |
| 326 event_handlers_[method] = callback; |
| 327 } |
| 294 | 328 |
| 295 HeadlessDevToolsClientImpl::PendingMessage::PendingMessage( | 329 HeadlessDevToolsClientImpl::Callback::Callback() {} |
| 296 PendingMessage&& other) = default; | |
| 297 | 330 |
| 298 HeadlessDevToolsClientImpl::PendingMessage::PendingMessage( | 331 HeadlessDevToolsClientImpl::Callback::Callback(Callback&& other) = default; |
| 299 base::Callback<void()> callback) | 332 |
| 333 HeadlessDevToolsClientImpl::Callback::Callback(base::Callback<void()> callback) |
| 300 : callback(callback) {} | 334 : callback(callback) {} |
| 301 | 335 |
| 302 HeadlessDevToolsClientImpl::PendingMessage::PendingMessage( | 336 HeadlessDevToolsClientImpl::Callback::Callback( |
| 303 base::Callback<void(const base::Value&)> callback) | 337 base::Callback<void(const base::Value&)> callback) |
| 304 : callback_with_result(callback) {} | 338 : callback_with_result(callback) {} |
| 305 | 339 |
| 306 HeadlessDevToolsClientImpl::PendingMessage::~PendingMessage() {} | 340 HeadlessDevToolsClientImpl::Callback::~Callback() {} |
| 307 | 341 |
| 308 HeadlessDevToolsClientImpl::PendingMessage& | 342 HeadlessDevToolsClientImpl::Callback& HeadlessDevToolsClientImpl::Callback:: |
| 309 HeadlessDevToolsClientImpl::PendingMessage::operator=(PendingMessage&& other) = | 343 operator=(Callback&& other) = default; |
| 310 default; | |
| 311 | 344 |
| 312 } // namespace headless | 345 } // namespace headless |
| OLD | NEW |