blob: aa5e1ac535c5b64ed1b0dd05334e8812a204c5d5 [file] [log] [blame]
[email protected]a0a5a3a2012-12-18 00:32:101// 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]70019152012-12-19 11:44:195#include "content/browser/devtools/devtools_browser_target.h"
[email protected]a0a5a3a2012-12-18 00:32:106
[email protected]f6ebde62013-01-15 15:43:077#include "base/bind.h"
[email protected]f6ebde62013-01-15 15:43:078#include "base/location.h"
[email protected]a0a5a3a2012-12-18 00:32:109#include "base/logging.h"
10#include "base/memory/scoped_ptr.h"
[email protected]f6ebde62013-01-15 15:43:0711#include "base/message_loop_proxy.h"
[email protected]a0a5a3a2012-12-18 00:32:1012#include "base/values.h"
[email protected]f6ebde62013-01-15 15:43:0713#include "net/server/http_server.h"
[email protected]a0a5a3a2012-12-18 00:32:1014
[email protected]dcf115a2013-01-16 19:00:2115namespace content {
16
[email protected]f6ebde62013-01-15 15:43:0717DevToolsBrowserTarget::DevToolsBrowserTarget(
18 base::MessageLoopProxy* message_loop_proxy,
19 net::HttpServer* http_server,
20 int connection_id)
[email protected]dcf115a2013-01-16 19:00:2121 : message_loop_proxy_(message_loop_proxy),
22 http_server_(http_server),
23 connection_id_(connection_id),
[email protected]87ae20d2013-02-21 03:55:0924 handlers_deleter_(&handlers_),
[email protected]dcf115a2013-01-16 19:00:2125 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]a0a5a3a2012-12-18 00:32:1026}
27
28DevToolsBrowserTarget::~DevToolsBrowserTarget() {
[email protected]a0a5a3a2012-12-18 00:32:1029}
30
[email protected]87ae20d2013-02-21 03:55:0931void DevToolsBrowserTarget::RegisterDomainHandler(
32 const std::string& domain,
33 DevToolsProtocol::Handler* handler) {
[email protected]a0a5a3a2012-12-18 00:32:1034 DCHECK(handlers_.find(domain) == handlers_.end());
35 handlers_[domain] = handler;
[email protected]87ae20d2013-02-21 03:55:0936 handler->SetNotifier(base::Bind(&DevToolsBrowserTarget::OnNotification,
37 weak_factory_.GetWeakPtr()));
[email protected]a0a5a3a2012-12-18 00:32:1038}
39
40std::string DevToolsBrowserTarget::HandleMessage(const std::string& data) {
[email protected]72c507e992013-02-04 14:58:3641 std::string error_response;
42 scoped_ptr<DevToolsProtocol::Command> command(
43 DevToolsProtocol::ParseCommand(data, &error_response));
[email protected]87ae20d2013-02-21 03:55:0944 if (!command)
[email protected]72c507e992013-02-04 14:58:3645 return error_response;
[email protected]a0a5a3a2012-12-18 00:32:1046
[email protected]87ae20d2013-02-21 03:55:0947 if (handlers_.find(command->domain()) == handlers_.end())
48 return command->NoSuchMethodErrorResponse()->Serialize();
[email protected]a0a5a3a2012-12-18 00:32:1049
[email protected]72c507e992013-02-04 14:58:3650 scoped_ptr<DevToolsProtocol::Response> response(
51 handlers_[command->domain()]->HandleCommand(command.get()));
[email protected]87ae20d2013-02-21 03:55:0952 if (!response)
53 return command->NoSuchMethodErrorResponse()->Serialize();
[email protected]72c507e992013-02-04 14:58:3654 return response->Serialize();
[email protected]dcf115a2013-01-16 19:00:2155}
[email protected]a0a5a3a2012-12-18 00:32:1056
[email protected]87ae20d2013-02-21 03:55:0957void DevToolsBrowserTarget::OnNotification(const std::string& message) {
[email protected]dcf115a2013-01-16 19:00:2158 message_loop_proxy_->PostTask(
59 FROM_HERE,
60 base::Bind(&net::HttpServer::SendOverWebSocket,
61 http_server_,
62 connection_id_,
[email protected]87ae20d2013-02-21 03:55:0963 message));
[email protected]a0a5a3a2012-12-18 00:32:1064}
65
66} // namespace content