File: testing_browser_process.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (368 lines) | stat: -rw-r--r-- 14,077 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
// 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.

// An implementation of BrowserProcess for unit tests that fails for most
// services. By preventing creation of services, we reduce dependencies and
// keep the profile clean. Clients of this class must handle the NULL return
// value, however.

#ifndef CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_
#define CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/common/buildflags.h"
#include "chrome/test/base/testing_browser_process_platform_part.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/activity_reporter/activity_reporter.h"
#include "components/signin/core/browser/active_primary_accounts_metrics_recorder.h"
#include "extensions/buildflags/buildflags.h"
#include "media/media_buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "ui/base/unowned_user_data/unowned_user_data_host.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/upgrade_detector/build_state.h"
#endif

class BackgroundModeManager;
class NotificationPlatformBridge;
class NotificationUIManager;
class PrefService;
class TestingPrefServiceSimple;
class SystemNotificationHelper;

namespace extensions {
class ExtensionsBrowserClient;
}

namespace gcm {
class GCMDriver;
}

namespace metrics {
class MetricsService;
}

namespace network {
class TestNetworkConnectionTracker;
class TestNetworkQualityTracker;
}  // namespace network

namespace os_crypt_async {
class OSCryptAsync;
}

namespace policy {
class PolicyService;
}

namespace resource_coordinator {
class ResourceCoordinatorParts;
}

namespace supervised_user {
class DeviceParentalControls;
}  // namespace supervised_user

namespace variations {
class VariationsService;
}

