File: chrome_browser_main_extra_parts_ash.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (467 lines) | stat: -rw-r--r-- 19,299 bytes parent folder | download
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
// 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/browser/ui/ash/chrome_browser_main_extra_parts_ash.h"

#include <memory>
#include <utility>

#include "ash/components/arc/arc_features.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/quick_pair/keyed_service/quick_pair_mediator.h"
#include "ash/shell.h"
#include "ash/system/video_conference/fake_video_conference_tray_controller.h"
#include "ash/system/video_conference/video_conference_tray_controller.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/functional/callback.h"
#include "base/scoped_observation.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/ash/app_list/app_list_client_impl.h"
#include "chrome/browser/ash/arc/util/arc_window_watcher.h"
#include "chrome/browser/ash/crosapi/browser_util.h"
#include "chrome/browser/ash/display/refresh_rate_controller.h"
#include "chrome/browser/ash/game_mode/game_mode_controller.h"
#include "chrome/browser/ash/geolocation/system_geolocation_source.h"
#include "chrome/browser/ash/login/signin/signin_error_notifier_factory.h"
#include "chrome/browser/ash/policy/display/display_resolution_handler.h"
#include "chrome/browser/ash/policy/display/display_rotation_default_handler.h"
#include "chrome/browser/ash/policy/display/display_settings_handler.h"
#include "chrome/browser/ash/privacy_hub/privacy_hub_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/sync/sync_error_notifier_factory.h"
#include "chrome/browser/ash/system/timezone_resolver_manager.h"
#include "chrome/browser/ash/wallpaper_handlers/wallpaper_fetcher_delegate.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chromeos/tablet_mode/tablet_mode_page_behavior.h"
#include "chrome/browser/enterprise/connectors/device_trust/attestation/ash/ash_attestation_cleanup_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/accessibility/accessibility_controller_client.h"
#include "chrome/browser/ui/ash/ambient/ambient_client_impl.h"
#include "chrome/browser/ui/ash/app_access_notifier.h"
#include "chrome/browser/ui/ash/arc_open_url_delegate_impl.h"
#include "chrome/browser/ui/ash/ash_shell_init.h"
#include "chrome/browser/ui/ash/ash_web_view_factory_impl.h"
#include "chrome/browser/ui/ash/cast_config_controller_media_router.h"
#include "chrome/browser/ui/ash/chrome_new_window_client.h"
#include "chrome/browser/ui/ash/chrome_new_window_delegate_provider.h"
#include "chrome/browser/ui/ash/crosapi_new_window_delegate.h"
#include "chrome/browser/ui/ash/desks/desks_client.h"
#include "chrome/browser/ui/ash/ime_controller_client_impl.h"
#include "chrome/browser/ui/ash/in_session_auth_dialog_client.h"
#include "chrome/browser/ui/ash/in_session_auth_token_provider_impl.h"
#include "chrome/browser/ui/ash/login_screen_client_impl.h"
#include "chrome/browser/ui/ash/media_client_impl.h"
#include "chrome/browser/ui/ash/network/mobile_data_notifications.h"
#include "chrome/browser/ui/ash/network/network_connect_delegate.h"
#include "chrome/browser/ui/ash/network/network_portal_notification_controller.h"
#include "chrome/browser/ui/ash/projector/projector_app_client_impl.h"
#include "chrome/browser/ui/ash/projector/projector_client_impl.h"
#include "chrome/browser/ui/ash/screen_orientation_delegate_chromeos.h"
#include "chrome/browser/ui/ash/session_controller_client_impl.h"
#include "chrome/browser/ui/ash/shelf/app_service/exo_app_type_resolver.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/system_tray_client_impl.h"
#include "chrome/browser/ui/ash/tab_cluster_ui_client.h"
#include "chrome/browser/ui/ash/vpn_list_forwarder.h"
#include "chrome/browser/ui/ash/wallpaper_controller_client_impl.h"
#include "chrome/browser/ui/quick_answers/read_write_cards_manager_impl.h"
#include "chrome/browser/ui/views/select_file_dialog_extension.h"
#include "chrome/browser/ui/views/select_file_dialog_extension_factory.h"
#include "chrome/browser/ui/views/tabs/tab_scrubber_chromeos.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/heatmap/heatmap_palm_detector.h"
#include "chromeos/ash/components/network/network_connect.h"
#include "chromeos/ash/components/network/portal_detector/network_portal_detector.h"
#include "chromeos/ash/services/bluetooth_config/fast_pair_delegate.h"
#include "chromeos/ash/services/bluetooth_config/in_process_instance.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/core/session_manager_observer.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "services/device/public/cpp/geolocation/geolocation_manager.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/ozone/public/ozone_platform.h"

#if BUILDFLAG(ENABLE_WAYLAND_SERVER)
#include "chrome/browser/exo_parts.h"
#endif

