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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
content / browser / devtools / protocol / browser_handler.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/protocol/browser_handler.h"
#include <string.h>
#include <algorithm>
#include <memory>
#include "base/command_line.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/download/public/common/download_item.h"
#include "content/browser/devtools/browser_devtools_agent_host.h"
#include "content/browser/devtools/devtools_manager.h"
#include "content/browser/devtools/protocol/devtools_download_manager_delegate.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/permissions/permission_controller_impl.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/user_agent.h"
#include "net/base/filename_util.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "url/gurl.h"
#include "v8/include/v8-version-string.h"
using blink::PermissionType;
namespace content {
namespace protocol {
BrowserHandler::BrowserHandler(bool allow_set_download_behavior)
: DevToolsDomainHandler(Browser::Metainfo::domainName),
download_events_enabled_(false),
allow_set_download_behavior_(allow_set_download_behavior) {}
BrowserHandler::~BrowserHandler() = default;
Response BrowserHandler::Disable() {
// TODO: this leaks context ids for all contexts with overridden permissions.
for (auto& browser_context_id : contexts_with_overridden_permissions_) {
content::BrowserContext* browser_context = nullptr;
std::string error;
Maybe<std::string> context_id =
browser_context_id == "" ? Maybe<std::string>()
: Maybe<std::string>(browser_context_id);
FindBrowserContext(context_id, &browser_context);
if (browser_context) {
PermissionControllerImpl* permission_controller =
PermissionControllerImpl::FromBrowserContext(browser_context);
permission_controller->ResetOverridesForDevTools();
}
}
contexts_with_overridden_permissions_.clear();
// TODO: this leaks context ids for all contexts with overridden downloads.
for (auto& browser_context_id : contexts_with_overridden_downloads_) {
content::BrowserContext* browser_context = nullptr;
std::string error;
Maybe<std::string> context_id =
browser_context_id == "" ? Maybe<std::string>()
: Maybe<std::string>(browser_context_id);
FindBrowserContext(context_id, &browser_context);
if (browser_context) {
auto* delegate =
DevToolsDownloadManagerDelegate::GetInstance(browser_context);
if (delegate) {
delegate->set_download_behavior(
DevToolsDownloadManagerDelegate::DownloadBehavior::DEFAULT);
}
}
}
contexts_with_overridden_downloads_.clear();
SetDownloadEventsEnabled(false);
histograms_snapshots_.clear();
return Response::Success();
}
void BrowserHandler::Wire(UberDispatcher* dispatcher) {
frontend_ = std::make_unique<Browser::Frontend>(dispatcher->channel());
Browser::Dispatcher::wire(dispatcher, this);
}
Response BrowserHandler::GetVersion(std::string* protocol_version,
std::string* product,
std::string* revision,
std::string* user_agent,
std::string* js_version) {
*protocol_version = DevToolsAgentHost::GetProtocolVersion();
*revision = GetChromiumGitRevision();
*product = GetContentClient()->browser()->GetProduct();
*user_agent = GetContentClient()->browser()->GetUserAgent();
*js_version = V8_VERSION_STRING;
return Response::Success();
}
namespace {
// Parses PermissionDescriptors (|descriptor|) into their appropriate
// PermissionType |permission_type| by duplicating the logic in the methods
// //third_party/blink/renderer/modules/permissions:permissions
// ::ParsePermission and
// //content/browser/permissions:permission_service_impl
// ::PermissionDescriptorToPermissionType, producing an error in
// |error_message| as necessary.
// TODO(crbug.com/40638575): De-duplicate this logic.
Response PermissionDescriptorToPermissionType(
std::unique_ptr<protocol::Browser::PermissionDescriptor> descriptor,
PermissionType* permission_type) {
const std::string name = descriptor->GetName();
if (name == "geolocation") {
*permission_type = PermissionType::GEOLOCATION;
} else if (name == "camera") {
if (descriptor->GetPanTiltZoom(false))
*permission_type = PermissionType::CAMERA_PAN_TILT_ZOOM;
else
*permission_type = PermissionType::VIDEO_CAPTURE;
} else if (name == "microphone") {
*permission_type = PermissionType::AUDIO_CAPTURE;
} else if (name == "notifications") {
*permission_type = PermissionType::NOTIFICATIONS;
} else if (name == "persistent-storage") {
*permission_type = PermissionType::DURABLE_STORAGE;
} else if (name == "push") {
if (!descriptor->GetUserVisibleOnly(false)) {
return Response::InvalidParams(
"Push Permission without userVisibleOnly:true isn't supported");
}
*permission_type = PermissionType::NOTIFICATIONS;
} else if (name == "midi") {
if (descriptor->GetSysex(false))
*permission_type = PermissionType::MIDI_SYSEX;
else
*permission_type = PermissionType::MIDI;
} else if (name == "background-sync") {
*permission_type = PermissionType::BACKGROUND_SYNC;
} else if (name == "ambient-light-sensor" || name == "accelerometer" ||
name == "gyroscope" || name == "magnetometer") {
*permission_type = PermissionType::SENSORS;
} else if (name == "clipboard-read") {
*permission_type = PermissionType::CLIPBOARD_READ_WRITE;
} else if (name == "clipboard-write") {
if (descriptor->GetAllowWithoutSanitization(false))
*permission_type = PermissionType::CLIPBOARD_READ_WRITE;
else
*permission_type = PermissionType::CLIPBOARD_SANITIZED_WRITE;
} else if (name == "payment-handler") {
*permission_type = PermissionType::PAYMENT_HANDLER;
} else if (name == "background-fetch") {
*permission_type = PermissionType::BACKGROUND_FETCH;
} else if (name == "idle-detection") {
*permission_type = PermissionType::IDLE_DETECTION;
} else if (name == "periodic-background-sync") {
*permission_type = PermissionType::PERIODIC_BACKGROUND_SYNC;
} else if (name == "screen-wake-lock") {
*permission_type = PermissionType::WAKE_LOCK_SCREEN;
} else if (name == "system-wake-lock") {
*permission_type = PermissionType::WAKE_LOCK_SYSTEM;
} else if (name == "nfc") {
*permission_type = PermissionType::NFC;
} else if (name == "window-management") {
*permission_type = PermissionType::WINDOW_MANAGEMENT;
} else if (name == "local-fonts") {
*permission_type = PermissionType::LOCAL_FONTS;
} else if (name == "display-capture") {
*permission_type = PermissionType::DISPLAY_CAPTURE;
} else if (name == "storage-access") {
*permission_type = PermissionType::STORAGE_ACCESS_GRANT;
} else if (name == "top-level-storage-access") {
*permission_type = PermissionType::TOP_LEVEL_STORAGE_ACCESS;
} else if (name == "captured-surface-control") {
*permission_type = PermissionType::CAPTURED_SURFACE_CONTROL;
} else if (name == "speaker-selection") {
*permission_type = PermissionType::SPEAKER_SELECTION;
} else if (name == "keyboard-lock") {
*permission_type = PermissionType::KEYBOARD_LOCK;
} else if (name == "pointer-lock") {
*permission_type = PermissionType::POINTER_LOCK;
} else if (name == "fullscreen") {
if (!descriptor->GetAllowWithoutGesture(false)) {
// There is no PermissionType for fullscreen with user gesture.
return Response::InvalidParams(
"Fullscreen Permission only supports allowWithoutGesture:true");
}
*permission_type = PermissionType::AUTOMATIC_FULLSCREEN;
} else if (name == "web-app-installation") {
*permission_type = PermissionType::WEB_APP_INSTALLATION;
} else {
return Response::InvalidParams("Invalid PermissionDescriptor name: " +
name);
}
return Response::Success();
}
Response FromProtocolPermissionType(
const protocol::Browser::PermissionType& type,
PermissionType* out_type) {
if (type == protocol::Browser::PermissionTypeEnum::Notifications) {
*out_type = PermissionType::NOTIFICATIONS;
} else if (type == protocol::Browser::PermissionTypeEnum::Geolocation) {
*out_type = PermissionType::GEOLOCATION;
} else if (type ==
protocol::Browser::PermissionTypeEnum::ProtectedMediaIdentifier) {
*out_type = PermissionType::PROTECTED_MEDIA_IDENTIFIER;
} else if (type == protocol::Browser::PermissionTypeEnum::Midi) {
*out_type = PermissionType::MIDI;
} else if (type == protocol::Browser::PermissionTypeEnum::MidiSysex) {
*out_type = PermissionType::MIDI_SYSEX;
} else if (type == protocol::Browser::PermissionTypeEnum::DurableStorage) {
*out_type = PermissionType::DURABLE_STORAGE;
} else if (type == protocol::Browser::PermissionTypeEnum::AudioCapture) {
*out_type = PermissionType::AUDIO_CAPTURE;
} else if (type == protocol::Browser::PermissionTypeEnum::VideoCapture) {
*out_type = PermissionType::VIDEO_CAPTURE;
} else if (type ==
protocol::Browser::PermissionTypeEnum::VideoCapturePanTiltZoom) {
*out_type = PermissionType::CAMERA_PAN_TILT_ZOOM;
} else if (type == protocol::Browser::PermissionTypeEnum::BackgroundSync) {
*out_type = PermissionType::BACKGROUND_SYNC;
} else if (type == protocol::Browser::PermissionTypeEnum::Sensors) {
*out_type = PermissionType::SENSORS;
} else if (type ==
protocol::Browser::PermissionTypeEnum::ClipboardReadWrite) {
*out_type = PermissionType::CLIPBOARD_READ_WRITE;
} else if (type ==
protocol::Browser::PermissionTypeEnum::ClipboardSanitizedWrite) {
*out_type = PermissionType::CLIPBOARD_SANITIZED_WRITE;
} else if (type == protocol::Browser::PermissionTypeEnum::PaymentHandler) {
*out_type = PermissionType::PAYMENT_HANDLER;
} else if (type == protocol::Browser::PermissionTypeEnum::BackgroundFetch) {
*out_type = PermissionType::BACKGROUND_FETCH;
} else if (type == protocol::Browser::PermissionTypeEnum::IdleDetection) {
*out_type = PermissionType::IDLE_DETECTION;
} else if (type ==
protocol::Browser::PermissionTypeEnum::PeriodicBackgroundSync) {
*out_type = PermissionType::PERIODIC_BACKGROUND_SYNC;
} else if (type == protocol::Browser::PermissionTypeEnum::WakeLockScreen) {
*out_type = PermissionType::WAKE_LOCK_SCREEN;
} else if (type == protocol::Browser::PermissionTypeEnum::WakeLockSystem) {
*out_type = PermissionType::WAKE_LOCK_SYSTEM;
} else if (type == protocol::Browser::PermissionTypeEnum::Nfc) {
*out_type = PermissionType::NFC;
} else if (type == protocol::Browser::PermissionTypeEnum::WindowManagement) {
*out_type = PermissionType::WINDOW_MANAGEMENT;
} else if (type == protocol::Browser::PermissionTypeEnum::LocalFonts) {
*out_type = PermissionType::LOCAL_FONTS;
} else if (type == protocol::Browser::PermissionTypeEnum::DisplayCapture) {
*out_type = PermissionType::DISPLAY_CAPTURE;
} else if (type == protocol::Browser::PermissionTypeEnum::StorageAccess) {
*out_type = PermissionType::STORAGE_ACCESS_GRANT;
} else if (type ==
protocol::Browser::PermissionTypeEnum::TopLevelStorageAccess) {
*out_type = PermissionType::TOP_LEVEL_STORAGE_ACCESS;
} else if (type ==
protocol::Browser::PermissionTypeEnum::CapturedSurfaceControl) {
*out_type = PermissionType::CAPTURED_SURFACE_CONTROL;
} else if (type == protocol::Browser::PermissionTypeEnum::SpeakerSelection) {
*out_type = PermissionType::SPEAKER_SELECTION;
} else if (type ==
protocol::Browser::PermissionTypeEnum::WebAppInstallation) {
*out_type = PermissionType::WEB_APP_INSTALLATION;
} else {
return Response::InvalidParams("Unknown permission type: " + type);
}
return Response::Success();
}
Response PermissionSettingToPermissionStatus(
const protocol::Browser::PermissionSetting& setting,
blink::mojom::PermissionStatus* out_status) {
if (setting == protocol::Browser::PermissionSettingEnum::Granted) {
*out_status = blink::mojom::PermissionStatus::GRANTED;
} else if (setting == protocol::Browser::PermissionSettingEnum::Denied) {
*out_status = blink::mojom::PermissionStatus::DENIED;
} else if (setting == protocol::Browser::PermissionSettingEnum::Prompt) {
*out_status = blink::mojom::PermissionStatus::ASK;
} else {
return Response::InvalidParams("Unknown permission setting: " + setting);
}
return Response::Success();
}
} // namespace
// static
Response BrowserHandler::FindBrowserContext(
const Maybe<std::string>& browser_context_id,
BrowserContext** browser_context) {
DevToolsManagerDelegate* delegate =
DevToolsManager::GetInstance()->delegate();
if (!delegate)
return Response::ServerError(
"Browser context management is not supported.");
if (!browser_context_id.has_value()) {
*browser_context = delegate->GetDefaultBrowserContext();
if (*browser_context == nullptr)
return Response::ServerError(
"Browser context management is not supported.");
return Response::Success();
}
std::string context_id = browser_context_id.value();
for (auto* context : delegate->GetBrowserContexts()) {
if (context->UniqueId() == context_id) {
*browser_context = context;
return Response::Success();
}
}
return Response::InvalidParams("Failed to find browser context for id " +
context_id);
}
// static
std::vector<BrowserHandler*> BrowserHandler::ForAgentHost(
DevToolsAgentHostImpl* host) {
return host->HandlersByName<BrowserHandler>(Browser::Metainfo::domainName);
}
Response BrowserHandler::SetPermission(
std::unique_ptr<protocol::Browser::PermissionDescriptor> permission,
const protocol::Browser::PermissionSetting& setting,
Maybe<std::string> origin,
Maybe<std::string> browser_context_id) {
BrowserContext* browser_context = nullptr;
Response response = FindBrowserContext(browser_context_id, &browser_context);
if (!response.IsSuccess())
return response;
PermissionType type;
Response parse_response =
PermissionDescriptorToPermissionType(std::move(permission), &type);
if (!parse_response.IsSuccess())
return parse_response;
blink::mojom::PermissionStatus permission_status;
Response setting_response =
PermissionSettingToPermissionStatus(setting, &permission_status);
if (!setting_response.IsSuccess())
return setting_response;
PermissionControllerImpl* permission_controller =
PermissionControllerImpl::FromBrowserContext(browser_context);
std::optional<url::Origin> overridden_origin;
if (origin.has_value()) {
overridden_origin = url::Origin::Create(GURL(origin.value()));
if (overridden_origin->opaque())
return Response::InvalidParams(
"Permission can't be granted to opaque origins.");
}
PermissionControllerImpl::OverrideStatus status =
permission_controller->SetOverrideForDevTools(overridden_origin, type,
permission_status);
if (status != PermissionControllerImpl::OverrideStatus::kOverrideSet) {
return Response::InvalidParams(
"Permission can't be granted in current context.");
}
contexts_with_overridden_permissions_.insert(
browser_context_id.value_or(std::string()));
return Response::Success();
}
Response BrowserHandler::GrantPermissions(
std::unique_ptr<protocol::Array<protocol::Browser::PermissionType>>
permissions,
Maybe<std::string> origin,
Maybe<std::string> browser_context_id) {
BrowserContext* browser_context = nullptr;
Response response = FindBrowserContext(browser_context_id, &browser_context);
if (!response.IsSuccess())
return response;
std::vector<PermissionType> internal_permissions;
internal_permissions.reserve(permissions->size());
for (const protocol::Browser::PermissionType& t : *permissions) {
PermissionType type;
Response type_response = FromProtocolPermissionType(t, &type);
if (!type_response.IsSuccess())
return type_response;
internal_permissions.push_back(type);
}
PermissionControllerImpl* permission_controller =
PermissionControllerImpl::FromBrowserContext(browser_context);
std::optional<url::Origin> overridden_origin;
if (origin.has_value()) {
overridden_origin = url::Origin::Create(GURL(origin.value()));
if (overridden_origin->opaque())
return Response::InvalidParams(
"Permission can't be granted to opaque origins.");
}
PermissionControllerImpl::OverrideStatus status =
permission_controller->GrantOverridesForDevTools(overridden_origin,
internal_permissions);
if (status != PermissionControllerImpl::OverrideStatus::kOverrideSet) {
return Response::InvalidParams(
"Permissions can't be granted in current context.");
}
contexts_with_overridden_permissions_.insert(browser_context_id.value_or(""));
return Response::Success();
}
Response BrowserHandler::ResetPermissions(
Maybe<std::string> browser_context_id) {
BrowserContext* browser_context = nullptr;
Response response = FindBrowserContext(browser_context_id, &browser_context);
if (!response.IsSuccess())
return response;
PermissionControllerImpl* permission_controller =
PermissionControllerImpl::FromBrowserContext(browser_context);
permission_controller->ResetOverridesForDevTools();
contexts_with_overridden_permissions_.erase(browser_context_id.value_or(""));
return Response::Success();
}
Response BrowserHandler::SetDownloadBehavior(
const std::string& behavior,
Maybe<std::string> browser_context_id,
Maybe<std::string> download_path,
Maybe<bool> events_enabled) {
BrowserContext* browser_context = nullptr;
Response response = FindBrowserContext(browser_context_id, &browser_context);
if (!response.IsSuccess())
return response;
response = DoSetDownloadBehavior(behavior, browser_context,
std::move(download_path));
if (!response.IsSuccess())
return response;
SetDownloadEventsEnabled(events_enabled.value_or(false));
return response;
}
Response BrowserHandler::DoSetDownloadBehavior(
const std::string& behavior,
BrowserContext* browser_context,
Maybe<std::string> download_path) {
if (!allow_set_download_behavior_)
return Response::ServerError("Not allowed");
if (behavior == Browser::SetDownloadBehavior::BehaviorEnum::Allow &&
!download_path.has_value()) {
return Response::InvalidParams("downloadPath not provided");
}
DevToolsManagerDelegate* manager_delegate =
DevToolsManager::GetInstance()->delegate();
if (!manager_delegate) {
return Response::ServerError(
"Browser context management is not supported.");
}
auto* delegate =
DevToolsDownloadManagerDelegate::GetOrCreateInstance(browser_context);
if (behavior == Browser::SetDownloadBehavior::BehaviorEnum::Allow) {
delegate->set_download_behavior(
DevToolsDownloadManagerDelegate::DownloadBehavior::ALLOW);
delegate->set_download_path(download_path.value());
} else if (behavior ==
Browser::SetDownloadBehavior::BehaviorEnum::AllowAndName) {
delegate->set_download_behavior(
DevToolsDownloadManagerDelegate::DownloadBehavior::ALLOW_AND_NAME);
delegate->set_download_path(download_path.value());
} else if (behavior == Browser::SetDownloadBehavior::BehaviorEnum::Deny) {
delegate->set_download_behavior(
DevToolsDownloadManagerDelegate::DownloadBehavior::DENY);
} else {
delegate->set_download_behavior(
DevToolsDownloadManagerDelegate::DownloadBehavior::DEFAULT);
}
contexts_with_overridden_downloads_.insert(
manager_delegate->GetDefaultBrowserContext() == browser_context
? ""
: browser_context->UniqueId());
return Response::Success();
}
Response BrowserHandler::CancelDownload(const std::string& guid,
Maybe<std::string> browser_context_id) {
BrowserContext* browser_context = nullptr;
Response response = FindBrowserContext(browser_context_id, &browser_context);
if (!response.IsSuccess())
return response;
auto* delegate =
DevToolsDownloadManagerDelegate::GetOrCreateInstance(browser_context);
auto* download_item = delegate->GetDownloadByGuid(guid);
if (!download_item)
return Response::InvalidParams("No download item found for the given GUID");
// DownloadItem::Cancel is implemented in a soft way, where there would be no
// error triggered if the state is not suitable for cancallation (e.g.
// already cancelled or finished).
download_item->Cancel(true);
return Response::Success();
}
Response BrowserHandler::GetHistograms(
const Maybe<std::string> in_query,
const Maybe<bool> in_delta,
std::unique_ptr<Array<Browser::Histogram>>* const out_histograms) {
DCHECK(out_histograms);
bool get_deltas = in_delta.value_or(false);
*out_histograms = std::make_unique<Array<Browser::Histogram>>();
for (base::HistogramBase* const h :
base::StatisticsRecorder::Sort(base::StatisticsRecorder::WithName(
base::StatisticsRecorder::GetHistograms(), in_query.value_or("")))) {
DCHECK(h);
(*out_histograms)->emplace_back(GetHistogramData(*h, get_deltas));
}
return Response::Success();
}
Response BrowserHandler::GetHistogram(
const std::string& in_name,
const Maybe<bool> in_delta,
std::unique_ptr<Browser::Histogram>* const out_histogram) {
// Get histogram by name.
base::HistogramBase* const in_histogram =
base::StatisticsRecorder::FindHistogram(in_name);
if (!in_histogram)
return Response::InvalidParams("Cannot find histogram: " + in_name);
DCHECK(out_histogram);
*out_histogram = GetHistogramData(*in_histogram, in_delta.value_or(false));
return Response::Success();
}
Response BrowserHandler::GetBrowserCommandLine(
std::unique_ptr<protocol::Array<std::string>>* arguments) {
*arguments = std::make_unique<protocol::Array<std::string>>();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
// The commandline is potentially sensitive, only return it if it
// contains kEnableAutomation.
if (command_line->HasSwitch(switches::kEnableAutomation)) {
for (const auto& arg : command_line->argv()) {
#if BUILDFLAG(IS_WIN)
(*arguments)->emplace_back(base::WideToUTF8(arg));
#else
(*arguments)->emplace_back(arg);
#endif
}
return Response::Success();
} else {
return Response::ServerError(
"Command line not returned because --enable-automation not set.");
}
}
Response BrowserHandler::Crash() {
CHECK(false);
return Response::Success();
}
Response BrowserHandler::CrashGpuProcess() {
auto* host = GpuProcessHost::Get();
if (host) {
host->gpu_service()->Crash();
}
return Response::Success();
}
void BrowserHandler::OnDownloadUpdated(download::DownloadItem* item) {
std::string state;
switch (item->GetState()) {
case download::DownloadItem::IN_PROGRESS:
state = Browser::DownloadProgress::StateEnum::InProgress;
break;
case download::DownloadItem::COMPLETE:
state = Browser::DownloadProgress::StateEnum::Completed;
break;
case download::DownloadItem::CANCELLED:
case download::DownloadItem::INTERRUPTED:
state = Browser::DownloadProgress::StateEnum::Canceled;
break;
case download::DownloadItem::MAX_DOWNLOAD_STATE:
NOTREACHED_IN_MIGRATION();
}
frontend_->DownloadProgress(item->GetGuid(), item->GetTotalBytes(),
item->GetReceivedBytes(), state);
if (state != Browser::DownloadProgress::StateEnum::InProgress) {
item->RemoveObserver(this);
pending_downloads_.erase(item);
}
}
void BrowserHandler::OnDownloadDestroyed(download::DownloadItem* item) {
pending_downloads_.erase(item);
}
void BrowserHandler::DownloadWillBegin(FrameTreeNode* ftn,
download::DownloadItem* item) {
if (!download_events_enabled_)
return;
const std::u16string likely_filename = net::GetSuggestedFilename(
item->GetURL(), item->GetContentDisposition(), std::string(),
item->GetSuggestedFilename(), item->GetMimeType(), "download");
frontend_->DownloadWillBegin(
ftn->current_frame_host()->devtools_frame_token().ToString(),
item->GetGuid(), item->GetURL().spec(),
base::UTF16ToUTF8(likely_filename));
item->AddObserver(this);
pending_downloads_.insert(item);
}
void BrowserHandler::SetDownloadEventsEnabled(bool enabled) {
if (!enabled) {
for (download::DownloadItem* item : pending_downloads_) {
item->RemoveObserver(this);
}
pending_downloads_.clear();
}
download_events_enabled_ = enabled;
}
std::unique_ptr<Browser::Histogram> BrowserHandler::GetHistogramData(
const base::HistogramBase& histogram,
bool get_delta) {
std::unique_ptr<base::HistogramSamples> data = histogram.SnapshotSamples();
std::unique_ptr<base::HistogramSamples> previous_data;
if (get_delta) {
auto it = histograms_snapshots_.find(histogram.histogram_name());
if (it != histograms_snapshots_.end()) {
previous_data = std::move(it->second);
data->Subtract(*previous_data);
}
}
auto out_buckets = std::make_unique<Array<Browser::Bucket>>();
for (const std::unique_ptr<base::SampleCountIterator> it = data->Iterator();
!it->Done(); it->Next()) {
base::HistogramBase::Count count;
base::HistogramBase::Sample low;
int64_t high;
it->Get(&low, &high, &count);
out_buckets->emplace_back(Browser::Bucket::Create()
.SetLow(low)
.SetHigh(high)
.SetCount(count)
.Build());
}
auto result = Browser::Histogram::Create()
.SetName(histogram.histogram_name())
.SetSum(data->sum())
.SetCount(data->TotalCount())
.SetBuckets(std::move(out_buckets))
.Build();
// Keep track of the data we returned for future delta requests.
if (get_delta) {
if (previous_data) {
// If we had subtracted previous data, re-add it to get the full snapshot.
data->Add(*previous_data);
}
histograms_snapshots_[histogram.histogram_name()] = std::move(data);
}
return result;
}
} // namespace protocol
} // namespace content