File: install_bounce_metric.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 (125 lines) | stat: -rw-r--r-- 3,947 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
117
118
119
120
121
122
123
124
125
// 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/web_applications/install_bounce_metric.h"

#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_thread.h"

namespace {

std::optional<base::Time>& GetTimeOverride() {
  static std::optional<base::Time> time_override;
  return time_override;
}

base::Time GetTime() {
  if (!GetTimeOverride())
    return base::Time::Now();
  return *GetTimeOverride();
}

// TODO(alancutter): Dedupe Time/Value conversion logic with
// app_banner_settings_helper.cc and PrefService.
std::optional<base::Time> ParseTime(const base::Value* value) {
  if (!value || !value->is_string())
    return std::nullopt;

  int64_t integer;
  if (!base::StringToInt64(value->GetString(), &integer))
    return std::nullopt;

  return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(integer));
}

base::Value SerializeTime(const base::Time& time) {
  return base::Value(
      base::NumberToString(time.ToDeltaSinceWindowsEpoch().InMicroseconds()));
}

const char kInstallTimestamp[] = "install_timestamp";
const char kInstallSource[] = "install_source";

struct InstallMetrics {
  base::Time timestamp;
  webapps::WebappInstallSource source;
};

std::optional<InstallMetrics> ParseInstallMetricsFromPrefs(
    const PrefService* pref_service,
    const webapps::AppId& app_id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  const base::Value::Dict& ids_to_metrics =
      pref_service->GetDict(prefs::kWebAppInstallMetrics);

  const base::Value::Dict* metrics = ids_to_metrics.FindDict(app_id);
  if (!metrics)
    return std::nullopt;

  std::optional<base::Time> timestamp =
      ParseTime(metrics->Find(kInstallTimestamp));
  if (!timestamp)
    return std::nullopt;

  const base::Value* source = metrics->Find(kInstallSource);
  if (!source || !source->is_int())
    return std::nullopt;

  return InstallMetrics{
      *timestamp, static_cast<webapps::WebappInstallSource>(source->GetInt())};
}

void WriteInstallMetricsToPrefs(const InstallMetrics& install_metrics,
                                PrefService* pref_service,
                                const webapps::AppId& app_id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  base::Value::Dict dict;
  dict.Set(kInstallTimestamp, SerializeTime(install_metrics.timestamp));
  dict.Set(kInstallSource, static_cast<int>(install_metrics.source));

  ScopedDictPrefUpdate update(pref_service, prefs::kWebAppInstallMetrics);
  update->Set(app_id, std::move(dict));
}

}  // namespace

namespace web_app {

void SetInstallBounceMetricTimeForTesting(std::optional<base::Time> time) {
  GetTimeOverride() = time;
}

void RegisterInstallBounceMetricProfilePrefs(PrefRegistrySimple* registry) {
  registry->RegisterDictionaryPref(prefs::kWebAppInstallMetrics);
}

void RecordWebAppInstallationTimestamp(
    PrefService* pref_service,
    const webapps::AppId& app_id,
    webapps::WebappInstallSource install_source) {
  WriteInstallMetricsToPrefs(InstallMetrics{GetTime(), install_source},
                             pref_service, app_id);
}

void RecordWebAppUninstallation(PrefService* pref_service,
                                const webapps::AppId& app_id) {
  std::optional<InstallMetrics> metrics =
      ParseInstallMetricsFromPrefs(pref_service, app_id);
  if (!metrics)
    return;

  constexpr base::TimeDelta kMaxInstallBounceDuration = base::Hours(1);
  if (GetTime() - metrics->timestamp > kMaxInstallBounceDuration)
    return;

  UMA_HISTOGRAM_ENUMERATION("Webapp.Install.InstallBounce", metrics->source);
}

}  // namespace web_app