File: kiosk_app_service_launcher.h

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 (118 lines) | stat: -rw-r--r-- 4,764 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_SERVICE_LAUNCHER_H_
#define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_SERVICE_LAUNCHER_H_

#include <optional>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "build/buildflag.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/launch_result_type.h"
#include "chrome/browser/profiles/profile.h"
#include "components/services/app_service/public/cpp/app.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/instance_registry.h"
#include "components/services/app_service/public/cpp/instance_update.h"

namespace chromeos {

// This class launches a Kiosk app with the following steps:
// 1. Checks if the app is ready to be launched. If not then observes the
//    registry cache until the app is ready.
// 2. Starts the app using `AppServiceProxy::LaunchAppWithParams()` interface
//    and waits for the launch to complete.
class KioskAppServiceLauncher :
    public apps::InstanceRegistry::Observer,
    public apps::AppRegistryCache::Observer {
 public:
  // Callback when the app is launched by App Service. App window instance is
  // not active at this point. If called with false then the app launch has
  // failed. Corresponds to `KioskLaunchController::OnAppLaunched()`.
  using AppLaunchedCallback = base::OnceCallback<void(bool)>;

  // Histogram to log the app readiness while launching app.
  static constexpr char kLaunchAppReadinessUMA[] =
      "Kiosk.AppService.Launch.AppReadiness";

  explicit KioskAppServiceLauncher(Profile* profile);
  KioskAppServiceLauncher(const KioskAppServiceLauncher&) = delete;
  KioskAppServiceLauncher& operator=(const KioskAppServiceLauncher&) = delete;
  ~KioskAppServiceLauncher() override;

  // Checks if the Kiosk app is ready to be launched by App Service. If it's
  // ready then launches the app immediately. Otherwise waits for it to be ready
  // and launches the app later. Should only be called once per Kiosk launch.
  // This function does not wait for app window to become active, which should
  // be handled in the caller of this class.
  void CheckAndMaybeLaunchApp(const std::string& app_id,
                              AppLaunchedCallback app_launched_callback);

  // Ensures that `app_type` is initialized in App Service.
  void EnsureAppTypeInitialized(
      apps::AppType app_type,
      base::OnceClosure app_type_initialized_callback);

  // Same as the other `CheckAndMaybeLaunchApp`, but also waits for app window
  // to be visible by observing `apps::InstanceRegistry`.
  void CheckAndMaybeLaunchApp(const std::string& app_id,
                              AppLaunchedCallback app_launched_callback,
                              base::OnceClosure app_visible_callback);

  void SetLaunchUrl(const GURL& launch_url);

 private:
  void LaunchAppInternal();

  void OnAppLaunched(apps::LaunchResult&& result);

  // apps::AppRegistryCache::Observer:
  void OnAppUpdate(const apps::AppUpdate& update) override;
  void OnAppTypePublishing(const std::vector<apps::AppPtr>& deltas,
                           apps::AppType app_type) override;
  void OnAppRegistryCacheWillBeDestroyed(
      apps::AppRegistryCache* cache) override;

  // apps::InstanceRegistry::Observer:
  void OnInstanceUpdate(const apps::InstanceUpdate& update) override;
  void OnInstanceRegistryWillBeDestroyed(
      apps::InstanceRegistry* cache) override;

  std::string app_id_;

  apps::AppType app_type_;

  // A keyed service. Not owned by this class.
  raw_ptr<apps::AppServiceProxy> app_service_;

  base::OnceClosure app_type_initialized_callback_;

  AppLaunchedCallback app_launched_callback_;
  std::optional<GURL> launch_url_;

  base::ScopedObservation<apps::AppRegistryCache,
                          apps::AppRegistryCache::Observer>
      app_registry_observation_{this};

  base::OnceClosure app_visible_callback_;

  base::ScopedObservation<apps::InstanceRegistry,
                          apps::InstanceRegistry::Observer>
      instance_registry_observation_{this};

  base::WeakPtrFactory<KioskAppServiceLauncher> weak_ptr_factory_{this};
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_SERVICE_LAUNCHER_H_