File: testing_browser_process.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (720 lines) | stat: -rw-r--r-- 24,347 bytes parent folder | download | duplicates (3)
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/testing_browser_process.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/test/task_environment.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/download/download_request_limiter.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/media/webrtc/webrtc_log_uploader.h"
#include "chrome/browser/notifications/notification_platform_bridge.h"
#include "chrome/browser/notifications/stub_notification_platform_bridge.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/browser/permissions/chrome_permissions_client.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/resource_coordinator/resource_coordinator_parts.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/serial/serial_policy_allowed_ports.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_browser_process_platform_part.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/embedder_support/origin_trials/origin_trials_settings_storage.h"
#include "components/metrics/metrics_service.h"
#include "components/network_time/network_time_tracker.h"
#include "components/os_crypt/async/browser/test_utils.h"
#include "components/permissions/permissions_client.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/subresource_filter/content/shared/browser/ruleset_service.h"
#include "content/public/browser/network_service_instance.h"
#include "extensions/buildflags/buildflags.h"
#include "media/media_buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "services/device/public/cpp/geolocation/buildflags.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "services/network/test/test_network_quality_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(OS_LEVEL_GEOLOCATION_PERMISSION_SUPPORTED)
#include "services/device/public/cpp/device_features.h"
#include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
#include "services/device/public/cpp/test/fake_geolocation_system_permission_manager.h"
#endif

#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
#include "chrome/browser/background/extensions/background_mode_manager.h"
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/apps/platform_apps/chrome_apps_browser_api_provider.h"
#include "chrome/browser/media_galleries/media_file_system_registry.h"
#include "chrome/browser/ui/apps/chrome_app_window_client.h"
#include "components/storage_monitor/storage_monitor.h"
#include "components/storage_monitor/test_storage_monitor.h"
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
#include "chrome/browser/extensions/chrome_extensions_browser_client.h"
#endif

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/background_printing_manager.h"
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#endif

#if !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/hid/hid_pinned_notification.h"
#include "chrome/browser/usb/usb_pinned_notification.h"
#else
#include "chrome/browser/hid/hid_status_icon.h"
#include "chrome/browser/usb/usb_status_icon.h"
#endif  // BUILDFLAG(IS_CHROMEOS)
#include "components/component_updater/component_updater_service.h"
#include "components/keep_alive_registry/keep_alive_registry.h"
#if !BUILDFLAG(IS_CHROMEOS)
#include "components/enterprise/browser/controller/chrome_browser_cloud_management_controller.h"
#endif  // !BUILDFLAG(IS_CHROMEOS)
#endif  // !BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#endif

#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
#include "chrome/browser/notifications/notification_ui_manager.h"
#endif

// static
TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
  return static_cast<TestingBrowserProcess*>(g_browser_process);
}

// static
void TestingBrowserProcess::CreateInstance() {
  DCHECK(!g_browser_process);
  TestingBrowserProcess* process = new TestingBrowserProcess;
  // Set |g_browser_process| before initializing the TestingBrowserProcess
  // because some members may depend on |g_browser_process| (in particular,
  // ChromeExtensionsBrowserClient).
  g_browser_process = process;
  process->Init();

#if BUILDFLAG(OS_LEVEL_GEOLOCATION_PERMISSION_SUPPORTED)
  if (features::IsOsLevelGeolocationPermissionSupportEnabled()) {
    auto fake_geolocation_system_permission_manager =
        std::make_unique<device::FakeGeolocationSystemPermissionManager>();
    fake_geolocation_system_permission_manager->SetSystemPermission(
        device::LocationSystemPermissionStatus::kAllowed);
    device::GeolocationSystemPermissionManager::SetInstance(
        std::move(fake_geolocation_system_permission_manager));
  }
#endif  // BUILDFLAG(OS_LEVEL_GEOLOCATION_PERMISSION_SUPPORTED)
}

// static
void TestingBrowserProcess::DeleteInstance() {
  // g_browser_process must be null during its own destruction.
  BrowserProcess* browser_process = g_browser_process;
  g_browser_process = nullptr;
  delete browser_process;
}

// static
void TestingBrowserProcess::TearDownAndDeleteInstance() {
  TestingBrowserProcess::DeleteInstance();
}

TestingBrowserProcess::TestingBrowserProcess()
    : testing_local_state_(std::make_unique<TestingPrefServiceSimple>()),
      platform_part_(std::make_unique<TestingBrowserProcessPlatformPart>()),
      os_crypt_async_(os_crypt_async::GetTestOSCryptAsyncForTesting()) {
  RegisterLocalState(testing_local_state_->registry());

  // Observe TaskEnvironment to get a chance to teardown components before
  // ThreadPool is destroyed.
  // In production, BrowserProcess is destroyed while ThreadPool is still
  // active.
  base::test::TaskEnvironment::AddDestructionObserver(this);
}

