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
|
// 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 "components/user_education/common/product_messaging_controller.h"
#include <sstream>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "components/user_education/common/session/user_education_session_manager.h"
#include "components/user_education/common/user_education_data.h"
#include "components/user_education/common/user_education_storage_service.h"
namespace user_education {
namespace internal {
DEFINE_REQUIRED_NOTICE_IDENTIFIER(kShowAfterAllNotices);
}
// RequiredNoticePriorityHandle
RequiredNoticePriorityHandle::RequiredNoticePriorityHandle() = default;
RequiredNoticePriorityHandle::RequiredNoticePriorityHandle(
RequiredNoticeId notice_id,
base::WeakPtr<ProductMessagingController> controller)
: notice_id_(notice_id), controller_(controller) {}
RequiredNoticePriorityHandle::RequiredNoticePriorityHandle(
RequiredNoticePriorityHandle&& other) noexcept
: notice_id_(std::exchange(other.notice_id_, RequiredNoticeId())),
controller_(std::move(other.controller_)) {}
RequiredNoticePriorityHandle& RequiredNoticePriorityHandle::operator=(
RequiredNoticePriorityHandle&& other) noexcept {
if (this != &other) {
notice_id_ = std::exchange(other.notice_id_, RequiredNoticeId());
controller_ = std::move(other.controller_);
}
return *this;
}
RequiredNoticePriorityHandle::~RequiredNoticePriorityHandle() {
Release();
}
RequiredNoticePriorityHandle::operator bool() const {
return notice_id_ && controller_;
}
bool RequiredNoticePriorityHandle::operator!() const {
return !static_cast<bool>(*this);
}
void RequiredNoticePriorityHandle::SetShown() {
CHECK(static_cast<bool>(this));
shown_ = true;
if (controller_) {
controller_->OnNoticeShown(notice_id_);
}
}
void RequiredNoticePriorityHandle::Release() {
if (!*this) {
return;
}
controller_->ReleaseHandle(notice_id_, shown_);
controller_.reset();
notice_id_ = RequiredNoticeId();
}
// ProductMessagingController::RequiredNoticeData
struct ProductMessagingController::RequiredNoticeData {
RequiredNoticeData() = default;
RequiredNoticeData(RequiredNoticeData&&) = default;
RequiredNoticeData& operator=(RequiredNoticeData&&) = default;
RequiredNoticeData(RequiredNoticeShowCallback callback_,
std::vector<RequiredNoticeId> show_after_,
std::vector<RequiredNoticeId> blocked_by_)
: callback(std::move(callback_)),
show_after(std::move(show_after_)),
blocked_by(std::move(blocked_by_)) {}
~RequiredNoticeData() = default;
RequiredNoticeShowCallback callback;
std::vector<RequiredNoticeId> show_after;
std::vector<RequiredNoticeId> blocked_by;
};
// ProductMessagingController
ProductMessagingController::ProductMessagingController() = default;
ProductMessagingController::~ProductMessagingController() = default;
void ProductMessagingController::Init(
UserEducationSessionProvider& session_provider,
UserEducationStorageService& storage_service) {
storage_service_ = &storage_service;
if (session_provider.GetNewSessionSinceStartup()) {
storage_service_->ResetProductMessagingData();
}
session_subscription_ =
session_provider.AddNewSessionCallback(base::BindRepeating(
&ProductMessagingController::OnNewSession, base::Unretained(this)));
}
bool ProductMessagingController::IsNoticeQueued(
RequiredNoticeId notice_id) const {
return base::Contains(pending_notices_, notice_id);
}
void ProductMessagingController::QueueRequiredNotice(
RequiredNoticeId notice_id,
RequiredNoticeShowCallback ready_to_start_callback,
std::initializer_list<RequiredNoticeId> always_show_after,
std::initializer_list<RequiredNoticeId> blocked_by) {
CHECK(notice_id);
CHECK(!ready_to_start_callback.is_null());
CHECK(!base::Contains(blocked_by, internal::kShowAfterAllNotices));
// Cannot re-queue the current notice.
if (current_notice_ == notice_id) {
return;
}
const auto result = pending_notices_.emplace(
notice_id,
RequiredNoticeData(std::move(ready_to_start_callback),
std::move(always_show_after), std::move(blocked_by)));
CHECK(result.second) << "Duplicate required notice ID: " << notice_id;
MaybeShowNextRequiredNotice();
}
void ProductMessagingController::UnqueueRequiredNotice(
RequiredNoticeId notice_id) {
pending_notices_.erase(notice_id);
}
base::CallbackListSubscription
ProductMessagingController::AddRequiredNoticePriorityHandleGrantedCallback(
StatusUpdateCallback callback) {
return handle_granted_callbacks_.Add(std::move(callback));
}
base::CallbackListSubscription
ProductMessagingController::AddRequiredNoticeShownCallback(
StatusUpdateCallback callback) {
return notice_shown_callbacks_.Add(std::move(callback));
}
void ProductMessagingController::ReleaseHandle(RequiredNoticeId notice_id,
bool notice_shown) {
CHECK_EQ(current_notice_, notice_id);
if (notice_shown) {
ProductMessagingData data = storage_service_->ReadProductMessagingData();
const auto insert_result = data.shown_notices.insert(notice_id.GetName());
if (insert_result.second) {
storage_service_->SaveProductMessagingData(data);
}
}
current_notice_ = RequiredNoticeId();
MaybeShowNextRequiredNotice();
}
void ProductMessagingController::MaybeShowNextRequiredNotice() {
if (!ready_to_show()) {
return;
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
&ProductMessagingController::MaybeShowNextRequiredNoticeImpl,
weak_ptr_factory_.GetWeakPtr()));
}
void ProductMessagingController::PurgeBlockedNotices() {
ProductMessagingData stored_data =
storage_service_->ReadProductMessagingData();
std::vector<RequiredNoticeId> to_purge;
for (const auto& [id, data] : pending_notices_) {
if (stored_data.shown_notices.contains(id.GetName())) {
to_purge.push_back(id);
continue;
}
for (auto blocker : data.blocked_by) {
if (stored_data.shown_notices.contains(blocker.GetName())) {
to_purge.push_back(id);
break;
}
}
}
for (auto id : to_purge) {
pending_notices_.erase(id);
}
}
void ProductMessagingController::MaybeShowNextRequiredNoticeImpl() {
if (!ready_to_show()) {
return;
}
PurgeBlockedNotices();
if (pending_notices_.empty()) {
return;
}
// Find a notice that is not waiting for any other notices to show.
RequiredNoticeId to_show;
for (const auto& [id, data] : pending_notices_) {
bool excluded = false;
bool show_after_all = false;
for (auto after : data.show_after) {
if (after == internal::kShowAfterAllNotices) {
show_after_all = true;
} else if (pending_notices_.contains(after)) {
excluded = true;
break;
}
}
for (auto blocker : data.blocked_by) {
if (pending_notices_.contains(blocker)) {
excluded = true;
break;
}
}
if (!excluded) {
if (!show_after_all) {
to_show = id;
break;
} else if (!to_show) {
to_show = id;
}
}
}
if (!to_show) {
NOTREACHED() << "Circular dependency in required notifications:"
<< DumpData();
}
// Fire the next notice.
RequiredNoticeShowCallback cb = std::move(pending_notices_[to_show].callback);
pending_notices_.erase(to_show);
current_notice_ = to_show;
std::move(cb).Run(
RequiredNoticePriorityHandle(to_show, weak_ptr_factory_.GetWeakPtr()));
handle_granted_callbacks_.Notify(to_show);
}
void ProductMessagingController::OnNewSession() {
storage_service_->ResetProductMessagingData();
}
void ProductMessagingController::OnNoticeShown(RequiredNoticeId notice_id) {
if (notice_id == current_notice_) {
notice_shown_callbacks_.Notify(notice_id);
}
}
std::string ProductMessagingController::DumpData() const {
std::ostringstream oss;
for (const auto& [id, data] : pending_notices_) {
oss << "\n{ id: " << id << " show_after: { ";
for (const auto& after : data.show_after) {
oss << after << ", ";
}
oss << "} blocked_by: { ";
for (const auto& blocker : data.blocked_by) {
oss << blocker << ", ";
}
oss << "} }";
}
return oss.str();
}
} // namespace user_education
|