namespace {
ChromeBrowserMainExtraPartsAsh* g_instance = nullptr;
}  // namespace

namespace internal {

// Creates a ChromeShelfController on the first active session notification.
// Used to avoid constructing a ChromeShelfController with no active profile.
class ChromeShelfControllerInitializer
    : public session_manager::SessionManagerObserver {
 public:
  ChromeShelfControllerInitializer() {
    session_manager::SessionManager::Get()->AddObserver(this);
  }

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

  ~ChromeShelfControllerInitializer() override {
    if (!chrome_shelf_controller_)
      session_manager::SessionManager::Get()->RemoveObserver(this);
  }

  // session_manager::SessionManagerObserver:
  void OnSessionStateChanged() override {
    TRACE_EVENT0("ui",
                 "ChromeShelfControllerInitializer::OnSessionStateChanged");
    DCHECK(!chrome_shelf_controller_);
    DCHECK(!ChromeShelfController::instance());

    if (session_manager::SessionManager::Get()->session_state() ==
        session_manager::SessionState::ACTIVE) {
      chrome_shelf_controller_ = std::make_unique<ChromeShelfController>(
          nullptr, ash::ShelfModel::Get());
      chrome_shelf_controller_->Init();

      session_manager::SessionManager::Get()->RemoveObserver(this);
    }
  }

 private:
  std::unique_ptr<ChromeShelfController> chrome_shelf_controller_;
};

}  // namespace internal

// static
ChromeBrowserMainExtraPartsAsh* ChromeBrowserMainExtraPartsAsh::Get() {
  return g_instance;
}

ChromeBrowserMainExtraPartsAsh::ChromeBrowserMainExtraPartsAsh() {
  CHECK(!g_instance);
  g_instance = this;
}

ChromeBrowserMainExtraPartsAsh::~ChromeBrowserMainExtraPartsAsh() {
  CHECK_EQ(g_instance, this);
  g_instance = nullptr;
}

void ChromeBrowserMainExtraPartsAsh::PreCreateMainMessageLoop() {
  user_profile_loaded_observer_ = std::make_unique<UserProfileLoadedObserver>();
}

