File: web_app_icon_health_checks.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 (116 lines) | stat: -rw-r--r-- 4,399 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
112
113
114
115
116
// 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.

#include "chrome/browser/ui/web_applications/diagnostics/web_app_icon_health_checks.h"

#include <algorithm>

#include "base/barrier_closure.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/run_loop.h"
#include "chrome/browser/web_applications/app_service/web_app_publisher_helper.h"
#include "chrome/browser/web_applications/commands/web_app_icon_diagnostic_command.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"

namespace web_app {

WebAppIconHealthChecks::WebAppIconHealthChecks(Profile* profile)
    : profile_(profile),
      web_apps_published_event_(profile, apps::AppType::kWeb) {}

WebAppIconHealthChecks::~WebAppIconHealthChecks() = default;

void WebAppIconHealthChecks::Start(base::OnceClosure done_callback) {
  DCHECK(!done_callback_);
  done_callback_ = std::move(done_callback);
  web_apps_published_event_.Post(
      base::BindOnce(&WebAppIconHealthChecks::RunDiagnostics, GetWeakPtr()));
}

base::WeakPtr<WebAppIconHealthChecks> WebAppIconHealthChecks::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void WebAppIconHealthChecks::OnWebAppWillBeUninstalled(
    const webapps::AppId& app_id) {
  if (apps_running_icon_diagnostics_.erase(app_id) > 0) {
    run_complete_callback_.Run();
  }
}

void WebAppIconHealthChecks::OnWebAppInstallManagerDestroyed() {}

void WebAppIconHealthChecks::RunDiagnostics() {
  WebAppProvider* provider =
      WebAppProvider::GetForLocalAppsUnchecked(profile_.get());

  install_manager_observation_.Observe(&provider->install_manager());

  std::vector<webapps::AppId> app_ids =
      provider->registrar_unsafe().GetAppIds();
  run_complete_callback_ = base::BarrierClosure(
      app_ids.size(),
      base::BindOnce(&WebAppIconHealthChecks::RecordDiagnosticResults,
                     GetWeakPtr()));

  for (const webapps::AppId& app_id : app_ids) {
    apps_running_icon_diagnostics_.emplace(app_id);
    provider->scheduler().RunIconDiagnosticsForApp(
        app_id, base::BindOnce(&WebAppIconHealthChecks::SaveDiagnosticForApp,
                               GetWeakPtr(), app_id));
  }
}

void WebAppIconHealthChecks::SaveDiagnosticForApp(
    webapps::AppId app_id,
    std::optional<WebAppIconDiagnosticResult> result) {
  apps_running_icon_diagnostics_.erase(app_id);
  if (result) {
    results_.push_back(*std::move(result));
  }
  run_complete_callback_.Run();
}

void WebAppIconHealthChecks::RecordDiagnosticResults() {
  install_manager_observation_.Reset();

  using Result = WebAppIconDiagnosticResult;
  auto count = [&](auto member) {
    return std::ranges::count(results_, true, member);
  };

  base::UmaHistogramCounts100("WebApp.Icon.AppsWithEmptyDownloadedIconSizes",
                              count(&Result::has_empty_downloaded_icon_sizes));
  base::UmaHistogramCounts100("WebApp.Icon.AppsWithGeneratedIconFlag",
                              count(&Result::has_generated_icon_flag));
  base::UmaHistogramCounts100("WebApp.Icon.AppsWithGeneratedIconBitmap",
                              count(&Result::has_generated_icon_bitmap));
  base::UmaHistogramCounts100(
      "WebApp.Icon.AppsWithGeneratedIconFlagFalseNegative",
      count(&Result::has_generated_icon_flag_false_negative));
  base::UmaHistogramCounts100("WebApp.Icon.AppsWithEmptyIconBitmap",
                              count(&Result::has_empty_icon_bitmap));
  base::UmaHistogramCounts100("WebApp.Icon.AppsWithEmptyIconFile",
                              count(&Result::has_empty_icon_file));
  base::UmaHistogramCounts100("WebApp.Icon.AppsWithMissingIconFile",
                              count(&Result::has_missing_icon_file));
  // TODO(crbug.com/40858602):
  // Measure:
  // - Bitmap:
  //   - WebApp.Icon.AppsWithFallbackGreyBox
  //   - WebApp.Icon.AppsWithEmptyBitmapFile
  // - Aggregate:
  //   - WebApp.Icon.AppsWithBrokenState
  //   - WebApp.Icon.AppsWithBrokenStateForInstallSource
  //   - WebApp.Icon.AppsWithBrokenStateOnShelf

  std::move(done_callback_).Run();
}

}  // namespace web_app