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
|
// 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/web_applications/generated_icon_fix_util.h"
#include <stdint.h>
#include <compare>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include "base/feature_list.h"
#include "base/notreached.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/to_string.h"
#include "base/values.h"
#include "chrome/browser/web_applications/locks/with_app_resources.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/common/chrome_features.h"
#include "components/sync/base/time.h"
namespace web_app {
namespace generated_icon_fix_util {
namespace {
constexpr base::TimeDelta kFixWindowDuration = base::Days(7);
constexpr base::TimeDelta kFixAttemptThrottleDuration = base::Days(1);
// Capping the number of attempts should be redundant with the throttle + window
// but, because retries on failure are self propagating, have an explicit
// attempt count to be extra safe. Ordinarily a constraint like this would be
// CHECK'd but because these attempts run at start up it wouldn't be good for
// stability.
constexpr uint32_t kMaxAttemptCount =
kFixWindowDuration.IntDiv(kFixAttemptThrottleDuration);
static_assert(kMaxAttemptCount == 7u);
std::optional<base::Time> g_now_override_for_testing_;
base::Time Now() {
return g_now_override_for_testing_.value_or(base::Time::Now());
}
} // namespace
bool IsValid(const proto::GeneratedIconFix& generated_icon_fix) {
return generated_icon_fix.has_source() &&
generated_icon_fix.source() !=
proto::GENERATED_ICON_FIX_SOURCE_UNKNOWN &&
generated_icon_fix.has_window_start_time() &&
generated_icon_fix.has_attempt_count();
}
base::Value ToDebugValue(const proto::GeneratedIconFix* generated_icon_fix) {
if (!generated_icon_fix) {
return base::Value();
}
base::Value::Dict debug_value;
debug_value.Set("source", base::ToString(generated_icon_fix->source()));
debug_value.Set("window_start_time",
base::ToString(syncer::ProtoTimeToTime(
generated_icon_fix->window_start_time())));
debug_value.Set("last_attempt_time",
generated_icon_fix->has_last_attempt_time()
? base::Value(base::ToString(syncer::ProtoTimeToTime(
generated_icon_fix->last_attempt_time())))
: base::Value());
debug_value.Set("attempt_count", base::saturated_cast<int>(
generated_icon_fix->attempt_count()));
return base::Value(std::move(debug_value));
}
void SetNowForTesting(base::Time now) {
g_now_override_for_testing_ = now;
}
bool HasRemainingAttempts(const WebApp& app) {
const std::optional<proto::GeneratedIconFix>& generated_icon_fix =
app.generated_icon_fix();
if (!generated_icon_fix.has_value()) {
return true;
}
return generated_icon_fix->attempt_count() < kMaxAttemptCount;
}
bool IsWithinFixTimeWindow(const WebApp& app) {
const std::optional<proto::GeneratedIconFix>& generated_icon_fix =
app.generated_icon_fix();
if (!generated_icon_fix.has_value()) {
return true;
}
base::TimeDelta duration_since_window_started =
Now() - syncer::ProtoTimeToTime(generated_icon_fix->window_start_time());
return duration_since_window_started <= kFixWindowDuration;
}
void EnsureFixTimeWindowStarted(WithAppResources& resources,
ScopedRegistryUpdate& update,
const webapps::AppId& app_id,
proto::GeneratedIconFixSource source) {
if (resources.registrar()
.GetAppById(app_id)
->generated_icon_fix()
.has_value()) {
return;
}
update->UpdateApp(app_id)->SetGeneratedIconFix(
CreateInitialTimeWindow(source));
}
proto::GeneratedIconFix CreateInitialTimeWindow(
proto::GeneratedIconFixSource source) {
proto::GeneratedIconFix generated_icon_fix;
generated_icon_fix.set_source(source);
generated_icon_fix.set_window_start_time(syncer::TimeToProtoTime(Now()));
generated_icon_fix.set_attempt_count(0);
return generated_icon_fix;
}
base::TimeDelta GetThrottleDuration(const WebApp& app) {
const std::optional<proto::GeneratedIconFix> generated_icon_fix =
app.generated_icon_fix();
if (!generated_icon_fix.has_value() ||
!generated_icon_fix->has_last_attempt_time()) {
return base::TimeDelta();
}
base::TimeDelta throttle_duration =
syncer::ProtoTimeToTime(generated_icon_fix->last_attempt_time()) +
kFixAttemptThrottleDuration - Now();
// Negative durations could cause us to skip ahead of other tasks already in
// the task queue when used in PostDelayedTask() so clamp to 0.
return throttle_duration.is_negative() ? base::TimeDelta()
: throttle_duration;
}
void RecordFixAttempt(WithAppResources& resources,
ScopedRegistryUpdate& update,
const webapps::AppId& app_id,
proto::GeneratedIconFixSource source) {
EnsureFixTimeWindowStarted(resources, update, app_id, source);
WebApp* app = update->UpdateApp(app_id);
proto::GeneratedIconFix generated_icon_fix =
app->generated_icon_fix().value();
generated_icon_fix.set_attempt_count(generated_icon_fix.attempt_count() + 1);
generated_icon_fix.set_last_attempt_time(syncer::TimeToProtoTime(Now()));
app->SetGeneratedIconFix(std::move(generated_icon_fix));
}
} // namespace generated_icon_fix_util
namespace proto {
bool operator==(const proto::GeneratedIconFix& a,
const proto::GeneratedIconFix& b) {
return a.SerializeAsString() == b.SerializeAsString();
}
std::ostream& operator<<(std::ostream& out,
const proto::GeneratedIconFixSource& source) {
switch (source) {
case proto::GENERATED_ICON_FIX_SOURCE_UNKNOWN:
NOTREACHED();
case proto::GENERATED_ICON_FIX_SOURCE_SYNC_INSTALL:
return out << "SyncInstall";
case proto::GENERATED_ICON_FIX_SOURCE_RETROACTIVE:
return out << "Retroactive";
case proto::GENERATED_ICON_FIX_SOURCE_MANIFEST_UPDATE:
return out << "ManifestUpdate";
}
}
} // namespace proto
} // namespace web_app
|