File: kiosk_system_session.cc

package info (click to toggle)
chromium 140.0.7339.80-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,064 kB
  • sloc: cpp: 35,092,386; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,797; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (185 lines) | stat: -rw-r--r-- 6,546 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 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/ash/app_mode/kiosk_system_session.h"

#include <memory>
#include <optional>
#include <string>

#include "ash/accessibility/accessibility_controller.h"
#include "base/check.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/notimplemented.h"
#include "chrome/browser/ash/app_mode/app_launch_utils.h"
#include "chrome/browser/ash/app_mode/auto_sleep/device_weekly_scheduled_suspend_controller.h"
#include "chrome/browser/ash/app_mode/kiosk_app_types.h"
#include "chrome/browser/ash/app_mode/kiosk_app_update_service.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/ash/app_mode/kiosk_mode_idle_app_name_notification.h"
#include "chrome/browser/ash/app_mode/metrics/network_connectivity_metrics_service.h"
#include "chrome/browser/ash/app_mode/metrics/periodic_metrics_service.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chromeos/app_mode/kiosk_browser_window_handler.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/manifest_handlers/offline_enabled_info.h"

namespace ash {

namespace {

// Start the floating accessibility menu in ash-chrome if the
// `FloatingAccessibilityMenuEnabled` policy is enabled.
void StartFloatingAccessibilityMenu() {
  auto* accessibility_controller = AccessibilityController::Get();
  if (accessibility_controller) {
    accessibility_controller->ShowFloatingMenuIfEnabled();
  }
}

bool IsOfflineEnabledForApp(const std::string& app_id, Profile* profile) {
  extensions::ExtensionRegistry* extension_registry =
      extensions::ExtensionRegistry::Get(profile);
  CHECK(extension_registry);

  const extensions::Extension* primary_app =
      extension_registry->GetInstalledExtension(app_id);
  if (!primary_app) {
    return false;
  }

  return extensions::OfflineEnabledInfo::IsOfflineEnabled(primary_app);
}

}  // namespace

KioskSystemSession::KioskSystemSession(
    PrefService& local_state,
    Profile* profile,
    const KioskAppId& kiosk_app_id,
    const std::optional<std::string>& app_name)
    : local_state_(local_state),
      profile_(profile),
      browser_session_(profile),
      kiosk_app_id_(kiosk_app_id),
      network_metrics_service_(
          std::make_unique<NetworkConnectivityMetricsService>(local_state)),
      periodic_metrics_service_(
          std::make_unique<PeriodicMetricsService>(&local_state)),
      device_weekly_scheduled_suspend_controller_(
          std::make_unique<DeviceWeeklyScheduledSuspendController>(
              &local_state)),
      low_disk_metrics_service_(local_state),
      network_state_observer_(profile->GetPrefs()) {
  switch (kiosk_app_id_.type) {
    case KioskAppType::kChromeApp:
      InitForChromeAppKiosk();
      break;
    case KioskAppType::kWebApp:
      InitForWebKiosk(app_name);
      break;
    case KioskAppType::kIsolatedWebApp:
      InitForIwaKiosk(app_name);
      break;
    case KioskAppType::kArcvmApp:
      // TODO(crbug.com/418950414): Implement kiosk system session for ARCVM
      // kiosk. Decide if KioskBrowserSession and metrics are needed.
      NOTIMPLEMENTED() << "No KioskSystemSession init for Arcvm kiosk";
  }
}

KioskSystemSession::~KioskSystemSession() = default;

void KioskSystemSession::InitForChromeAppKiosk() {
  const std::string& app_id = kiosk_app_id_.app_id.value();
  browser_session_.InitForChromeAppKiosk(app_id);
  InitKioskAppUpdateService(app_id);
  SetRebootAfterUpdateIfNecessary();
  InitCommon(IsOfflineEnabledForApp(app_id, profile()));
}

void KioskSystemSession::InitForWebKiosk(
    const std::optional<std::string>& app_name) {
  browser_session_.InitForWebKiosk(app_name);
  InitCommon(/*is_offline_enabled=*/true);
}

void KioskSystemSession::InitForIwaKiosk(
    const std::optional<std::string>& app_name) {
  browser_session_.InitForIwaKiosk(app_name);
  InitCommon(/*is_offline_enabled=*/true);
}

void KioskSystemSession::InitCommon(bool is_offline_enabled) {
  StartFloatingAccessibilityMenu();

  periodic_metrics_service_->RecordPreviousSessionMetrics();
  periodic_metrics_service_->StartRecordingPeriodicMetrics(is_offline_enabled);
}

void KioskSystemSession::ShuttingDown() {
  network_metrics_service_.reset();
}

void KioskSystemSession::InitKioskAppUpdateService(const std::string& app_id) {
  // Set the app_id for the current instance of KioskAppUpdateService.
  auto* update_service = KioskAppUpdateServiceFactory::GetForProfile(profile());
  DCHECK(update_service);
  if (update_service) {
    update_service->Init(app_id);
  }

  // Start to monitor external update from usb stick.
  KioskChromeAppManager::Get()->MonitorKioskExternalUpdate();
}

void KioskSystemSession::SetRebootAfterUpdateIfNecessary() {
  policy::BrowserPolicyConnectorAsh* connector =
      g_browser_process->platform_part()->browser_policy_connector_ash();
  if (!connector->IsDeviceEnterpriseManaged()) {
    local_state_->SetBoolean(::prefs::kRebootAfterUpdate, true);
    KioskModeIdleAppNameNotification::Initialize();
  }
}

void KioskSystemSession::OnGuestAdded(
    content::WebContents* guest_web_contents) {
  browser_session_.OnGuestAdded(guest_web_contents);
}

void KioskSystemSession::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterBooleanPref(
      prefs::kKioskActiveWiFiCredentialsScopeChangeEnabled, false);
}

Profile* KioskSystemSession::profile() const {
  CHECK(profile_);
  return profile_;
}

bool KioskSystemSession::is_shutting_down() const {
  return browser_session_.is_shutting_down();
}

Browser* KioskSystemSession::GetSettingsBrowserForTesting() {
  return browser_session_.GetSettingsBrowserForTesting();  // IN-TEST
}

void KioskSystemSession::SetOnHandleBrowserCallbackForTesting(
    base::RepeatingCallback<void(bool)> callback) {
  browser_session_.SetOnHandleBrowserCallbackForTesting(callback);  // IN-TEST
}

}  // namespace ash