| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/devtools/devtools_browser_target.h" | 5 #include "content/browser/devtools/devtools_browser_target.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/devtools/devtools_protocol.h" | |
| 14 #include "net/server/http_server.h" | 13 #include "net/server/http_server.h" |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 DevToolsBrowserTarget::DomainHandler::~DomainHandler() { | |
| 19 } | |
| 20 | |
| 21 void DevToolsBrowserTarget::DomainHandler::RegisterCommandHandler( | |
| 22 const std::string& command, | |
| 23 CommandHandler handler) { | |
| 24 command_handlers_[command] = handler; | |
| 25 } | |
| 26 | |
| 27 DevToolsBrowserTarget::DomainHandler::DomainHandler(const std::string& domain) | |
| 28 : domain_(domain) { | |
| 29 } | |
| 30 | |
| 31 scoped_ptr<DevToolsProtocol::Response> | |
| 32 DevToolsBrowserTarget::DomainHandler::HandleCommand( | |
| 33 DevToolsProtocol::Command* command) { | |
| 34 CommandHandlers::iterator it = command_handlers_.find(command->method()); | |
| 35 if (it == command_handlers_.end()) { | |
| 36 return command->NoSuchMethodErrorResponse(); | |
| 37 } | |
| 38 return (it->second).Run(command); | |
| 39 } | |
| 40 | |
| 41 void DevToolsBrowserTarget::DomainHandler::SendNotification( | |
| 42 const std::string& method, | |
| 43 base::DictionaryValue* params) { | |
| 44 notifier_.Run(method, params); | |
| 45 } | |
| 46 | |
| 47 DevToolsBrowserTarget::DevToolsBrowserTarget( | 17 DevToolsBrowserTarget::DevToolsBrowserTarget( |
| 48 base::MessageLoopProxy* message_loop_proxy, | 18 base::MessageLoopProxy* message_loop_proxy, |
| 49 net::HttpServer* http_server, | 19 net::HttpServer* http_server, |
| 50 int connection_id) | 20 int connection_id) |
| 51 : message_loop_proxy_(message_loop_proxy), | 21 : message_loop_proxy_(message_loop_proxy), |
| 52 http_server_(http_server), | 22 http_server_(http_server), |
| 53 connection_id_(connection_id), | 23 connection_id_(connection_id), |
| 24 handlers_deleter_(&handlers_), |
| 54 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 55 } | 26 } |
| 56 | 27 |
| 57 DevToolsBrowserTarget::~DevToolsBrowserTarget() { | 28 DevToolsBrowserTarget::~DevToolsBrowserTarget() { |
| 58 for (DomainHandlerMap::iterator i = handlers_.begin(); i != handlers_.end(); | |
| 59 ++i) | |
| 60 delete i->second; | |
| 61 } | 29 } |
| 62 | 30 |
| 63 void DevToolsBrowserTarget::RegisterDomainHandler(DomainHandler* handler) { | 31 void DevToolsBrowserTarget::RegisterDomainHandler( |
| 64 std::string domain = handler->domain(); | 32 const std::string& domain, |
| 33 DevToolsProtocol::Handler* handler) { |
| 65 DCHECK(handlers_.find(domain) == handlers_.end()); | 34 DCHECK(handlers_.find(domain) == handlers_.end()); |
| 66 handlers_[domain] = handler; | 35 handlers_[domain] = handler; |
| 67 handler->set_notifier(Bind(&DevToolsBrowserTarget::SendNotification, | 36 handler->SetNotifier(base::Bind(&DevToolsBrowserTarget::OnNotification, |
| 68 weak_factory_.GetWeakPtr())); | 37 weak_factory_.GetWeakPtr())); |
| 69 } | 38 } |
| 70 | 39 |
| 71 std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { | 40 std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { |
| 72 std::string error_response; | 41 std::string error_response; |
| 73 scoped_ptr<DevToolsProtocol::Command> command( | 42 scoped_ptr<DevToolsProtocol::Command> command( |
| 74 DevToolsProtocol::ParseCommand(data, &error_response)); | 43 DevToolsProtocol::ParseCommand(data, &error_response)); |
| 75 if (!command.get()) | 44 if (!command) |
| 76 return error_response; | 45 return error_response; |
| 77 | 46 |
| 78 if (handlers_.find(command->domain()) == handlers_.end()) { | 47 if (handlers_.find(command->domain()) == handlers_.end()) |
| 79 scoped_ptr<DevToolsProtocol::Response> response( | 48 return command->NoSuchMethodErrorResponse()->Serialize(); |
| 80 command->NoSuchMethodErrorResponse()); | |
| 81 return response->Serialize(); | |
| 82 } | |
| 83 | 49 |
| 84 scoped_ptr<DevToolsProtocol::Response> response( | 50 scoped_ptr<DevToolsProtocol::Response> response( |
| 85 handlers_[command->domain()]->HandleCommand(command.get())); | 51 handlers_[command->domain()]->HandleCommand(command.get())); |
| 86 | 52 if (!response) |
| 53 return command->NoSuchMethodErrorResponse()->Serialize(); |
| 87 return response->Serialize(); | 54 return response->Serialize(); |
| 88 } | 55 } |
| 89 | 56 |
| 90 void DevToolsBrowserTarget::SendNotification(const std::string& method, | 57 void DevToolsBrowserTarget::OnNotification(const std::string& message) { |
| 91 DictionaryValue* params) { | |
| 92 DevToolsProtocol::Notification notification(method, params); | |
| 93 message_loop_proxy_->PostTask( | 58 message_loop_proxy_->PostTask( |
| 94 FROM_HERE, | 59 FROM_HERE, |
| 95 base::Bind(&net::HttpServer::SendOverWebSocket, | 60 base::Bind(&net::HttpServer::SendOverWebSocket, |
| 96 http_server_, | 61 http_server_, |
| 97 connection_id_, | 62 connection_id_, |
| 98 notification.Serialize())); | 63 message)); |
| 99 } | 64 } |
| 100 | 65 |
| 101 } // namespace content | 66 } // namespace content |
| OLD | NEW |