Upstream version 7.36.149.0
[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/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"
27
28 #ifdef ENABLE_VTUNE_JIT_INTERFACE
29 #include "v8/src/third_party/vtune/v8-vtune.h"
30 #endif
31
32 #include <dwrite.h>
33
34 namespace content {
35 namespace {
36
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();
41   if (render_thread) {
42     render_thread->PreCacheFont(logfont);
43   }
44 }
45
46 void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
47                                 const wchar_t* text,
48                                 unsigned int text_length) {
49   RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
50   if (render_thread_impl) {
51     render_thread_impl->PreCacheFontCharacters(
52         logfont,
53         base::string16(text, text_length));
54   }
55 }
56
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);
63 }
64
65 }  // namespace
66
67 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
68     const MainFunctionParams& parameters)
69         : parameters_(parameters),
70           sandbox_test_module_(NULL) {
71 }
72
73 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
74 }
75
76 void RendererMainPlatformDelegate::PlatformInitialize() {
77   const CommandLine& command_line = parameters_.command_line;
78
79 #ifdef ENABLE_VTUNE_JIT_INTERFACE
80   if (command_line.HasSwitch(switches::kEnableVtune))
81     vTune::InitializeVtuneForV8();
82 #endif
83
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);
87
88   bool use_direct_write = ShouldUseDirectWrite();
89   if (!no_sandbox) {
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());
97
98     if (use_direct_write) {
99       WarmupDirectWrite();
100     } else {
101       SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
102       skia::SetSkiaEnsureTypefaceCharactersAccessible(
103           SkiaPreCacheFontCharacters);
104     }
105   }
106   blink::WebFontRendering::setUseDirectWrite(use_direct_write);
107   if (use_direct_write) {
108     blink::WebRuntimeFeatures::enableSubpixelFontScaling(true);
109   }
110 }
111
112 void RendererMainPlatformDelegate::PlatformUninitialize() {
113 }
114
115 bool RendererMainPlatformDelegate::InitSandboxTests(bool no_sandbox) {
116   const CommandLine& command_line = parameters_.command_line;
117
118   DVLOG(1) << "Started renderer with " << command_line.GetCommandLineString();
119
120   sandbox::TargetServices* target_services =
121       parameters_.sandbox_info->target_services;
122
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_) {
130         return false;
131       }
132     }
133   }
134   return true;
135 }
136
137 bool RendererMainPlatformDelegate::EnableSandbox() {
138   sandbox::TargetServices* target_services =
139       parameters_.sandbox_info->target_services;
140
141   if (target_services) {
142     // Cause advapi32 to load before the sandbox is turned on.
143     unsigned int dummy_rand;
144     rand_s(&dummy_rand);
145     // Warm up language subsystems before the sandbox is turned on.
146     ::GetUserDefaultLangID();
147     ::GetUserDefaultLCID();
148
149     target_services->LowerToken();
150     return true;
151   }
152   return false;
153 }
154
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_,
159                                                           kRenderTestCall));
160     DCHECK(run_security_tests);
161     if (run_security_tests) {
162       int test_count = 0;
163       DVLOG(1) << "Running renderer security tests";
164       BOOL result = run_security_tests(&test_count);
165       CHECK(result) << "Test number " << test_count << " has failed.";
166     }
167   }
168 }
169
170 }  // namespace content