File: kiosk_arcvm_app_launcher.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 (111 lines) | stat: -rw-r--r-- 3,852 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
// 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.

// TODO(crbug.com/418858226): Add tests for this file.
#include "chrome/browser/ash/app_mode/arcvm_app/kiosk_arcvm_app_launcher.h"

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

#include "ash/wm/window_pin_util.h"
#include "base/check.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "components/exo/wm_helper.h"
#include "ui/aura/env.h"

namespace ash {

KioskArcvmAppLauncher::KioskArcvmAppLauncher(ArcAppListPrefs* prefs,
                                             const std::string& app_id,
                                             Owner* owner)
    : app_id_(app_id), prefs_(prefs), owner_(owner) {
  // TODO(crbug.com/418636317): Refactor to use ScopedObservation. Move the
  // observation logic outside the constructor. Avoid ExoWindow observation.
  prefs_->AddObserver(this);
  exo::WMHelper::GetInstance()->AddExoWindowObserver(this);
}

KioskArcvmAppLauncher::~KioskArcvmAppLauncher() {
  StopObserving();
}

void KioskArcvmAppLauncher::LaunchApp(content::BrowserContext* context) {
  // Launching the app by app id in landscape mode and in non-touch mode.
  arc::LaunchApp(context, app_id_, ui::EF_NONE,
                 arc::UserInteractionType::NOT_USER_INITIATED);
}

void KioskArcvmAppLauncher::OnTaskCreated(int32_t task_id,
                                          const std::string& package_name,
                                          const std::string& activity,
                                          const std::string& intent,
                                          int32_t session_id) {
  std::unique_ptr<ArcAppListPrefs::AppInfo> app = prefs_->GetApp(app_id_);
  if (!app || app->package_name != package_name || app->activity != activity) {
    return;
  }
  task_id_ = std::make_optional(task_id);
  // The app window may have been created already.
  for (aura::Window* window : windows_) {
    if (CheckAndPinWindow(window)) {
      break;
    }
  }
}

void KioskArcvmAppLauncher::OnExoWindowCreated(aura::Window* window) {
  window->AddObserver(this);
  windows_.insert(window);

  // The `window`’s task ID might not be set yet. Record the window and
  // OnTaskCreated will check if it's the window we're looking for.
  if (!task_id_.has_value()) {
    return;
  }

  CheckAndPinWindow(window);
}

void KioskArcvmAppLauncher::OnWindowDestroying(aura::Window* window) {
  // TODO(crbug.com/418637197): Refactor to use
  // base/scoped_multi_source_observation.h
  window->RemoveObserver(this);
  windows_.erase(window);
}

// If the correct window is found and pinned, this method makes a call to the
// owner -> OnAppWindowLaunched which, timing wise, might be later than when the
// window is actually created and `OnTaskCreated` is invoked.
bool KioskArcvmAppLauncher::CheckAndPinWindow(aura::Window* const window) {
  CHECK(task_id_.has_value());
  if (task_id_ != arc::GetWindowTaskId(window)) {
    return false;
  }
  // Stop observing as target window is already found.
  StopObserving();
  PinWindow(window, /*trusted=*/true);
  if (owner_) {
    owner_->OnAppWindowLaunched();
  }
  return true;
}

void KioskArcvmAppLauncher::StopObserving() {
  // TODO(crbug.com/418858871): Remove this check.
  if (exo::WMHelper::HasInstance()) {
    // The `HasInstance` check is here, because of a crash where the `WMHelper`
    // was already destroyed. We do not know how we would get into this
    // position. See crbug.com/281992317 for reference.
    exo::WMHelper::GetInstance()->RemoveExoWindowObserver(this);
  }
  for (aura::Window* window : windows_) {
    window->RemoveObserver(this);
  }
  windows_.clear();
  prefs_->RemoveObserver(this);
}

}  // namespace ash