- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / nacl / nacl_exe_win_64.cc
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/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_device_source.h"
12 #include "base/process/launch.h"
13 #include "base/process/memory.h"
14 #include "base/strings/string_util.h"
15 #include "base/timer/hi_res_timer_manager.h"
16 #include "chrome/app/chrome_breakpad_client.h"
17 #include "chrome/common/chrome_result_codes.h"
18 #include "chrome/common/logging_chrome.h"
19 #include "components/breakpad/app/breakpad_client.h"
20 #include "components/breakpad/app/breakpad_win.h"
21 #include "components/nacl/broker/nacl_broker_listener.h"
22 #include "components/nacl/common/nacl_switches.h"
23 #include "components/nacl/loader/nacl_listener.h"
24 #include "components/nacl/loader/nacl_main_platform_delegate.h"
25 #include "content/public/app/startup_helper_win.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/main_function_params.h"
28 #include "content/public/common/sandbox_init.h"
29 #include "sandbox/win/src/sandbox_types.h"
30
31 extern int NaClMain(const content::MainFunctionParams&);
32
33 namespace {
34
35 base::LazyInstance<chrome::ChromeBreakpadClient>::Leaky
36     g_chrome_breakpad_client = LAZY_INSTANCE_INITIALIZER;
37
38 } // namespace
39
40 // main() routine for the NaCl broker process.
41 // This is necessary for supporting NaCl in Chrome on Win64.
42 int NaClBrokerMain(const content::MainFunctionParams& parameters) {
43   base::MessageLoopForIO main_message_loop;
44   base::PlatformThread::SetName("CrNaClBrokerMain");
45
46   scoped_ptr<base::PowerMonitorSource> power_monitor_source(
47       new base::PowerMonitorDeviceSource());
48   base::PowerMonitor power_monitor(power_monitor_source.Pass());
49   base::HighResolutionTimerManager hi_res_timer_manager;
50
51   NaClBrokerListener listener;
52   listener.Listen();
53
54   return 0;
55 }
56
57 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
58   sandbox::SandboxInterfaceInfo sandbox_info = {0};
59   content::InitializeSandboxInfo(&sandbox_info);
60
61   base::AtExitManager exit_manager;
62   CommandLine::Init(0, NULL);
63
64   breakpad::SetBreakpadClient(g_chrome_breakpad_client.Pointer());
65   breakpad::InitCrashReporter();
66
67   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
68   std::string process_type =
69       command_line.GetSwitchValueASCII(switches::kProcessType);
70
71   // Copy what ContentMain() does.
72   base::EnableTerminationOnHeapCorruption();
73   base::EnableTerminationOnOutOfMemory();
74   content::RegisterInvalidParamHandler();
75   content::SetupCRT(command_line);
76   // Route stdio to parent console (if any) or create one.
77   if (command_line.HasSwitch(switches::kEnableLogging))
78     base::RouteStdioToConsole();
79
80   // Initialize the sandbox for this process.
81   bool sandbox_initialized_ok = content::InitializeSandbox(&sandbox_info);
82   // Die if the sandbox can't be enabled.
83   CHECK(sandbox_initialized_ok) << "Error initializing sandbox for "
84                                 << process_type;
85   content::MainFunctionParams main_params(command_line);
86   main_params.sandbox_info = &sandbox_info;
87
88   if (process_type == switches::kNaClLoaderProcess)
89     return NaClMain(main_params);
90
91   if (process_type == switches::kNaClBrokerProcess)
92     return NaClBrokerMain(main_params);
93
94   CHECK(false) << "Unknown NaCl 64 process.";
95   return -1;
96 }