Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / renderer_main_platform_delegate_win.cc
blob2d769e861657a4d7ad21de9150a20d38dbf6b889
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/public/common/content_switches.h"
15 #include "content/public/common/injection_test_win.h"
16 #include "content/public/renderer/render_font_warmup_win.h"
17 #include "content/public/renderer/render_thread.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "sandbox/win/src/sandbox.h"
20 #include "skia/ext/fontmgr_default_win.h"
21 #include "third_party/WebKit/public/web/WebRuntimeFeatures.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"
26 #include "ui/gfx/hud_font.h"
27 #include "ui/gfx/win/direct_write.h"
28 #include "ui/gfx/win/dpi.h"
30 #include <dwrite.h>
32 namespace content {
33 namespace {
35 // Windows-only skia sandbox support
36 // These are used for GDI-path rendering.
37 void SkiaPreCacheFont(const LOGFONT& logfont) {
38 RenderThread* render_thread = RenderThread::Get();
39 if (render_thread) {
40 render_thread->PreCacheFont(logfont);
44 void WarmupDirectWrite() {
45 // The objects used here are intentionally not freed as we want the Skia
46 // code to use these objects after warmup.
47 SetDefaultSkiaFactory(GetPreSandboxWarmupFontMgr());
49 // We need to warm up *some* font for DirectWrite. We also need to pass one
50 // down for the CC HUD code, so use the same one here. Note that we don't use
51 // a monospace as would be nice in an attempt to avoid a small startup time
52 // regression, see http://crbug.com/463613.
53 skia::RefPtr<SkTypeface> hud_typeface = skia::AdoptRef(
54 GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0));
55 DoPreSandboxWarmupForTypeface(hud_typeface.get());
56 gfx::SetHudTypeface(hud_typeface);
59 } // namespace
61 RendererMainPlatformDelegate::RendererMainPlatformDelegate(
62 const MainFunctionParams& parameters)
63 : parameters_(parameters),
64 sandbox_test_module_(NULL) {
67 RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
70 void RendererMainPlatformDelegate::PlatformInitialize() {
71 const base::CommandLine& command_line = parameters_.command_line;
73 // Be mindful of what resources you acquire here. They can be used by
74 // malicious code if the renderer gets compromised.
75 bool no_sandbox = command_line.HasSwitch(switches::kNoSandbox);
77 bool use_direct_write = gfx::win::ShouldUseDirectWrite();
78 if (!no_sandbox) {
79 // ICU DateFormat class (used in base/time_format.cc) needs to get the
80 // Olson timezone ID by accessing the registry keys under
81 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
82 // After TimeZone::createDefault is called once here, the timezone ID is
83 // cached and there's no more need to access the registry. If the sandbox
84 // is disabled, we don't have to make this dummy call.
85 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
87 if (use_direct_write) {
88 WarmupDirectWrite();
89 } else {
90 SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
93 blink::WebFontRendering::setUseDirectWrite(use_direct_write);
94 blink::WebFontRendering::setDeviceScaleFactor(gfx::GetDPIScale());
97 void RendererMainPlatformDelegate::PlatformUninitialize() {
100 bool RendererMainPlatformDelegate::EnableSandbox() {
101 sandbox::TargetServices* target_services =
102 parameters_.sandbox_info->target_services;
104 if (target_services) {
105 // Cause advapi32 to load before the sandbox is turned on.
106 unsigned int dummy_rand;
107 rand_s(&dummy_rand);
108 // Warm up language subsystems before the sandbox is turned on.
109 ::GetUserDefaultLangID();
110 ::GetUserDefaultLCID();
112 #if defined(ADDRESS_SANITIZER)
113 // Bind and leak dbghelp.dll before the token is lowered, otherwise
114 // AddressSanitizer will crash when trying to symbolize a report.
115 if (!LoadLibraryA("dbghelp.dll"))
116 return false;
117 #endif
119 target_services->LowerToken();
120 return true;
122 return false;
125 } // namespace content