OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/headless_content_main_delegate.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/trace_event/trace_event.h" |
| 11 #include "content/public/browser/browser_main_runner.h" |
| 12 #include "content/public/common/content_switches.h" |
| 13 #include "headless/lib/browser/headless_content_browser_client.h" |
| 14 #include "headless/lib/headless_browser_impl.h" |
| 15 #include "headless/lib/renderer/headless_content_renderer_client.h" |
| 16 #include "headless/lib/utility/headless_content_utility_client.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/ozone/public/ozone_switches.h" |
| 19 |
| 20 namespace headless { |
| 21 namespace { |
| 22 // Keep in sync with content/common/content_constants_internal.h. |
| 23 const int kTraceEventBrowserProcessSortIndex = -6; |
| 24 |
| 25 HeadlessContentMainDelegate* g_current_headless_content_main_delegate = nullptr; |
| 26 } // namespace |
| 27 |
| 28 HeadlessContentMainDelegate::HeadlessContentMainDelegate( |
| 29 scoped_ptr<HeadlessBrowserImpl> browser) |
| 30 : content_client_(browser->options()), browser_(std::move(browser)) { |
| 31 DCHECK(!g_current_headless_content_main_delegate); |
| 32 g_current_headless_content_main_delegate = this; |
| 33 } |
| 34 |
| 35 HeadlessContentMainDelegate::~HeadlessContentMainDelegate() { |
| 36 DCHECK(g_current_headless_content_main_delegate == this); |
| 37 g_current_headless_content_main_delegate = nullptr; |
| 38 } |
| 39 |
| 40 bool HeadlessContentMainDelegate::BasicStartupComplete(int* exit_code) { |
| 41 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); |
| 42 |
| 43 command_line.AppendSwitch(switches::kNoSandbox); |
| 44 command_line.AppendSwitch(switches::kSingleProcess); |
| 45 |
| 46 // The headless backend is automatically chosen for a headless build, but also |
| 47 // adding it here allows us to run in a non-headless build too. |
| 48 command_line.AppendSwitchASCII(switches::kOzonePlatform, "headless"); |
| 49 |
| 50 // TODO(skyostil): Investigate using Mesa/SwiftShader for output. |
| 51 command_line.AppendSwitch(switches::kDisableGpu); |
| 52 |
| 53 SetContentClient(&content_client_); |
| 54 return false; |
| 55 } |
| 56 |
| 57 void HeadlessContentMainDelegate::PreSandboxStartup() { |
| 58 InitializeResourceBundle(); |
| 59 } |
| 60 |
| 61 int HeadlessContentMainDelegate::RunProcess( |
| 62 const std::string& process_type, |
| 63 const content::MainFunctionParams& main_function_params) { |
| 64 if (!process_type.empty()) |
| 65 return -1; |
| 66 |
| 67 base::trace_event::TraceLog::GetInstance()->SetProcessName("HeadlessBrowser"); |
| 68 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( |
| 69 kTraceEventBrowserProcessSortIndex); |
| 70 |
| 71 scoped_ptr<content::BrowserMainRunner> browser_runner_( |
| 72 content::BrowserMainRunner::Create()); |
| 73 |
| 74 int exit_code = browser_runner_->Initialize(main_function_params); |
| 75 DCHECK_LT(exit_code, 0) << "content::BrowserMainRunner::Initialize failed in " |
| 76 "HeadlessContentMainDelegate::RunProcess"; |
| 77 |
| 78 browser_->RunOnStartCallback(); |
| 79 browser_runner_->Run(); |
| 80 browser_runner_->Shutdown(); |
| 81 |
| 82 // Return value >=0 here to disable calling content::BrowserMain. |
| 83 return 0; |
| 84 } |
| 85 |
| 86 void HeadlessContentMainDelegate::ZygoteForked() { |
| 87 // TODO(skyostil): Disable the zygote host. |
| 88 } |
| 89 |
| 90 // static |
| 91 HeadlessContentMainDelegate* HeadlessContentMainDelegate::GetInstance() { |
| 92 return g_current_headless_content_main_delegate; |
| 93 } |
| 94 |
| 95 // static |
| 96 void HeadlessContentMainDelegate::InitializeResourceBundle() { |
| 97 base::FilePath pak_file; |
| 98 bool r = PathService::Get(base::DIR_MODULE, &pak_file); |
| 99 DCHECK(r); |
| 100 pak_file = pak_file.Append(FILE_PATH_LITERAL("content_shell.pak")); |
| 101 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file); |
| 102 } |
| 103 |
| 104 content::ContentBrowserClient* |
| 105 HeadlessContentMainDelegate::CreateContentBrowserClient() { |
| 106 browser_client_.reset(new HeadlessContentBrowserClient(browser_.get())); |
| 107 return browser_client_.get(); |
| 108 } |
| 109 |
| 110 content::ContentRendererClient* |
| 111 HeadlessContentMainDelegate::CreateContentRendererClient() { |
| 112 renderer_client_.reset(new HeadlessContentRendererClient); |
| 113 return renderer_client_.get(); |
| 114 } |
| 115 |
| 116 content::ContentUtilityClient* |
| 117 HeadlessContentMainDelegate::CreateContentUtilityClient() { |
| 118 utility_client_.reset(new HeadlessContentUtilityClient); |
| 119 return utility_client_.get(); |
| 120 } |
| 121 |
| 122 } // namespace headless |
OLD | NEW |