OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/test/test_launcher.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/chrome_constants.h" |
| 12 #include "chrome/test/base/chrome_test_suite.h" |
| 13 |
| 14 #if defined(OS_MACOSX) |
| 15 #include "chrome/browser/chrome_browser_application_mac.h" |
| 16 #endif // defined(OS_MACOSX) |
| 17 |
| 18 #if defined(OS_WIN) |
| 19 #include "base/base_switches.h" |
| 20 #include "content/common/sandbox_policy.h" |
| 21 #include "sandbox/src/dep.h" |
| 22 #include "sandbox/src/sandbox_factory.h" |
| 23 #include "sandbox/src/sandbox_types.h" |
| 24 |
| 25 // The entry point signature of chrome.dll. |
| 26 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*, wchar_t*); |
| 27 #endif // defined(OS_WIN) |
| 28 |
| 29 class ChromeTestLauncherDelegate : public test_launcher::TestLauncherDelegate { |
| 30 public: |
| 31 ChromeTestLauncherDelegate() { |
| 32 } |
| 33 |
| 34 virtual ~ChromeTestLauncherDelegate() { |
| 35 } |
| 36 |
| 37 virtual void EarlyInitialize() OVERRIDE { |
| 38 #if defined(OS_MACOSX) |
| 39 chrome_browser_application_mac::RegisterBrowserCrApp(); |
| 40 #endif |
| 41 } |
| 42 |
| 43 virtual bool Run(int argc, char** argv, int* return_code) OVERRIDE { |
| 44 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 45 |
| 46 // TODO(pkasting): This "single_process vs. single-process" design is |
| 47 // terrible UI. Instead, there should be some sort of signal flag on the |
| 48 // command line, with all subsequent arguments passed through to the |
| 49 // underlying browser. |
| 50 if (command_line->HasSwitch(test_launcher::kSingleProcessTestsFlag) || |
| 51 command_line->HasSwitch( |
| 52 test_launcher::kSingleProcessTestsAndChromeFlag) || |
| 53 command_line->HasSwitch(test_launcher::kGTestListTestsFlag) || |
| 54 command_line->HasSwitch(test_launcher::kGTestHelpFlag)) { |
| 55 #if defined(OS_WIN) |
| 56 if (command_line->HasSwitch(test_launcher::kSingleProcessTestsFlag)) { |
| 57 // This is the browser process, so setup the sandbox broker. |
| 58 sandbox::BrokerServices* broker_services = |
| 59 sandbox::SandboxFactory::GetBrokerServices(); |
| 60 if (broker_services) { |
| 61 sandbox::InitBrokerServices(broker_services); |
| 62 // Precreate the desktop and window station used by the renderers. |
| 63 sandbox::TargetPolicy* policy = broker_services->CreatePolicy(); |
| 64 sandbox::ResultCode result = policy->CreateAlternateDesktop(true); |
| 65 CHECK(sandbox::SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION != result); |
| 66 policy->Release(); |
| 67 } |
| 68 } |
| 69 #endif |
| 70 *return_code = ChromeTestSuite(argc, argv).Run(); |
| 71 return true; |
| 72 } |
| 73 |
| 74 #if defined(OS_WIN) |
| 75 if (command_line->HasSwitch(switches::kProcessType)) { |
| 76 // This is a child process, call ChromeMain. |
| 77 FilePath chrome_path(command_line->GetProgram().DirName()); |
| 78 chrome_path = chrome_path.Append(chrome::kBrowserResourcesDll); |
| 79 HMODULE dll = LoadLibrary(chrome_path.value().c_str()); |
| 80 DLL_MAIN entry_point = |
| 81 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll, "ChromeMain")); |
| 82 if (!entry_point) |
| 83 return false; |
| 84 |
| 85 // Initialize the sandbox services. |
| 86 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 87 sandbox_info.target_services = |
| 88 sandbox::SandboxFactory::GetTargetServices(); |
| 89 *return_code = |
| 90 entry_point(GetModuleHandle(NULL), &sandbox_info, GetCommandLineW()); |
| 91 return true; |
| 92 } |
| 93 #endif // defined(OS_WIN) |
| 94 return false; |
| 95 } |
| 96 |
| 97 virtual bool AdjustChildProcessCommandLine( |
| 98 CommandLine* command_line) OVERRIDE { |
| 99 CommandLine new_command_line(command_line->GetProgram()); |
| 100 CommandLine::SwitchMap switches = command_line->GetSwitches(); |
| 101 |
| 102 // Strip out user-data-dir if present. We will add it back in again later. |
| 103 switches.erase(switches::kUserDataDir); |
| 104 |
| 105 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); |
| 106 iter != switches.end(); ++iter) { |
| 107 new_command_line.AppendSwitchNative((*iter).first, (*iter).second); |
| 108 } |
| 109 |
| 110 // Create a new user data dir and pass it to the child. |
| 111 ScopedTempDir temp_dir; |
| 112 if (!temp_dir.CreateUniqueTempDir() || !temp_dir.IsValid()) { |
| 113 LOG(ERROR) << "Error creating temp profile directory"; |
| 114 return false; |
| 115 } |
| 116 new_command_line.AppendSwitchPath(switches::kUserDataDir, temp_dir.path()); |
| 117 |
| 118 // file:// access for ChromeOS. |
| 119 new_command_line.AppendSwitch(switches::kAllowFileAccess); |
| 120 |
| 121 *command_line = new_command_line; |
| 122 return true; |
| 123 } |
| 124 |
| 125 private: |
| 126 DISALLOW_COPY_AND_ASSIGN(ChromeTestLauncherDelegate); |
| 127 }; |
| 128 |
| 129 int main(int argc, char** argv) { |
| 130 ChromeTestLauncherDelegate launcher_delegate; |
| 131 return test_launcher::LaunchTests(&launcher_delegate, argc, argv); |
| 132 } |
OLD | NEW |