TestingBrowserProcess::~TestingBrowserProcess() {
  base::test::TaskEnvironment::RemoveDestructionObserver(this);

  // Tear down components for tests that do not have TaskEnvironment.
  MaybeStartTearDown();

#if BUILDFLAG(ENABLE_EXTENSIONS)
  extensions::ExtensionsBrowserClient::Set(nullptr);
  extensions::AppWindowClient::Set(nullptr);
#endif

  if (test_network_connection_tracker_) {
    content::SetNetworkConnectionTrackerForTesting(nullptr);
  }

  // Destroy objects in the same way as BrowserProcessImpl does.
  serial_policy_allowed_ports_.reset();
  testing_local_state_.reset();
  browser_policy_connector_.reset();

  // Destructors for some objects owned by TestingBrowserProcess will use
  // g_browser_process if it is not null, so it must be null before proceeding.
  DCHECK_EQ(static_cast<BrowserProcess*>(nullptr), g_browser_process);
}

void TestingBrowserProcess::Init() {
  features_ = GlobalFeatures::CreateGlobalFeatures();
  features_->Init();

  // Assume locale is initialized to "en" during initialization.
  features_->application_locale_storage()->Set("en");

  // See comment in constructor.
  if (!network::TestNetworkConnectionTracker::HasInstance()) {
    test_network_connection_tracker_ =
        network::TestNetworkConnectionTracker::CreateInstance();
    content::SetNetworkConnectionTrackerForTesting(
        test_network_connection_tracker_.get());
  }

#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
  extensions_browser_client_ =
      std::make_unique<extensions::ChromeExtensionsBrowserClient>();
  extensions_browser_client_->Init();
  extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
#endif  // BUILDFLAG(ENABLE_EXTENSIONS_CORE)

#if BUILDFLAG(ENABLE_EXTENSIONS)
  extensions_browser_client_->AddAPIProvider(
      std::make_unique<chrome_apps::ChromeAppsBrowserAPIProvider>());
  extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance());
#endif

  // Make sure permissions client has been set.
  ChromePermissionsClient::GetInstance();

#if !BUILDFLAG(IS_ANDROID)
  KeepAliveRegistry::GetInstance()->SetIsShuttingDown(false);
#if BUILDFLAG(IS_CHROMEOS)
  hid_system_tray_icon_ = std::make_unique<HidPinnedNotification>();
  usb_system_tray_icon_ = std::make_unique<UsbPinnedNotification>();
#else
  hid_system_tray_icon_ = std::make_unique<HidStatusIcon>();
  usb_system_tray_icon_ = std::make_unique<UsbStatusIcon>();
#endif  // BUILDFLAG(IS_CHROMEOS)
#endif  // !BUILDFLAG(IS_ANDROID)
}

void TestingBrowserProcess::FlushLocalStateAndReply(base::OnceClosure reply) {
  // This could be implemented the same way as in BrowserProcessImpl but it's
  // not currently expected to be used by TestingBrowserProcess users so we
  // don't bother.
  NOTREACHED();
}

void TestingBrowserProcess::EndSession() {
}

metrics_services_manager::MetricsServicesManager*
TestingBrowserProcess::GetMetricsServicesManager() {
  return nullptr;
}

metrics::MetricsService* TestingBrowserProcess::metrics_service() {
  return metrics_service_;
}

embedder_support::OriginTrialsSettingsStorage*
TestingBrowserProcess::GetOriginTrialsSettingsStorage() {
  if (!origin_trials_settings_storage_) {
    origin_trials_settings_storage_ =
        std::make_unique<embedder_support::OriginTrialsSettingsStorage>();
  }
  return origin_trials_settings_storage_.get();
}

SystemNetworkContextManager*
TestingBrowserProcess::system_network_context_manager() {
  return nullptr;
}

scoped_refptr<network::SharedURLLoaderFactory>
TestingBrowserProcess::shared_url_loader_factory() {
  return shared_url_loader_factory_;
}

network::NetworkQualityTracker*
TestingBrowserProcess::network_quality_tracker() {
  if (!test_network_quality_tracker_) {
    test_network_quality_tracker_ =
        std::make_unique<network::TestNetworkQualityTracker>();
  }
  return test_network_quality_tracker_.get();
}

