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 "base/base_paths.h"
6#include "base/command_line.h"
7#include "base/debug/debugger.h"
8#include "base/files/file_path.h"
9#include "base/i18n/rtl.h"
10#include "base/message_loop/message_loop.h"
11#include "base/path_service.h"
12#include "base/threading/platform_thread.h"
13#include "build/build_config.h"
14#include "content/child/child_process.h"
15#include "content/common/content_constants_internal.h"
16#include "content/common/sandbox_linux/sandbox_linux.h"
17#include "content/ppapi_plugin/ppapi_thread.h"
18#include "content/public/common/content_client.h"
19#include "content/public/common/content_switches.h"
20#include "content/public/common/main_function_params.h"
21#include "content/public/plugin/content_plugin_client.h"
22#include "crypto/nss_util.h"
23#include "ppapi/proxy/plugin_globals.h"
24#include "ppapi/proxy/proxy_module.h"
25#include "ui/base/ui_base_switches.h"
26
27#if defined(OS_WIN)
28#include "sandbox/win/src/sandbox.h"
29#include "third_party/skia/include/ports/SkTypeface_win.h"
30#endif
31
32#if defined(OS_CHROMEOS)
33#include "base/file_util.h"
34#endif
35
36#if defined(OS_LINUX)
37#include "content/public/common/sandbox_init.h"
38#endif
39
40#if defined(OS_POSIX) && !defined(OS_ANDROID)
41#include <stdlib.h>
42#endif
43
44#if defined(OS_WIN)
45sandbox::TargetServices* g_target_services = NULL;
46#else
47void* g_target_services = 0;
48#endif
49
50namespace content {
51
52namespace {
53
54#if defined(OS_WIN)
55// Windows-only skia sandbox support
56void SkiaPreCacheFont(const LOGFONT& logfont) {
57  ppapi::proxy::PluginGlobals::Get()->PreCacheFontForFlash(
58      reinterpret_cast<const void*>(&logfont));
59}
60#endif
61
62}  // namespace
63
64// Main function for starting the PPAPI plugin process.
65int PpapiPluginMain(const MainFunctionParams& parameters) {
66  const CommandLine& command_line = parameters.command_line;
67
68#if defined(OS_WIN)
69  g_target_services = parameters.sandbox_info->target_services;
70#endif
71
72  // If |g_target_services| is not null this process is sandboxed. One side
73  // effect is that we can't pop dialogs like ChildProcess::WaitForDebugger()
74  // does.
75  if (command_line.HasSwitch(switches::kPpapiStartupDialog)) {
76    if (g_target_services)
77      base::debug::WaitForDebugger(2*60, false);
78    else
79      ChildProcess::WaitForDebugger("Ppapi");
80  }
81
82  // Set the default locale to be the current UI language. WebKit uses ICU's
83  // default locale for some font settings (especially switching between
84  // Japanese and Chinese fonts for the same characters).
85  if (command_line.HasSwitch(switches::kLang)) {
86    std::string locale = command_line.GetSwitchValueASCII(switches::kLang);
87    base::i18n::SetICUDefaultLocale(locale);
88
89#if defined(OS_POSIX) && !defined(OS_ANDROID)
90    // TODO(shess): Flash appears to have a POSIX locale dependency
91    // outside of the existing PPAPI ICU support.  Certain games hang
92    // while loading, and it seems related to datetime formatting.
93    // http://crbug.com/155396
94    // http://crbug.com/155671
95    //
96    // ICU can accept "en-US" or "en_US", but POSIX wants "en_US".
97    std::replace(locale.begin(), locale.end(), '-', '_');
98    locale.append(".UTF-8");
99    setlocale(LC_ALL, locale.c_str());
100    setenv("LANG", locale.c_str(), 0);
101#endif
102  }
103
104#if defined(OS_CHROMEOS)
105  // Specifies $HOME explicitly because some plugins rely on $HOME but
106  // no other part of Chrome OS uses that.  See crbug.com/335290.
107  base::FilePath homedir;
108  PathService::Get(base::DIR_HOME, &homedir);
109  setenv("HOME", homedir.value().c_str(), 1);
110#endif
111
112  base::MessageLoop main_message_loop;
113  base::PlatformThread::SetName("CrPPAPIMain");
114  base::debug::TraceLog::GetInstance()->SetProcessName("PPAPI Process");
115  base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
116      kTraceEventPpapiProcessSortIndex);
117
118#if defined(OS_LINUX) && defined(USE_NSS)
119  // Some out-of-process PPAPI plugins use NSS.
120  // NSS must be initialized before enabling the sandbox below.
121  crypto::InitNSSSafely();
122#endif
123
124  // Allow the embedder to perform any necessary per-process initialization
125  // before the sandbox is initialized.
126  if (GetContentClient()->plugin())
127    GetContentClient()->plugin()->PreSandboxInitialization();
128
129#if defined(OS_LINUX)
130  LinuxSandbox::InitializeSandbox();
131#endif
132
133  ChildProcess ppapi_process;
134  ppapi_process.set_main_thread(
135      new PpapiThread(parameters.command_line, false));  // Not a broker.
136
137#if defined(OS_WIN)
138  SkTypeface_SetEnsureLOGFONTAccessibleProc(SkiaPreCacheFont);
139#endif
140
141  main_message_loop.Run();
142  return 0;
143}
144
145}  // namespace content
146