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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// Copyright 2017 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 "extensions/renderer/ipc_message_sender.h"
#include <map>
#include "base/guid.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/worker_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/features/feature.h"
#include "extensions/renderer/message_target.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/worker_thread_dispatcher.h"
namespace extensions {
namespace {
class MainThreadIPCMessageSender : public IPCMessageSender {
public:
MainThreadIPCMessageSender() : render_thread_(content::RenderThread::Get()) {}
~MainThreadIPCMessageSender() override {}
void SendRequestIPC(ScriptContext* context,
std::unique_ptr<ExtensionHostMsg_Request_Params> params,
binding::RequestThread thread) override {
content::RenderFrame* frame = context->GetRenderFrame();
if (!frame)
return;
switch (thread) {
case binding::RequestThread::UI:
frame->Send(
new ExtensionHostMsg_Request(frame->GetRoutingID(), *params));
break;
case binding::RequestThread::IO:
frame->Send(new ExtensionHostMsg_RequestForIOThread(
frame->GetRoutingID(), *params));
break;
}
}
void SendOnRequestResponseReceivedIPC(int request_id) override {}
void SendAddUnfilteredEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_AddListener(
context->GetExtensionID(), context->url(), event_name, kMainThreadId));
}
void SendRemoveUnfilteredEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_RemoveListener(
context->GetExtensionID(), context->url(), event_name, kMainThreadId));
}
void SendAddUnfilteredLazyEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_AddLazyListener(
context->GetExtensionID(), event_name));
}
void SendRemoveUnfilteredLazyEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_RemoveLazyListener(
context->GetExtensionID(), event_name));
}
void SendAddFilteredEventListenerIPC(ScriptContext* context,
const std::string& event_name,
const base::DictionaryValue& filter,
bool is_lazy) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_AddFilteredListener(
context->GetExtensionID(), event_name, base::nullopt, filter, is_lazy));
}
void SendRemoveFilteredEventListenerIPC(ScriptContext* context,
const std::string& event_name,
const base::DictionaryValue& filter,
bool remove_lazy_listener) override {
DCHECK_NE(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
render_thread_->Send(new ExtensionHostMsg_RemoveFilteredListener(
context->GetExtensionID(), event_name, base::nullopt, filter,
remove_lazy_listener));
}
void SendOpenMessageChannel(ScriptContext* script_context,
const PortId& port_id,
const MessageTarget& target,
const std::string& channel_name,
bool include_tls_channel_id) override {
content::RenderFrame* render_frame = script_context->GetRenderFrame();
DCHECK(render_frame);
int routing_id = render_frame->GetRoutingID();
const Extension* extension = script_context->extension();
switch (target.type) {
case MessageTarget::EXTENSION: {
ExtensionMsg_ExternalConnectionInfo info;
if (extension && !extension->is_hosted_app())
info.source_id = extension->id();
info.target_id = *target.extension_id;
info.source_url = script_context->url();
render_thread_->Send(new ExtensionHostMsg_OpenChannelToExtension(
routing_id, info, channel_name, include_tls_channel_id, port_id));
break;
}
case MessageTarget::TAB: {
DCHECK(extension);
ExtensionMsg_TabTargetConnectionInfo info;
info.tab_id = *target.tab_id;
info.frame_id = *target.frame_id;
render_frame->Send(new ExtensionHostMsg_OpenChannelToTab(
routing_id, info, extension->id(), channel_name, port_id));
break;
}
case MessageTarget::NATIVE_APP:
render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
routing_id, *target.native_application_name, port_id));
break;
}
}
void SendOpenMessagePort(int routing_id, const PortId& port_id) override {
render_thread_->Send(
new ExtensionHostMsg_OpenMessagePort(routing_id, port_id));
}
void SendCloseMessagePort(int routing_id,
const PortId& port_id,
bool close_channel) override {
render_thread_->Send(new ExtensionHostMsg_CloseMessagePort(
routing_id, port_id, close_channel));
}
void SendPostMessageToPort(int routing_id,
const PortId& port_id,
const Message& message) override {
render_thread_->Send(
new ExtensionHostMsg_PostMessage(routing_id, port_id, message));
}
private:
content::RenderThread* const render_thread_;
DISALLOW_COPY_AND_ASSIGN(MainThreadIPCMessageSender);
};
class WorkerThreadIPCMessageSender : public IPCMessageSender {
public:
WorkerThreadIPCMessageSender(WorkerThreadDispatcher* dispatcher,
int64_t service_worker_version_id)
: dispatcher_(dispatcher),
service_worker_version_id_(service_worker_version_id) {}
~WorkerThreadIPCMessageSender() override {}
void SendRequestIPC(ScriptContext* context,
std::unique_ptr<ExtensionHostMsg_Request_Params> params,
binding::RequestThread thread) override {
DCHECK(!context->GetRenderFrame());
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_EQ(binding::RequestThread::UI, thread);
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
int worker_thread_id = content::WorkerThread::GetCurrentId();
params->worker_thread_id = worker_thread_id;
params->service_worker_version_id = service_worker_version_id_;
std::string guid = base::GenerateGUID();
request_id_to_guid_[params->request_id] = guid;
// Keeps the worker alive during extension function call. Balanced in
// HandleWorkerResponse().
dispatcher_->Send(new ExtensionHostMsg_IncrementServiceWorkerActivity(
service_worker_version_id_, guid));
dispatcher_->Send(new ExtensionHostMsg_RequestWorker(*params));
}
void SendOnRequestResponseReceivedIPC(int request_id) override {
std::map<int, std::string>::iterator iter =
request_id_to_guid_.find(request_id);
DCHECK(iter != request_id_to_guid_.end());
dispatcher_->Send(new ExtensionHostMsg_DecrementServiceWorkerActivity(
service_worker_version_id_, iter->second));
request_id_to_guid_.erase(iter);
}
void SendAddUnfilteredEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
dispatcher_->Send(new ExtensionHostMsg_AddListener(
context->GetExtensionID(), context->service_worker_scope(), event_name,
content::WorkerThread::GetCurrentId()));
}
void SendRemoveUnfilteredEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
dispatcher_->Send(new ExtensionHostMsg_RemoveListener(
context->GetExtensionID(), context->service_worker_scope(), event_name,
content::WorkerThread::GetCurrentId()));
}
void SendAddUnfilteredLazyEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
dispatcher_->Send(new ExtensionHostMsg_AddLazyServiceWorkerListener(
context->GetExtensionID(), event_name,
context->service_worker_scope()));
}
void SendRemoveUnfilteredLazyEventListenerIPC(
ScriptContext* context,
const std::string& event_name) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
dispatcher_->Send(new ExtensionHostMsg_RemoveLazyServiceWorkerListener(
context->GetExtensionID(), event_name,
context->service_worker_scope()));
}
void SendAddFilteredEventListenerIPC(ScriptContext* context,
const std::string& event_name,
const base::DictionaryValue& filter,
bool is_lazy) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
ServiceWorkerIdentifier sw_identifier;
sw_identifier.scope = context->service_worker_scope();
sw_identifier.thread_id = content::WorkerThread::GetCurrentId(),
dispatcher_->Send(new ExtensionHostMsg_AddFilteredListener(
context->GetExtensionID(), event_name, sw_identifier, filter, is_lazy));
}
void SendRemoveFilteredEventListenerIPC(ScriptContext* context,
const std::string& event_name,
const base::DictionaryValue& filter,
bool remove_lazy_listener) override {
DCHECK_EQ(Feature::SERVICE_WORKER_CONTEXT, context->context_type());
DCHECK_NE(kMainThreadId, content::WorkerThread::GetCurrentId());
ServiceWorkerIdentifier sw_identifier;
sw_identifier.scope = context->service_worker_scope();
sw_identifier.thread_id = content::WorkerThread::GetCurrentId(),
dispatcher_->Send(new ExtensionHostMsg_RemoveFilteredListener(
context->GetExtensionID(), event_name, sw_identifier, filter,
remove_lazy_listener));
}
void SendOpenMessageChannel(ScriptContext* script_context,
const PortId& port_id,
const MessageTarget& target,
const std::string& channel_name,
bool include_tls_channel_id) override {
NOTIMPLEMENTED();
}
void SendOpenMessagePort(int routing_id, const PortId& port_id) override {
NOTIMPLEMENTED();
}
void SendCloseMessagePort(int routing_id,
const PortId& port_id,
bool close_channel) override {
NOTIMPLEMENTED();
}
void SendPostMessageToPort(int routing_id,
const PortId& port_id,
const Message& message) override {
NOTIMPLEMENTED();
}
private:
WorkerThreadDispatcher* const dispatcher_;
const int64_t service_worker_version_id_;
// request id -> GUID map for each outstanding requests.
std::map<int, std::string> request_id_to_guid_;
DISALLOW_COPY_AND_ASSIGN(WorkerThreadIPCMessageSender);
};
} // namespace
IPCMessageSender::IPCMessageSender() {}
IPCMessageSender::~IPCMessageSender() = default;
// static
std::unique_ptr<IPCMessageSender>
IPCMessageSender::CreateMainThreadIPCMessageSender() {
return std::make_unique<MainThreadIPCMessageSender>();
}
// static
std::unique_ptr<IPCMessageSender>
IPCMessageSender::CreateWorkerThreadIPCMessageSender(
WorkerThreadDispatcher* dispatcher,
int64_t service_worker_version_id) {
return std::make_unique<WorkerThreadIPCMessageSender>(
dispatcher, service_worker_version_id);
}
} // namespace extensions
|