ProfileManager* TestingBrowserProcess::profile_manager() {
  return profile_manager_.get();
}

void TestingBrowserProcess::SetMetricsService(
    metrics::MetricsService* metrics_service) {
  metrics_service_ = metrics_service;
}

void TestingBrowserProcess::SetProfileManager(
    std::unique_ptr<ProfileManager> profile_manager) {
#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
  // NotificationUIManager can contain references to elements in the current
  // ProfileManager. So when we change the ProfileManager (typically during test
  // shutdown) make sure to reset any objects that might maintain references to
  // it.
  notification_ui_manager_.reset();
#endif
  profile_manager_ = std::move(profile_manager);
}

void TestingBrowserProcess::SetVariationsService(
    variations::VariationsService* variations_service) {
  variations_service_ = variations_service;
}

PrefService* TestingBrowserProcess::local_state() {
  return testing_local_state_.get();
}

signin::ActivePrimaryAccountsMetricsRecorder*
TestingBrowserProcess::active_primary_accounts_metrics_recorder() {
  return nullptr;
}

variations::VariationsService* TestingBrowserProcess::variations_service() {
  return variations_service_;
}

StartupData* TestingBrowserProcess::startup_data() {
  return nullptr;
}

policy::ChromeBrowserPolicyConnector*
TestingBrowserProcess::browser_policy_connector() {
  if (!browser_policy_connector_) {
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
    // Make sure that the machine policy directory does not exist so that
    // machine-wide policies do not affect tests.
    // Note that passing false as last argument to OverrideAndCreateIfNeeded
    // means that the directory will not be created.
    // If a test needs to place a file in this directory in the future, we could
    // create a temporary directory and make its path available to tests.
    base::FilePath local_policy_path("/tmp/non/existing/directory");
    EXPECT_TRUE(base::PathService::OverrideAndCreateIfNeeded(
        chrome::DIR_POLICY_FILES, local_policy_path, true, false));
#endif

#if BUILDFLAG(IS_CHROMEOS)
    browser_policy_connector_ =
        std::make_unique<policy::BrowserPolicyConnectorAsh>();
#else
    browser_policy_connector_ =
        std::make_unique<policy::ChromeBrowserPolicyConnector>();
#endif  // BUILDFLAG(IS_CHROMEOS)

    // Note: creating the ChromeBrowserPolicyConnector invokes BrowserThread::
    // GetTaskRunnerForThread(), which initializes a base::LazyInstance of
    // BrowserThreadTaskRunners. However, the threads that these task runners
    // would run tasks on are *also* created lazily and might not exist yet.
    // Creating them requires a MessageLoop, which a test can optionally create
    // and manage itself, so don't do it here.
  }
  return browser_policy_connector_.get();
}

policy::PolicyService* TestingBrowserProcess::policy_service() {
  return browser_policy_connector()->GetPolicyService();
}

IconManager* TestingBrowserProcess::icon_manager() {
  return nullptr;
}

GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
  return nullptr;
}

#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
  return nullptr;
}

void TestingBrowserProcess::set_background_mode_manager_for_test(
    std::unique_ptr<BackgroundModeManager> manager) {
  NOTREACHED();
}
#endif

StatusTray* TestingBrowserProcess::status_tray() {
  return status_tray_.get();
}

safe_browsing::SafeBrowsingService*
TestingBrowserProcess::safe_browsing_service() {
  return sb_service_.get();
}

WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
  return webrtc_log_uploader_.get();
}

subresource_filter::RulesetService*
TestingBrowserProcess::subresource_filter_ruleset_service() {
  return subresource_filter_ruleset_service_.get();
}

subresource_filter::RulesetService*
TestingBrowserProcess::fingerprinting_protection_ruleset_service() {
  return fingerprinting_protection_ruleset_service_.get();
}

BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
  return platform_part_.get();
}

NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
  if (!notification_ui_manager_.get())
    notification_ui_manager_ = NotificationUIManager::Create();
  return notification_ui_manager_.get();
#else
  return nullptr;
#endif
}

NotificationPlatformBridge*
TestingBrowserProcess::notification_platform_bridge() {
  if (!notification_platform_bridge_.get()) {
    notification_platform_bridge_ =
        std::make_unique<StubNotificationPlatformBridge>();
  }
  return notification_platform_bridge_.get();
}

#if !BUILDFLAG(IS_ANDROID)
IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
  return nullptr;
}
#endif

void TestingBrowserProcess::CreateDevToolsProtocolHandler() {}

void TestingBrowserProcess::CreateDevToolsAutoOpener() {
}

