- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / renderer_main_platform_delegate_win.cc
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.
4
5 #include "content/renderer/renderer_main_platform_delegate.h"
6
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"
20
21 #ifdef ENABLE_VTUNE_JIT_INTERFACE
22 #include "v8/src/third_party/vtune/v8-vtune.h"
23 #endif
24
25 namespace content {
26 namespace {
27
28 // Windows-only skia sandbox support
29 void SkiaPreCacheFont(const LOGFONT& logfont) {
30   RenderThread* render_thread = RenderThread::Get();
31   if (render_thread) {
32     render_thread->PreCacheFont(logfont);
33   }
34 }
35
36 void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
37                                 const wchar_t* text,
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));
43   }
44 }
45
46 #if !defined(NDEBUG)
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);
51 }
52 #endif  // !NDEBUG
53
54 }  // namespace
55
56 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
57     const MainFunctionParams& parameters)
58         : parameters_(parameters),
59           sandbox_test_module_(NULL) {
60 }
61
62 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
63 }
64
65 void RendererMainPlatformDelegate::PlatformInitialize() {
66 #if !defined(NDEBUG)
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
70   // thread.
71   PCHECK(
72       SetWindowsHookEx(WH_CBT, WindowsHookCBT, NULL, ::GetCurrentThreadId()));
73 #endif  // !NDEBUG
74
75   const CommandLine& command_line = parameters_.command_line;
76
77 #ifdef ENABLE_VTUNE_JIT_INTERFACE
78   if (command_line.HasSwitch(switches::kEnableVtune))
79     vTune::InitializeVtuneForV8();
80 #endif
81
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);
85
86   if (!no_sandbox) {
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);
97   }
98 }
99
100 void RendererMainPlatformDelegate::PlatformUninitialize() {
101 }
102
103 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
104   const CommandLine& command_line = parameters_.command_line;
105
106   DVLOG(1) << "Started renderer with " << command_line.GetCommandLineString();
107
108   sandbox::TargetServices* target_services =
109       parameters_.sandbox_info->target_services;
110
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_) {
118         return false;
119       }
120     }
121   }
122   return true;
123 }
124
125 bool RendererMainPlatformDelegate::EnableSandbox() {
126   sandbox::TargetServices* target_services =
127       parameters_.sandbox_info->target_services;
128
129   if (target_services) {
130     // Cause advapi32 to load before the sandbox is turned on.
131     unsigned int dummy_rand;
132     rand_s(&dummy_rand);
133     // Warm up language subsystems before the sandbox is turned on.
134     ::GetUserDefaultLangID();
135     ::GetUserDefaultLCID();
136
137     target_services->LowerToken();
138     return true;
139   }
140   return false;
141 }
142
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_,
147                                                           kRenderTestCall));
148     DCHECK(run_security_tests);
149     if (run_security_tests) {
150       int test_count = 0;
151       DVLOG(1) << "Running renderer security tests";
152       BOOL result = run_security_tests(&test_count);
153       CHECK(result) << "Test number " << test_count << " has failed.";
154     }
155   }
156 }
157
158 }  // namespace content