Home | History | Annotate | Download | only in nacl
      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/breakpad_win.h"
     17 #include "chrome/app/chrome_breakpad_client.h"
     18 #include "chrome/common/chrome_result_codes.h"
     19 #include "chrome/common/logging_chrome.h"
     20 #include "components/breakpad/breakpad_client.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   const CommandLine& parsed_command_line = parameters.command_line;
     44 
     45   base::MessageLoopForIO main_message_loop;
     46   base::PlatformThread::SetName("CrNaClBrokerMain");
     47 
     48   scoped_ptr<base::PowerMonitorSource> power_monitor_source(
     49       new base::PowerMonitorDeviceSource());
     50   base::PowerMonitor power_monitor(power_monitor_source.Pass());
     51   base::HighResolutionTimerManager hi_res_timer_manager;
     52 
     53   NaClBrokerListener listener;
     54   listener.Listen();
     55 
     56   return 0;
     57 }
     58 
     59 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
     60   sandbox::SandboxInterfaceInfo sandbox_info = {0};
     61   content::InitializeSandboxInfo(&sandbox_info);
     62 
     63   base::AtExitManager exit_manager;
     64   CommandLine::Init(0, NULL);
     65 
     66   breakpad::SetBreakpadClient(g_chrome_breakpad_client.Pointer());
     67   InitCrashReporter();
     68 
     69   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
     70   std::string process_type =
     71       command_line.GetSwitchValueASCII(switches::kProcessType);
     72 
     73   // Copy what ContentMain() does.
     74   base::EnableTerminationOnHeapCorruption();
     75   base::EnableTerminationOnOutOfMemory();
     76   content::RegisterInvalidParamHandler();
     77   content::SetupCRT(command_line);
     78   // Route stdio to parent console (if any) or create one.
     79   if (command_line.HasSwitch(switches::kEnableLogging))
     80     base::RouteStdioToConsole();
     81 
     82   // Initialize the sandbox for this process.
     83   bool sandbox_initialized_ok = content::InitializeSandbox(&sandbox_info);
     84   // Die if the sandbox can't be enabled.
     85   CHECK(sandbox_initialized_ok) << "Error initializing sandbox for "
     86                                 << process_type;
     87   content::MainFunctionParams main_params(command_line);
     88   main_params.sandbox_info = &sandbox_info;
     89 
     90   if (process_type == switches::kNaClLoaderProcess)
     91     return NaClMain(main_params);
     92 
     93   if (process_type == switches::kNaClBrokerProcess)
     94     return NaClBrokerMain(main_params);
     95 
     96   CHECK(false) << "Unknown NaCl 64 process.";
     97   return -1;
     98 }
     99