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/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/vector_platform_device_emf_win.h"
18 #include "third_party/icu/source/i18n/unicode/timezone.h"
19 #include "third_party/skia/include/ports/SkTypeface_win.h"
21 #ifdef ENABLE_VTUNE_JIT_INTERFACE
22 #include "v8/src/third_party/vtune/v8-vtune.h"
28 // Windows-only skia sandbox support
29 void SkiaPreCacheFont(const LOGFONT& logfont) {
30 RenderThread* render_thread = RenderThread::Get();
32 render_thread->PreCacheFont(logfont);
36 void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
38 unsigned int text_length) {
39 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
40 if (render_thread_impl) {
41 render_thread_impl->PreCacheFontCharacters(logfont,
42 string16(text, text_length));
47 LRESULT CALLBACK WindowsHookCBT(int code, WPARAM w_param, LPARAM l_param) {
48 CHECK_NE(code, HCBT_CREATEWND)
49 << "Should not be creating windows in the renderer!";
50 return CallNextHookEx(NULL, code, w_param, l_param);
56 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
57 const MainFunctionParams& parameters)
58 : parameters_(parameters),
59 sandbox_test_module_(NULL) {
62 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
65 void RendererMainPlatformDelegate::PlatformInitialize() {
67 // Install a check that we're not creating windows in the renderer. See
68 // http://crbug.com/230122 for background. TODO(scottmg): Ideally this would
69 // check all threads in the renderer, but it currently only checks the main
72 SetWindowsHookEx(WH_CBT, WindowsHookCBT, NULL, ::GetCurrentThreadId()));
75 const CommandLine& command_line = parameters_.command_line;
77 #ifdef ENABLE_VTUNE_JIT_INTERFACE
78 if (command_line.HasSwitch(switches::kEnableVtune))
79 vTune::InitializeVtuneForV8();
82 // Be mindful of what resources you acquire here. They can be used by
83 // malicious code if the renderer gets compromised.
84 bool no_sandbox = command_line.HasSwitch(switches::kNoSandbox);
87 // ICU DateFormat class (used in base/time_format.cc) needs to get the
88 // Olson timezone ID by accessing the registry keys under
89 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
90 // After TimeZone::createDefault is called once here, the timezone ID is
91 // cached and there's no more need to access the registry. If the sandbox
92 // is disabled, we don't have to make this dummy call.
93 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
94 SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
95 skia::SetSkiaEnsureTypefaceCharactersAccessible(
96 SkiaPreCacheFontCharacters);
100 void RendererMainPlatformDelegate::PlatformUninitialize() {
103 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
104 const CommandLine& command_line = parameters_.command_line;
106 DVLOG(1) << "Started renderer with " << command_line.GetCommandLineString();
108 sandbox::TargetServices* target_services =
109 parameters_.sandbox_info->target_services;
111 if (target_services && !no_sandbox) {
112 std::wstring test_dll_name =
113 command_line.GetSwitchValueNative(switches::kTestSandbox);
114 if (!test_dll_name.empty()) {
115 sandbox_test_module_ = LoadLibrary(test_dll_name.c_str());
116 DCHECK(sandbox_test_module_);
117 if (!sandbox_test_module_) {
125 bool RendererMainPlatformDelegate::EnableSandbox() {
126 sandbox::TargetServices* target_services =
127 parameters_.sandbox_info->target_services;
129 if (target_services) {
130 // Cause advapi32 to load before the sandbox is turned on.
131 unsigned int dummy_rand;
133 // Warm up language subsystems before the sandbox is turned on.
134 ::GetUserDefaultLangID();
135 ::GetUserDefaultLCID();
137 target_services->LowerToken();
143 void RendererMainPlatformDelegate::RunSandboxTests(bool no_sandbox) {
144 if (sandbox_test_module_) {
145 RunRendererTests run_security_tests =
146 reinterpret_cast<RunRendererTests>(GetProcAddress(sandbox_test_module_,
148 DCHECK(run_security_tests);
149 if (run_security_tests) {
151 DVLOG(1) << "Running renderer security tests";
152 BOOL result = run_security_tests(&test_count);
153 CHECK(result) << "Test number " << test_count << " has failed.";
158 } // namespace content