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
|
// Copyright 2012 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/shell/renderer/shell_content_renderer_client.h"
#include <string>
#include "base/base_switches.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/immediate_crash.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/pass_key.h"
#include "components/cdm/renderer/external_clear_key_key_system_info.h"
#include "components/network_hints/renderer/web_prescient_networking_impl.h"
#include "components/web_cache/renderer/web_cache_impl.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/pseudonymization_util.h"
#include "content/public/common/web_identity.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/test/test_service.mojom.h"
#include "content/shell/common/main_frame_counter_test_impl.h"
#include "content/shell/common/power_monitor_test_impl.h"
#include "content/shell/common/shell_switches.h"
#include "content/shell/renderer/shell_render_frame_observer.h"
#include "mojo/public/cpp/bindings/binder_map.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "net/base/net_errors.h"
#include "ppapi/buildflags/buildflags.h"
#include "sandbox/policy/sandbox.h"
#include "third_party/blink/public/platform/url_loader_throttle_provider.h"
#include "third_party/blink/public/platform/web_url_error.h"
#include "third_party/blink/public/web/modules/credentialmanagement/throttle_helper.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_testing_support.h"
#include "third_party/blink/public/web/web_view.h"
#include "v8/include/v8-initialization.h"
#include "v8/include/v8.h"
#if BUILDFLAG(ENABLE_PLUGINS)
#include "ppapi/shared_impl/ppapi_switches.h" // nogncheck
#endif
#if BUILDFLAG(ENABLE_MOJO_CDM)
#include "base/feature_list.h"
#include "media/base/media_switches.h"
#endif
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && \
(defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
#define ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX
#include "base/debug/stack_trace.h"
#include "v8/include/v8-wasm-trap-handler-posix.h"
#endif
namespace content {
namespace {
// A test service which can be driven by browser tests for various reasons.
class TestRendererServiceImpl : public mojom::TestService {
public:
explicit TestRendererServiceImpl(
mojo::PendingReceiver<mojom::TestService> receiver)
: receiver_(this, std::move(receiver)) {
receiver_.set_disconnect_handler(base::BindOnce(
&TestRendererServiceImpl::OnConnectionError, base::Unretained(this)));
}
TestRendererServiceImpl(const TestRendererServiceImpl&) = delete;
TestRendererServiceImpl& operator=(const TestRendererServiceImpl&) = delete;
~TestRendererServiceImpl() override {}
private:
void OnConnectionError() { delete this; }
// mojom::TestService:
void DoSomething(DoSomethingCallback callback) override {
// Instead of responding normally, unbind the pipe, write some garbage,
// and go away.
const std::string kBadMessage = "This is definitely not a valid response!";
mojo::ScopedMessagePipeHandle pipe = receiver_.Unbind().PassPipe();
MojoResult rv = mojo::WriteMessageRaw(
pipe.get(), kBadMessage.data(), kBadMessage.size(), nullptr, 0,
MOJO_WRITE_MESSAGE_FLAG_NONE);
DCHECK_EQ(rv, MOJO_RESULT_OK);
// Deletes this.
OnConnectionError();
}
void DoTerminateProcess(DoTerminateProcessCallback callback) override {
NOTREACHED();
}
void DoCrashImmediately(DoCrashImmediatelyCallback callback) override {
// This intentionally crashes the process and needs to be fatal regardless
// of DCHECK level. This is unlike the other NOTREACHED()s which are not
// expected to get called at all.
base::ImmediateCrash();
}
void CreateFolder(CreateFolderCallback callback) override { NOTREACHED(); }
void GetRequestorName(GetRequestorNameCallback callback) override {
std::move(callback).Run("Not implemented.");
}
void CreateReadOnlySharedMemoryRegion(
const std::string& message,
CreateReadOnlySharedMemoryRegionCallback callback) override {
NOTREACHED();
}
void CreateWritableSharedMemoryRegion(
const std::string& message,
CreateWritableSharedMemoryRegionCallback callback) override {
NOTREACHED();
}
void CreateUnsafeSharedMemoryRegion(
const std::string& message,
CreateUnsafeSharedMemoryRegionCallback callback) override {
NOTREACHED();
}
void CloneSharedMemoryContents(
base::ReadOnlySharedMemoryRegion region,
CloneSharedMemoryContentsCallback callback) override {
NOTREACHED();
}
void IsProcessSandboxed(IsProcessSandboxedCallback callback) override {
std::move(callback).Run(sandbox::policy::Sandbox::IsProcessSandboxed());
}
void PseudonymizeString(const std::string& value,
PseudonymizeStringCallback callback) override {
std::move(callback).Run(
PseudonymizationUtil::PseudonymizeStringForTesting(value));
}
void PassWriteableFile(base::File file,
PassWriteableFileCallback callback) override {
std::move(callback).Run();
}
void WriteToPreloadedPipe() override { NOTREACHED(); }
mojo::Receiver<mojom::TestService> receiver_;
};
class ShellContentRendererUrlLoaderThrottleProvider
: public blink::URLLoaderThrottleProvider {
public:
ShellContentRendererUrlLoaderThrottleProvider()
: main_thread_task_runner_(
content::RenderThread::IsMainThread()
? base::SequencedTaskRunner::GetCurrentDefault()
: nullptr) {}
// This constructor works in conjunction with Clone().
ShellContentRendererUrlLoaderThrottleProvider(
const scoped_refptr<base::SequencedTaskRunner>& main_thread_task_runner,
base::PassKey<ShellContentRendererUrlLoaderThrottleProvider>)
: main_thread_task_runner_(std::move(main_thread_task_runner)) {}
std::unique_ptr<URLLoaderThrottleProvider> Clone() override {
return std::make_unique<ShellContentRendererUrlLoaderThrottleProvider>(
main_thread_task_runner_,
base::PassKey<ShellContentRendererUrlLoaderThrottleProvider>());
}
std::vector<std::unique_ptr<blink::URLLoaderThrottle>> CreateThrottles(
base::optional_ref<const blink::LocalFrameToken> local_frame_token,
const network::ResourceRequest& request) override {
std::vector<std::unique_ptr<blink::URLLoaderThrottle>> throttles;
if (local_frame_token.has_value()) {
auto throttle =
content::MaybeCreateIdentityUrlLoaderThrottle(base::BindRepeating(
[](const blink::LocalFrameToken& token,
const scoped_refptr<base::SequencedTaskRunner>
main_thread_task_runner,
const url::Origin& origin,
blink::mojom::IdpSigninStatus status) {
if (content::RenderThread::IsMainThread()) {
blink::SetIdpSigninStatus(token, origin, status);
return;
}
if (main_thread_task_runner) {
main_thread_task_runner->PostTask(
FROM_HERE, base::BindOnce(&blink::SetIdpSigninStatus,
token, origin, status));
}
},
local_frame_token.value(), main_thread_task_runner_));
if (throttle)
throttles.push_back(std::move(throttle));
}
return throttles;
}
void SetOnline(bool is_online) override {}
private:
// Set only when `this` was created on the main thread, or cloned from a
// provider which was created on the main thread.
scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner_;
};
void CreateRendererTestService(
mojo::PendingReceiver<mojom::TestService> receiver) {
// Owns itself.
new TestRendererServiceImpl(std::move(receiver));
}
} // namespace
ShellContentRendererClient::ShellContentRendererClient() {}
ShellContentRendererClient::~ShellContentRendererClient() {
}
void ShellContentRendererClient::SetUpWebAssemblyTrapHandler() {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
// Mac and Windows use the default implementation (where the default v8 trap
// handler gets set up).
ContentRendererClient::SetUpWebAssemblyTrapHandler();
return;
#else
base::CommandLine* const command_line =
base::CommandLine::ForCurrentProcess();
const bool crash_reporter_enabled =
command_line->HasSwitch(switches::kEnableCrashReporter)
#if BUILDFLAG(IS_POSIX)
|| command_line->HasSwitch(switches::kEnableCrashReporterForTesting)
#endif // BUILDFLAG(IS_POSIX)
;
if (crash_reporter_enabled) {
// If either --enable-crash-reporter or --enable-crash-reporter-for-testing
// is enabled it should take care of signal handling for us, use the default
// implementation which doesn't register an additional handler.
ContentRendererClient::SetUpWebAssemblyTrapHandler();
return;
}
const bool use_v8_default_handler =
#if defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableInProcessStackTraces)
#else
true
#endif // defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
;
if (use_v8_default_handler) {
// There is no signal handler yet, but it's okay if v8 registers one.
v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/true);
return;
}
#if defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
if (base::debug::SetStackDumpFirstChanceCallback(
v8::TryHandleWebAssemblyTrapPosix)) {
// Crashpad and Breakpad are disabled, but the in-process stack dump
// handlers are enabled, so set the callback on the stack dump handlers.
v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/false);
return;
}
// As the registration of the callback failed, we don't enable trap
// handlers.
#endif // defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
}
void ShellContentRendererClient::RenderThreadStarted() {
web_cache_impl_ = std::make_unique<web_cache::WebCacheImpl>();
}
void ShellContentRendererClient::ExposeInterfacesToBrowser(
mojo::BinderMap* binders) {
binders->Add<mojom::TestService>(
base::BindRepeating(&CreateRendererTestService),
base::SingleThreadTaskRunner::GetCurrentDefault());
binders->Add<mojom::PowerMonitorTest>(
base::BindRepeating(&PowerMonitorTestImpl::MakeSelfOwnedReceiver),
base::SingleThreadTaskRunner::GetCurrentDefault());
binders->Add<mojom::MainFrameCounterTest>(
base::BindRepeating(&MainFrameCounterTestImpl::Bind),
base::SingleThreadTaskRunner::GetCurrentDefault());
binders->Add<web_cache::mojom::WebCache>(
base::BindRepeating(&web_cache::WebCacheImpl::BindReceiver,
base::Unretained(web_cache_impl_.get())),
base::SingleThreadTaskRunner::GetCurrentDefault());
}
void ShellContentRendererClient::RenderFrameCreated(RenderFrame* render_frame) {
// TODO(danakj): The ShellRenderFrameObserver is doing stuff only for
// browser tests. If we only create that for browser tests then the override
// of this method in WebTestContentRendererClient would not be needed.
new ShellRenderFrameObserver(render_frame);
}
void ShellContentRendererClient::PrepareErrorPage(
RenderFrame* render_frame,
const blink::WebURLError& error,
const std::string& http_method,
content::mojom::AlternativeErrorPageOverrideInfoPtr
alternative_error_page_info,
std::string* error_html) {
if (error_html && error_html->empty()) {
*error_html =
"<head><title>Error</title></head><body>Could not load the requested "
"resource.<br/>Error code: " +
base::NumberToString(error.reason()) +
(error.reason() < 0 ? " (" + net::ErrorToString(error.reason()) + ")"
: "") +
"</body>";
}
}
void ShellContentRendererClient::PrepareErrorPageForHttpStatusError(
content::RenderFrame* render_frame,
const blink::WebURLError& error,
const std::string& http_method,
int http_status,
content::mojom::AlternativeErrorPageOverrideInfoPtr
alternative_error_page_info,
std::string* error_html) {
if (error_html) {
*error_html =
"<head><title>Error</title></head><body>Server returned HTTP status " +
base::NumberToString(http_status) + "</body>";
}
}
void ShellContentRendererClient::DidInitializeWorkerContextOnWorkerThread(
v8::Local<v8::Context> context) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kExposeInternalsForTesting)) {
blink::WebTestingSupport::InjectInternalsObject(context);
}
}
std::unique_ptr<blink::URLLoaderThrottleProvider>
ShellContentRendererClient::CreateURLLoaderThrottleProvider(
blink::URLLoaderThrottleProviderType provider_type) {
return std::make_unique<ShellContentRendererUrlLoaderThrottleProvider>();
}
#if BUILDFLAG(ENABLE_MOJO_CDM)
std::unique_ptr<media::KeySystemSupportRegistration>
ShellContentRendererClient::GetSupportedKeySystems(
content::RenderFrame* render_frame,
media::GetSupportedKeySystemsCB cb) {
media::KeySystemInfos key_systems;
if (base::FeatureList::IsEnabled(media::kExternalClearKeyForTesting))
key_systems.push_back(
std::make_unique<cdm::ExternalClearKeyKeySystemInfo>());
std::move(cb).Run(std::move(key_systems));
return nullptr;
}
#endif
std::unique_ptr<blink::WebPrescientNetworking>
ShellContentRendererClient::CreatePrescientNetworking(
RenderFrame* render_frame) {
return std::make_unique<network_hints::WebPrescientNetworkingImpl>(
render_frame);
}
} // namespace content
|