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/strings/string16.h"
11 #include "base/win/scoped_comptr.h"
12 #include "base/win/win_util.h"
13 #include "base/win/windows_version.h"
14 #include "content/common/sandbox_win.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/injection_test_win.h"
17 #include "content/public/renderer/render_font_warmup_win.h"
18 #include "content/public/renderer/render_thread.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "sandbox/win/src/sandbox.h"
21 #include "skia/ext/vector_platform_device_emf_win.h"
22 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
23 #include "third_party/WebKit/public/web/win/WebFontRendering.h"
24 #include "third_party/icu/source/i18n/unicode/timezone.h"
25 #include "third_party/skia/include/ports/SkFontMgr.h"
26 #include "third_party/skia/include/ports/SkTypeface_win.h"
28 #ifdef ENABLE_VTUNE_JIT_INTERFACE
29 #include "v8/src/third_party/vtune/v8-vtune.h"
37 // Windows-only skia sandbox support
38 // These are used for GDI-path rendering.
39 void SkiaPreCacheFont(const LOGFONT& logfont) {
40 RenderThread* render_thread = RenderThread::Get();
42 render_thread->PreCacheFont(logfont);
46 void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
48 unsigned int text_length) {
49 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
50 if (render_thread_impl) {
51 render_thread_impl->PreCacheFontCharacters(
53 base::string16(text, text_length));
57 void WarmupDirectWrite() {
58 // The objects used here are intentionally not freed as we want the Skia
59 // code to use these objects after warmup.
60 SkTypeface* typeface =
61 GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0);
62 DoPreSandboxWarmupForTypeface(typeface);
67 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
68 const MainFunctionParams& parameters)
69 : parameters_(parameters),
70 sandbox_test_module_(NULL) {
73 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
76 void RendererMainPlatformDelegate::PlatformInitialize() {
77 const CommandLine& command_line = parameters_.command_line;
79 #ifdef ENABLE_VTUNE_JIT_INTERFACE
80 if (command_line.HasSwitch(switches::kEnableVtune))
81 vTune::InitializeVtuneForV8();
84 // Be mindful of what resources you acquire here. They can be used by
85 // malicious code if the renderer gets compromised.
86 bool no_sandbox = command_line.HasSwitch(switches::kNoSandbox);
88 bool use_direct_write = ShouldUseDirectWrite();
90 // ICU DateFormat class (used in base/time_format.cc) needs to get the
91 // Olson timezone ID by accessing the registry keys under
92 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
93 // After TimeZone::createDefault is called once here, the timezone ID is
94 // cached and there's no more need to access the registry. If the sandbox
95 // is disabled, we don't have to make this dummy call.
96 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
98 if (use_direct_write) {
101 SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
102 skia::SetSkiaEnsureTypefaceCharactersAccessible(
103 SkiaPreCacheFontCharacters);
106 blink::WebFontRendering::setUseDirectWrite(use_direct_write);
107 if (use_direct_write) {
108 blink::WebRuntimeFeatures::enableSubpixelFontScaling(true);
112 void RendererMainPlatformDelegate::PlatformUninitialize() {
115 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
116 const CommandLine& command_line = parameters_.command_line;
118 DVLOG(1) << "Started renderer with " << command_line.GetCommandLineString();
120 sandbox::TargetServices* target_services =
121 parameters_.sandbox_info->target_services;
123 if (target_services && !no_sandbox) {
124 std::wstring test_dll_name =
125 command_line.GetSwitchValueNative(switches::kTestSandbox);
126 if (!test_dll_name.empty()) {
127 sandbox_test_module_ = LoadLibrary(test_dll_name.c_str());
128 DCHECK(sandbox_test_module_);
129 if (!sandbox_test_module_) {
137 bool RendererMainPlatformDelegate::EnableSandbox() {
138 sandbox::TargetServices* target_services =
139 parameters_.sandbox_info->target_services;
141 if (target_services) {
142 // Cause advapi32 to load before the sandbox is turned on.
143 unsigned int dummy_rand;
145 // Warm up language subsystems before the sandbox is turned on.
146 ::GetUserDefaultLangID();
147 ::GetUserDefaultLCID();
149 target_services->LowerToken();
155 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) {
156 if (sandbox_test_module_) {
157 RunRendererTests run_security_tests =
158 reinterpret_cast<RunRendererTests>(GetProcAddress(sandbox_test_module_,
160 DCHECK(run_security_tests);
161 if (run_security_tests) {
163 DVLOG(1) << "Running renderer security tests";
164 BOOL result = run_security_tests(&test_count);
165 CHECK(result) << "Test number " << test_count << " has failed.";
170 } // namespace content