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