summaryrefslogtreecommitdiffstats
path: root/content/ppapi_plugin/ppapi_plugin_main.cc
blob: 53bd14ce34b86f135f94ea912ef22c167a5d7ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/debug/crash_logging.h"
#include "base/debug/debugger.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "content/child/child_process.h"
#include "content/common/content_constants_internal.h"
#include "content/common/sandbox_linux/sandbox_linux.h"
#include "content/ppapi_plugin/ppapi_thread.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
#include "content/public/plugin/content_plugin_client.h"
#include "ipc/ipc_sender.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/proxy_module.h"
#include "ui/base/ui_base_switches.h"

#if defined(OS_WIN)
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "content/child/dwrite_font_proxy/dwrite_font_proxy_init_win.h"
#include "content/common/font_warmup_win.h"
#include "content/public/common/dwrite_font_platform_win.h"
#include "sandbox/win/src/sandbox.h"
#include "third_party/WebKit/public/web/win/WebFontRendering.h"
#include "third_party/skia/include/ports/SkTypeface_win.h"
#include "ui/gfx/win/direct_write.h"
#include "ui/gfx/win/dpi.h"
#endif

#if defined(OS_CHROMEOS)
#include "base/files/file_util.h"
#endif

#if defined(OS_LINUX)
#include "content/public/common/sandbox_init.h"
#endif

#if defined(OS_POSIX) && !defined(OS_ANDROID)
#include <stdlib.h>
#endif

#if defined(OS_WIN)
sandbox::TargetServices* g_target_services = NULL;
#else
void* g_target_services = 0;
#endif

namespace content {

namespace {

#if defined(OS_WIN)
// Windows-only skia sandbox support
void SkiaPreCacheFont(const LOGFONT& logfont) {
  ppapi::proxy::PluginGlobals::Get()->PreCacheFontForFlash(
      reinterpret_cast<const void*>(&logfont));
}
#endif

}  // namespace

// Main function for starting the PPAPI plugin process.
int PpapiPluginMain(const MainFunctionParams& parameters) {
  const base::CommandLine& command_line = parameters.command_line;

#if defined(OS_WIN)
  g_target_services = parameters.sandbox_info->target_services;
#endif

  // If |g_target_services| is not null this process is sandboxed. One side
  // effect is that we can't pop dialogs like ChildProcess::WaitForDebugger()
  // does.
  if (command_line.HasSwitch(switches::kPpapiStartupDialog)) {
    if (g_target_services)
      base::debug::WaitForDebugger(2*60, false);
    else
      ChildProcess::WaitForDebugger("Ppapi");
  }

  // Set the default locale to be the current UI language. WebKit uses ICU's
  // default locale for some font settings (especially switching between
  // Japanese and Chinese fonts for the same characters).
  if (command_line.HasSwitch(switches::kLang)) {
    std::string locale = command_line.GetSwitchValueASCII(switches::kLang);
    base::i18n::SetICUDefaultLocale(locale);

#if defined(OS_POSIX) && !defined(OS_ANDROID)
    // TODO(shess): Flash appears to have a POSIX locale dependency
    // outside of the existing PPAPI ICU support.  Certain games hang
    // while loading, and it seems related to datetime formatting.
    // http://crbug.com/155396
    // http://crbug.com/155671
    //
    // ICU can accept "en-US" or "en_US", but POSIX wants "en_US".
    std::replace(locale.begin(), locale.end(), '-', '_');
    locale.append(".UTF-8");
    setlocale(LC_ALL, locale.c_str());
    setenv("LANG", locale.c_str(), 0);
#endif
  }

#if defined(OS_CHROMEOS)
  // Specifies $HOME explicitly because some plugins rely on $HOME but
  // no other part of Chrome OS uses that.  See crbug.com/335290.
  base::FilePath homedir;
  PathService::Get(base::DIR_HOME, &homedir);
  setenv("HOME", homedir.value().c_str(), 1);
#endif

  base::MessageLoop main_message_loop;
  base::PlatformThread::SetName("CrPPAPIMain");
  base::trace_event::TraceLog::GetInstance()->SetProcessName("PPAPI Process");
  base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
      kTraceEventPpapiProcessSortIndex);

  // Allow the embedder to perform any necessary per-process initialization
  // before the sandbox is initialized.
  if (GetContentClient()->plugin())
    GetContentClient()->plugin()->PreSandboxInitialization();

#if defined(OS_LINUX)
  LinuxSandbox::InitializeSandbox();
#endif

  ChildProcess ppapi_process;
  ppapi_process.set_main_thread(
      new PpapiThread(parameters.command_line, false));  // Not a broker.

#if defined(OS_WIN)
  if (!base::win::IsUser32AndGdi32Available())
    gfx::win::MaybeInitializeDirectWrite();
  bool use_direct_write = gfx::win::IsDirectWriteEnabled();
  if (use_direct_write) {
    if (ShouldUseDirectWriteFontProxyFieldTrial()) {
      InitializeDWriteFontProxy(
          base::Bind(&ppapi::proxy::PluginGlobals::GetBrowserSender,
                     base::Unretained(ppapi::proxy::PluginGlobals::Get())));
    } else {
      WarmupDirectWrite();
    }
  } else {
    SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
  }

  blink::WebFontRendering::setUseDirectWrite(use_direct_write);
  blink::WebFontRendering::setDeviceScaleFactor(gfx::GetDPIScale());
#endif

  main_message_loop.Run();

#if defined(OS_WIN)
  if (ShouldUseDirectWriteFontProxyFieldTrial())
    UninitializeDWriteFontProxy();
#endif
  return 0;
}

}  // namespace content