| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "headless/lib/headless_content_main_delegate.h" | 5 #include "headless/lib/headless_content_main_delegate.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/environment.h" | 11 #include "base/environment.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "components/crash/content/app/breakpad_linux.h" | 18 #include "components/crash/content/app/breakpad_linux.h" |
| 19 #include "content/public/browser/browser_main_runner.h" | 19 #include "content/public/browser/browser_main_runner.h" |
| 20 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 21 #include "headless/lib/browser/headless_browser_impl.h" | 21 #include "headless/lib/browser/headless_browser_impl.h" |
| 22 #include "headless/lib/browser/headless_content_browser_client.h" | 22 #include "headless/lib/browser/headless_content_browser_client.h" |
| 23 #include "headless/lib/headless_crash_reporter_client.h" | 23 #include "headless/lib/headless_crash_reporter_client.h" |
| 24 #include "headless/lib/headless_macros.h" | 24 #include "headless/lib/headless_macros.h" |
| 25 #include "headless/lib/renderer/headless_content_renderer_client.h" | |
| 26 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 27 #include "ui/base/ui_base_switches.h" | 26 #include "ui/base/ui_base_switches.h" |
| 28 #include "ui/gfx/switches.h" | 27 #include "ui/gfx/switches.h" |
| 29 #include "ui/gl/gl_switches.h" | 28 #include "ui/gl/gl_switches.h" |
| 30 #include "ui/ozone/public/ozone_switches.h" | 29 #include "ui/ozone/public/ozone_switches.h" |
| 31 | 30 |
| 32 #ifdef HEADLESS_USE_EMBEDDED_RESOURCES | 31 #ifdef HEADLESS_USE_EMBEDDED_RESOURCES |
| 33 #include "headless/embedded_resource_pak.h" | 32 #include "headless/embedded_resource_pak.h" |
| 34 #endif | 33 #endif |
| 35 | 34 |
| 36 #if defined(OS_MACOSX) | 35 #if defined(OS_MACOSX) |
| 37 #include "components/crash/content/app/crashpad.h" | 36 #include "components/crash/content/app/crashpad.h" |
| 38 #endif | 37 #endif |
| 39 | 38 |
| 39 #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
| 40 #include "headless/lib/renderer/headless_content_renderer_client.h" |
| 41 #endif |
| 42 |
| 40 namespace headless { | 43 namespace headless { |
| 41 namespace { | 44 namespace { |
| 42 // Keep in sync with content/common/content_constants_internal.h. | 45 // Keep in sync with content/common/content_constants_internal.h. |
| 43 // TODO(skyostil): Add a tracing test for this. | 46 // TODO(skyostil): Add a tracing test for this. |
| 44 const int kTraceEventBrowserProcessSortIndex = -6; | 47 const int kTraceEventBrowserProcessSortIndex = -6; |
| 45 | 48 |
| 46 HeadlessContentMainDelegate* g_current_headless_content_main_delegate = nullptr; | 49 HeadlessContentMainDelegate* g_current_headless_content_main_delegate = nullptr; |
| 47 | 50 |
| 48 base::LazyInstance<HeadlessCrashReporterClient>::Leaky g_headless_crash_client = | 51 base::LazyInstance<HeadlessCrashReporterClient>::Leaky g_headless_crash_client = |
| 49 LAZY_INSTANCE_INITIALIZER; | 52 LAZY_INSTANCE_INITIALIZER; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } else { | 91 } else { |
| 89 command_line->AppendSwitch(switches::kDisableGpu); | 92 command_line->AppendSwitch(switches::kDisableGpu); |
| 90 } | 93 } |
| 91 | 94 |
| 92 SetContentClient(&content_client_); | 95 SetContentClient(&content_client_); |
| 93 return false; | 96 return false; |
| 94 } | 97 } |
| 95 | 98 |
| 96 void HeadlessContentMainDelegate::InitLogging( | 99 void HeadlessContentMainDelegate::InitLogging( |
| 97 const base::CommandLine& command_line) { | 100 const base::CommandLine& command_line) { |
| 101 const std::string process_type = |
| 102 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 98 #if !defined(OS_WIN) | 103 #if !defined(OS_WIN) |
| 99 if (!command_line.HasSwitch(switches::kEnableLogging)) | 104 if (!command_line.HasSwitch(switches::kEnableLogging)) |
| 100 return; | 105 return; |
| 101 #endif | 106 #else |
| 107 // Child processes in Windows are not able to initialize logging. |
| 108 if (!process_type.empty()) |
| 109 return; |
| 110 #endif // !defined(OS_WIN) |
| 102 | 111 |
| 103 logging::LoggingDestination log_mode; | 112 logging::LoggingDestination log_mode; |
| 104 base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); | 113 base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 105 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") { | 114 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") { |
| 106 log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 115 log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 107 } else { | 116 } else { |
| 108 base::FilePath custom_filename( | 117 base::FilePath custom_filename( |
| 109 command_line.GetSwitchValuePath(switches::kEnableLogging)); | 118 command_line.GetSwitchValuePath(switches::kEnableLogging)); |
| 110 if (custom_filename.empty()) { | 119 if (custom_filename.empty()) { |
| 111 log_mode = logging::LOG_TO_ALL; | 120 log_mode = logging::LOG_TO_ALL; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 136 } else { | 145 } else { |
| 137 log_path = log_filename; | 146 log_path = log_filename; |
| 138 } | 147 } |
| 139 | 148 |
| 140 std::string filename; | 149 std::string filename; |
| 141 std::unique_ptr<base::Environment> env(base::Environment::Create()); | 150 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 142 if (env->GetVar(kLogFileName, &filename) && !filename.empty()) { | 151 if (env->GetVar(kLogFileName, &filename) && !filename.empty()) { |
| 143 log_path = base::FilePath::FromUTF8Unsafe(filename); | 152 log_path = base::FilePath::FromUTF8Unsafe(filename); |
| 144 } | 153 } |
| 145 | 154 |
| 146 const std::string process_type = | |
| 147 command_line.GetSwitchValueASCII(switches::kProcessType); | |
| 148 | |
| 149 settings.logging_dest = log_mode; | 155 settings.logging_dest = log_mode; |
| 150 settings.log_file = log_path.value().c_str(); | 156 settings.log_file = log_path.value().c_str(); |
| 151 settings.lock_log = logging::DONT_LOCK_LOG_FILE; | 157 settings.lock_log = logging::DONT_LOCK_LOG_FILE; |
| 152 settings.delete_old = process_type.empty() ? logging::DELETE_OLD_LOG_FILE | 158 settings.delete_old = process_type.empty() ? logging::DELETE_OLD_LOG_FILE |
| 153 : logging::APPEND_TO_OLD_LOG_FILE; | 159 : logging::APPEND_TO_OLD_LOG_FILE; |
| 154 bool success = logging::InitLogging(settings); | 160 bool success = logging::InitLogging(settings); |
| 155 DCHECK(success); | 161 DCHECK(success); |
| 156 } | 162 } |
| 157 | 163 |
| 158 void HeadlessContentMainDelegate::InitCrashReporter( | 164 void HeadlessContentMainDelegate::InitCrashReporter( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 187 if (command_line.HasSwitch(switches::kEnableLogging)) | 193 if (command_line.HasSwitch(switches::kEnableLogging)) |
| 188 InitLogging(command_line); | 194 InitLogging(command_line); |
| 189 #endif // defined(OS_WIN) | 195 #endif // defined(OS_WIN) |
| 190 InitCrashReporter(command_line); | 196 InitCrashReporter(command_line); |
| 191 InitializeResourceBundle(); | 197 InitializeResourceBundle(); |
| 192 } | 198 } |
| 193 | 199 |
| 194 int HeadlessContentMainDelegate::RunProcess( | 200 int HeadlessContentMainDelegate::RunProcess( |
| 195 const std::string& process_type, | 201 const std::string& process_type, |
| 196 const content::MainFunctionParams& main_function_params) { | 202 const content::MainFunctionParams& main_function_params) { |
| 203 #if !defined(CHROME_MULTIPLE_DLL_CHILD) |
| 204 |
| 197 if (!process_type.empty()) | 205 if (!process_type.empty()) |
| 198 return -1; | 206 return -1; |
| 199 | 207 |
| 200 base::trace_event::TraceLog::GetInstance()->SetProcessName("HeadlessBrowser"); | 208 base::trace_event::TraceLog::GetInstance()->SetProcessName("HeadlessBrowser"); |
| 201 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( | 209 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( |
| 202 kTraceEventBrowserProcessSortIndex); | 210 kTraceEventBrowserProcessSortIndex); |
| 203 | 211 |
| 204 std::unique_ptr<content::BrowserMainRunner> browser_runner( | 212 std::unique_ptr<content::BrowserMainRunner> browser_runner( |
| 205 content::BrowserMainRunner::Create()); | 213 content::BrowserMainRunner::Create()); |
| 206 | 214 |
| 207 int exit_code = browser_runner->Initialize(main_function_params); | 215 int exit_code = browser_runner->Initialize(main_function_params); |
| 208 DCHECK_LT(exit_code, 0) << "content::BrowserMainRunner::Initialize failed in " | 216 DCHECK_LT(exit_code, 0) << "content::BrowserMainRunner::Initialize failed in " |
| 209 "HeadlessContentMainDelegate::RunProcess"; | 217 "HeadlessContentMainDelegate::RunProcess"; |
| 210 | 218 |
| 211 browser_->RunOnStartCallback(); | 219 browser_->RunOnStartCallback(); |
| 212 browser_runner->Run(); | 220 browser_runner->Run(); |
| 213 browser_.reset(); | 221 browser_.reset(); |
| 214 browser_runner->Shutdown(); | 222 browser_runner->Shutdown(); |
| 215 | 223 |
| 216 // Return value >=0 here to disable calling content::BrowserMain. | 224 // Return value >=0 here to disable calling content::BrowserMain. |
| 217 return 0; | 225 return 0; |
| 226 #else |
| 227 return -1; |
| 228 #endif // !defined(CHROME_MULTIPLE_DLL_CHILD) |
| 218 } | 229 } |
| 219 | 230 |
| 220 #if !defined(OS_MACOSX) && defined(OS_POSIX) && !defined(OS_ANDROID) | 231 #if !defined(OS_MACOSX) && defined(OS_POSIX) && !defined(OS_ANDROID) |
| 221 void HeadlessContentMainDelegate::ZygoteForked() { | 232 void HeadlessContentMainDelegate::ZygoteForked() { |
| 222 const base::CommandLine& command_line( | 233 const base::CommandLine& command_line( |
| 223 *base::CommandLine::ForCurrentProcess()); | 234 *base::CommandLine::ForCurrentProcess()); |
| 224 const std::string process_type = | 235 const std::string process_type = |
| 225 command_line.GetSwitchValueASCII(switches::kProcessType); | 236 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 226 // Unconditionally try to turn on crash reporting since we do not have access | 237 // Unconditionally try to turn on crash reporting since we do not have access |
| 227 // to the latest browser options at this point when testing. Breakpad will | 238 // to the latest browser options at this point when testing. Breakpad will |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 if (!base::PathExists(pak_file)) | 279 if (!base::PathExists(pak_file)) |
| 269 pak_file = dir_module.Append(FILE_PATH_LITERAL("Resources/resources.pak")); | 280 pak_file = dir_module.Append(FILE_PATH_LITERAL("Resources/resources.pak")); |
| 270 #endif | 281 #endif |
| 271 ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 282 ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 272 pak_file, ui::SCALE_FACTOR_NONE); | 283 pak_file, ui::SCALE_FACTOR_NONE); |
| 273 #endif | 284 #endif |
| 274 } | 285 } |
| 275 | 286 |
| 276 content::ContentBrowserClient* | 287 content::ContentBrowserClient* |
| 277 HeadlessContentMainDelegate::CreateContentBrowserClient() { | 288 HeadlessContentMainDelegate::CreateContentBrowserClient() { |
| 289 #if defined(CHROME_MULTIPLE_DLL_CHILD) |
| 290 return nullptr; |
| 291 #else |
| 278 browser_client_ = | 292 browser_client_ = |
| 279 base::MakeUnique<HeadlessContentBrowserClient>(browser_.get()); | 293 base::MakeUnique<HeadlessContentBrowserClient>(browser_.get()); |
| 280 return browser_client_.get(); | 294 return browser_client_.get(); |
| 295 #endif |
| 281 } | 296 } |
| 282 | 297 |
| 283 content::ContentRendererClient* | 298 content::ContentRendererClient* |
| 284 HeadlessContentMainDelegate::CreateContentRendererClient() { | 299 HeadlessContentMainDelegate::CreateContentRendererClient() { |
| 300 #if defined(CHROME_MULTIPLE_DLL_BROWSER) |
| 301 return nullptr; |
| 302 #else |
| 285 renderer_client_ = base::MakeUnique<HeadlessContentRendererClient>(); | 303 renderer_client_ = base::MakeUnique<HeadlessContentRendererClient>(); |
| 286 return renderer_client_.get(); | 304 return renderer_client_.get(); |
| 305 #endif |
| 287 } | 306 } |
| 288 | 307 |
| 289 } // namespace headless | 308 } // namespace headless |
| OLD | NEW |