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