File: app_service_log_source.cc

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 (79 lines) | stat: -rw-r--r-- 2,898 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
// Copyright 2023 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/system_logs/app_service_log_source.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/metrics/app_platform_metrics.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics_utils.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"

namespace system_logs {

namespace {

constexpr char kAppServiceLogEntry[] = "app_service";

}  // namespace

AppServiceLogSource::AppServiceLogSource()
    : SystemLogsSource("AppServiceLog") {}

AppServiceLogSource::~AppServiceLogSource() = default;

void AppServiceLogSource::Fetch(SysLogsSourceCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!callback.is_null());

  auto response = std::make_unique<SystemLogsResponse>();

  const auto* user = user_manager::UserManager::Get()->GetPrimaryUser();
  if (!user) {
    std::move(callback).Run(std::move(response));
    return;
  }
  Profile* profile = ash::ProfileHelper::Get()->GetProfileByUser(user);
  if (!profile ||
      !apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    std::move(callback).Run(std::move(response));
    return;
  }

  std::set<std::string> running;
  std::set<std::string> seen;
  std::stringstream log_data;

  auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
  proxy->InstanceRegistry().ForEachInstance(
      [&running](const apps::InstanceUpdate& update) {
        running.insert(update.AppId());
      });
  proxy->AppRegistryCache().ForEachApp([&log_data, &running, &seen, profile](
                                           const apps::AppUpdate& update) {
    std::string status =
        base::Contains(running, update.AppId()) ? "running" : "installed";
    auto app_type = apps::GetAppTypeHistogramName(
        apps::GetAppTypeName(profile, update.AppType(), update.AppId(),
                             apps::LaunchContainer::kLaunchContainerNone));
    std::string id =
        apps::AppPlatformMetrics::GetURLForApp(profile, update.AppId()).spec();
    // Different apps can coalesce to the same ID, only report the first
    // instance.
    if (id.empty() || base::Contains(seen, id)) {
      return;
    }
    log_data << id << ", " << app_type << ", " << status << std::endl;
    seen.insert(id);
  });

  (*response)[kAppServiceLogEntry] = log_data.str();
  std::move(callback).Run(std::move(response));
}

}  // namespace system_logs