File: app_service_wrapper.h

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 (187 lines) | stat: -rw-r--r-- 7,542 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
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
// Copyright 2019 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_ASH_CHILD_ACCOUNTS_TIME_LIMITS_APP_SERVICE_WRAPPER_H_
#define CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_APP_SERVICE_WRAPPER_H_

#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "chrome/browser/apps/app_service/app_service_proxy_forward.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/instance_registry.h"

class Profile;

namespace base {
class UnguessableToken;
}  // namespace base

namespace gfx {
class ImageSkia;
}  // namespace gfx

namespace ash {
namespace app_time {

class AppId;
struct PauseAppInfo;

// Wrapper around AppService.
// Provides abstraction layer for Per-App Time Limits (PATL). Takes care of
// types conversions and data filetering, so those operations are not spread
// around the PATL code.
class AppServiceWrapper : public apps::AppRegistryCache::Observer,
                          public apps::InstanceRegistry::Observer {
 public:
  // Notifies listeners about app state changes.
  // Listener only get updates about apps that are relevant for PATL feature.
  class EventListener : public base::CheckedObserver {
   public:
    // Called when app with |app_id| is installed and at the beginning of each
    // user session (because AppService does not store apps information between
    // sessions).
    virtual void OnAppInstalled(const AppId& app_id) {}

    // Called when app with |app_id| is uninstalled.
    virtual void OnAppUninstalled(const AppId& app_id) {}

    // Called when app with |app_id| become available for usage. Usually when
    // app is unblocked.
    virtual void OnAppAvailable(const AppId& app_id) {}

    // Called when app with |app_id| become disabled and cannot be used.
    virtual void OnAppBlocked(const AppId& app_id) {}

    // Called when app with |app_id| becomes active.
    // Active means that the app is in usage (visible in foreground).
    // If the app is launched multiple times, |instance_id| indicates which
    // of the instances is active.
    // |timestamp| indicates the time when the app became active.
    virtual void OnAppActive(const AppId& app_id,
                             const base::UnguessableToken& instance_id,
                             base::Time timestamp) {}

    // Called when app with |app_id| becomes inactive.
    // Inactive means that the app is not in the foreground. It still can run
    // and be partially visible. |timestamp| indicates the time when the app
    // became inactive. |instance_id| to specify which of the application's
    // potentially multiple instances became inactive. Note: This can be called
    // for the app that is already inactive.
    virtual void OnAppInactive(const AppId& app_id,
                               const base::UnguessableToken& instance_id,
                               base::Time timestamp) {}

    // Called when app with |app_id| is destroyed.
    // |timestamp| indicates the time when the app is destroyed.
    // |instance_id| to specify which of the application's potentially multiple
    // instances was destroyed.
    virtual void OnAppDestroyed(const AppId& app_id,
                                const base::UnguessableToken& instance_id,
                                base::Time timestamp) {}
  };

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

  // Pauses the app identified by |PauseAppInfo::app_id|.
  // Uses |PauseAppInfo::daily_limit| to communicate applied time restriction to
  // the user by showing the dialog. After this is called user will not be able
  // to launch the app and the visual effect will be applied to the icon.
  // |PauseAppInfo::show_pause_dialog| indicates whether the user should be
  // notified with a dialog.
  void PauseApp(const PauseAppInfo& pause_app);
  void PauseApps(const std::vector<PauseAppInfo>& paused_apps);

  // Resets time restriction from the app identified with |app_id|. After this
  // is called user will be able to use the app again and the visual effect
  // will be removed from the icon.
  void ResumeApp(const AppId& app_id);

  // Launches app identified by |app_service_id| with no event flags explicitly
  // and default display id.
  void LaunchApp(const std::string& app_service_id);

  // Returns installed apps that are relevant for Per-App Time Limits feature.
  // Installed apps of unsupported types will not be included.
  std::vector<AppId> GetInstalledApps() const;

  // Returns true if the application is a hidden arc++ app.
  bool IsHiddenArcApp(const AppId& app_id) const;

  // Returns the list of arc++ apps hidden from user.
  std::vector<AppId> GetHiddenArcApps() const;

  // Returns short name of the app identified by |app_id|.
  // Might return empty string.
  std::string GetAppName(const AppId& app_id) const;

  // Returns the uncompressed image icon for app identified by |app_id| with
  // size |size_hint_in_dp|.
  void GetAppIcon(const AppId& app_id,
                  int size_hint_in_dp,
                  base::OnceCallback<void(std::optional<gfx::ImageSkia>)>
                      on_icon_ready) const;

  // Returns app service id for the app identified by |app_id|.
  // App service id will be only different from app_id.app_id() for ARC++ apps.
  // It does not make sense to call it for other apps.
  std::string GetAppServiceId(const AppId& app_id) const;

  // Return true if the App with |app_service_id| is installed.
  bool IsAppInstalled(const std::string& app_service_id);

  // Returns AppId from |app_service_id| and |app_type|.
  AppId AppIdFromAppServiceId(const std::string& app_service_id,
                              apps::AppType app_type) const;

  void AddObserver(EventListener* observer);
  void RemoveObserver(EventListener* observer);

  // apps::AppRegistryCache::Observer:
  void OnAppUpdate(const apps::AppUpdate& update) override;
  void OnAppRegistryCacheWillBeDestroyed(
      apps::AppRegistryCache* cache) override;

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

  apps::InstanceRegistry& GetInstanceRegistry() const;

 private:
  apps::AppServiceProxy* GetAppProxy() const;
  apps::AppRegistryCache& GetAppCache() const;

  // Return whether app with |app_id| should be included for per-app time
  // limits.
  bool ShouldIncludeApp(const AppId& app_id) const;

  base::ObserverList<EventListener> listeners_;

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

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

  const raw_ptr<Profile> profile_;
};

}  // namespace app_time
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_CHILD_ACCOUNTS_TIME_LIMITS_APP_SERVICE_WRAPPER_H_