[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / renderer / renderer_main_platform_delegate_win.cc
blob959bee576d3a503332d1e869fa7e5cfc5be679d1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/renderer_main_platform_delegate.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string16.h"
11 #include "base/win/win_util.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/common/injection_test_win.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "content/renderer/render_thread_impl.h"
16 #include "sandbox/win/src/sandbox.h"
17 #include "skia/ext/skia_sandbox_support_win.h"
18 #include "skia/ext/vector_platform_device_emf_win.h"
19 #include "unicode/timezone.h"
21 namespace content {
22 namespace {
24 // Windows-only skia sandbox support
25 void SkiaPreCacheFont(const LOGFONT& logfont) {
26 RenderThread* render_thread = RenderThread::Get();
27 if (render_thread) {
28 render_thread->PreCacheFont(logfont);
32 void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
33 const wchar_t* text,
34 unsigned int text_length) {
35 content::RenderThreadImpl* render_thread_impl =
36 content::RenderThreadImpl::current();
37 if (render_thread_impl) {
38 render_thread_impl->PreCacheFontCharacters(logfont,
39 string16(text, text_length));
43 void InitExitInterceptions() {
44 // If code subsequently tries to exit using exit(), _exit(), abort(), or
45 // ExitProcess(), force a crash (since otherwise these would be silent
46 // terminations and fly under the radar).
47 base::win::SetShouldCrashOnProcessDetach(true);
48 base::win::SetAbortBehaviorForCrashReporting();
51 } // namespace
53 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
54 const MainFunctionParams& parameters)
55 : parameters_(parameters),
56 sandbox_test_module_(NULL) {
59 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
62 void RendererMainPlatformDelegate::PlatformInitialize() {
63 InitExitInterceptions();
65 // Be mindful of what resources you acquire here. They can be used by
66 // malicious code if the renderer gets compromised.
67 const CommandLine& command_line = parameters_.command_line;
68 bool no_sandbox = command_line.HasSwitch(switches::kNoSandbox);
70 if (!no_sandbox) {
71 // ICU DateFormat class (used in base/time_format.cc) needs to get the
72 // Olson timezone ID by accessing the registry keys under
73 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
74 // After TimeZone::createDefault is called once here, the timezone ID is
75 // cached and there's no more need to access the registry. If the sandbox
76 // is disabled, we don't have to make this dummy call.
77 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
78 SetSkiaEnsureTypefaceAccessible(SkiaPreCacheFont);
79 skia::SetSkiaEnsureTypefaceCharactersAccessible(
80 SkiaPreCacheFontCharacters);
84 void RendererMainPlatformDelegate::PlatformUninitialize() {
85 // At this point we are shutting down in a normal code path, so undo our
86 // hack to crash on exit.
87 base::win::SetShouldCrashOnProcessDetach(false);
90 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
91 const CommandLine& command_line = parameters_.command_line;
93 DVLOG(1) << "Started renderer with " << command_line.GetCommandLineString();
95 sandbox::TargetServices* target_services =
96 parameters_.sandbox_info->target_services;
98 if (target_services && !no_sandbox) {
99 std::wstring test_dll_name =
100 command_line.GetSwitchValueNative(switches::kTestSandbox);
101 if (!test_dll_name.empty()) {
102 sandbox_test_module_ = LoadLibrary(test_dll_name.c_str());
103 DCHECK(sandbox_test_module_);
104 if (!sandbox_test_module_) {
105 return false;
109 return true;
112 bool RendererMainPlatformDelegate::EnableSandbox() {
113 sandbox::TargetServices* target_services =
114 parameters_.sandbox_info->target_services;
116 if (target_services) {
117 // Cause advapi32 to load before the sandbox is turned on.
118 unsigned int dummy_rand;
119 rand_s(&dummy_rand);
120 // Warm up language subsystems before the sandbox is turned on.
121 ::GetUserDefaultLangID();
122 ::GetUserDefaultLCID();
124 target_services->LowerToken();
125 return true;
127 return false;
130 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) {
131 if (sandbox_test_module_) {
132 RunRendererTests run_security_tests =
133 reinterpret_cast<RunRendererTests>(GetProcAddress(sandbox_test_module_,
134 kRenderTestCall));
135 DCHECK(run_security_tests);
136 if (run_security_tests) {
137 int test_count = 0;
138 DVLOG(1) << "Running renderer security tests";
139 BOOL result = run_security_tests(&test_count);
140 CHECK(result) << "Test number " << test_count << " has failed.";
145 } // namespace content