| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/debugger/devtools_protocol_handler.h" | 5 #include "chrome/browser/debugger/devtools_protocol_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/debugger/inspectable_tab_proxy.h" | 9 #include "chrome/browser/debugger/inspectable_tab_proxy.h" |
| 11 #include "chrome/browser/debugger/devtools_remote_message.h" | 10 #include "chrome/browser/debugger/devtools_remote_message.h" |
| 12 #include "chrome/browser/debugger/devtools_remote_listen_socket.h" | 11 #include "chrome/browser/debugger/devtools_remote_listen_socket.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 | 13 |
| 15 DevToolsProtocolHandler::DevToolsProtocolHandler(int port) | 14 DevToolsProtocolHandler::DevToolsProtocolHandler(int port) |
| 16 : port_(port), | 15 : port_(port), |
| 17 connection_(NULL), | 16 connection_(NULL), |
| 18 server_(NULL) { | 17 server_(NULL) { |
| 19 ui_loop_ = MessageLoop::current(); | |
| 20 io_loop_ = g_browser_process->io_thread()->message_loop(); | |
| 21 inspectable_tab_proxy_.reset(new InspectableTabProxy); | 18 inspectable_tab_proxy_.reset(new InspectableTabProxy); |
| 22 } | 19 } |
| 23 | 20 |
| 24 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 21 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
| 25 // Stop() must be called prior to this being called | 22 // Stop() must be called prior to this being called |
| 26 DCHECK(server_.get() == NULL); | 23 DCHECK(server_.get() == NULL); |
| 27 DCHECK(connection_.get() == NULL); | 24 DCHECK(connection_.get() == NULL); |
| 28 } | 25 } |
| 29 | 26 |
| 30 void DevToolsProtocolHandler::Start() { | 27 void DevToolsProtocolHandler::Start() { |
| 31 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 28 ChromeThread::PostTask( |
| 32 this, &DevToolsProtocolHandler::Init)); | 29 ChromeThread::IO, FROM_HERE, |
| 30 NewRunnableMethod(this, &DevToolsProtocolHandler::Init)); |
| 33 } | 31 } |
| 34 | 32 |
| 35 void DevToolsProtocolHandler::Init() { | 33 void DevToolsProtocolHandler::Init() { |
| 36 server_ = DevToolsRemoteListenSocket::Listen( | 34 server_ = DevToolsRemoteListenSocket::Listen( |
| 37 "127.0.0.1", port_, this, this); | 35 "127.0.0.1", port_, this, this); |
| 38 } | 36 } |
| 39 | 37 |
| 40 void DevToolsProtocolHandler::Stop() { | 38 void DevToolsProtocolHandler::Stop() { |
| 41 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 39 ChromeThread::PostTask( |
| 42 this, &DevToolsProtocolHandler::Teardown)); | 40 ChromeThread::IO, FROM_HERE, |
| 41 NewRunnableMethod(this, &DevToolsProtocolHandler::Teardown)); |
| 43 tool_to_listener_map_.clear(); // Releases all scoped_refptr's to listeners | 42 tool_to_listener_map_.clear(); // Releases all scoped_refptr's to listeners |
| 44 } | 43 } |
| 45 | 44 |
| 46 // Run in I/O thread | 45 // Run in I/O thread |
| 47 void DevToolsProtocolHandler::Teardown() { | 46 void DevToolsProtocolHandler::Teardown() { |
| 48 connection_ = NULL; | 47 connection_ = NULL; |
| 49 server_ = NULL; | 48 server_ = NULL; |
| 50 } | 49 } |
| 51 | 50 |
| 52 void DevToolsProtocolHandler::RegisterDestination( | 51 void DevToolsProtocolHandler::RegisterDestination( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 66 | 65 |
| 67 void DevToolsProtocolHandler::HandleMessage( | 66 void DevToolsProtocolHandler::HandleMessage( |
| 68 const DevToolsRemoteMessage& message) { | 67 const DevToolsRemoteMessage& message) { |
| 69 std::string tool = message.GetHeaderWithEmptyDefault( | 68 std::string tool = message.GetHeaderWithEmptyDefault( |
| 70 DevToolsRemoteMessageHeaders::kTool); | 69 DevToolsRemoteMessageHeaders::kTool); |
| 71 ToolToListenerMap::const_iterator it = tool_to_listener_map_.find(tool); | 70 ToolToListenerMap::const_iterator it = tool_to_listener_map_.find(tool); |
| 72 if (it == tool_to_listener_map_.end()) { | 71 if (it == tool_to_listener_map_.end()) { |
| 73 NOTREACHED(); // an unsupported tool, bail out | 72 NOTREACHED(); // an unsupported tool, bail out |
| 74 return; | 73 return; |
| 75 } | 74 } |
| 76 DCHECK(MessageLoop::current() == io_loop_); | 75 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 77 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 76 ChromeThread::PostTask( |
| 78 it->second.get(), &DevToolsRemoteListener::HandleMessage, message)); | 77 ChromeThread::UI, FROM_HERE, |
| 78 NewRunnableMethod( |
| 79 it->second.get(), &DevToolsRemoteListener::HandleMessage, message)); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void DevToolsProtocolHandler::Send(const DevToolsRemoteMessage& message) { | 82 void DevToolsProtocolHandler::Send(const DevToolsRemoteMessage& message) { |
| 82 if (connection_ != NULL) { | 83 if (connection_ != NULL) { |
| 83 connection_->Send(message.ToString()); | 84 connection_->Send(message.ToString()); |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 | 87 |
| 87 void DevToolsProtocolHandler::DidAccept(ListenSocket *server, | 88 void DevToolsProtocolHandler::DidAccept(ListenSocket *server, |
| 88 ListenSocket *connection) { | 89 ListenSocket *connection) { |
| 89 DCHECK(MessageLoop::current() == io_loop_); | 90 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 90 if (connection_ == NULL) { | 91 if (connection_ == NULL) { |
| 91 connection_ = connection; | 92 connection_ = connection; |
| 92 connection_->AddRef(); | 93 connection_->AddRef(); |
| 93 } | 94 } |
| 94 // else the connection will get deleted itself with scoped_refptr | 95 // else the connection will get deleted itself with scoped_refptr |
| 95 } | 96 } |
| 96 | 97 |
| 97 void DevToolsProtocolHandler::DidRead(ListenSocket *connection, | 98 void DevToolsProtocolHandler::DidRead(ListenSocket *connection, |
| 98 const std::string& data) { | 99 const std::string& data) { |
| 99 // Not used. | 100 // Not used. |
| 100 } | 101 } |
| 101 | 102 |
| 102 void DevToolsProtocolHandler::DidClose(ListenSocket *sock) { | 103 void DevToolsProtocolHandler::DidClose(ListenSocket *sock) { |
| 103 DCHECK(MessageLoop::current() == io_loop_); | 104 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 104 DCHECK(connection_ == sock); | 105 DCHECK(connection_ == sock); |
| 105 connection_ = NULL; | 106 connection_ = NULL; |
| 106 sock->Release(); | 107 sock->Release(); |
| 107 for (ToolToListenerMap::const_iterator it = tool_to_listener_map_.begin(), | 108 for (ToolToListenerMap::const_iterator it = tool_to_listener_map_.begin(), |
| 108 end = tool_to_listener_map_.end(); | 109 end = tool_to_listener_map_.end(); |
| 109 it != end; | 110 it != end; |
| 110 ++it) { | 111 ++it) { |
| 111 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 112 ChromeThread::PostTask( |
| 112 it->second.get(), &DevToolsRemoteListener::OnConnectionLost)); | 113 ChromeThread::UI, FROM_HERE, |
| 114 NewRunnableMethod( |
| 115 it->second.get(), &DevToolsRemoteListener::OnConnectionLost)); |
| 113 } | 116 } |
| 114 } | 117 } |
| OLD | NEW |