class TestingBrowserProcess
    : public BrowserProcess,
      public base::test::TaskEnvironment::DestructionObserver {
 public:
  // Initializes |g_browser_process| with a new TestingBrowserProcess.
  static void CreateInstance();

  // Cleanly destroys |g_browser_process|, which has special deletion semantics.
  static void DeleteInstance();

  // Convenience method to get g_browser_process as a TestingBrowserProcess*.
  static TestingBrowserProcess* GetGlobal();

  // Convenience method to both teardown and destroy the TestingBrowserProcess
  // instance
  static void TearDownAndDeleteInstance();

  TestingBrowserProcess(const TestingBrowserProcess&) = delete;
  TestingBrowserProcess& operator=(const TestingBrowserProcess&) = delete;

  // Handles creating the global features and optionally testing profile manager
  // in the correct order. Should be called after Init() in unit tests that need
  // to to create GlobalFeatures after modifying feature flags, or unit tests
  // that need more than just the core GlobalFeatures initialized.
  raw_ptr<TestingProfileManager> SetUpGlobalFeaturesForTesting(
      bool profile_manager);

  // Destroys the global features, testing profile manager, and resource
  // coordinator parts in the correct order. Should be used if (and only if)
  // SetUpGlobalFeaturesForTesting() was used in initialization.
  void TearDownGlobalFeaturesForTesting();

  // BrowserProcess overrides:
  ui::UnownedUserDataHost& GetUnownedUserDataHost() override;
  const ui::UnownedUserDataHost& GetUnownedUserDataHost() const override;
  void EndSession() override;
  void FlushLocalStateAndReply(base::OnceClosure reply) override;
  metrics_services_manager::MetricsServicesManager* GetMetricsServicesManager()
      override;
  metrics::MetricsService* metrics_service() override;
  SystemNetworkContextManager* system_network_context_manager() override;
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory()
      override;
  network::NetworkQualityTracker* network_quality_tracker() override;
  embedder_support::OriginTrialsSettingsStorage*
  GetOriginTrialsSettingsStorage() override;
  ProfileManager* profile_manager() override;
  PrefService* local_state() override;
  signin::ActivePrimaryAccountsMetricsRecorder*
  active_primary_accounts_metrics_recorder() override;
  variations::VariationsService* variations_service() override;
  policy::ChromeBrowserPolicyConnector* browser_policy_connector() override;
  policy::PolicyService* policy_service() override;
  IconManager* icon_manager() override;
  GpuModeManager* gpu_mode_manager() override;
#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
  BackgroundModeManager* background_mode_manager() override;
  void set_background_mode_manager_for_test(
      std::unique_ptr<BackgroundModeManager> manager) override;
#endif
  StatusTray* status_tray() override;
  safe_browsing::SafeBrowsingService* safe_browsing_service() override;
  subresource_filter::RulesetService* subresource_filter_ruleset_service()
      override;
  BrowserProcessPlatformPart* platform_part() override;

  NotificationUIManager* notification_ui_manager() override;
  NotificationPlatformBridge* notification_platform_bridge() override;
#if !BUILDFLAG(IS_ANDROID)
  IntranetRedirectDetector* intranet_redirect_detector() override;
#endif
  void CreateDevToolsProtocolHandler() override;
  void CreateDevToolsAutoOpener() override;
  bool IsShuttingDown() override;
  printing::PrintJobManager* print_job_manager() override;
  printing::PrintPreviewDialogController* print_preview_dialog_controller()
      override;
  printing::BackgroundPrintingManager* background_printing_manager() override;
  supervised_user::DeviceParentalControls& device_parental_controls() override;
  const std::string& GetApplicationLocale() override;
  void SetApplicationLocale(const std::string& actual_locale) override;
  DownloadStatusUpdater* download_status_updater() override;
  DownloadRequestLimiter* download_request_limiter() override;
  StartupData* startup_data() override;

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  void StartAutoupdateTimer() override {}
#endif

  activity_reporter::ActivityReporter* activity_reporter() override;
  component_updater::ComponentUpdateService* component_updater() override;
#if BUILDFLAG(IS_CHROMEOS)
  MediaFileSystemRegistry* media_file_system_registry() override;
#endif

  WebRtcLogUploader* webrtc_log_uploader() override;

  network_time::NetworkTimeTracker* network_time_tracker() override;

#if !BUILDFLAG(IS_ANDROID)
  gcm::GCMDriver* gcm_driver() override;
#endif
  resource_coordinator::TabManager* GetTabManager() override;
  resource_coordinator::ResourceCoordinatorParts* resource_coordinator_parts()
      override;
  SerialPolicyAllowedPorts* serial_policy_allowed_ports() override;
#if !BUILDFLAG(IS_ANDROID)
  HidSystemTrayIcon* hid_system_tray_icon() override;
  UsbSystemTrayIcon* usb_system_tray_icon() override;
#endif
  os_crypt_async::OSCryptAsync* os_crypt_async() override;
  void set_additional_os_crypt_async_provider_for_test(
      size_t precedence,
      std::unique_ptr<os_crypt_async::KeyProvider> provider) override;

  BuildState* GetBuildState() override;
  GlobalFeatures* GetFeatures() override;

  // TaskEnvironment::DestructionObserver:
  void WillDestroyCurrentTaskEnvironment() override;

  void SetMetricsService(metrics::MetricsService* metrics_service);
  void SetProfileManager(std::unique_ptr<ProfileManager> profile_manager);
  void SetSafeBrowsingService(safe_browsing::SafeBrowsingService* sb_service);
  void SetVariationsService(variations::VariationsService* variations_service);
  void SetWebRtcLogUploader(std::unique_ptr<WebRtcLogUploader> uploader);
  void SetRulesetService(
      std::unique_ptr<subresource_filter::RulesetService> ruleset_service);
  void SetSharedURLLoaderFactory(
      scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory);
#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
  void SetNotificationUIManager(
      std::unique_ptr<NotificationUIManager> notification_ui_manager);
#endif
  void SetSystemNotificationHelper(
      std::unique_ptr<SystemNotificationHelper> system_notification_helper);
  void SetShuttingDown(bool is_shutting_down);
  TestingBrowserProcessPlatformPart* GetTestPlatformPart();
  void SetStatusTray(std::unique_ptr<StatusTray> status_tray);
#if !BUILDFLAG(IS_ANDROID)
  void SetComponentUpdater(
      std::unique_ptr<component_updater::ComponentUpdateService>
          component_updater);
  void SetHidSystemTrayIcon(
      std::unique_ptr<HidSystemTrayIcon> hid_system_tray_icon);
  void SetUsbSystemTrayIcon(
      std::unique_ptr<UsbSystemTrayIcon> usb_system_tray_icon);
#endif

  // Same as local_state() but provides TestingPrefServiceSimple interface.
  TestingPrefServiceSimple* GetTestingLocalState();

 private:
  // See CreateInstance() and DestoryInstance() above.
  TestingBrowserProcess();
  ~TestingBrowserProcess() override;

  void Init();

  // Perform necessary cleanup prior to destruction of |g_browser_process|
  void MaybeStartTearDown();

  void ShutdownBrowserPolicyConnector();

  void CreateGlobalFeaturesPreProfileManager();
  void CreateGlobalFeaturesPostProfileManager();

  ui::UnownedUserDataHost unowned_user_data_host_;

  // This member needs to stay at or near the top of the list so it gets
  // destroyed late in the shutdown process. Several other members rely on
  // |features_|, so having it lower in this file could cause use-after-free
  // issues.
  std::unique_ptr<GlobalFeatures> features_;

  // The value returned by `IsShuttingDown()`.
  bool is_shutting_down_ = false;

  // Used as a guard for `MaybeStartTearDown()`.
  bool is_torn_down_ = false;

  std::unique_ptr<policy::ChromeBrowserPolicyConnector>
      browser_policy_connector_;
  std::unique_ptr<network::TestNetworkQualityTracker>
      test_network_quality_tracker_;
  raw_ptr<metrics::MetricsService> metrics_service_ = nullptr;
  raw_ptr<variations::VariationsService> variations_service_ = nullptr;

  // The `ProfileManager` is here as part of the `BrowserProcess` API. It is
  // typically created during initialization of the `TestingProfileManager`, but
  // can also be set directly by tests using `SetProfileManager()`.
  std::unique_ptr<ProfileManager> profile_manager_;

  // The `TestingProfileManager` (not to be confused with the `ProfileManager`
  // above) is created by `SetUpGlobalFeaturesForTesting()` and cleaned up by
  // `TearDownGlobalFeaturesForTesting()`. Initialization causes the above
  // `ProfileManager` to also be created and set.
  std::unique_ptr<TestingProfileManager> testing_profile_manager_;

  std::unique_ptr<TestingPrefServiceSimple> testing_local_state_;

#if BUILDFLAG(ENABLE_CHROME_NOTIFICATIONS)
  std::unique_ptr<NotificationUIManager> notification_ui_manager_;
#endif

  std::unique_ptr<NotificationPlatformBridge> notification_platform_bridge_;
  std::unique_ptr<SystemNotificationHelper> system_notification_helper_;
  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;

  std::unique_ptr<embedder_support::OriginTrialsSettingsStorage>
      origin_trials_settings_storage_;

#if BUILDFLAG(ENABLE_PRINTING)
  std::unique_ptr<printing::PrintJobManager> print_job_manager_;
#endif

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  std::unique_ptr<printing::BackgroundPrintingManager>
      background_printing_manager_;
  std::unique_ptr<printing::PrintPreviewDialogController>
      print_preview_dialog_controller_;
#endif

  std::unique_ptr<supervised_user::DeviceParentalControls>
      device_parental_controls_;

  scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_;
  std::unique_ptr<subresource_filter::RulesetService>
      subresource_filter_ruleset_service_;
  std::unique_ptr<WebRtcLogUploader> webrtc_log_uploader_;

  std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;

  // The following objects are not owned by TestingBrowserProcess:
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;

  std::unique_ptr<TestingBrowserProcessPlatformPart> platform_part_;
  std::unique_ptr<network::TestNetworkConnectionTracker>
      test_network_connection_tracker_;

#if BUILDFLAG(IS_CHROMEOS)
  std::unique_ptr<MediaFileSystemRegistry> media_file_system_registry_;
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
  std::unique_ptr<extensions::ExtensionsBrowserClient>
      extensions_browser_client_;
#endif

  std::unique_ptr<resource_coordinator::ResourceCoordinatorParts>
      resource_coordinator_parts_;

  std::unique_ptr<SerialPolicyAllowedPorts> serial_policy_allowed_ports_;
  std::unique_ptr<activity_reporter::ActivityReporter> activity_reporter_;
#if !BUILDFLAG(IS_ANDROID)
  std::unique_ptr<HidSystemTrayIcon> hid_system_tray_icon_;
  std::unique_ptr<UsbSystemTrayIcon> usb_system_tray_icon_;
  std::unique_ptr<component_updater::ComponentUpdateService> component_updater_;
  BuildState build_state_;
#endif

  std::unique_ptr<StatusTray> status_tray_;
  std::unique_ptr<os_crypt_async::OSCryptAsync> os_crypt_async_;
};

// RAII (resource acquisition is initialization) for TestingBrowserProcess.
// Allows you to initialize TestingBrowserProcess before other member variables.
//
// This can be helpful if you are running a unit test inside the browser_tests
// suite because browser_tests do not make a TestingBrowserProcess for you.
//
// class MyUnitTestRunningAsBrowserTest : public testing::Test {
//  ...stuff...
//  private:
//   TestingBrowserProcessInitializer initializer_;
// };
class TestingBrowserProcessInitializer {
 public:
  TestingBrowserProcessInitializer();
  TestingBrowserProcessInitializer(const TestingBrowserProcessInitializer&) =
      delete;
  TestingBrowserProcessInitializer& operator=(
      const TestingBrowserProcessInitializer&) = delete;
  ~TestingBrowserProcessInitializer();
};

#endif  // CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_