File: app_service_wrapper.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 (338 lines) | stat: -rw-r--r-- 11,423 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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.

#include "chrome/browser/ash/child_accounts/time_limits/app_service_wrapper.h"

#include <map>
#include <optional>
#include <set>
#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/child_accounts/time_limits/app_time_limit_utils.h"
#include "chrome/browser/ash/child_accounts/time_limits/app_types.h"
#include "chrome/browser/profiles/profile.h"
#include "components/services/app_service/public/cpp/app_launch_util.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/icon_effects.h"
#include "components/services/app_service/public/cpp/instance_update.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "ui/gfx/image/image_skia.h"

namespace ash {
namespace app_time {

namespace {

// Gets AppId from |update|.
AppId AppIdFromAppUpdate(const apps::AppUpdate& update) {
  bool is_arc = update.AppType() == apps::AppType::kArc;
  return AppId(update.AppType(),
               is_arc ? update.PublisherId() : update.AppId());
}

// Gets AppId from |update|.
AppId AppIdFromInstanceUpdate(const apps::InstanceUpdate& update,
                              apps::AppRegistryCache* app_cache) {
  AppId app_id(apps::AppType::kUnknown, update.AppId());

  app_cache->ForOneApp(update.AppId(),
                       [&app_id](const apps::AppUpdate& update) {
                         app_id = AppIdFromAppUpdate(update);
                       });
  return app_id;
}

// Gets app service id from |app_id|.
std::string AppServiceIdFromAppId(const AppId& app_id, Profile* profile) {
  return app_id.app_type() == apps::AppType::kArc
             ? arc::ArcPackageNameToAppId(app_id.app_id(), profile)
             : app_id.app_id();
}

apps::PauseData PauseAppInfoToPauseData(const PauseAppInfo& pause_info) {
  apps::PauseData details;
  details.should_show_pause_dialog = pause_info.show_pause_dialog;
  details.hours = pause_info.daily_limit.InHours();
  details.minutes = pause_info.daily_limit.InMinutes() % 60;
  return details;
}

}  // namespace

AppServiceWrapper::AppServiceWrapper(Profile* profile) : profile_(profile) {
  app_registry_cache_observer_.Observe(&GetAppCache());
  instance_registry_observation_.Observe(&GetInstanceRegistry());
}

AppServiceWrapper::~AppServiceWrapper() = default;

void AppServiceWrapper::PauseApp(const PauseAppInfo& pause_app) {
  const std::map<std::string, apps::PauseData> apps{
      {GetAppServiceId(pause_app.app_id), PauseAppInfoToPauseData(pause_app)}};
  GetAppProxy()->PauseApps(apps);
}

void AppServiceWrapper::PauseApps(
    const std::vector<PauseAppInfo>& paused_apps) {
  std::map<std::string, apps::PauseData> apps;
  for (const auto& entry : paused_apps) {
    apps[GetAppServiceId(entry.app_id)] = PauseAppInfoToPauseData(entry);
  }
  GetAppProxy()->PauseApps(apps);
}

void AppServiceWrapper::ResumeApp(const AppId& app_id) {
  const std::set<std::string> apps{GetAppServiceId(app_id)};
  GetAppProxy()->UnpauseApps(apps);
}

void AppServiceWrapper::LaunchApp(const std::string& app_service_id) {
  GetAppProxy()->Launch(
      app_service_id, ui::EF_NONE, apps::LaunchSource::kFromParentalControls,
      std::make_unique<apps::WindowInfo>(display::kDefaultDisplayId));
}

std::vector<AppId> AppServiceWrapper::GetInstalledApps() const {
  std::vector<AppId> installed_apps;
  GetAppCache().ForEachApp(
      [&installed_apps, this](const apps::AppUpdate& update) {
        if (!apps_util::IsInstalled(update.Readiness()))
          return;

        const AppId app_id = AppIdFromAppUpdate(update);
        if (!ShouldIncludeApp(app_id))
          return;

        installed_apps.push_back(app_id);
      });
  return installed_apps;
}

bool AppServiceWrapper::IsHiddenArcApp(const AppId& app_id) const {
  if (app_id.app_type() != apps::AppType::kArc)
    return false;

  bool is_hidden = false;
  const std::string app_service_id = AppServiceIdFromAppId(app_id, profile_);

  GetAppCache().ForOneApp(
      app_service_id, [&is_hidden](const apps::AppUpdate& update) {
        if (!apps_util::IsInstalled(update.Readiness()))
          return;

        is_hidden = !update.ShowInLauncher().value_or(false);
      });

  return is_hidden;
}

std::vector<AppId> AppServiceWrapper::GetHiddenArcApps() const {
  std::vector<AppId> hidden_arc_apps;
  GetAppCache().ForEachApp([&hidden_arc_apps](const apps::AppUpdate& update) {
    if (!apps_util::IsInstalled(update.Readiness()))
      return;

    const AppId app_id = AppIdFromAppUpdate(update);
    if (app_id.app_type() != apps::AppType::kArc ||
        update.ShowInLauncher().value_or(true)) {
      return;
    }

    hidden_arc_apps.push_back(app_id);
  });
  return hidden_arc_apps;
}

std::string AppServiceWrapper::GetAppName(const AppId& app_id) const {
  const std::string app_service_id = AppServiceIdFromAppId(app_id, profile_);
  DCHECK(!app_service_id.empty());

  std::string app_name;
  GetAppCache().ForOneApp(
      app_service_id,
      [&app_name](const apps::AppUpdate& update) { app_name = update.Name(); });
  return app_name;
}

void AppServiceWrapper::GetAppIcon(
    const AppId& app_id,
    int size_hint_in_dp,
    base::OnceCallback<void(std::optional<gfx::ImageSkia>)> on_icon_ready)
    const {
  const std::string app_service_id = AppServiceIdFromAppId(app_id, profile_);
  DCHECK(!app_service_id.empty());

  GetAppProxy()->LoadIconWithIconEffects(
      app_service_id, apps::IconEffects::kNone, apps::IconType::kStandard,
      size_hint_in_dp,
      /* allow_placeholder_icon */ false,
      base::BindOnce(
          [](base::OnceCallback<void(std::optional<gfx::ImageSkia>)> callback,
             apps::IconValuePtr icon_value) {
            if (!icon_value ||
                icon_value->icon_type != apps::IconType::kStandard) {
              std::move(callback).Run(std::nullopt);
            } else {
              std::move(callback).Run(icon_value->uncompressed);
            }
          },
          std::move(on_icon_ready)));
}

std::string AppServiceWrapper::GetAppServiceId(const AppId& app_id) const {
  return AppServiceIdFromAppId(app_id, profile_);
}

bool AppServiceWrapper::IsAppInstalled(const std::string& app_id) {
  return GetAppCache().GetAppType(app_id) != apps::AppType::kUnknown;
}

AppId AppServiceWrapper::AppIdFromAppServiceId(
    const std::string& app_service_id,
    apps::AppType app_type) const {
  std::optional<AppId> app_id;
  GetAppCache().ForOneApp(app_service_id,
                          [&app_id](const apps::AppUpdate& update) {
                            app_id = AppIdFromAppUpdate(update);
                          });
  DCHECK(app_id);
  return *app_id;
}

void AppServiceWrapper::AddObserver(EventListener* listener) {
  DCHECK(listener);
  listeners_.AddObserver(listener);
}

void AppServiceWrapper::RemoveObserver(EventListener* listener) {
  DCHECK(listener);
  listeners_.RemoveObserver(listener);
}

void AppServiceWrapper::OnAppUpdate(const apps::AppUpdate& update) {
  if (!update.ReadinessChanged())
    return;

  const AppId app_id = AppIdFromAppUpdate(update);
  if (!ShouldIncludeApp(app_id))
    return;

  switch (update.Readiness()) {
    case apps::Readiness::kReady:
      for (auto& listener : listeners_)
        if (update.StateIsNull()) {
          // It is the first update about this app.
          // Note that AppService does not store info between sessions and this
          // will be called at the beginning of every session.
          listener.OnAppInstalled(app_id);
        } else {
          listener.OnAppAvailable(app_id);
        }
      break;
    case apps::Readiness::kUninstalledByUser:
    case apps::Readiness::kUninstalledByNonUser:
      for (auto& listener : listeners_)
        listener.OnAppUninstalled(app_id);
      break;
    case apps::Readiness::kDisabledByUser:
    case apps::Readiness::kDisabledByPolicy:
    case apps::Readiness::kDisabledByBlocklist:
    case apps::Readiness::kDisabledByLocalSettings:
      for (auto& listener : listeners_)
        listener.OnAppBlocked(app_id);
      break;
    default:
      break;
  }
}

void AppServiceWrapper::OnAppRegistryCacheWillBeDestroyed(
    apps::AppRegistryCache* cache) {
  app_registry_cache_observer_.Reset();
}

void AppServiceWrapper::OnInstanceUpdate(const apps::InstanceUpdate& update) {
  if (!update.StateChanged())
    return;

  bool is_active = update.State() & apps::InstanceState::kActive;
  bool is_destroyed = update.State() & apps::InstanceState::kDestroyed;
  const AppId app_id = AppIdFromInstanceUpdate(update, &GetAppCache());
  // When the window is destroyed, the app might be destroyed from extensions,
  // then ShouldIncludeApp returns false. But if the app type is valid, listener
  // should be notified as well to stop the activities on the app window.
  if (!ShouldIncludeApp(app_id) &&
      !(app_id.app_type() != apps::AppType::kUnknown && is_destroyed)) {
    return;
  }

  for (auto& listener : listeners_) {
    if (is_active) {
      listener.OnAppActive(app_id, update.InstanceId(),
                           update.LastUpdatedTime());
    } else {
      listener.OnAppInactive(app_id, update.InstanceId(),
                             update.LastUpdatedTime());
    }

    if (is_destroyed) {
      listener.OnAppDestroyed(app_id, update.InstanceId(),
                              update.LastUpdatedTime());
    }
  }
}

void AppServiceWrapper::OnInstanceRegistryWillBeDestroyed(
    apps::InstanceRegistry* cache) {
  instance_registry_observation_.Reset();
}

apps::AppServiceProxy* AppServiceWrapper::GetAppProxy() const {
  return apps::AppServiceProxyFactory::GetForProfile(profile_);
}

apps::AppRegistryCache& AppServiceWrapper::GetAppCache() const {
  return GetAppProxy()->AppRegistryCache();
}

apps::InstanceRegistry& AppServiceWrapper::GetInstanceRegistry() const {
  return GetAppProxy()->InstanceRegistry();
}

bool AppServiceWrapper::ShouldIncludeApp(const AppId& app_id) const {
  if (IsHiddenArcApp(app_id))
    return false;

  if (app_id.app_type() == apps::AppType::kChromeApp) {
    const extensions::Extension* extension =
        extensions::ExtensionRegistry::Get(profile_)->GetExtensionById(
            app_id.app_id(),
            extensions::ExtensionRegistry::IncludeFlag::EVERYTHING);

    // If we are not able to find the extension, return false.
    if (!extension)
      return false;

    // Some preinstalled apps that open in browser window are legacy packaged
    // apps. Example Google Slides app.
    return extension->is_hosted_app() || extension->is_legacy_packaged_app();
  }

  return app_id.app_type() == apps::AppType::kArc ||
         app_id.app_type() == apps::AppType::kWeb;
}

}  // namespace app_time
}  // namespace ash