1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
headless / lib / browser / headless_browser_impl.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "headless/lib/browser/headless_browser_impl.h"
#include <string>
#include <utility>
#include <vector>
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "components/embedder_support/user_agent_utils.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/user_agent.h"
#include "headless/lib/browser/headless_browser_context_impl.h"
#include "headless/lib/browser/headless_web_contents_impl.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#if BUILDFLAG(IS_WIN)
#include "base/command_line.h"
#include "headless/public/switches.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
#endif
#if defined(HEADLESS_USE_PREFS)
#include "components/os_crypt/sync/os_crypt.h" // nogncheck
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_service_factory.h"
#endif
#if defined(HEADLESS_USE_POLICY)
#include "components/headless/policy/headless_mode_policy.h" // nogncheck
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "headless/lib/browser/policy/headless_policies.h"
#endif
#if defined(HEADLESS_SUPPORT_FIELD_TRIALS)
#include "components/metrics/metrics_service.h" // nogncheck
#include "components/variations/service/variations_service.h" // nogncheck
#endif
namespace headless {
namespace {
// Product name for building the default user agent string.
const char kHeadlessProductName[] = "HeadlessChrome";
#if defined(HEADLESS_USE_PREFS)
const base::FilePath::CharType kLocalStateFilename[] =
FILE_PATH_LITERAL("Local State");
#endif
} // namespace
HeadlessBrowser::Options::Options()
: user_agent(content::BuildUserAgentFromProduct(
HeadlessBrowser::GetProductNameAndVersion())) {}
HeadlessBrowser::Options::Options(Options&& options) = default;
HeadlessBrowser::Options::~Options() = default;
HeadlessBrowser::Options& HeadlessBrowser::Options::operator=(
Options&& options) = default;
bool HeadlessBrowser::Options::DevtoolsServerEnabled() {
return (devtools_pipe_enabled || devtools_port.has_value());
}
/// static
std::string HeadlessBrowser::GetProductNameAndVersion() {
return std::string(kHeadlessProductName) + "/" + PRODUCT_VERSION;
}
/// static
blink::UserAgentMetadata HeadlessBrowser::GetUserAgentMetadata() {
auto metadata = embedder_support::GetUserAgentMetadata(nullptr);
// Skip override brand version information if components' API returns a blank
// UserAgentMetadata.
if (metadata == blink::UserAgentMetadata()) {
return metadata;
}
std::string significant_version = version_info::GetMajorVersionNumber();
constexpr bool kEnableUpdatedGreaseByPolicy = true;
// Use the major version number as a greasing seed
int seed = 1;
bool got_seed = base::StringToInt(significant_version, &seed);
DCHECK(got_seed);
// Rengenerate the brand version lists with kHeadlessProductName.
metadata.brand_version_list = embedder_support::GenerateBrandVersionList(
seed, kHeadlessProductName, significant_version,
kEnableUpdatedGreaseByPolicy,
blink::UserAgentBrandVersionType::kMajorVersion);
metadata.brand_full_version_list = embedder_support::GenerateBrandVersionList(
seed, kHeadlessProductName, metadata.full_version,
kEnableUpdatedGreaseByPolicy,
blink::UserAgentBrandVersionType::kFullVersion);
return metadata;
}
HeadlessBrowserImpl::HeadlessBrowserImpl(
base::OnceCallback<void(HeadlessBrowser*)> on_start_callback)
: on_start_callback_(std::move(on_start_callback)) {}
HeadlessBrowserImpl::~HeadlessBrowserImpl() = default;
void HeadlessBrowserImpl::SetOptions(HeadlessBrowser::Options options) {
options_ = std::move(options);
}
HeadlessBrowserContext::Builder
HeadlessBrowserImpl::CreateBrowserContextBuilder() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return HeadlessBrowserContext::Builder(this);
}
scoped_refptr<base::SingleThreadTaskRunner>
HeadlessBrowserImpl::BrowserMainThread() const {
return content::GetUIThreadTaskRunner({});
}
void HeadlessBrowserImpl::Shutdown() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
weak_ptr_factory_.InvalidateWeakPtrs();
// Make sure GetAllBrowserContexts is sane if called after this point.
auto tmp = std::move(browser_contexts_);
tmp.clear();
if (system_request_context_manager_) {
content::GetIOThreadTaskRunner({})->DeleteSoon(
FROM_HERE, system_request_context_manager_.release());
}
// We might have posted task during shutdown, let these run
// before quitting the message loop. See ~HeadlessWebContentsImpl
// for additional context.
if (quit_main_message_loop_) {
BrowserMainThread()->PostTask(FROM_HERE,
std::move(quit_main_message_loop_));
}
}
void HeadlessBrowserImpl::ShutdownWithExitCode(int exit_code) {
exit_code_ = exit_code;
Shutdown();
}
std::vector<HeadlessBrowserContext*>
HeadlessBrowserImpl::GetAllBrowserContexts() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::vector<HeadlessBrowserContext*> result;
result.reserve(browser_contexts_.size());
for (const auto& browser_context_pair : browser_contexts_) {
result.push_back(browser_context_pair.second.get());
}
return result;
}
HeadlessBrowserContext* HeadlessBrowserImpl::CreateBrowserContext(
HeadlessBrowserContext::Builder* builder) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto browser_context = HeadlessBrowserContextImpl::Create(builder);
HeadlessBrowserContext* result = browser_context.get();
browser_contexts_[browser_context->Id()] = std::move(browser_context);
return result;
}
void HeadlessBrowserImpl::DestroyBrowserContext(
HeadlessBrowserContextImpl* browser_context) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
int erased = browser_contexts_.erase(browser_context->Id());
DCHECK(erased);
if (default_browser_context_ == browser_context)
SetDefaultBrowserContext(nullptr);
}
void HeadlessBrowserImpl::SetDefaultBrowserContext(
HeadlessBrowserContext* browser_context) {
DCHECK(!browser_context ||
this == HeadlessBrowserContextImpl::From(browser_context)->browser());
default_browser_context_ = browser_context;
if (default_browser_context_ && !system_request_context_manager_) {
system_request_context_manager_ =
HeadlessRequestContextManager::CreateSystemContext(
HeadlessBrowserContextImpl::From(browser_context)->options());
}
}
HeadlessBrowserContext* HeadlessBrowserImpl::GetDefaultBrowserContext() {
return default_browser_context_;
}
base::WeakPtr<HeadlessBrowserImpl> HeadlessBrowserImpl::GetWeakPtr() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return weak_ptr_factory_.GetWeakPtr();
}
HeadlessWebContentsImpl* HeadlessBrowserImpl::GetWebContentsForWindowId(
const int window_id) {
for (HeadlessBrowserContext* context : GetAllBrowserContexts()) {
for (HeadlessWebContents* web_contents : context->GetAllWebContents()) {
auto* contents = HeadlessWebContentsImpl::From(web_contents);
if (contents->window_id() == window_id) {
return contents;
}
}
}
return nullptr;
}
HeadlessBrowserContext* HeadlessBrowserImpl::GetBrowserContextForId(
const std::string& id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto find_it = browser_contexts_.find(id);
if (find_it == browser_contexts_.end())
return nullptr;
return find_it->second.get();
}
bool HeadlessBrowserImpl::ShouldStartDevToolsServer() {
if (!options()->DevtoolsServerEnabled()) {
return false;
}
#if defined(HEADLESS_USE_POLICY)
CHECK(local_state_);
if (!IsRemoteDebuggingAllowed(local_state_.get())) {
// Follow content/browser/devtools/devtools_http_handler.cc that reports its
// remote debugging port on stderr for symmetry.
fputs("\nDevTools remote debugging is disallowed by the system admin.\n",
stderr);
fflush(stderr);
return false;
}
#endif
return true;
}
void HeadlessBrowserImpl::PreMainMessageLoopRun() {
PlatformInitialize();
// We don't support the tethering domain on this agent host.
agent_host_ = content::DevToolsAgentHost::CreateForBrowser(
nullptr, content::DevToolsAgentHost::CreateServerSocketCallback());
PlatformStart();
std::move(on_start_callback_).Run(this);
}
void HeadlessBrowserImpl::WillRunMainMessageLoop(base::RunLoop& run_loop) {
quit_main_message_loop_ = run_loop.QuitClosure();
}
void HeadlessBrowserImpl::PostMainMessageLoopRun() {
#if defined(HEADLESS_USE_PREFS)
if (local_state_) {
local_state_->CommitPendingWrite();
local_state_.reset(nullptr);
}
#endif
#if defined(HEADLESS_USE_POLICY)
if (policy_connector_) {
policy_connector_->Shutdown();
policy_connector_.reset(nullptr);
}
#endif
}
#if defined(HEADLESS_USE_POLICY)
policy::PolicyService* HeadlessBrowserImpl::GetPolicyService() {
return policy_connector_ ? policy_connector_->GetPolicyService() : nullptr;
}
#endif
#if defined(HEADLESS_USE_PREFS)
void HeadlessBrowserImpl::CreatePrefService() {
CHECK(!local_state_);
scoped_refptr<PersistentPrefStore> pref_store;
if (options()->user_data_dir.empty()) {
pref_store = base::MakeRefCounted<InMemoryPrefStore>();
} else {
base::FilePath local_state_file =
options()->user_data_dir.Append(kLocalStateFilename);
pref_store = base::MakeRefCounted<JsonPrefStore>(
local_state_file,
/*pref_filter=*/nullptr,
/*file_task_runner=*/
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN}),
/*read_only=*/true);
auto result = pref_store->ReadPrefs();
if (result != JsonPrefStore::PREF_READ_ERROR_NONE &&
result != JsonPrefStore::PREF_READ_ERROR_NO_FILE) {
LOG(ERROR) << "Failed to read prefs in '" << local_state_file
<< "', error: " << result;
BrowserMainThread()->PostTask(
FROM_HERE,
base::BindOnce(&HeadlessBrowserImpl::ShutdownWithExitCode,
weak_ptr_factory_.GetWeakPtr(), EXIT_FAILURE));
return;
}
}
auto pref_registry = base::MakeRefCounted<user_prefs::PrefRegistrySyncable>();
#if BUILDFLAG(IS_WIN)
OSCrypt::RegisterLocalPrefs(pref_registry.get());
#endif
#if defined(HEADLESS_SUPPORT_FIELD_TRIALS)
metrics::MetricsService::RegisterPrefs(pref_registry.get());
variations::VariationsService::RegisterPrefs(pref_registry.get());
#endif
PrefServiceFactory factory;
#if defined(HEADLESS_USE_POLICY)
RegisterHeadlessPrefs(pref_registry.get());
policy_connector_ =
std::make_unique<policy::HeadlessBrowserPolicyConnector>();
factory.set_managed_prefs(
policy_connector_->CreatePrefStore(policy::POLICY_LEVEL_MANDATORY));
BrowserContextDependencyManager::GetInstance()
->RegisterProfilePrefsForServices(pref_registry.get());
#endif // defined(HEADLESS_USE_POLICY)
factory.set_user_prefs(pref_store);
local_state_ = factory.Create(std::move(pref_registry));
#if BUILDFLAG(IS_WIN)
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (!command_line->HasSwitch(switches::kDisableCookieEncryption) &&
OSCrypt::InitWithExistingKey(local_state_.get()) != OSCrypt::kSuccess) {
command_line->AppendSwitch(switches::kDisableCookieEncryption);
}
#endif // BUILDFLAG(IS_WIN)
}
PrefService* HeadlessBrowserImpl::GetPrefs() {
return local_state_.get();
}
#endif // defined(HEADLESS_USE_PREFS)
} // namespace headless