void ChromeBrowserMainExtraPartsAsh::PreProfileInit() {
  if (base::FeatureList::IsEnabled(arc::kEnableArcIdleManager) ||
      base::FeatureList::IsEnabled(arc::kVmmSwapPolicy)) {
    // Early init so that later objects can rely on this one.
    arc_window_watcher_ = std::make_unique<ash::ArcWindowWatcher>();
  }

  // NetworkConnect handles the network connection state machine for the UI.
  network_connect_delegate_ = std::make_unique<NetworkConnectDelegate>();
  ash::NetworkConnect::Initialize(network_connect_delegate_.get());

  cast_config_controller_media_router_ =
      std::make_unique<CastConfigControllerMediaRouter>();

  // Needed by AmbientController in ash.
  ambient_client_ = std::make_unique<AmbientClientImpl>();

  // This controller MUST be initialized before the UI (AshShellInit) is
  // constructed. The video conferencing views will observe and have their own
  // reference to this controller, and will assume it exists for as long as they
  // themselves exist.
  if (ash::features::IsVideoConferenceEnabled()) {
    // `VideoConferenceTrayController` relies on audio and camera services to
    // function properly, so we will use the fake version when system bus is not
    // available so that this works on linux-chromeos and unit tests.
    if (ash::DBusThreadManager::Get()->GetSystemBus()) {
      video_conference_tray_controller_ =
          std::make_unique<ash::VideoConferenceTrayController>();
    } else {
      video_conference_tray_controller_ =
          std::make_unique<ash::FakeVideoConferenceTrayController>();
    }
  }

  ash_shell_init_ = std::make_unique<AshShellInit>();

  screen_orientation_delegate_ =
      std::make_unique<ScreenOrientationDelegateChromeos>();

  app_list_client_ = std::make_unique<AppListClientImpl>();

  // Must be available at login screen, so initialize before profile.
  accessibility_controller_client_ =
      std::make_unique<AccessibilityControllerClient>();

  {
    auto chrome_new_window_client = std::make_unique<ChromeNewWindowClient>();
    auto crosapi_new_window_delegate =
        std::make_unique<CrosapiNewWindowDelegate>(
            chrome_new_window_client.get());
    new_window_delegate_provider_ =
        std::make_unique<ChromeNewWindowDelegateProvider>(
            std::move(chrome_new_window_client),
            std::move(crosapi_new_window_delegate));
    arc_open_url_delegate_impl_ = std::make_unique<ArcOpenUrlDelegateImpl>();
  }

  ime_controller_client_ = std::make_unique<ImeControllerClientImpl>(
      ash::input_method::InputMethodManager::Get());
  ime_controller_client_->Init();

  in_session_auth_dialog_client_ =
      std::make_unique<InSessionAuthDialogClient>();

  in_session_auth_token_provider_ =
      std::make_unique<ash::InSessionAuthTokenProviderImpl>();

  // NOTE: The WallpaperControllerClientImpl must be initialized before the
  // session controller, because the session controller triggers the loading
  // of users, which itself calls a code path which eventually reaches the
  // WallpaperControllerClientImpl singleton instance via
  // ash::ChromeUserManagerImpl.
  wallpaper_controller_client_ =
      std::make_unique<WallpaperControllerClientImpl>(
          std::make_unique<wallpaper_handlers::WallpaperFetcherDelegateImpl>());
  wallpaper_controller_client_->Init();

  session_controller_client_ = std::make_unique<SessionControllerClientImpl>();
  session_controller_client_->Init();
  // By this point ash shell should have initialized its D-Bus signal
  // listeners, so inform the session manager that Ash is initialized.
  session_controller_client_->EmitAshInitialized();

  system_tray_client_ = std::make_unique<SystemTrayClientImpl>();
  network_connect_delegate_->SetSystemTrayClient(system_tray_client_.get());

  if (ash::features::IsTabClusterUIEnabled()) {
    ash::TabClusterUIController* tab_cluster_ui_controller =
        ash::Shell::Get()->tab_cluster_ui_controller();
    DCHECK(tab_cluster_ui_controller);
    tab_cluster_ui_client_ =
        std::make_unique<TabClusterUIClient>(tab_cluster_ui_controller);
  }

  tablet_mode_page_behavior_ = std::make_unique<TabletModePageBehavior>();
  vpn_list_forwarder_ = std::make_unique<VpnListForwarder>();

  chrome_shelf_controller_initializer_ =
      std::make_unique<internal::ChromeShelfControllerInitializer>();

  ui::SelectFileDialog::SetFactory(
      std::make_unique<SelectFileDialogExtensionFactory>());

#if BUILDFLAG(ENABLE_WAYLAND_SERVER)
  exo_parts_ = ExoParts::CreateIfNecessary();
  if (exo_parts_) {
    exo::WMHelper::GetInstance()->RegisterAppPropertyResolver(
        std::make_unique<ExoAppTypeResolver>());
  }
#endif

  // Result is unused, but `TimezoneResolverManager` must be created here for
  // its internal initialization to succeed.
  g_browser_process->platform_part()->GetTimezoneResolverManager();

  projector_app_client_ = std::make_unique<ProjectorAppClientImpl>();
  projector_client_ = std::make_unique<ProjectorClientImpl>();

  desks_client_ = std::make_unique<DesksClient>();

  attestation_cleanup_manager_ =
      std::make_unique<enterprise_connectors::AshAttestationCleanupManager>();

  ash::bluetooth_config::FastPairDelegate* delegate =
      ash::features::IsFastPairEnabled()
          ? ash::Shell::Get()->quick_pair_mediator()->GetFastPairDelegate()
          : nullptr;

  ash::bluetooth_config::Initialize(delegate);

  // Create geolocation manager
  device::GeolocationManager::SetInstance(
      ash::SystemGeolocationSource::CreateGeolocationManagerOnAsh());

  ui::OzonePlatform::GetInstance()->SetPalmDetector(
      std::make_unique<ash::HeatmapPalmDetector>());
}

void ChromeBrowserMainExtraPartsAsh::PostProfileInit(Profile* profile,
                                                     bool is_initial_profile) {
  // The setup below is intended to run for only the initial profile.
  if (!is_initial_profile)
    return;

  login_screen_client_ = std::make_unique<LoginScreenClientImpl>();
  // https://crbug.com/884127 ensuring that LoginScreenClientImpl is initialized
  // before using it InitializeDeviceDisablingManager.
  g_browser_process->platform_part()->InitializeDeviceDisablingManager();

  media_client_ = std::make_unique<MediaClientImpl>();
  media_client_->Init();

  // Passes (and continues passing) the current camera count to the PrivacyHub.
  ash::privacy_hub_util::SetUpCameraCountObserver();

  app_access_notifier_ = std::make_unique<AppAccessNotifier>();
  ash::privacy_hub_util::SetAppAccessNotifier(app_access_notifier_.get());

  // Instantiate DisplaySettingsHandler after CrosSettings has been
  // initialized.
  display_settings_handler_ =
      std::make_unique<policy::DisplaySettingsHandler>();
  display_settings_handler_->RegisterHandler(
      std::make_unique<policy::DisplayResolutionHandler>());
  display_settings_handler_->RegisterHandler(
      std::make_unique<policy::DisplayRotationDefaultHandler>());
  display_settings_handler_->Start();

  // Do not create a NetworkPortalNotificationController for tests since the
  // NetworkPortalDetector instance may be replaced.
  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kTestType)) {
    network_portal_notification_controller_ =
        std::make_unique<ash::NetworkPortalNotificationController>();
  }

  ash_web_view_factory_ = std::make_unique<AshWebViewFactoryImpl>();
  bool force_throttle = base::CommandLine::ForCurrentProcess()->HasSwitch(
      ash::switches::kForceRefreshRateThrottle);

  game_mode_controller_ = std::make_unique<game_mode::GameModeController>();
  refresh_rate_controller_ = std::make_unique<ash::RefreshRateController>(
      ash::Shell::Get()->display_configurator(), ash::PowerStatus::Get(),
      game_mode_controller_.get(), force_throttle);

  // Initialize TabScrubberChromeOS after the Ash Shell has been initialized.
  TabScrubberChromeOS::GetInstance();

  read_write_cards_manager_ =
      std::make_unique<chromeos::ReadWriteCardsManagerImpl>();
}

