File: kiosk_iwa_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (256 lines) | stat: -rw-r--r-- 8,391 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 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/isolated_web_app/kiosk_iwa_manager.h"

#include <algorithm>
#include <iterator>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ref.h"
#include "chrome/browser/ash/app_mode/isolated_web_app/kiosk_iwa_data.h"
#include "chrome/browser/ash/app_mode/kiosk_app_manager_base.h"
#include "chrome/browser/ash/app_mode/kiosk_app_types.h"
#include "chrome/browser/ash/app_mode/kiosk_cryptohome_remover.h"
#include "chrome/browser/ash/policy/core/device_local_account.h"
#include "chrome/browser/chromeos/app_mode/kiosk_web_app_update_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chromeos/ash/components/policy/device_local_account/device_local_account_type.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "url/origin.h"

namespace ash {

namespace {
KioskIwaManager* g_kiosk_iwa_manager_instance = nullptr;

// TODO(crbug.com/375623747): Make common helpers for all kiosk app managers.
std::string GetAutoLoginIdSetting() {
  std::string auto_login_id_setting;
  CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
                                 &auto_login_id_setting);
  return auto_login_id_setting;
}

int GetAutoLoginDelaySetting() {
  int auto_login_delay_setting = 0;

  // unset setting also defaults to zero.
  CrosSettings::Get()->GetInteger(kAccountsPrefDeviceLocalAccountAutoLoginDelay,
                                  &auto_login_delay_setting);
  return auto_login_delay_setting;
}
}  // namespace

// static
const char KioskIwaManager::kIwaKioskDictionaryName[] = "iwa-kiosk";

// static
void KioskIwaManager::RegisterPrefs(PrefRegistrySimple* registry) {
  registry->RegisterDictionaryPref(kIwaKioskDictionaryName);
}

// static
KioskIwaManager* KioskIwaManager::Get() {
  CHECK(g_kiosk_iwa_manager_instance);
  return g_kiosk_iwa_manager_instance;
}

KioskIwaManager::KioskIwaManager(PrefService& local_state)
    : local_state_(local_state) {
  CHECK(!g_kiosk_iwa_manager_instance);  // Only one instance is allowed.
  g_kiosk_iwa_manager_instance = this;
  UpdateAppsFromPolicy();
}

KioskIwaManager::~KioskIwaManager() {
  g_kiosk_iwa_manager_instance = nullptr;
}

KioskAppManagerBase::AppList KioskIwaManager::GetApps() const {
  if (!ash::features::IsIsolatedWebAppKioskEnabled()) {
    return {};
  }

  AppList result;
  for (const auto& iwa_app_data : isolated_web_apps_) {
    // TODO(crbug.com/361017701): fill in the install url
    result.emplace_back(*iwa_app_data);
  }
  return result;
}

const KioskIwaData* KioskIwaManager::GetApp(const AccountId& account_id) const {
  if (!ash::features::IsIsolatedWebAppKioskEnabled()) {
    return nullptr;
  }

  const auto iter = std::ranges::find_if(
      isolated_web_apps_,
      [&account_id](const std::unique_ptr<KioskIwaData>& app) {
        return app->account_id() == account_id;
      });

  if (iter == isolated_web_apps_.end()) {
    return nullptr;
  }
  return iter->get();
}

void KioskIwaManager::UpdateApp(const AccountId& account_id,
                                const std::string& title,
                                const GURL& /*start_url*/,
                                const web_app::IconBitmaps& icon_bitmaps) {
  for (auto& iwa_data : isolated_web_apps_) {
    if (iwa_data->account_id() == account_id) {
      iwa_data->Update(title, icon_bitmaps);
      return;
    }
  }
  NOTREACHED();
}

const std::optional<AccountId>& KioskIwaManager::GetAutoLaunchAccountId()
    const {
  return auto_launch_id_;
}

void KioskIwaManager::OnKioskSessionStarted(const KioskAppId& app_id) {
  CHECK_EQ(app_id.type, KioskAppType::kIsolatedWebApp);
  NotifySessionInitialized();
}

void KioskIwaManager::StartObservingAppUpdate(Profile* profile,
                                              const AccountId& account_id) {
  app_update_observer_ = std::make_unique<chromeos::KioskWebAppUpdateObserver>(
      profile, account_id, KioskIwaData::kIconSize,
      base::BindRepeating(&KioskIwaManager::UpdateApp,
                          weak_ptr_factory_.GetWeakPtr()));
}

void KioskIwaManager::AddAppForTesting(
    const policy::DeviceLocalAccount& account) {
  KioskIwaDataMap dummy;
  ProcessDeviceLocalAccount(account, dummy);
  NotifyKioskAppsChanged();
}

void KioskIwaManager::UpdateAppsFromPolicy() {
  if (!ash::features::IsIsolatedWebAppKioskEnabled()) {
    // keeps KioskIwaManager empty if the feature is disabled.
    Reset();
    return;
  }

  auto previous_apps = GetAppsAndReset();

  const std::vector<policy::DeviceLocalAccount> device_local_accounts =
      policy::GetDeviceLocalAccounts(CrosSettings::Get());
  for (const policy::DeviceLocalAccount& account : device_local_accounts) {
    ProcessDeviceLocalAccount(account, previous_apps);
  }

  CancelCryptohomeRemovalsForCurrentApps();
  RemoveApps(previous_apps);
  NotifyKioskAppsChanged();
}

void KioskIwaManager::Reset() {
  isolated_web_apps_.clear();
  auto_launch_id_.reset();
  auto_launched_with_zero_delay_ = false;
}

void KioskIwaManager::MaybeSetAutoLaunchInfo(
    const std::string& policy_account_id,
    const AccountId& kiosk_app_account_id) {
  const auto auto_login_id_setting = GetAutoLoginIdSetting();

  if (!auto_login_id_setting.empty() &&
      auto_login_id_setting == policy_account_id) {
    auto_launch_id_ = kiosk_app_account_id;
    // TODO(crbug.com/375620498): remove auto_launched_with_zero_delay_.
    // Kiosk mode only supports immediate auto launch.
    auto_launched_with_zero_delay_ = (GetAutoLoginDelaySetting() == 0);
  }
}

void KioskIwaManager::CancelCryptohomeRemovalsForCurrentApps() {
  for (const auto& iwa : isolated_web_apps_) {
    KioskCryptohomeRemover::CancelDelayedCryptohomeRemoval(iwa->account_id());
  }
}

void KioskIwaManager::RemoveApps(const KioskIwaDataMap& previous_apps) const {
  std::vector<const KioskAppDataBase*> apps_to_remove;
  std::ranges::transform(previous_apps, std::back_inserter(apps_to_remove),
                         [](const auto& kv) { return kv.second.get(); });
  ClearRemovedApps(apps_to_remove);
}

KioskIwaManager::KioskIwaDataMap KioskIwaManager::GetAppsAndReset() {
  KioskIwaDataMap result;
  for (auto& app : isolated_web_apps_) {
    result[app->app_id()] = std::move(app);
  }
  Reset();
  return result;
}

void KioskIwaManager::ProcessDeviceLocalAccount(
    const policy::DeviceLocalAccount& account,
    KioskIwaDataMap& previous_apps) {
  if (account.type != policy::DeviceLocalAccountType::kKioskIsolatedWebApp) {
    return;
  }

  auto new_iwa_data = KioskIwaData::Create(
      account.user_id, account.kiosk_iwa_info, *this, local_state_.get());

  if (!new_iwa_data) {
    LOG(WARNING) << "Cannot create Kiosk IWA data for account "
                 << account.account_id;
    return;
  }

  // TODO(crbug.com/378065964): Revisit app data processing below after
  // implementing icon and title.
  auto previous_match = previous_apps.find(new_iwa_data->app_id());
  if (previous_match != previous_apps.end()) {
    // Keep this app from deletion.
    auto previous_iwa_data = std::move(previous_match->second);
    previous_apps.erase(previous_match);

    // Replace with the new IWA entry if there are changes (e.g. different
    // update manifest URL or version setting).
    if (*new_iwa_data != *previous_iwa_data) {
      isolated_web_apps_.push_back(std::move(new_iwa_data));
      isolated_web_apps_.back()->LoadFromCache();
    } else {
      isolated_web_apps_.push_back(std::move(previous_iwa_data));
    }
  } else {
    // Add the new IWA entry (no previous matches).
    isolated_web_apps_.push_back(std::move(new_iwa_data));
    isolated_web_apps_.back()->LoadFromCache();
  }

  MaybeSetAutoLaunchInfo(account.account_id,
                         isolated_web_apps_.back()->account_id());
}

}  // namespace ash