Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: headless/lib/headless_content_main_delegate.cc

Issue 1674263002: headless: Initial headless embedder API implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix shutdown memory leak. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_.reset();
82 browser_runner->Shutdown();
83
84 // Return value >=0 here to disable calling content::BrowserMain.
85 return 0;
86 }
87
88 void HeadlessContentMainDelegate::ZygoteForked() {
89 // TODO(skyostil): Disable the zygote host.
90 }
91
92 // static
93 HeadlessContentMainDelegate* HeadlessContentMainDelegate::GetInstance() {
94 return g_current_headless_content_main_delegate;
95 }
96
97 // static
98 void HeadlessContentMainDelegate::InitializeResourceBundle() {
99 base::FilePath pak_file;
100 bool r = PathService::Get(base::DIR_MODULE, &pak_file);
Ryan Sleevi 2016/02/25 22:04:36 STYLE: "r" is not descriptive enough.
Sami 2016/02/26 18:49:16 Done.
101 DCHECK(r);
102 pak_file = pak_file.Append(FILE_PATH_LITERAL("headless_lib.pak"));
103 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
104 }
105
106 content::ContentBrowserClient*
107 HeadlessContentMainDelegate::CreateContentBrowserClient() {
108 browser_client_.reset(new HeadlessContentBrowserClient(browser_.get()));
109 return browser_client_.get();
110 }
111
112 content::ContentRendererClient*
113 HeadlessContentMainDelegate::CreateContentRendererClient() {
114 renderer_client_.reset(new HeadlessContentRendererClient);
115 return renderer_client_.get();
116 }
117
118 content::ContentUtilityClient*
119 HeadlessContentMainDelegate::CreateContentUtilityClient() {
120 utility_client_.reset(new HeadlessContentUtilityClient);
121 return utility_client_.get();
122 }
123
124 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698