[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7001915 | 2012-12-19 11:44:19 | [diff] [blame] | 5 | #include "content/browser/devtools/devtools_browser_target.h" |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 6 | |
[email protected] | f6ebde6 | 2013-01-15 15:43:07 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | f6ebde6 | 2013-01-15 15:43:07 | [diff] [blame] | 8 | #include "base/location.h" |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | f6ebde6 | 2013-01-15 15:43:07 | [diff] [blame] | 11 | #include "base/message_loop_proxy.h" |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | f6ebde6 | 2013-01-15 15:43:07 | [diff] [blame] | 13 | #include "net/server/http_server.h" |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 14 | |
[email protected] | dcf115a | 2013-01-16 19:00:21 | [diff] [blame] | 15 | namespace content { |
| 16 | |
[email protected] | f6ebde6 | 2013-01-15 15:43:07 | [diff] [blame] | 17 | DevToolsBrowserTarget::DevToolsBrowserTarget( |
| 18 | base::MessageLoopProxy* message_loop_proxy, |
| 19 | net::HttpServer* http_server, |
| 20 | int connection_id) |
[email protected] | dcf115a | 2013-01-16 19:00:21 | [diff] [blame] | 21 | : message_loop_proxy_(message_loop_proxy), |
| 22 | http_server_(http_server), |
| 23 | connection_id_(connection_id), |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 24 | handlers_deleter_(&handlers_), |
[email protected] | dcf115a | 2013-01-16 19:00:21 | [diff] [blame] | 25 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | DevToolsBrowserTarget::~DevToolsBrowserTarget() { |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 31 | void DevToolsBrowserTarget::RegisterDomainHandler( |
| 32 | const std::string& domain, |
| 33 | DevToolsProtocol::Handler* handler) { |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 34 | DCHECK(handlers_.find(domain) == handlers_.end()); |
| 35 | handlers_[domain] = handler; |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 36 | handler->SetNotifier(base::Bind(&DevToolsBrowserTarget::OnNotification, |
| 37 | weak_factory_.GetWeakPtr())); |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) { |
[email protected] | 72c507e99 | 2013-02-04 14:58:36 | [diff] [blame] | 41 | std::string error_response; |
| 42 | scoped_ptr<DevToolsProtocol::Command> command( |
| 43 | DevToolsProtocol::ParseCommand(data, &error_response)); |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 44 | if (!command) |
[email protected] | 72c507e99 | 2013-02-04 14:58:36 | [diff] [blame] | 45 | return error_response; |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 46 | |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 47 | if (handlers_.find(command->domain()) == handlers_.end()) |
| 48 | return command->NoSuchMethodErrorResponse()->Serialize(); |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 49 | |
[email protected] | 72c507e99 | 2013-02-04 14:58:36 | [diff] [blame] | 50 | scoped_ptr<DevToolsProtocol::Response> response( |
| 51 | handlers_[command->domain()]->HandleCommand(command.get())); |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 52 | if (!response) |
| 53 | return command->NoSuchMethodErrorResponse()->Serialize(); |
[email protected] | 72c507e99 | 2013-02-04 14:58:36 | [diff] [blame] | 54 | return response->Serialize(); |
[email protected] | dcf115a | 2013-01-16 19:00:21 | [diff] [blame] | 55 | } |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 56 | |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 57 | void DevToolsBrowserTarget::OnNotification(const std::string& message) { |
[email protected] | dcf115a | 2013-01-16 19:00:21 | [diff] [blame] | 58 | message_loop_proxy_->PostTask( |
| 59 | FROM_HERE, |
| 60 | base::Bind(&net::HttpServer::SendOverWebSocket, |
| 61 | http_server_, |
| 62 | connection_id_, |
[email protected] | 87ae20d | 2013-02-21 03:55:09 | [diff] [blame] | 63 | message)); |
[email protected] | a0a5a3a | 2012-12-18 00:32:10 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace content |