1 // Copyright (c) 2011 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 5 #ifndef CHROME_BROWSER_DEBUGGER_DEVTOOLS_PROTOCOL_HANDLER_H_ 6 #define CHROME_BROWSER_DEBUGGER_DEVTOOLS_PROTOCOL_HANDLER_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/hash_tables.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "chrome/browser/debugger/devtools_remote.h" 15 #include "net/base/listen_socket.h" 16 17 class InspectableTabProxy; 18 class DevToolsRemoteListenSocket; 19 class DevToolsRemoteMessage; 20 21 // Dispatches DevToolsRemoteMessages to their appropriate handlers (Tools) 22 // based on the "Tool" message header value. 23 class DevToolsProtocolHandler 24 : public DevToolsRemoteListener, 25 public OutboundSocketDelegate { 26 public: 27 typedef base::hash_map< std::string, scoped_refptr<DevToolsRemoteListener> > 28 ToolToListenerMap; 29 30 static scoped_refptr<DevToolsProtocolHandler> Start(int port); 31 32 // Called from the main thread in order to stop protocol handler. 33 // Will schedule tear down task on IO thread. 34 void Stop(); 35 36 // Registers a |listener| to handle messages for a certain |tool_name| Tool. 37 // |listener| is the new message handler to register. 38 // As DevToolsRemoteListener inherits base::RefCountedThreadSafe, 39 // you should have no problems with ownership and destruction. 40 // |tool_name| is the name of the Tool to associate the listener with. 41 void RegisterDestination(DevToolsRemoteListener* listener, 42 const std::string& tool_name); 43 44 // Unregisters a |listener| so that it will no longer handle messages 45 // directed to the specified |tool_name| tool. 46 void UnregisterDestination(DevToolsRemoteListener* listener, 47 const std::string& tool_name); 48 49 InspectableTabProxy* inspectable_tab_proxy() { 50 return inspectable_tab_proxy_.get(); 51 } 52 53 // DevToolsRemoteListener interface 54 virtual void HandleMessage(const DevToolsRemoteMessage& message); 55 virtual void OnAcceptConnection(ListenSocket *connection); 56 virtual void OnConnectionLost(); 57 58 // OutboundSocketDelegate interface 59 virtual void Send(const DevToolsRemoteMessage& message); 60 61 private: 62 explicit DevToolsProtocolHandler(int port); 63 virtual ~DevToolsProtocolHandler(); 64 void Start(); 65 66 void Init(); 67 void Teardown(); 68 int port_; 69 ToolToListenerMap tool_to_listener_map_; 70 scoped_refptr<ListenSocket> connection_; 71 scoped_refptr<DevToolsRemoteListenSocket> server_; 72 scoped_ptr<InspectableTabProxy> inspectable_tab_proxy_; 73 DISALLOW_COPY_AND_ASSIGN(DevToolsProtocolHandler); 74 }; 75 76 #endif // CHROME_BROWSER_DEBUGGER_DEVTOOLS_PROTOCOL_HANDLER_H_ 77