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