bool TestingBrowserProcess::IsShuttingDown() {
  return is_shutting_down_;
}

printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
#if BUILDFLAG(ENABLE_PRINTING)
  if (!print_job_manager_.get())
    print_job_manager_ = std::make_unique<printing::PrintJobManager>();
  return print_job_manager_.get();
#else
  NOTIMPLEMENTED();
  return nullptr;
#endif
}

printing::PrintPreviewDialogController*
TestingBrowserProcess::print_preview_dialog_controller() {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  if (!print_preview_dialog_controller_) {
    print_preview_dialog_controller_ =
        std::make_unique<printing::PrintPreviewDialogController>();
  }
  return print_preview_dialog_controller_.get();
#else
  NOTIMPLEMENTED();
  return nullptr;
#endif
}

printing::BackgroundPrintingManager*
TestingBrowserProcess::background_printing_manager() {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  if (!background_printing_manager_.get()) {
    background_printing_manager_ =
        std::make_unique<printing::BackgroundPrintingManager>();
  }
  return background_printing_manager_.get();
#else
  NOTIMPLEMENTED();
  return nullptr;
#endif
}

const std::string& TestingBrowserProcess::GetApplicationLocale() {
  CHECK(features_);
  CHECK(features_->application_locale_storage());
  return features_->application_locale_storage()->Get();
}

void TestingBrowserProcess::SetApplicationLocale(
    const std::string& actual_locale) {
  CHECK(features_);
  CHECK(features_->application_locale_storage());
  return features_->application_locale_storage()->Set(actual_locale);
}

DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
  return nullptr;
}

DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
  if (!download_request_limiter_)
    download_request_limiter_ = base::MakeRefCounted<DownloadRequestLimiter>();
  return download_request_limiter_.get();
}

component_updater::ComponentUpdateService*
TestingBrowserProcess::component_updater() {
#if !BUILDFLAG(IS_ANDROID)
  return component_updater_.get();
#else
  return nullptr;
#endif
}

MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return nullptr;
#else
  if (!media_file_system_registry_)
    media_file_system_registry_ = std::make_unique<MediaFileSystemRegistry>();
  return media_file_system_registry_.get();
#endif
}

network_time::NetworkTimeTracker*
TestingBrowserProcess::network_time_tracker() {
  if (!network_time_tracker_) {
    CHECK(local_state());
    network_time_tracker_ = std::make_unique<network_time::NetworkTimeTracker>(
        std::unique_ptr<base::Clock>(new base::DefaultClock()),
        std::unique_ptr<base::TickClock>(new base::DefaultTickClock()),
        local_state(), nullptr, std::nullopt);
  }
  return network_time_tracker_.get();
}

#if !BUILDFLAG(IS_ANDROID)
gcm::GCMDriver* TestingBrowserProcess::gcm_driver() {
  return nullptr;
}
#endif

resource_coordinator::ResourceCoordinatorParts*
TestingBrowserProcess::resource_coordinator_parts() {
  if (!resource_coordinator_parts_) {
    resource_coordinator_parts_ =
        std::make_unique<resource_coordinator::ResourceCoordinatorParts>();
  }
  return resource_coordinator_parts_.get();
}

SerialPolicyAllowedPorts* TestingBrowserProcess::serial_policy_allowed_ports() {
  if (!serial_policy_allowed_ports_) {
    serial_policy_allowed_ports_ =
        std::make_unique<SerialPolicyAllowedPorts>(local_state());
  }
  return serial_policy_allowed_ports_.get();
}

#if !BUILDFLAG(IS_ANDROID)
HidSystemTrayIcon* TestingBrowserProcess::hid_system_tray_icon() {
  return hid_system_tray_icon_.get();
}

UsbSystemTrayIcon* TestingBrowserProcess::usb_system_tray_icon() {
  return usb_system_tray_icon_.get();
}
#endif

os_crypt_async::OSCryptAsync* TestingBrowserProcess::os_crypt_async() {
  return os_crypt_async_.get();
}

void TestingBrowserProcess::set_additional_os_crypt_async_provider_for_test(
    size_t precedence,
    std::unique_ptr<os_crypt_async::KeyProvider> provider) {
  // Not implemented.
  NOTREACHED();
}

BuildState* TestingBrowserProcess::GetBuildState() {
#if !BUILDFLAG(IS_ANDROID)
  return &build_state_;
#else
  return nullptr;
#endif
}

GlobalFeatures* TestingBrowserProcess::GetFeatures() {
  return features_.get();
}

