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/win/WebFontRendering.h"
23 #include "third_party/icu/source/i18n/unicode/timezone.h"
24 #include "third_party/skia/include/ports/SkFontMgr.h"
25 #include "third_party/skia/include/ports/SkTypeface_win.h"
27 #ifdef ENABLE_VTUNE_JIT_INTERFACE
28 #include "v8/src/third_party/vtune/v8-vtune.h"
36 // Windows-only skia sandbox support
37 // These are used for GDI-path rendering.
38 void SkiaPreCacheFont(const LOGFONT
& logfont
) {
39 RenderThread
* render_thread
= RenderThread::Get();
41 render_thread
->PreCacheFont(logfont
);
45 void SkiaPreCacheFontCharacters(const LOGFONT
& logfont
,
47 unsigned int text_length
) {
48 RenderThreadImpl
* render_thread_impl
= RenderThreadImpl::current();
49 if (render_thread_impl
) {
50 render_thread_impl
->PreCacheFontCharacters(
52 base::string16(text
, text_length
));
56 void WarmupDirectWrite() {
57 // The objects used here are intentionally not freed as we want the Skia
58 // code to use these objects after warmup.
59 SkTypeface
* typeface
=
60 GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0);
61 DoPreSandboxWarmupForTypeface(typeface
);
66 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
67 const MainFunctionParams
& parameters
)
68 : parameters_(parameters
),
69 sandbox_test_module_(NULL
) {
72 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
75 void RendererMainPlatformDelegate::PlatformInitialize() {
76 const CommandLine
& command_line
= parameters_
.command_line
;
78 #ifdef ENABLE_VTUNE_JIT_INTERFACE
79 if (command_line
.HasSwitch(switches::kEnableVtune
))
80 vTune::InitializeVtuneForV8();
83 // Be mindful of what resources you acquire here. They can be used by
84 // malicious code if the renderer gets compromised.
85 bool no_sandbox
= command_line
.HasSwitch(switches::kNoSandbox
);
87 bool use_direct_write
= ShouldUseDirectWrite();
89 // ICU DateFormat class (used in base/time_format.cc) needs to get the
90 // Olson timezone ID by accessing the registry keys under
91 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
92 // After TimeZone::createDefault is called once here, the timezone ID is
93 // cached and there's no more need to access the registry. If the sandbox
94 // is disabled, we don't have to make this dummy call.
95 scoped_ptr
<icu::TimeZone
> zone(icu::TimeZone::createDefault());
97 if (use_direct_write
) {
100 SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont
);
101 skia::SetSkiaEnsureTypefaceCharactersAccessible(
102 SkiaPreCacheFontCharacters
);
105 blink::WebFontRendering::setUseDirectWrite(use_direct_write
);
106 blink::WebFontRendering::setUseSubpixelPositioning(use_direct_write
);
109 void RendererMainPlatformDelegate::PlatformUninitialize() {
112 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox
) {
113 const CommandLine
& command_line
= parameters_
.command_line
;
115 DVLOG(1) << "Started renderer with " << command_line
.GetCommandLineString();
117 sandbox::TargetServices
* target_services
=
118 parameters_
.sandbox_info
->target_services
;
120 if (target_services
&& !no_sandbox
) {
121 std::wstring test_dll_name
=
122 command_line
.GetSwitchValueNative(switches::kTestSandbox
);
123 if (!test_dll_name
.empty()) {
124 sandbox_test_module_
= LoadLibrary(test_dll_name
.c_str());
125 DCHECK(sandbox_test_module_
);
126 if (!sandbox_test_module_
) {
134 bool RendererMainPlatformDelegate::EnableSandbox() {
135 sandbox::TargetServices
* target_services
=
136 parameters_
.sandbox_info
->target_services
;
138 if (target_services
) {
139 // Cause advapi32 to load before the sandbox is turned on.
140 unsigned int dummy_rand
;
142 // Warm up language subsystems before the sandbox is turned on.
143 ::GetUserDefaultLangID();
144 ::GetUserDefaultLCID();
146 target_services
->LowerToken();
152 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox
) {
153 if (sandbox_test_module_
) {
154 RunRendererTests run_security_tests
=
155 reinterpret_cast<RunRendererTests
>(GetProcAddress(sandbox_test_module_
,
157 DCHECK(run_security_tests
);
158 if (run_security_tests
) {
160 DVLOG(1) << "Running renderer security tests";
161 BOOL result
= run_security_tests(&test_count
);
162 CHECK(result
) << "Test number " << test_count
<< " has failed.";
167 } // namespace content