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/notifications/scheduler/internal/init_aware_scheduler.h"
#include <utility>
#include "base/functional/bind.h"
#include "chrome/browser/notifications/scheduler/public/notification_params.h"
namespace notifications {
InitAwareNotificationScheduler::InitAwareNotificationScheduler(
std::unique_ptr<NotificationScheduler> impl)
: impl_(std::move(impl)) {}
InitAwareNotificationScheduler::~InitAwareNotificationScheduler() = default;
void InitAwareNotificationScheduler::Init(InitCallback init_callback) {
DCHECK(!init_success_.has_value());
impl_->Init(base::BindOnce(&InitAwareNotificationScheduler::OnInitialized,
weak_ptr_factory_.GetWeakPtr(),
std::move(init_callback)));
}
void InitAwareNotificationScheduler::Schedule(
std::unique_ptr<NotificationParams> params) {
if (IsReady()) {
impl_->Schedule(std::move(params));
return;
}
MaybeCacheClosure(base::BindOnce(&InitAwareNotificationScheduler::Schedule,
weak_ptr_factory_.GetWeakPtr(),
std::move(params)));
}
void InitAwareNotificationScheduler::DeleteAllNotifications(
SchedulerClientType type) {
if (IsReady()) {
impl_->DeleteAllNotifications(type);
return;
}
MaybeCacheClosure(
base::BindOnce(&InitAwareNotificationScheduler::DeleteAllNotifications,
weak_ptr_factory_.GetWeakPtr(), type));
}
void InitAwareNotificationScheduler::GetClientOverview(
SchedulerClientType type,
ClientOverview::ClientOverviewCallback callback) {
if (IsReady()) {
impl_->GetClientOverview(type, std::move(callback));
return;
}
MaybeCacheClosure(base::BindOnce(
&InitAwareNotificationScheduler::GetClientOverview,
weak_ptr_factory_.GetWeakPtr(), type, std::move(callback)));
}
void InitAwareNotificationScheduler::OnStartTask(
TaskFinishedCallback callback) {
if (IsReady()) {
impl_->OnStartTask(std::move(callback));
return;
}
MaybeCacheClosure(base::BindOnce(&InitAwareNotificationScheduler::OnStartTask,
weak_ptr_factory_.GetWeakPtr(),
std::move(callback)));
}
void InitAwareNotificationScheduler::OnStopTask() {
if (IsReady()) {
impl_->OnStopTask();
return;
}
MaybeCacheClosure(base::BindOnce(&InitAwareNotificationScheduler::OnStopTask,
weak_ptr_factory_.GetWeakPtr()));
}
void InitAwareNotificationScheduler::OnUserAction(
const UserActionData& action_data) {
if (IsReady()) {
impl_->OnUserAction(action_data);
return;
}
MaybeCacheClosure(
base::BindOnce(&InitAwareNotificationScheduler::OnUserAction,
weak_ptr_factory_.GetWeakPtr(), action_data));
}
void InitAwareNotificationScheduler::OnInitialized(InitCallback init_callback,
bool success) {
init_success_ = success;
if (!success) {
cached_closures_.clear();
std::move(init_callback).Run(false);
return;
}
// Flush all cached calls.
for (auto it = cached_closures_.begin(); it != cached_closures_.end(); ++it) {
std::move(*it).Run();
}
cached_closures_.clear();
std::move(init_callback).Run(true);
}
bool InitAwareNotificationScheduler::IsReady() const {
return init_success_.has_value() && *init_success_;
}
void InitAwareNotificationScheduler::MaybeCacheClosure(
base::OnceClosure closure) {
DCHECK(closure);
// Drop the call if initialization failed.
if (init_success_.has_value() && !*init_success_)
return;
// Cache the closure to invoke later.
cached_closures_.emplace_back(std::move(closure));
}
} // namespace notifications
|