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