Commit 8d6bcb05 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Notification scheduler: Notify client about service initialization.

This CL informs the clients API about initialization result and list of
guids of scheduled notifications. Also some minor polish for the client
interface.

Bug: 963288
Change-Id: I887f668b0f201583b208497ad4cd782a1679be4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1670053
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671854}
parent 3d7c5b9f
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h" #include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h"
#include "chrome/browser/notifications/scheduler/internal/display_decider.h" #include "chrome/browser/notifications/scheduler/internal/display_decider.h"
#include "chrome/browser/notifications/scheduler/internal/distribution_policy.h" #include "chrome/browser/notifications/scheduler/internal/distribution_policy.h"
...@@ -132,9 +133,34 @@ class NotificationSchedulerImpl : public NotificationScheduler, ...@@ -132,9 +133,34 @@ class NotificationSchedulerImpl : public NotificationScheduler,
void OnInitialized(std::unique_ptr<InitHelper>, void OnInitialized(std::unique_ptr<InitHelper>,
InitCallback init_callback, InitCallback init_callback,
bool success) { bool success) {
// TODO(xingliu): Inform the clients about initialization results and tear // TODO(xingliu): Tear down internal components if initialization failed.
// down internal components.
std::move(init_callback).Run(success); std::move(init_callback).Run(success);
NotifyClientsAfterInit(success);
}
void NotifyClientsAfterInit(bool success) {
std::vector<SchedulerClientType> clients;
context_->client_registrar()->GetRegisteredClients(&clients);
for (auto type : clients) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&NotificationSchedulerImpl::NotifyClientAfterInit,
weak_ptr_factory_.GetWeakPtr(), type, success));
}
}
void NotifyClientAfterInit(SchedulerClientType type, bool success) {
std::vector<const NotificationEntry*> notifications;
context_->notification_manager()->GetNotifications(type, &notifications);
std::set<std::string> guids;
for (const auto* notification : notifications) {
DCHECK(notification);
guids.emplace(notification->guid);
}
auto* client = context_->client_registrar()->GetClient(type);
DCHECK(client);
client->OnSchedulerInitialized(success, std::move(guids));
} }
// NotificationBackgroundTaskScheduler::Handler implementation. // NotificationBackgroundTaskScheduler::Handler implementation.
...@@ -184,8 +210,6 @@ class NotificationSchedulerImpl : public NotificationScheduler, ...@@ -184,8 +210,6 @@ class NotificationSchedulerImpl : public NotificationScheduler,
task_start_time, std::move(notifications), std::move(client_states), task_start_time, std::move(notifications), std::move(client_states),
&results); &results);
// TODO(xingliu): Update impression data after notification shown.
// See https://crbug.com/965133.
for (const auto& guid : results) { for (const auto& guid : results) {
context_->notification_manager()->DisplayNotification(guid); context_->notification_manager()->DisplayNotification(guid);
} }
......
...@@ -12,7 +12,7 @@ WebUIClient::WebUIClient() = default; ...@@ -12,7 +12,7 @@ WebUIClient::WebUIClient() = default;
WebUIClient::~WebUIClient() = default; WebUIClient::~WebUIClient() = default;
void WebUIClient::ShowNotification( void WebUIClient::BeforeShowNotification(
std::unique_ptr<NotificationData> notification_data, std::unique_ptr<NotificationData> notification_data,
NotificationDataCallback callback) { NotificationDataCallback callback) {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
......
...@@ -21,8 +21,9 @@ class WebUIClient : public NotificationSchedulerClient { ...@@ -21,8 +21,9 @@ class WebUIClient : public NotificationSchedulerClient {
private: private:
// NotificationSchedulerClient implementation. // NotificationSchedulerClient implementation.
void ShowNotification(std::unique_ptr<NotificationData> notification_data, void BeforeShowNotification(
NotificationDataCallback callback) override; std::unique_ptr<NotificationData> notification_data,
NotificationDataCallback callback) override;
void OnSchedulerInitialized(bool success, void OnSchedulerInitialized(bool success,
std::set<std::string> guids) override; std::set<std::string> guids) override;
void OnUserAction(UserActionType action_type, void OnUserAction(UserActionType action_type,
......
...@@ -22,35 +22,16 @@ namespace notifications { ...@@ -22,35 +22,16 @@ namespace notifications {
// The client interface to receive events from notification scheduler. // The client interface to receive events from notification scheduler.
class NotificationSchedulerClient { class NotificationSchedulerClient {
public: public:
// Defines user actions type.
enum class UserActionType {
// The user clicks on the notification body.
kClick = 0,
// The user clicks on the notification button.
kButtonClick = 1,
// The user dismisses the notification.
kDismiss = 2,
};
// Information about button clicks.
struct ButtonClickInfo {
// Unique id of the button.
std::string button_id;
// Associate impression type for the button.
ActionButtonType type = ActionButtonType::kUnknownAction;
};
using NotificationDataCallback = using NotificationDataCallback =
base::OnceCallback<void(std::unique_ptr<NotificationData>)>; base::OnceCallback<void(std::unique_ptr<NotificationData>)>;
NotificationSchedulerClient() = default; NotificationSchedulerClient() = default;
virtual ~NotificationSchedulerClient() = default; virtual ~NotificationSchedulerClient() = default;
// Called when the notification should be displayed to the user. The clients // Called before the notification should be displayed to the user. The clients
// can overwrite data in |notification_data| and return the updated data in // can overwrite data in |notification_data| and return the updated data in
// |callback|. // |callback|.
virtual void ShowNotification( virtual void BeforeShowNotification(
std::unique_ptr<NotificationData> notification_data, std::unique_ptr<NotificationData> notification_data,
NotificationDataCallback callback) = 0; NotificationDataCallback callback) = 0;
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_ #ifndef CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_
#define CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_ #define CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_
#include <string>
namespace notifications { namespace notifications {
// Enum to describe the time to process scheduled notification data. // Enum to describe the time to process scheduled notification data.
...@@ -68,6 +70,16 @@ enum class ImpressionResult { ...@@ -68,6 +70,16 @@ enum class ImpressionResult {
kMaxValue = kNeutral kMaxValue = kNeutral
}; };
// Defines user actions type.
enum class UserActionType {
// The user clicks on the notification body.
kClick = 0,
// The user clicks on the notification button.
kButtonClick = 1,
// The user dismisses the notification.
kDismiss = 2,
};
// Categorizes type of notification buttons. Different type of button clicks // Categorizes type of notification buttons. Different type of button clicks
// may result in change of notification shown frequency. // may result in change of notification shown frequency.
enum class ActionButtonType { enum class ActionButtonType {
...@@ -81,6 +93,15 @@ enum class ActionButtonType { ...@@ -81,6 +93,15 @@ enum class ActionButtonType {
kUnhelpful = 2, kUnhelpful = 2,
}; };
// Information about button clicks.
struct ButtonClickInfo {
// Unique id of the button.
std::string button_id;
// Associate impression type for the button.
ActionButtonType type = ActionButtonType::kUnknownAction;
};
} // namespace notifications } // namespace notifications
#endif // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_ #endif // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULER_TYPES_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment