File: kiosk_arcvm_app_service.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 (264 lines) | stat: -rw-r--r-- 9,138 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
257
258
259
260
261
262
263
264
// Copyright 2025 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/arcvm_app/kiosk_arcvm_app_service.h"

#include <memory>
#include <optional>

#include "ash/public/cpp/app_list/app_list_config.h"
#include "base/check_deref.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/app_mode/arcvm_app/kiosk_arcvm_app_data.h"
#include "chrome/browser/ash/app_mode/arcvm_app/kiosk_arcvm_app_manager.h"
#include "chrome/browser/ash/app_mode/arcvm_app/kiosk_arcvm_app_service_factory.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"

namespace ash {

namespace {

constexpr size_t kNonComplianceReasonAppNotInstalled = 5;

}  // namespace

// static
KioskArcvmAppService* KioskArcvmAppService::Create(Profile* profile) {
  return new KioskArcvmAppService(profile);
}

// static
KioskArcvmAppService* KioskArcvmAppService::Get(
    content::BrowserContext* context) {
  return KioskArcvmAppServiceFactory::GetForBrowserContext(context);
}

void KioskArcvmAppService::AddObserver(KioskAppLauncher::Observer* observer) {
  observers_.AddObserver(observer);
}

void KioskArcvmAppService::RemoveObserver(
    KioskAppLauncher::Observer* observer) {
  observers_.RemoveObserver(observer);
}

void KioskArcvmAppService::Shutdown() {
  // TODO(crbug.com/418637197): Use ScopedObservation for each of the following.
  ArcAppListPrefs::Get(profile_)->RemoveObserver(this);
  // It can be null unittests.
  if (arc::ArcSessionManager::Get()) {
    arc::ArcSessionManager::Get()->RemoveObserver(this);
  }
  app_manager_->RemoveObserver(this);
  arc::ArcPolicyBridge::GetForBrowserContext(profile_)->RemoveObserver(this);
}

void KioskArcvmAppService::OnAppRegistered(
    const std::string& app_id,
    const ArcAppListPrefs::AppInfo& app_info) {
  if (!app_id_.empty() && app_id != app_id_) {
    return;
  }
  PreconditionsChanged();
}

void KioskArcvmAppService::OnAppStatesChanged(
    const std::string& app_id,
    const ArcAppListPrefs::AppInfo& app_info) {
  if (!app_id_.empty() && app_id != app_id_) {
    return;
  }
  PreconditionsChanged();
}

void KioskArcvmAppService::OnPackageListInitialRefreshed() {
  // The app could already be registered.
  PreconditionsChanged();
}

void KioskArcvmAppService::OnKioskAppsSettingsChanged() {
  PreconditionsChanged();
}

void KioskArcvmAppService::OnTaskCreated(int32_t task_id,
                                         const std::string& package_name,
                                         const std::string& activity,
                                         const std::string& intent,
                                         int32_t session_id) {
  // Store task id of the app to stop it later when needed.
  if (app_info_ && package_name == app_info_->package_name &&
      activity == app_info_->activity) {
    task_id_ = std::make_optional(task_id);
    observers_.NotifyAppLaunched();
  }
}

void KioskArcvmAppService::OnTaskDestroyed(int32_t task_id) {
  if (task_id_ == task_id) {
    ResetAppLauncher();
    // Trying to restart app if it was somehow closed or crashed
    // as kiosk app should always be running during the session.
    PreconditionsChanged();
  }
}

void KioskArcvmAppService::OnAppWindowLaunched() {
  observers_.NotifyAppWindowCreated();
}

void KioskArcvmAppService::OnIconUpdated(ArcAppIcon* icon) {
  CHECK_EQ(icon, app_icon_.get());
  if (icon->image_skia().isNull()) {
    app_icon_.release();
    return;
  }
  // TODO(crbug.com/418871771): Refactor to ash::AnnotatedAccountId::Get
  AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_);
  app_manager_->UpdateNameAndIcon(account_id, app_info_->name,
                                  app_icon_->image_skia());
  observers_.NotifyAppDataUpdated();
}

void KioskArcvmAppService::OnArcSessionRestarting() {
  // Reset state as the app is for sure not running.
  VLOG(2) << "Clearing ARC Kiosk state on restart";
  ResetAppLauncher();
}

void KioskArcvmAppService::OnArcSessionStopped(arc::ArcStopReason reason) {
  // Reset state as the app is for sure not running.
  VLOG(2) << "Clearing ARC Kiosk state on stop";
  ResetAppLauncher();
}

void KioskArcvmAppService::OnComplianceReportReceived(
    const base::Value* compliance_report) {
  VLOG(2) << "Compliance report received";
  compliance_report_received_ = true;
  pending_policy_app_installs_.clear();
  const base::Value::List* const details =
      compliance_report->GetDict().FindList("nonComplianceDetails");
  if (!details) {
    PreconditionsChanged();
    return;
  }

  for (const auto& detail : *details) {
    const base::Value::Dict& detail_dict = detail.GetDict();
    std::optional<size_t> reason = detail_dict.FindInt("nonComplianceReason");
    if (reason != kNonComplianceReasonAppNotInstalled) {
      continue;
    }
    const std::string* const app_name = detail_dict.FindString("packageName");
    if (!app_name || app_name->empty()) {
      continue;
    }
    pending_policy_app_installs_.insert(*app_name);
  }
  PreconditionsChanged();
}

KioskArcvmAppService::KioskArcvmAppService(Profile* profile)
    : profile_(profile),
      app_manager_(CHECK_DEREF(KioskArcvmAppManager::Get())) {
  ArcAppListPrefs::Get(profile_)->AddObserver(this);
  arc::ArcSessionManager::Get()->AddObserver(this);
  app_manager_->AddObserver(this);
  arc::ArcPolicyBridge::GetForBrowserContext(profile_)->AddObserver(this);
  PreconditionsChanged();
}

KioskArcvmAppService::~KioskArcvmAppService() = default;

void KioskArcvmAppService::RequestIconUpdate() {
  // Request only once when app_icon_ is not initialized.
  if (!app_info_ || !app_info_->ready || app_icon_) {
    return;
  }
  app_icon_ = std::make_unique<ArcAppIcon>(
      profile_, app_id_,
      SharedAppListConfig::instance().default_grid_icon_dimension(), this);
  app_icon_->image_skia().GetRepresentation(ui::GetSupportedResourceScaleFactor(
      display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor()));
  // Apply default image now and in case icon is updated then OnIconUpdated()
  // will be called additionally.
  OnIconUpdated(app_icon_.get());
}

void KioskArcvmAppService::PreconditionsChanged() {
  VLOG(2) << "Preconditions for kiosk app changed";
  app_id_ = GetAppId();
  if (app_id_.empty()) {
    VLOG(2) << "Kiosk app is not available";
    return;
  }
  app_info_ = ArcAppListPrefs::Get(profile_)->GetApp(app_id_);
  VLOG_IF(2, app_info_ && app_info_->ready) << "Kiosk app is ready";
  VLOG(2) << "Policy compliance is "
          << (compliance_report_received_ ? "reported" : "not yet reported");
  VLOG(2) << "Kiosk app with id: " << app_id_ << " is "
          << (app_launcher_ ? "already launched" : "not yet launched");
  VLOG(2) << "Kiosk app is policy "
          << (pending_policy_app_installs_.count(app_info_->package_name)
                  ? "non-compliant"
                  : "compliant");
  RequestIconUpdate();
  if (app_info_ && app_info_->ready && compliance_report_received_ &&
      pending_policy_app_installs_.count(app_info_->package_name) == 0 &&
      !task_id_.has_value()) {
    CHECK(!app_launcher_)
        << "App launcher already exists, not starting Kiosk app";
    VLOG(2) << "Starting kiosk app";
    app_launcher_ = std::make_unique<KioskArcvmAppLauncher>(
        ArcAppListPrefs::Get(profile_), app_id_, this);
    app_launcher_->LaunchApp(profile_);
    observers_.NotifyAppPrepared();
  } else if (task_id_.has_value()) {
    VLOG(2) << "Kiosk app should be closed";
    arc::CloseTask(task_id_.value());
    task_id_.reset();
  }
}

std::string_view KioskArcvmAppService::GetAppId() {
  // TODO(crbug.com/418871771): Refactor to ash::AnnotatedAccountId::Get
  AccountId account_id = multi_user_util::GetAccountIdFromProfile(profile_);
  const KioskArcvmAppData* app = app_manager_->GetAppByAccountId(account_id);
  if (!app) {
    return std::string_view();
  }
  std::unordered_set<std::string> app_ids =
      ArcAppListPrefs::Get(profile_)->GetAppsForPackage(app->package_name());
  if (app_ids.empty()) {
    return std::string_view();
  }
  // If `activity` and `intent` are not specified, return any app from the
  // package.
  if (app->activity().empty() && app->intent().empty()) {
    return *app_ids.begin();
  }
  // Check that the app is registered for given package.
  return app_ids.count(app->app_id()) ? app->app_id() : std::string_view();
}

void KioskArcvmAppService::ResetAppLauncher() {
  app_launcher_.reset();
  task_id_.reset();
}

// KioskArcvmAppService manages its own state by itself.
void KioskArcvmAppService::Initialize() {}
void KioskArcvmAppService::ContinueWithNetworkReady() {}
void KioskArcvmAppService::LaunchApp() {}

}  // namespace ash