| OLD | NEW |
| (Empty) |
| 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 // ExtensionsPorts service: wires extension message ports through the | |
| 6 // devtools remote protocol, allowing an external client program to | |
| 7 // exchange messages with Chrome extensions. | |
| 8 | |
| 9 #ifndef CHROME_BROWSER_DEBUGGER_EXTENSION_PORTS_REMOTE_SERVICE_H_ | |
| 10 #define CHROME_BROWSER_DEBUGGER_EXTENSION_PORTS_REMOTE_SERVICE_H_ | |
| 11 #pragma once | |
| 12 | |
| 13 #include <set> | |
| 14 #include <string> | |
| 15 | |
| 16 #include "base/basictypes.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "chrome/browser/debugger/devtools_remote.h" | |
| 19 #include "chrome/browser/extensions/extension_message_service.h" | |
| 20 #include "ipc/ipc_message.h" | |
| 21 | |
| 22 class DevToolsProtocolHandler; | |
| 23 class DevToolsRemoteMessage; | |
| 24 class GURL; | |
| 25 | |
| 26 namespace base { | |
| 27 class DictionaryValue; | |
| 28 class ListValue; | |
| 29 class Value; | |
| 30 } | |
| 31 | |
| 32 class ExtensionPortsRemoteService : public DevToolsRemoteListener, | |
| 33 public IPC::Message::Sender { | |
| 34 public: | |
| 35 // Specifies a tool name ("ExtensionPorts") handled by this class. | |
| 36 static const char kToolName[]; | |
| 37 | |
| 38 // |delegate| (never NULL) is the protocol handler instance which | |
| 39 // dispatches messages to this service. | |
| 40 // The ownership of |delegate| is NOT transferred to this class. | |
| 41 explicit ExtensionPortsRemoteService(DevToolsProtocolHandler* delegate); | |
| 42 | |
| 43 // DevToolsRemoteListener methods: | |
| 44 | |
| 45 // Processes |message| from the external client (where the tool is | |
| 46 // "ExtensionPorts"). | |
| 47 virtual void HandleMessage(const DevToolsRemoteMessage& message) OVERRIDE; | |
| 48 | |
| 49 // Gets invoked on the external client socket connection loss. | |
| 50 // Closes open message ports. | |
| 51 virtual void OnConnectionLost() OVERRIDE; | |
| 52 | |
| 53 // IPC::Message::Sender methods: | |
| 54 | |
| 55 // This is the callback through which the ExtensionMessageService | |
| 56 // passes us messages from extensions as well as disconnect events. | |
| 57 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 58 | |
| 59 private: | |
| 60 // Operation result returned in the "result" field in messages sent | |
| 61 // to the external client. | |
| 62 typedef enum { | |
| 63 RESULT_OK = 0, | |
| 64 RESULT_UNKNOWN_COMMAND, | |
| 65 RESULT_NO_SERVICE, | |
| 66 RESULT_PARAMETER_ERROR, | |
| 67 RESULT_UNKNOWN_PORT, | |
| 68 RESULT_TAB_NOT_FOUND, | |
| 69 RESULT_CONNECT_FAILED, // probably extension ID not found. | |
| 70 } Result; | |
| 71 | |
| 72 virtual ~ExtensionPortsRemoteService(); | |
| 73 | |
| 74 // Sends a JSON message with the |response| to the external client. | |
| 75 // |tool| and |destination| are used as the respective header values. | |
| 76 void SendResponse(const base::Value& response, | |
| 77 const std::string& tool, | |
| 78 const std::string& destination); | |
| 79 | |
| 80 // Handles requests from ExtensionMessageService to invoke specific | |
| 81 // JavaScript functions. Currently we only handle the "disconnect" function. | |
| 82 void OnExtensionMessageInvoke(const std::string& extension_id, | |
| 83 const std::string& function_name, | |
| 84 const base::ListValue& args, | |
| 85 const GURL& event_url); | |
| 86 // Handles a message sent from an extension through the | |
| 87 // ExtensionMessageService, to be passed to the external client. | |
| 88 void OnDeliverMessage(int port_id, const std::string& message); | |
| 89 // Handles a disconnect event sent from the ExtensionMessageService. | |
| 90 void OnExtensionPortDisconnected(int port_id); | |
| 91 | |
| 92 // Implementation for the commands we can receive from the external client. | |
| 93 // Opens a channel to an extension. | |
| 94 void ConnectCommand(base::DictionaryValue* content, | |
| 95 base::DictionaryValue* response); | |
| 96 // Disconnects a message port. | |
| 97 void DisconnectCommand(int port_id, base::DictionaryValue* response); | |
| 98 // Sends a message to an extension through an established message port. | |
| 99 void PostMessageCommand(int port_id, | |
| 100 base::DictionaryValue* content, | |
| 101 base::DictionaryValue* response); | |
| 102 | |
| 103 // The delegate is used to send responses and events back to the | |
| 104 // external client, and to resolve tab IDs. | |
| 105 DevToolsProtocolHandler* delegate_; | |
| 106 | |
| 107 // Set of message port IDs we successfully opened. | |
| 108 typedef std::set<int> PortIdSet; | |
| 109 PortIdSet openPortIds_; | |
| 110 | |
| 111 scoped_refptr<ExtensionMessageService> service_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(ExtensionPortsRemoteService); | |
| 114 }; | |
| 115 | |
| 116 #endif // CHROME_BROWSER_DEBUGGER_EXTENSION_PORTS_REMOTE_SERVICE_H_ | |
| OLD | NEW |