void ChromeBrowserMainExtraPartsAsh::PostBrowserStart() {
  mobile_data_notifications_ = std::make_unique<MobileDataNotifications>();

  did_post_browser_start_ = true;
  if (post_browser_start_callback_) {
    std::move(post_browser_start_callback_).Run();
  }
}

void ChromeBrowserMainExtraPartsAsh::PostMainMessageLoopRun() {
  ash::bluetooth_config::Shutdown();

  // Disable event dispatch before Exo starts closing windows to prevent
  // synthetic events from being dispatched. crbug.com/874156 and
  // crbug.com/1163269.
  ash::Shell::Get()->ShutdownEventDispatch();

#if BUILDFLAG(ENABLE_WAYLAND_SERVER)
  // ExoParts uses state from ash, delete it before ash so that exo can
  // uninstall correctly.
  exo_parts_.reset();
#endif

  mobile_data_notifications_.reset();
  chrome_shelf_controller_initializer_.reset();
  attestation_cleanup_manager_.reset();
  desks_client_.reset();

  projector_client_.reset();
  projector_app_client_.reset();

  wallpaper_controller_client_.reset();
  vpn_list_forwarder_.reset();

  tab_cluster_ui_client_.reset();

  // Initialized in PostProfileInit (which may not get called in some tests).
  refresh_rate_controller_.reset();
  game_mode_controller_.reset();
  ash_web_view_factory_.reset();
  network_portal_notification_controller_.reset();
  display_settings_handler_.reset();
  media_client_.reset();
  login_screen_client_.reset();

  ash::privacy_hub_util::SetAppAccessNotifier(nullptr);
  app_access_notifier_.reset();

  // Initialized in PreProfileInit (which may not get called in some tests).
  device::GeolocationManager::SetInstance(nullptr);
  system_tray_client_.reset();
  session_controller_client_.reset();
  ime_controller_client_.reset();
  in_session_auth_dialog_client_.reset();
  arc_open_url_delegate_impl_.reset();
  new_window_delegate_provider_.reset();
  accessibility_controller_client_.reset();
  // AppListClientImpl indirectly holds WebContents for answer card and
  // needs to be released before destroying the profile.
  app_list_client_.reset();
  ash_shell_init_.reset();

  // These instances must be destructed after `ash_shell_init_`.
  video_conference_tray_controller_.reset();
  read_write_cards_manager_.reset();

  ambient_client_.reset();

  cast_config_controller_media_router_.reset();
  if (ash::NetworkConnect::IsInitialized())
    ash::NetworkConnect::Shutdown();
  network_connect_delegate_.reset();
  user_profile_loaded_observer_.reset();
  arc_window_watcher_.reset();
}

class ChromeBrowserMainExtraPartsAsh::UserProfileLoadedObserver
    : public session_manager::SessionManagerObserver {
 public:
  UserProfileLoadedObserver() {
    session_observation_.Observe(session_manager::SessionManager::Get());
  }

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

  ~UserProfileLoadedObserver() override = default;

  // session_manager::SessionManagerObserver:
  void OnUserProfileLoaded(const AccountId& account_id) override {
    Profile* profile =
        ash::ProfileHelper::Get()->GetProfileByAccountId(account_id);
    if (ash::ProfileHelper::IsUserProfile(profile) &&
        !profile->IsGuestSession()) {
      // Start the error notifier services to show auth/sync notifications.
      ash::SigninErrorNotifierFactory::GetForProfile(profile);
      ash::SyncErrorNotifierFactory::GetForProfile(profile);
    }

    if (ChromeShelfController::instance()) {
      ChromeShelfController::instance()->OnUserProfileReadyToSwitch(profile);
    }
  }

 private:
  base::ScopedObservation<session_manager::SessionManager,
                          session_manager::SessionManagerObserver>
      session_observation_{this};
};