-
Notifications
You must be signed in to change notification settings - Fork 893
/
brave_content_renderer_client.cc
296 lines (254 loc) · 11.7 KB
/
brave_content_renderer_client.cc
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
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/renderer/brave_content_renderer_client.h"
#include <utility>
#include "base/feature_list.h"
#include "base/ranges/algorithm.h"
#include "brave/components/ai_chat/core/common/features.h"
#include "brave/components/ai_chat/renderer/page_content_extractor.h"
#include "brave/components/ai_rewriter/common/buildflags/buildflags.h"
#include "brave/components/brave_search/common/brave_search_utils.h"
#include "brave/components/brave_search/renderer/brave_search_render_frame_observer.h"
#include "brave/components/brave_shields/core/common/features.h"
#include "brave/components/brave_vpn/common/buildflags/buildflags.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.h"
#include "brave/components/playlist/common/buildflags/buildflags.h"
#include "brave/components/safe_builtins/renderer/safe_builtins.h"
#include "brave/components/script_injector/renderer/script_injector_render_frame_observer.h"
#include "brave/components/skus/common/features.h"
#include "brave/components/skus/renderer/skus_render_frame_observer.h"
#include "brave/components/speedreader/common/buildflags/buildflags.h"
#include "brave/renderer/brave_render_frame_observer.h"
#include "brave/renderer/brave_render_thread_observer.h"
#include "brave/renderer/brave_wallet/brave_wallet_render_frame_observer.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "chrome/renderer/chrome_render_thread_observer.h"
#include "chrome/renderer/process_state.h"
#include "chrome/renderer/url_loader_throttle_provider_impl.h"
#include "components/feed/content/renderer/rss_link_reader.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/renderer/render_thread.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h"
#include "third_party/blink/public/web/web_script_controller.h"
#include "third_party/widevine/cdm/buildflags.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_AI_REWRITER)
#include "brave/components/ai_rewriter/common/features.h"
#include "brave/components/ai_rewriter/renderer/ai_rewriter_agent.h"
#endif
#if BUILDFLAG(ENABLE_SPEEDREADER)
#include "brave/components/speedreader/common/features.h"
#include "brave/components/speedreader/renderer/speedreader_render_frame_observer.h"
#endif
#if BUILDFLAG(ENABLE_BRAVE_VPN)
#include "brave/components/brave_vpn/common/brave_vpn_utils.h"
#if BUILDFLAG(IS_ANDROID)
#include "brave/components/brave_mobile_subscription/renderer/android/subscription_render_frame_observer.h"
#endif // BUILDFLAG(IS_ANDROID)
#endif // BUILDFLAG(ENABLE_BRAVE_VPN)
#if BUILDFLAG(ENABLE_PLAYLIST)
#include "brave/components/playlist/common/features.h"
#include "brave/components/playlist/renderer/playlist_render_frame_observer.h"
#endif
#if BUILDFLAG(ENABLE_WIDEVINE)
#include "media/base/key_system_info.h"
#include "third_party/widevine/cdm/widevine_cdm_common.h"
#endif
namespace {
void MaybeRemoveWidevineSupport(media::GetSupportedKeySystemsCB cb,
media::KeySystemInfos key_systems) {
#if BUILDFLAG(ENABLE_WIDEVINE)
auto dynamic_params = BraveRenderThreadObserver::GetDynamicParams();
if (!dynamic_params.widevine_enabled) {
key_systems.erase(
base::ranges::remove(
key_systems, kWidevineKeySystem,
[](const std::unique_ptr<media::KeySystemInfo>& key_system) {
return key_system->GetBaseKeySystemName();
}),
key_systems.cend());
}
#endif
cb.Run(std::move(key_systems));
}
} // namespace
BraveContentRendererClient::BraveContentRendererClient() = default;
void BraveContentRendererClient::
SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() {
ChromeContentRendererClient::
SetRuntimeFeaturesDefaultsBeforeBlinkInitialization();
blink::WebRuntimeFeatures::EnableFledge(false);
// Disable topics APIs because kBrowsingTopics feature is disabled
blink::WebRuntimeFeatures::EnableTopicsAPI(false);
blink::WebRuntimeFeatures::EnableTopicsDocumentAPI(false);
blink::WebRuntimeFeatures::EnableWebGPUExperimentalFeatures(false);
blink::WebRuntimeFeatures::EnableWebNFC(false);
// These features don't have dedicated WebRuntimeFeatures wrappers.
blink::WebRuntimeFeatures::EnableFeatureFromString("AdTagging", false);
blink::WebRuntimeFeatures::EnableFeatureFromString("DigitalGoods", false);
if (!base::FeatureList::IsEnabled(blink::features::kFileSystemAccessAPI)) {
blink::WebRuntimeFeatures::EnableFeatureFromString("FileSystemAccessLocal",
false);
blink::WebRuntimeFeatures::EnableFeatureFromString(
"FileSystemAccessAPIExperimental", false);
}
blink::WebRuntimeFeatures::EnableFeatureFromString("FledgeMultiBid", false);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
if (base::FeatureList::IsEnabled(
blink::features::kMiddleButtonClickAutoscroll)) {
blink::WebRuntimeFeatures::EnableFeatureFromString("MiddleClickAutoscroll",
true);
}
#endif // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
}
BraveContentRendererClient::~BraveContentRendererClient() = default;
void BraveContentRendererClient::RenderThreadStarted() {
ChromeContentRendererClient::RenderThreadStarted();
brave_observer_ = std::make_unique<BraveRenderThreadObserver>();
content::RenderThread::Get()->AddObserver(brave_observer_.get());
brave_search_service_worker_holder_.SetBrowserInterfaceBrokerProxy(
browser_interface_broker_.get());
blink::WebScriptController::RegisterExtension(
brave::SafeBuiltins::CreateV8Extension());
}
void BraveContentRendererClient::RenderFrameCreated(
content::RenderFrame* render_frame) {
ChromeContentRendererClient::RenderFrameCreated(render_frame);
auto* rfo = new BraveRenderFrameObserver(render_frame);
auto* registry = rfo->registry();
new feed::RssLinkReader(render_frame, registry);
if (base::FeatureList::IsEnabled(
brave_shields::features::kBraveAdblockCosmeticFiltering)) {
auto dynamic_params_closure = base::BindRepeating([]() {
auto dynamic_params = BraveRenderThreadObserver::GetDynamicParams();
return dynamic_params.de_amp_enabled;
});
new cosmetic_filters::CosmeticFiltersJsRenderFrameObserver(
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL, dynamic_params_closure);
}
if (base::FeatureList::IsEnabled(
brave_wallet::features::kNativeBraveWalletFeature)) {
new brave_wallet::BraveWalletRenderFrameObserver(
render_frame,
base::BindRepeating(&BraveRenderThreadObserver::GetDynamicParams));
}
new script_injector::ScriptInjectorRenderFrameObserver(render_frame);
if (brave_search::IsDefaultAPIEnabled()) {
new brave_search::BraveSearchRenderFrameObserver(
render_frame, content::ISOLATED_WORLD_ID_GLOBAL);
}
if (base::FeatureList::IsEnabled(skus::features::kSkusFeature) &&
!IsIncognitoProcess()) {
new skus::SkusRenderFrameObserver(render_frame);
}
#if BUILDFLAG(IS_ANDROID)
if (brave_vpn::IsBraveVPNFeatureEnabled() ||
ai_chat::features::IsAIChatHistoryEnabled()) {
new brave_subscription::SubscriptionRenderFrameObserver(
render_frame, content::ISOLATED_WORLD_ID_GLOBAL);
}
#endif
#if BUILDFLAG(ENABLE_SPEEDREADER)
if (base::FeatureList::IsEnabled(speedreader::kSpeedreaderFeature)) {
new speedreader::SpeedreaderRenderFrameObserver(
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL);
}
#endif
#if BUILDFLAG(ENABLE_PLAYLIST)
if (base::FeatureList::IsEnabled(playlist::features::kPlaylist) &&
!IsIncognitoProcess()) {
new playlist::PlaylistRenderFrameObserver(
render_frame, base::BindRepeating([] {
return BraveRenderThreadObserver::GetDynamicParams().playlist_enabled;
}),
ISOLATED_WORLD_ID_BRAVE_INTERNAL);
}
#endif
if (ai_chat::features::IsAIChatEnabled() && !IsIncognitoProcess()) {
new ai_chat::PageContentExtractor(render_frame, registry,
content::ISOLATED_WORLD_ID_GLOBAL,
ISOLATED_WORLD_ID_BRAVE_INTERNAL);
}
#if BUILDFLAG(ENABLE_AI_REWRITER)
if (ai_rewriter::features::IsAIRewriterEnabled()) {
new ai_rewriter::AIRewriterAgent(render_frame, registry);
}
#endif
}
std::unique_ptr<media::KeySystemSupportRegistration>
BraveContentRendererClient::GetSupportedKeySystems(
content::RenderFrame* render_frame,
media::GetSupportedKeySystemsCB cb) {
return ChromeContentRendererClient::GetSupportedKeySystems(
render_frame, base::BindRepeating(&MaybeRemoveWidevineSupport, cb));
}
void BraveContentRendererClient::RunScriptsAtDocumentStart(
content::RenderFrame* render_frame) {
auto* observer =
cosmetic_filters::CosmeticFiltersJsRenderFrameObserver::Get(render_frame);
// Run this before any extensions
if (observer) {
observer->RunScriptsAtDocumentStart();
}
#if BUILDFLAG(ENABLE_PLAYLIST)
if (base::FeatureList::IsEnabled(playlist::features::kPlaylist)) {
if (auto* playlist_observer =
playlist::PlaylistRenderFrameObserver::Get(render_frame)) {
playlist_observer->RunScriptsAtDocumentStart();
}
}
#endif
ChromeContentRendererClient::RunScriptsAtDocumentStart(render_frame);
}
void BraveContentRendererClient::RunScriptsAtDocumentEnd(
content::RenderFrame* render_frame) {
#if BUILDFLAG(ENABLE_PLAYLIST)
if (base::FeatureList::IsEnabled(playlist::features::kPlaylist)) {
if (auto* playlist_observer =
playlist::PlaylistRenderFrameObserver::Get(render_frame)) {
playlist_observer->RunScriptsAtDocumentEnd();
}
}
#endif
ChromeContentRendererClient::RunScriptsAtDocumentEnd(render_frame);
}
void BraveContentRendererClient::WillEvaluateServiceWorkerOnWorkerThread(
blink::WebServiceWorkerContextProxy* context_proxy,
v8::Local<v8::Context> v8_context,
int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url,
const blink::ServiceWorkerToken& service_worker_token) {
brave_search_service_worker_holder_.WillEvaluateServiceWorkerOnWorkerThread(
context_proxy, v8_context, service_worker_version_id,
service_worker_scope, script_url);
ChromeContentRendererClient::WillEvaluateServiceWorkerOnWorkerThread(
context_proxy, v8_context, service_worker_version_id,
service_worker_scope, script_url, service_worker_token);
}
void BraveContentRendererClient::WillDestroyServiceWorkerContextOnWorkerThread(
v8::Local<v8::Context> v8_context,
int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url) {
brave_search_service_worker_holder_
.WillDestroyServiceWorkerContextOnWorkerThread(
v8_context, service_worker_version_id, service_worker_scope,
script_url);
ChromeContentRendererClient::WillDestroyServiceWorkerContextOnWorkerThread(
v8_context, service_worker_version_id, service_worker_scope, script_url);
}
std::unique_ptr<blink::URLLoaderThrottleProvider>
BraveContentRendererClient::CreateURLLoaderThrottleProvider(
blink::URLLoaderThrottleProviderType provider_type) {
return URLLoaderThrottleProviderImpl::Create(provider_type, this,
browser_interface_broker_.get());
}
bool BraveContentRendererClient::IsOnionAllowed() const {
return brave_observer_->IsOnionAllowed();
}