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/fontmgr_default_win.h"
22#include "skia/ext/vector_platform_device_emf_win.h"
23#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
24#include "third_party/WebKit/public/web/win/WebFontRendering.h"
25#include "third_party/icu/source/i18n/unicode/timezone.h"
26#include "third_party/skia/include/ports/SkFontMgr.h"
27#include "third_party/skia/include/ports/SkTypeface_win.h"
28#include "ui/gfx/win/dpi.h"
29
30#ifdef ENABLE_VTUNE_JIT_INTERFACE
31#include "v8/src/third_party/vtune/v8-vtune.h"
32#endif
33
34#include <dwrite.h>
35
36namespace content {
37namespace {
38
39// Windows-only skia sandbox support
40// These are used for GDI-path rendering.
41void SkiaPreCacheFont(const LOGFONT& logfont) {
42  RenderThread* render_thread = RenderThread::Get();
43  if (render_thread) {
44    render_thread->PreCacheFont(logfont);
45  }
46}
47
48void SkiaPreCacheFontCharacters(const LOGFONT& logfont,
49                                const wchar_t* text,
50                                unsigned int text_length) {
51  RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
52  if (render_thread_impl) {
53    render_thread_impl->PreCacheFontCharacters(
54        logfont,
55        base::string16(text, text_length));
56  }
57}
58
59void WarmupDirectWrite() {
60  // The objects used here are intentionally not freed as we want the Skia
61  // code to use these objects after warmup.
62  SkTypeface* typeface =
63      GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0);
64  DoPreSandboxWarmupForTypeface(typeface);
65  SetDefaultSkiaFactory(GetPreSandboxWarmupFontMgr());
66}
67
68}  // namespace
69
70RendererMainPlatformDelegate::RendererMainPlatformDelegate(
71    const MainFunctionParams& parameters)
72        : parameters_(parameters),
73          sandbox_test_module_(NULL) {
74}
75
76RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
77}
78
79void RendererMainPlatformDelegate::PlatformInitialize() {
80  const CommandLine& command_line = parameters_.command_line;
81
82#ifdef ENABLE_VTUNE_JIT_INTERFACE
83  if (command_line.HasSwitch(switches::kEnableVtune))
84    vTune::InitializeVtuneForV8();
85#endif
86
87  // Be mindful of what resources you acquire here. They can be used by
88  // malicious code if the renderer gets compromised.
89  bool no_sandbox = command_line.HasSwitch(switches::kNoSandbox);
90
91  bool use_direct_write = ShouldUseDirectWrite();
92  if (!no_sandbox) {
93    // ICU DateFormat class (used in base/time_format.cc) needs to get the
94    // Olson timezone ID by accessing the registry keys under
95    // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
96    // After TimeZone::createDefault is called once here, the timezone ID is
97    // cached and there's no more need to access the registry. If the sandbox
98    // is disabled, we don't have to make this dummy call.
99    scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
100
101    if (use_direct_write) {
102      WarmupDirectWrite();
103    } else {
104      SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
105      skia::SetSkiaEnsureTypefaceCharactersAccessible(
106          SkiaPreCacheFontCharacters);
107    }
108  }
109  blink::WebFontRendering::setUseDirectWrite(use_direct_write);
110  blink::WebFontRendering::setDeviceScaleFactor(gfx::GetDPIScale());
111  if (use_direct_write) {
112    blink::WebRuntimeFeatures::enableSubpixelFontScaling(true);
113  }
114}
115
116void RendererMainPlatformDelegate::PlatformUninitialize() {
117}
118
119bool RendererMainPlatformDelegate::EnableSandbox() {
120  sandbox::TargetServices* target_services =
121      parameters_.sandbox_info->target_services;
122
123  if (target_services) {
124    // Cause advapi32 to load before the sandbox is turned on.
125    unsigned int dummy_rand;
126    rand_s(&dummy_rand);
127    // Warm up language subsystems before the sandbox is turned on.
128    ::GetUserDefaultLangID();
129    ::GetUserDefaultLCID();
130
131    target_services->LowerToken();
132    return true;
133  }
134  return false;
135}
136
137}  // namespace content
138