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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
content / browser / renderer_host / render_frame_host_impl_interface_binders.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include <memory>
#include <vector>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/metrics_hashes.h"
#include "base/task/single_thread_task_runner.h"
#include "content/browser/accessibility/render_accessibility_host.h"
#include "content/browser/attribution_reporting/attribution_host.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/file_system/file_system_manager_impl.h"
#include "content/browser/geolocation/geolocation_service_impl.h"
#include "content/browser/manifest/manifest_manager_host.h"
#include "content/browser/renderer_host/back_forward_cache_impl.h"
#include "content/browser/renderer_host/page_lifecycle_state_manager.h"
#include "content/browser/renderer_host/render_frame_host_delegate.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/shared_storage/shared_storage_document_service_impl.h"
#include "content/common/dom_automation_controller.mojom.h"
#include "content/common/frame.mojom.h"
#include "content/public/browser/active_url_message_filter.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_client.h"
#include "media/mojo/mojom/media_player.mojom.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "net/base/features.h"
#include "ppapi/buildflags/buildflags.h"
#include "services/device/public/mojom/screen_orientation.mojom.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "services/service_manager/public/mojom/interface_provider.mojom.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/blob/blob_url_store.mojom.h"
#include "third_party/blink/public/mojom/blob/file_backed_blob_factory.mojom.h"
#include "third_party/blink/public/mojom/broadcastchannel/broadcast_channel.mojom.h"
#include "third_party/blink/public/mojom/frame/back_forward_cache_controller.mojom.h"
#include "third_party/blink/public/mojom/frame/frame.mojom.h"
#include "third_party/blink/public/mojom/manifest/manifest_observer.mojom.h"
#include "third_party/blink/public/mojom/page/display_cutout.mojom.h"
#include "third_party/blink/public/mojom/shared_storage/shared_storage.mojom.h"
#if BUILDFLAG(ENABLE_PPAPI)
#include "content/browser/renderer_host/render_frame_host_impl_ppapi_support.h"
#include "content/common/pepper_plugin.mojom.h"
#endif
namespace content {
namespace {
// This class is used to chain multiple mojo::MessageFilter. Messages will be
// processed by the filters in the same order as the filters are added with the
// Add() method. WillDispatch() might not be called for all filters or might see
// a modified message if a filter earlier in the chain discards or modifies it.
// Similarly a given filter instance might not receive a DidDispatchOrReject()
// call even if WillDispatch() was called if a filter further down the chain
// discarded it. Long story short, the order in which filters are added is
// important!
class MessageFilterChain final : public mojo::MessageFilter {
public:
MessageFilterChain() = default;
~MessageFilterChain() final = default;
bool WillDispatch(mojo::Message* message) override {
for (auto& filter : filters_) {
if (!filter->WillDispatch(message))
return false;
}
return true;
}
void DidDispatchOrReject(mojo::Message* message, bool accepted) override {
for (auto& filter : filters_) {
filter->DidDispatchOrReject(message, accepted);
}
}
// Adds a filter to the end of the chain. See class description for ordering
// implications.
void Add(std::unique_ptr<mojo::MessageFilter> filter) {
filters_.push_back(std::move(filter));
}
private:
std::vector<std::unique_ptr<mojo::MessageFilter>> filters_;
};
// This class can be added as a MessageFilter to a mojo receiver to detect
// messages received while the the associated frame is in the Back-Forward
// Cache. Documents that are in the bfcache should not be sending mojo messages
// back to the browser.
class BackForwardCacheMessageFilter : public mojo::MessageFilter {
public:
explicit BackForwardCacheMessageFilter(
RenderFrameHostImpl* render_frame_host,
const char* interface_name,
BackForwardCacheImpl::MessageHandlingPolicyWhenCached policy)
: render_frame_host_(render_frame_host),
interface_name_(interface_name),
policy_(policy) {}
~BackForwardCacheMessageFilter() override = default;
private:
// mojo::MessageFilter overrides.
bool WillDispatch(mojo::Message* message) override {
if (!render_frame_host_->render_view_host())
return false;
if (render_frame_host_->render_view_host()
->GetPageLifecycleStateManager()
->RendererExpectedToSendChannelAssociatedIpcs() ||
ProcessHoldsNonCachedPages() ||
policy_ == BackForwardCacheImpl::kMessagePolicyNone) {
return true;
}
BackForwardCacheImpl::VlogUnexpectedRendererToBrowserMessage(
interface_name_, message->name(), render_frame_host_);
TRACE_EVENT2(
"content", "BackForwardCacheMessageFilter::WillDispatch bad_message",
"interface_name", interface_name_, "message_name", message->name());
base::UmaHistogramSparse(
"BackForwardCache.UnexpectedRendererToBrowserMessage.InterfaceName",
static_cast<int32_t>(base::HashMetricName(interface_name_)));
switch (policy_) {
case BackForwardCacheImpl::kMessagePolicyNone:
case BackForwardCacheImpl::kMessagePolicyLog:
return true;
case BackForwardCacheImpl::kMessagePolicyDump:
base::debug::DumpWithoutCrashing();
return true;
}
}
void DidDispatchOrReject(mojo::Message* message, bool accepted) override {}
// TODO(crbug.com/40147948): Remove once a well-behaved frozen
// RenderFrame never send IPCs messages, even if there are active pages in the
// process.
bool ProcessHoldsNonCachedPages() {
return RenderViewHostImpl::HasNonBackForwardCachedInstancesForProcess(
render_frame_host_->GetProcess());
}
const raw_ptr<RenderFrameHostImpl> render_frame_host_;
const char* const interface_name_;
const BackForwardCacheImpl::MessageHandlingPolicyWhenCached policy_;
};
} // namespace
std::unique_ptr<mojo::MessageFilter>
RenderFrameHostImpl::CreateMessageFilterForAssociatedReceiverInternal(
const char* interface_name,
BackForwardCacheImpl::MessageHandlingPolicyWhenCached policy) {
auto filter_chain = std::make_unique<MessageFilterChain>();
filter_chain->Add(std::make_unique<BackForwardCacheMessageFilter>(
this, interface_name, policy));
// BackForwardCacheMessageFilter might drop messages so add
// ActiveUrlMessageFilter at the end of the chain as we need to make sure that
// the debug url is reset, that is, DidDispatchOrReject() is called if
// WillDispatch().
filter_chain->Add(std::make_unique<internal::ActiveUrlMessageFilter>(this));
return filter_chain;
}
void RenderFrameHostImpl::SetUpMojoConnection() {
CHECK(!associated_registry_);
associated_registry_ = std::make_unique<blink::AssociatedInterfaceRegistry>();
auto bind_frame_host_receiver =
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<mojom::FrameHost> receiver) {
impl->frame_host_associated_receiver_.Bind(std::move(receiver));
impl->frame_host_associated_receiver_.SetFilter(
impl->CreateMessageFilterForAssociatedReceiver(
mojom::FrameHost::Name_));
};
associated_registry_->AddInterface<mojom::FrameHost>(
base::BindRepeating(bind_frame_host_receiver, base::Unretained(this)));
associated_registry_->AddInterface<
blink::mojom::BackForwardCacheControllerHost>(base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<
blink::mojom::BackForwardCacheControllerHost> receiver) {
impl->back_forward_cache_controller_host_associated_receiver_.Bind(
std::move(receiver));
impl->back_forward_cache_controller_host_associated_receiver_.SetFilter(
impl->CreateMessageFilterForAssociatedReceiverInternal(
blink::mojom::BackForwardCacheControllerHost::Name_,
BackForwardCacheImpl::kMessagePolicyNone));
},
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::LocalFrameHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<blink::mojom::LocalFrameHost>
receiver) {
impl->local_frame_host_receiver_.Bind(std::move(receiver));
impl->local_frame_host_receiver_.SetFilter(
impl->CreateMessageFilterForAssociatedReceiver(
blink::mojom::LocalFrameHost::Name_));
},
base::Unretained(this)));
if (base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
associated_registry_->AddInterface<
blink::mojom::SharedStorageDocumentService>(base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<
blink::mojom::SharedStorageDocumentService> receiver) {
if (SharedStorageDocumentServiceImpl::GetForCurrentDocument(impl)) {
// The renderer somehow requested two shared storage worklets
// associated with the same document. This could indicate a
// compromised renderer, so let's terminate it.
mojo::ReportBadMessage(
"Attempted to request two shared storage worklets associated "
"with the same document.");
return;
}
SharedStorageDocumentServiceImpl::GetOrCreateForCurrentDocument(impl)
->Bind(std::move(receiver));
},
base::Unretained(this)));
}
if (is_main_frame()) {
associated_registry_->AddInterface<blink::mojom::LocalMainFrameHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<blink::mojom::LocalMainFrameHost>
receiver) {
impl->local_main_frame_host_receiver_.Bind(std::move(receiver));
impl->local_main_frame_host_receiver_.SetFilter(
impl->CreateMessageFilterForAssociatedReceiver(
blink::mojom::LocalMainFrameHost::Name_));
},
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::ManifestUrlChangeObserver>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<
blink::mojom::ManifestUrlChangeObserver> receiver) {
ManifestManagerHost::GetOrCreateForPage(impl->GetPage())
->BindObserver(std::move(receiver));
},
base::Unretained(this)));
}
// TODO(crbug.com/40249262): Avoid binding the DomAutomationControllerHost
// interface outside of tests.
associated_registry_->AddInterface<mojom::DomAutomationControllerHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<mojom::DomAutomationControllerHost>
receiver) {
impl->BindDomOperationControllerHostReceiver(std::move(receiver));
},
base::Unretained(this)));
file_system_manager_.reset(new FileSystemManagerImpl(
GetProcess()->GetID(),
GetProcess()->GetStoragePartition()->GetFileSystemContext(),
ChromeBlobStorageContext::GetFor(GetProcess()->GetBrowserContext())));
#if BUILDFLAG(ENABLE_PPAPI)
associated_registry_->AddInterface<mojom::PepperHost>(base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<mojom::PepperHost> receiver) {
impl->GetPpapiSupport().Bind(std::move(receiver));
},
base::Unretained(this)));
#endif
associated_registry_->AddInterface<media::mojom::MediaPlayerHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<media::mojom::MediaPlayerHost>
receiver) {
impl->delegate()->CreateMediaPlayerHostForRenderFrameHost(
impl, std::move(receiver));
},
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::DisplayCutoutHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<blink::mojom::DisplayCutoutHost>
receiver) {
impl->delegate()->BindDisplayCutoutHost(impl, std::move(receiver));
},
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::AttributionHost>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<blink::mojom::AttributionHost>
receiver) {
AttributionHost::BindReceiver(std::move(receiver), impl);
},
base::Unretained(this)));
associated_registry_->AddInterface<device::mojom::ScreenOrientation>(
base::BindRepeating(
[](RenderFrameHostImpl* impl,
mojo::PendingAssociatedReceiver<device::mojom::ScreenOrientation>
receiver) {
impl->delegate()->BindScreenOrientation(impl, std::move(receiver));
},
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::BroadcastChannelProvider>(
base::BindRepeating(&RenderFrameHostImpl::CreateBroadcastChannelProvider,
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::BlobURLStore>(
base::BindRepeating(
&RenderFrameHostImpl::BindBlobUrlStoreAssociatedReceiver,
base::Unretained(this)));
associated_registry_->AddInterface<blink::mojom::FileBackedBlobFactory>(
base::BindRepeating(&RenderFrameHostImpl::BindFileBackedBlobFactory,
base::Unretained(this)));
// Allow embedders to register their binders.
GetContentClient()
->browser()
->RegisterAssociatedInterfaceBindersForRenderFrameHost(
*this, *associated_registry_);
mojo::PendingRemote<service_manager::mojom::InterfaceProvider>
remote_interfaces;
GetMojomFrameInRenderer()->GetInterfaceProvider(
remote_interfaces.InitWithNewPipeAndPassReceiver());
remote_interfaces_ = std::make_unique<service_manager::InterfaceProvider>(
base::SingleThreadTaskRunner::GetCurrentDefault());
remote_interfaces_->Bind(std::move(remote_interfaces));
}
void RenderFrameHostImpl::TearDownMojoConnection() {
// While not directly a Mojo endpoint, |geolocation_service_| may attempt to
// cancel permission requests.
geolocation_service_.reset();
associated_registry_.reset();
mojo_image_downloader_.reset();
find_in_page_.reset();
local_frame_.reset();
local_main_frame_.reset();
frame_host_associated_receiver_.reset();
associated_interface_provider_receiver_.reset();
back_forward_cache_controller_host_associated_receiver_.reset();
frame_.reset();
frame_bindings_control_.reset();
local_frame_host_receiver_.reset();
non_associated_local_frame_host_receiver_.reset();
local_main_frame_host_receiver_.reset();
broker_receiver_.reset();
render_accessibility_.reset();
render_accessibility_host_.Reset();
dom_automation_controller_receiver_.reset();
#if BUILDFLAG(ENABLE_PPAPI)
ppapi_support_.reset();
#endif
// Audio stream factories are tied to a live RenderFrame: see
// //content/browser/media/forwarding_audio_stream_factory.h.
// Eagerly reset now to ensure that it is impossible to create streams
// associated with a RenderFrameHost without a live RenderFrame;
// otherwise, the `RenderFrameDeleted()` signal used to clean up streams
// will never fire.
audio_service_audio_output_stream_factory_.reset();
audio_service_audio_input_stream_factory_.reset();
}
} // namespace content