void TestingBrowserProcess::CreateGlobalFeaturesForTesting() {
  // To replace the GlobalFeatures, shutdown the default instance first.
  CHECK(features_);
  features_->Shutdown();
  features_.reset();

  features_ = GlobalFeatures::CreateGlobalFeatures();
  features_->Init();

  // Assume locale is initialized to "en" during initialization.
  features_->application_locale_storage()->Set("en");
}

void TestingBrowserProcess::WillDestroyCurrentTaskEnvironment() {
  // BrowserProcessImpl::StartTearDown() is triggered on PostMainMessageLoop in
  // production, which happens before ThreadPool is destroyed.
  MaybeStartTearDown();
}

resource_coordinator::TabManager* TestingBrowserProcess::GetTabManager() {
  return resource_coordinator_parts()->tab_manager();
}

void TestingBrowserProcess::SetSharedURLLoaderFactory(
    scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory) {
  shared_url_loader_factory_ = shared_url_loader_factory;
}

#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
void TestingBrowserProcess::SetNotificationUIManager(
    std::unique_ptr<NotificationUIManager> notification_ui_manager) {
  notification_ui_manager_.swap(notification_ui_manager);
}
#endif

void TestingBrowserProcess::SetSystemNotificationHelper(
    std::unique_ptr<SystemNotificationHelper> system_notification_helper) {
  system_notification_helper_ = std::move(system_notification_helper);
}

void TestingBrowserProcess::MaybeStartTearDown() {
  if (is_torn_down_) {
    return;
  }
  is_torn_down_ = true;

  network_time_tracker_.reset();
#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
  notification_ui_manager_.reset();
#endif
  ShutdownBrowserPolicyConnector();
}

void TestingBrowserProcess::ShutdownBrowserPolicyConnector() {
  if (browser_policy_connector_) {
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS)
    // Initial cleanup for ChromeBrowserCloudManagement, shutdown components
    // that depend on profile and notification system. For example,
    // ProfileManager observer and KeyServices observer need to be removed
    // before profiles.
    auto* cloud_management_controller =
        browser_policy_connector_->chrome_browser_cloud_management_controller();
    if (cloud_management_controller) {
      cloud_management_controller->ShutDown();
    }
#endif
    browser_policy_connector_->Shutdown();
  }
}

TestingBrowserProcessPlatformPart*
TestingBrowserProcess::GetTestPlatformPart() {
  return platform_part_.get();
}

void TestingBrowserProcess::SetSafeBrowsingService(
    safe_browsing::SafeBrowsingService* sb_service) {
  sb_service_ = sb_service;
}

void TestingBrowserProcess::SetWebRtcLogUploader(
    std::unique_ptr<WebRtcLogUploader> uploader) {
  webrtc_log_uploader_ = std::move(uploader);
}

void TestingBrowserProcess::SetRulesetService(
    std::unique_ptr<subresource_filter::RulesetService> ruleset_service) {
  subresource_filter_ruleset_service_.swap(ruleset_service);
}

void TestingBrowserProcess::SetFingerprintingProtectionRulesetService(
    std::unique_ptr<subresource_filter::RulesetService> ruleset_service) {
  fingerprinting_protection_ruleset_service_.swap(ruleset_service);
}

void TestingBrowserProcess::SetShuttingDown(bool is_shutting_down) {
  is_shutting_down_ = is_shutting_down;
}

void TestingBrowserProcess::SetStatusTray(
    std::unique_ptr<StatusTray> status_tray) {
  status_tray_ = std::move(status_tray);
}

#if !BUILDFLAG(IS_ANDROID)
void TestingBrowserProcess::SetComponentUpdater(
    std::unique_ptr<component_updater::ComponentUpdateService>
        component_updater) {
  component_updater_ = std::move(component_updater);
}

void TestingBrowserProcess::SetHidSystemTrayIcon(
    std::unique_ptr<HidSystemTrayIcon> hid_system_tray_icon) {
  hid_system_tray_icon_ = std::move(hid_system_tray_icon);
}

void TestingBrowserProcess::SetUsbSystemTrayIcon(
    std::unique_ptr<UsbSystemTrayIcon> usb_system_tray_icon) {
  usb_system_tray_icon_ = std::move(usb_system_tray_icon);
}
#endif

TestingPrefServiceSimple* TestingBrowserProcess::GetTestingLocalState() {
  return testing_local_state_.get();
}

///////////////////////////////////////////////////////////////////////////////

TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
  TestingBrowserProcess::CreateInstance();
}

TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
  TestingBrowserProcess::TearDownAndDeleteInstance();
}