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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// 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.
#include "chrome/browser/automation/extension_port_container.h"
#include "base/logging.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/automation/automation_provider.h"
#include "chrome/browser/automation/extension_automation_constants.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/common/automation_messages.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/render_messages.h"
// TODO(siggi): Find a more structured way to read and write JSON messages.
namespace ext = extension_automation_constants;
ExtensionPortContainer::ExtensionPortContainer(AutomationProvider* automation,
int tab_handle) :
automation_(automation), service_(NULL), port_id_(-1),
tab_handle_(tab_handle) {
service_ = automation_->profile()->GetExtensionMessageService();
DCHECK(service_);
}
ExtensionPortContainer::~ExtensionPortContainer() {
DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
if (port_id_ != -1)
service_->CloseChannel(port_id_);
}
bool ExtensionPortContainer::PostResponseToExternalPort(
const std::string& message) {
return automation_->Send(
new AutomationMsg_ForwardMessageToExternalHost(
0, tab_handle_, message, ext::kAutomationOrigin,
ext::kAutomationPortResponseTarget));
}
bool ExtensionPortContainer::PostMessageToExternalPort(
const std::string& message) {
return automation_->Send(
new AutomationMsg_ForwardMessageToExternalHost(
0, tab_handle_, message,
ext::kAutomationOrigin,
ext::kAutomationPortRequestTarget));
}
void ExtensionPortContainer::PostMessageFromExternalPort(
const std::string &message) {
service_->PostMessageFromRenderer(port_id_, message);
}
bool ExtensionPortContainer::Connect(const std::string &extension_id,
int process_id,
int routing_id,
int connection_id,
const std::string& channel_name,
const std::string& tab_json) {
DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
port_id_ = service_->OpenSpecialChannelToExtension(
extension_id, channel_name, tab_json, this);
if (port_id_ == -1) {
// In this case a disconnect message has been dispatched.
return false;
}
SendConnectionResponse(connection_id, port_id_);
return true;
}
void ExtensionPortContainer::SendConnectionResponse(int connection_id,
int port_id) {
// Compose the reply message.
scoped_ptr<DictionaryValue> msg_dict(new DictionaryValue());
msg_dict->SetInteger(ext::kAutomationRequestIdKey, ext::CHANNEL_OPENED);
msg_dict->SetInteger(ext::kAutomationConnectionIdKey, connection_id);
msg_dict->SetInteger(ext::kAutomationPortIdKey, port_id);
std::string msg_json;
base::JSONWriter::Write(msg_dict.get(), false, &msg_json);
PostResponseToExternalPort(msg_json);
}
bool ExtensionPortContainer::Send(IPC::Message *message) {
DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
IPC_BEGIN_MESSAGE_MAP(ExtensionPortContainer, *message)
IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
OnExtensionMessageInvoke)
IPC_MESSAGE_UNHANDLED_ERROR()
IPC_END_MESSAGE_MAP()
delete message;
return true;
}
void ExtensionPortContainer::OnExtensionMessageInvoke(
const std::string& extension_id,
const std::string& function_name,
const ListValue& args,
const GURL& event_url) {
if (function_name == ExtensionMessageService::kDispatchOnMessage) {
DCHECK_EQ(args.GetSize(), 2u);
std::string message;
int source_port_id;
if (args.GetString(0, &message) && args.GetInteger(1, &source_port_id))
OnExtensionHandleMessage(message, source_port_id);
} else if (function_name == ExtensionMessageService::kDispatchOnDisconnect) {
DCHECK_EQ(args.GetSize(), 1u);
int port_id;
if (args.GetInteger(0, &port_id))
OnExtensionPortDisconnected(port_id);
} else if (function_name == ExtensionMessageService::kDispatchOnConnect) {
// Do nothing.
// TODO(siggi): implement
} else {
NOTREACHED() << function_name << " shouldn't be called.";
}
}
void ExtensionPortContainer::OnExtensionHandleMessage(
const std::string& message, int source_port_id) {
// Compose the reply message and fire it away.
DictionaryValue msg_dict;
msg_dict.SetInteger(ext::kAutomationRequestIdKey, ext::POST_MESSAGE);
msg_dict.SetInteger(ext::kAutomationPortIdKey, port_id_);
msg_dict.SetString(ext::kAutomationMessageDataKey, message);
std::string msg_json;
base::JSONWriter::Write(&msg_dict, false, &msg_json);
PostMessageToExternalPort(msg_json);
}
void ExtensionPortContainer::OnExtensionPortDisconnected(int source_port_id) {
// Compose the disconnect message and fire it away.
DictionaryValue msg_dict;
msg_dict.SetInteger(ext::kAutomationRequestIdKey, ext::CHANNEL_CLOSED);
msg_dict.SetInteger(ext::kAutomationPortIdKey, port_id_);
std::string msg_json;
base::JSONWriter::Write(&msg_dict, false, &msg_json);
PostMessageToExternalPort(msg_json);
}
bool ExtensionPortContainer::InterceptMessageFromExternalHost(
const std::string& message, const std::string& origin,
const std::string& target, AutomationProvider* automation,
RenderViewHost *view_host, int tab_handle) {
if (target != ext::kAutomationPortRequestTarget)
return false;
if (origin != ext::kAutomationOrigin) {
// TODO(siggi): Should we block the message on wrong origin?
LOG(WARNING) << "Wrong origin on automation port message " << origin;
}
scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
DCHECK(message_value->IsType(Value::TYPE_DICTIONARY));
if (!message_value->IsType(Value::TYPE_DICTIONARY))
return true;
DictionaryValue* message_dict =
reinterpret_cast<DictionaryValue*>(message_value.get());
int command = -1;
bool got_value = message_dict->GetInteger(ext::kAutomationRequestIdKey,
&command);
DCHECK(got_value);
if (!got_value)
return true;
if (command == ext::OPEN_CHANNEL) {
// Extract the "extension_id" and "connection_id" parameters.
std::string extension_id;
got_value = message_dict->GetString(ext::kAutomationExtensionIdKey,
&extension_id);
DCHECK(got_value);
if (!got_value)
return true;
int connection_id;
got_value = message_dict->GetInteger(ext::kAutomationConnectionIdKey,
&connection_id);
DCHECK(got_value);
if (!got_value)
return true;
std::string channel_name;
// Channel name is optional.
message_dict->GetString(ext::kAutomationChannelNameKey, &channel_name);
// Tab information is optional, try to retrieve it
// and re-flatten it to a string.
std::string tab_json("null");
DictionaryValue* tab = NULL;
if (message_dict->GetDictionary(ext::kAutomationTabJsonKey, &tab))
base::JSONWriter::Write(tab, false, &tab_json);
int routing_id = view_host->routing_id();
// Create the extension port and connect it.
scoped_ptr<ExtensionPortContainer> port(
new ExtensionPortContainer(automation, tab_handle));
int process_id = view_host->process()->id();
if (port->Connect(extension_id, process_id, routing_id, connection_id,
channel_name, tab_json)) {
// We have a successful connection.
automation->AddPortContainer(port.release());
}
} else if (command == ext::POST_MESSAGE) {
int port_id = -1;
got_value = message_dict->GetInteger(ext::kAutomationPortIdKey, &port_id);
DCHECK(got_value);
if (!got_value)
return true;
std::string data;
got_value = message_dict->GetString(ext::kAutomationMessageDataKey, &data);
DCHECK(got_value);
if (!got_value)
return true;
ExtensionPortContainer* port = automation->GetPortContainer(port_id);
DCHECK(port);
if (port)
port->PostMessageFromExternalPort(data);
} else if (command == ext::CHANNEL_CLOSED) {
int port_id = -1;
got_value = message_dict->GetInteger(ext::kAutomationPortIdKey, &port_id);
DCHECK(got_value);
if (!got_value)
return true;
ExtensionPortContainer* port = automation->GetPortContainer(port_id);
DCHECK(port);
if (port) {
// This will delete the port and notify the other end of the disconnect.
automation->RemovePortContainer(port);
}
} else {
// We don't expect other messages here.
NOTREACHED();
}
return true;
}
|