Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | 5 #include <windows.h> |
| 6 #include <tchar.h> | 6 #include <tchar.h> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "chrome/app/breakpad_win.h" | 10 #include "chrome/app/breakpad_win.h" |
| 11 #include "chrome/app/client_util.h" | 11 #include "chrome/app/client_util.h" |
| 12 #include "content/public/app/startup_helper_win.h" | 12 #include "content/public/app/startup_helper_win.h" |
| 13 #include "content/public/common/result_codes.h" | 13 #include "content/public/common/result_codes.h" |
| 14 #include "sandbox/src/sandbox_factory.h" | 14 #include "sandbox/src/sandbox_factory.h" |
| 15 | 15 |
| 16 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) { | 16 namespace { |
|
darin (slow to review)
2012/03/16 21:12:57
nit: new line after namespace open
| |
| 17 typedef int (*InitMetro)(LPTHREAD_START_ROUTINE thread_proc); | |
| 18 // this environment variable controls the loading of the metro driver DLL. | |
|
darin (slow to review)
2012/03/16 21:12:57
brett-nit: "this" -> "This"
| |
| 19 const char* kMetroModeEnvVar = "CHROME_METRO_DLL"; | |
| 20 | |
| 21 int RunChrome(HINSTANCE instance) { | |
| 17 bool exit_now = true; | 22 bool exit_now = true; |
| 18 // We restarted because of a previous crash. Ask user if we should relaunch. | 23 // We restarted because of a previous crash. Ask user if we should relaunch. |
| 19 if (ShowRestartDialogIfCrashed(&exit_now)) { | 24 if (ShowRestartDialogIfCrashed(&exit_now)) { |
| 20 if (exit_now) | 25 if (exit_now) |
| 21 return content::RESULT_CODE_NORMAL_EXIT; | 26 return content::RESULT_CODE_NORMAL_EXIT; |
| 22 } | 27 } |
| 23 | 28 |
| 24 // Initialize the sandbox services. | 29 // Initialize the sandbox services. |
| 25 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | 30 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 26 content::InitializeSandboxInfo(&sandbox_info); | 31 content::InitializeSandboxInfo(&sandbox_info); |
| 27 | 32 |
| 28 // The exit manager is in charge of calling the dtors of singletons. | 33 // The exit manager is in charge of calling the dtors of singletons. |
| 29 base::AtExitManager exit_manager; | 34 base::AtExitManager exit_manager; |
| 30 | 35 |
| 31 // Initialize the commandline singleton from the environment. | 36 // Initialize the commandline singleton from the environment. |
| 32 CommandLine::Init(0, NULL); | 37 CommandLine::Init(0, NULL); |
| 33 | 38 |
| 34 // Load and launch the chrome dll. *Everything* happens inside. | 39 // Load and launch the chrome dll. *Everything* happens inside. |
| 35 MainDllLoader* loader = MakeMainDllLoader(); | 40 MainDllLoader* loader = MakeMainDllLoader(); |
| 36 int rc = loader->Launch(instance, &sandbox_info); | 41 int rc = loader->Launch(instance, &sandbox_info); |
| 37 loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded(); | 42 loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded(); |
| 38 delete loader; | 43 delete loader; |
| 39 | |
| 40 return rc; | 44 return rc; |
| 41 } | 45 } |
| 46 | |
| 47 // Helper class to manage the metro driver dll. When present in the system, | |
| 48 // the main process thread needs to call InitMetro() on the dll. | |
| 49 class MetroDriver { | |
|
darin (slow to review)
2012/03/16 21:12:57
feels like this should be in a file of its own
| |
| 50 public: | |
| 51 MetroDriver() : metro_dll_(0), init_metro_fn_(NULL) { | |
| 52 if (0 != ::GetEnvironmentVariableA(kMetroModeEnvVar, NULL, 0)) | |
| 53 return; | |
| 54 // We haven't tried to load the metro driver, this probably means we are the | |
| 55 // browser. Find it or not we set the environment variable because we don't | |
| 56 // want to keep trying in the child processes. | |
| 57 metro_dll_ = ::LoadLibraryA("metro_driver.dll"); | |
| 58 ::SetEnvironmentVariableA(kMetroModeEnvVar, metro_dll_ ? "1" : "0"); | |
| 59 if (!metro_dll_) | |
| 60 return; | |
| 61 init_metro_fn_ = reinterpret_cast<InitMetro>( | |
| 62 ::GetProcAddress(metro_dll_, "InitMetro")); | |
| 63 } | |
| 64 | |
| 65 // returns true if chrome is being launched in metro. If so we should | |
| 66 // call RunInMetro(). If not then we should just run chrome as usual. | |
| 67 bool in_metro_mode() const { | |
| 68 return (init_metro_fn_ != NULL); | |
| 69 } | |
| 70 | |
| 71 // Enter the metro main function, which will only return when chrome metro | |
| 72 // is closed. Once metro has initialized, the dll creates a new thread | |
| 73 // which will run|SurrogateMainThread|. | |
| 74 int RunInMetro(HINSTANCE instance) { | |
| 75 module_instance_ = instance; | |
| 76 return init_metro_fn_(&SurrogateMainThread); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 static HINSTANCE module_instance_; | |
| 81 | |
| 82 static DWORD WINAPI SurrogateMainThread(void*) { | |
| 83 return RunChrome(module_instance_); | |
| 84 } | |
| 85 | |
| 86 HMODULE metro_dll_; | |
| 87 InitMetro init_metro_fn_; | |
| 88 }; | |
| 89 | |
| 90 HINSTANCE MetroDriver::module_instance_; | |
| 91 } // namespace | |
|
darin (slow to review)
2012/03/16 21:12:57
nit: new line before namespace close
| |
| 92 | |
| 93 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) { | |
| 94 MetroDriver metro_driver; | |
| 95 if (metro_driver.in_metro_mode()) | |
| 96 return metro_driver.RunInMetro(instance); | |
| 97 return RunChrome(instance); | |
| 98 } | |
| OLD | NEW |