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
android_webview / browser / aw_content_browser_client_receiver_bindings.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/aw_browser_context.h"
#include "android_webview/browser/aw_content_browser_client.h"
#include "android_webview/browser/aw_print_manager.h"
#include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
#include "android_webview/browser/safe_browsing/aw_url_checker_delegate_impl.h"
#include "android_webview/common/aw_features.h"
#include "android_webview/common/mojom/render_message_filter.mojom.h"
#include "base/feature_list.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/cdm/browser/media_drm_storage_impl.h"
#include "components/content_capture/browser/onscreen_content_provider.h"
#include "components/network_hints/browser/simple_network_hints_handler_impl.h"
#include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/mojo_safe_browsing_impl.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "media/mojo/buildflags.h"
#include "mojo/public/cpp/bindings/binder_map.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/mojom/webview/webview_media_integrity.mojom.h"
#if BUILDFLAG(ENABLE_SPELLCHECK)
#include "components/spellcheck/browser/spell_check_host_impl.h"
#endif
namespace android_webview {
namespace {
#if BUILDFLAG(ENABLE_MOJO_CDM)
void CreateOriginId(cdm::MediaDrmStorageImpl::OriginIdObtainedCB callback) {
std::move(callback).Run(true, base::UnguessableToken::Create());
}
void AllowEmptyOriginIdCB(base::OnceCallback<void(bool)> callback) {
// Since CreateOriginId() always returns a non-empty origin ID, we don't need
// to allow empty origin ID.
std::move(callback).Run(false);
}
void CreateMediaDrmStorage(
content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<::media::mojom::MediaDrmStorage> receiver) {
CHECK(render_frame_host);
if (render_frame_host->GetLastCommittedOrigin().opaque()) {
DVLOG(1) << __func__ << ": Unique origin.";
return;
}
auto* aw_browser_context =
static_cast<AwBrowserContext*>(render_frame_host->GetBrowserContext());
DCHECK(aw_browser_context) << "AwBrowserContext not available.";
PrefService* pref_service = aw_browser_context->GetPrefService();
DCHECK(pref_service);
// The object will be deleted on connection error, or when the frame navigates
// away.
new cdm::MediaDrmStorageImpl(
*render_frame_host, pref_service, base::BindRepeating(&CreateOriginId),
base::BindRepeating(&AllowEmptyOriginIdCB), std::move(receiver));
}
#endif // BUILDFLAG(ENABLE_MOJO_CDM)
// Helper method that checks the RenderProcessHost is still alive before hopping
// over to the IO thread.
void MaybeCreateSafeBrowsing(
int rph_id,
base::RepeatingCallback<scoped_refptr<safe_browsing::UrlCheckerDelegate>()>
get_checker_delegate,
mojo::PendingReceiver<safe_browsing::mojom::SafeBrowsing> receiver) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::RenderProcessHost* render_process_host =
content::RenderProcessHost::FromID(rph_id);
if (!render_process_host)
return;
safe_browsing::MojoSafeBrowsingImpl::MaybeCreate(
rph_id, std::move(get_checker_delegate), std::move(receiver));
}
void BindNetworkHintsHandler(
content::RenderFrameHost* frame_host,
mojo::PendingReceiver<network_hints::mojom::NetworkHintsHandler> receiver) {
network_hints::SimpleNetworkHintsHandlerImpl::Create(frame_host,
std::move(receiver));
}
// This class handles android_webview.mojom.RenderMessageFilter Mojo interface's
// methods on IO thread.
class AwContentsMessageFilter : public mojom::RenderMessageFilter {
public:
explicit AwContentsMessageFilter(int process_id);
AwContentsMessageFilter(const AwContentsMessageFilter&) = delete;
AwContentsMessageFilter& operator=(const AwContentsMessageFilter&) = delete;
// mojom::RenderMessageFilter overrides:
void SubFrameCreated(
const blink::LocalFrameToken& parent_frame_token,
const blink::LocalFrameToken& child_frame_token) override;
~AwContentsMessageFilter() override;
private:
const int process_id_;
};
AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
: process_id_(process_id) {}
AwContentsMessageFilter::~AwContentsMessageFilter() = default;
void AwContentsMessageFilter::SubFrameCreated(
const blink::LocalFrameToken& parent_frame_token,
const blink::LocalFrameToken& child_frame_token) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
AwContentsIoThreadClient::SubFrameCreated(process_id_, parent_frame_token,
child_frame_token);
}
void CreateRenderMessageFilter(
int rph_id,
mojo::PendingReceiver<mojom::RenderMessageFilter> receiver) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
mojo::MakeSelfOwnedReceiver(std::make_unique<AwContentsMessageFilter>(rph_id),
std::move(receiver));
}
template <typename Interface>
void ForwardToJavaFrame(content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<Interface> receiver) {
render_frame_host->GetJavaInterfaces()->GetInterface(std::move(receiver));
}
void BindMediaIntegrityServiceReceiver(
content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<blink::mojom::WebViewMediaIntegrityService>
receiver) {
const url::Origin& origin = render_frame_host->GetLastCommittedOrigin();
// Note that this particular check respects the origin of the base URL
// supplied by loadDataWithBaseURL.
if ((origin.scheme() != url::kHttpScheme &&
origin.scheme() != url::kHttpsScheme) ||
!network::IsOriginPotentiallyTrustworthy(origin)) {
mojo::ReportBadMessage(
"Attempted to access WebView Media Integrity service for a "
"non-trustworthy or non-HTTP/HTTPS origin.");
return;
};
ForwardToJavaFrame<blink::mojom::WebViewMediaIntegrityService>(
render_frame_host, std::move(receiver));
}
} // anonymous namespace
void AwContentBrowserClient::BindMediaServiceReceiver(
content::RenderFrameHost* render_frame_host,
mojo::GenericPendingReceiver receiver) {
#if BUILDFLAG(ENABLE_MOJO_CDM)
if (auto r = receiver.As<media::mojom::MediaDrmStorage>()) {
CreateMediaDrmStorage(render_frame_host, std::move(r));
return;
}
#endif
}
void AwContentBrowserClient::
RegisterAssociatedInterfaceBindersForRenderFrameHost(
content::RenderFrameHost& render_frame_host,
blink::AssociatedInterfaceRegistry& associated_registry) {
associated_registry.AddInterface<autofill::mojom::AutofillDriver>(
base::BindRepeating(
&autofill::ContentAutofillDriverFactory::BindAutofillDriver,
&render_frame_host));
// TODO(lingqi): Swap the parameters so that lambda functions are not needed.
associated_registry.AddInterface<
content_capture::mojom::ContentCaptureReceiver>(base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::PendingAssociatedReceiver<
content_capture::mojom::ContentCaptureReceiver> receiver) {
content_capture::OnscreenContentProvider::BindContentCaptureReceiver(
std::move(receiver), render_frame_host);
},
&render_frame_host));
associated_registry.AddInterface<mojom::FrameHost>(base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::PendingAssociatedReceiver<mojom::FrameHost> receiver) {
AwRenderViewHostExt::BindFrameHost(std::move(receiver),
render_frame_host);
},
&render_frame_host));
associated_registry.AddInterface<page_load_metrics::mojom::PageLoadMetrics>(
base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::PendingAssociatedReceiver<
page_load_metrics::mojom::PageLoadMetrics> receiver) {
page_load_metrics::MetricsWebContentsObserver::BindPageLoadMetrics(
std::move(receiver), render_frame_host);
},
&render_frame_host));
associated_registry.AddInterface<printing::mojom::PrintManagerHost>(
base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::PendingAssociatedReceiver<printing::mojom::PrintManagerHost>
receiver) {
AwPrintManager::BindPrintManagerHost(std::move(receiver),
render_frame_host);
},
&render_frame_host));
associated_registry.AddInterface<
security_interstitials::mojom::InterstitialCommands>(base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::PendingAssociatedReceiver<
security_interstitials::mojom::InterstitialCommands> receiver) {
security_interstitials::SecurityInterstitialTabHelper::
BindInterstitialCommands(std::move(receiver), render_frame_host);
},
&render_frame_host));
}
void AwContentBrowserClient::ExposeInterfacesToRenderer(
service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
content::RenderProcessHost* render_process_host) {
registry->AddInterface<safe_browsing::mojom::SafeBrowsing>(
base::BindRepeating(
&MaybeCreateSafeBrowsing, render_process_host->GetDeprecatedID(),
base::BindRepeating(
&AwContentBrowserClient::GetSafeBrowsingUrlCheckerDelegate,
base::Unretained(this))),
content::GetUIThreadTaskRunner({}));
// Add the RenderMessageFilter creation callback, the callbkack will happen on
// the IO thread.
registry->AddInterface<mojom::RenderMessageFilter>(base::BindRepeating(
&CreateRenderMessageFilter, render_process_host->GetDeprecatedID()));
}
void AwContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
map->Add<network_hints::mojom::NetworkHintsHandler>(
base::BindRepeating(&BindNetworkHintsHandler));
#if BUILDFLAG(ENABLE_SPELLCHECK)
auto create_spellcheck_host =
[](content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<spellcheck::mojom::SpellCheckHost> receiver) {
mojo::MakeSelfOwnedReceiver(std::make_unique<SpellCheckHostImpl>(),
std::move(receiver));
};
map->Add<spellcheck::mojom::SpellCheckHost>(
base::BindRepeating(create_spellcheck_host),
content::GetUIThreadTaskRunner({}));
#endif
if (base::FeatureList::IsEnabled(
features::kWebViewMediaIntegrityApiBlinkExtension)) {
map->Add<blink::mojom::WebViewMediaIntegrityService>(
base::BindRepeating(&BindMediaIntegrityServiceReceiver));
}
}
void AwContentBrowserClient::
RegisterMojoBinderPoliciesForSameOriginPrerendering(
content::MojoBinderPolicyMap& policy_map) {
policy_map.SetAssociatedPolicy<page_load_metrics::mojom::PageLoadMetrics>(
content::MojoBinderAssociatedPolicy::kGrant);
}
} // namespace android_webview