• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DictionaryValue;
25 class GURL;
26 class ListValue;
27 class Value;
28 
29 class ExtensionPortsRemoteService : public DevToolsRemoteListener,
30                                     public IPC::Message::Sender {
31  public:
32   // Specifies a tool name ("ExtensionPorts") handled by this class.
33   static const std::string kToolName;
34 
35   // |delegate| (never NULL) is the protocol handler instance which
36   // dispatches messages to this service.
37   // The ownership of |delegate| is NOT transferred to this class.
38   explicit ExtensionPortsRemoteService(DevToolsProtocolHandler* delegate);
39 
40   // DevToolsRemoteListener methods:
41 
42   // Processes |message| from the external client (where the tool is
43   // "ExtensionPorts").
44   virtual void HandleMessage(const DevToolsRemoteMessage& message);
45 
46   // Gets invoked on the external client socket connection loss.
47   // Closes open message ports.
48   virtual void OnConnectionLost();
49 
50   // IPC::Message::Sender methods:
51 
52   // This is the callback through which the ExtensionMessageService
53   // passes us messages from extensions as well as disconnect events.
54   virtual bool Send(IPC::Message* msg);
55 
56  private:
57   // Operation result returned in the "result" field in messages sent
58   // to the external client.
59   typedef enum {
60     RESULT_OK = 0,
61     RESULT_UNKNOWN_COMMAND,
62     RESULT_NO_SERVICE,
63     RESULT_PARAMETER_ERROR,
64     RESULT_UNKNOWN_PORT,
65     RESULT_TAB_NOT_FOUND,
66     RESULT_CONNECT_FAILED,  // probably extension ID not found.
67   } Result;
68 
69   virtual ~ExtensionPortsRemoteService();
70 
71   // Sends a JSON message with the |response| to the external client.
72   // |tool| and |destination| are used as the respective header values.
73   void SendResponse(const Value& response,
74                     const std::string& tool,
75                     const std::string& destination);
76 
77   // Handles a message from the ExtensionMessageService.
78   void OnExtensionMessageInvoke(const std::string& extension_id,
79                                 const std::string& function_name,
80                                 const ListValue& args,
81                                 const GURL& event_url);
82   // Handles a message sent from an extension through the
83   // ExtensionMessageService, to be passed to the external client.
84   void OnExtensionMessage(const std::string& message, int port_id);
85   // Handles a disconnect event sent from the ExtensionMessageService.
86   void OnExtensionPortDisconnected(int port_id);
87 
88   // Implementation for the commands we can receive from the external client.
89   // Opens a channel to an extension.
90   void ConnectCommand(DictionaryValue* content, DictionaryValue* response);
91   // Disconnects a message port.
92   void DisconnectCommand(int port_id, DictionaryValue* response);
93   // Sends a message to an extension through an established message port.
94   void PostMessageCommand(int port_id, DictionaryValue* content,
95                           DictionaryValue* response);
96 
97   // The delegate is used to send responses and events back to the
98   // external client, and to resolve tab IDs.
99   DevToolsProtocolHandler* delegate_;
100 
101   // Set of message port IDs we successfully opened.
102   typedef std::set<int> PortIdSet;
103   PortIdSet openPortIds_;
104 
105   scoped_refptr<ExtensionMessageService> service_;
106 
107   DISALLOW_COPY_AND_ASSIGN(ExtensionPortsRemoteService);
108 };
109 
110 #endif  // CHROME_BROWSER_DEBUGGER_EXTENSION_PORTS_REMOTE_SERVICE_H_
111