1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/notifications/notification_manager.h"
7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h"
9 #include "content/child/notifications/notification_dispatcher.h"
10 #include "content/child/thread_safe_sender.h"
11 #include "content/child/worker_task_runner.h"
12 #include "content/common/platform_notification_messages.h"
13 #include "content/public/common/show_desktop_notification_params.h"
14 #include "third_party/WebKit/public/platform/WebNotificationData.h"
15 #include "third_party/WebKit/public/platform/WebNotificationDelegate.h"
16 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
19 using blink::WebNotificationPermission;
24 int CurrentWorkerId() {
25 return WorkerTaskRunner::Instance()->CurrentWorkerId();
30 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky
31 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER;
33 NotificationManager::NotificationManager(
34 ThreadSafeSender* thread_safe_sender,
35 NotificationDispatcher* notification_dispatcher)
36 : thread_safe_sender_(thread_safe_sender),
37 notification_dispatcher_(notification_dispatcher) {
38 g_notification_manager_tls.Pointer()->Set(this);
41 NotificationManager::~NotificationManager() {
42 g_notification_manager_tls.Pointer()->Set(nullptr);
45 NotificationManager* NotificationManager::ThreadSpecificInstance(
46 ThreadSafeSender* thread_safe_sender,
47 NotificationDispatcher* notification_dispatcher) {
48 if (g_notification_manager_tls.Pointer()->Get())
49 return g_notification_manager_tls.Pointer()->Get();
51 NotificationManager* manager = new NotificationManager(
52 thread_safe_sender, notification_dispatcher);
53 if (WorkerTaskRunner::Instance()->CurrentWorkerId())
54 WorkerTaskRunner::Instance()->AddStopObserver(manager);
58 void NotificationManager::OnWorkerRunLoopStopped() {
62 void NotificationManager::show(
63 const blink::WebSerializedOrigin& origin,
64 const blink::WebNotificationData& notification_data,
65 blink::WebNotificationDelegate* delegate) {
67 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
69 active_notifications_[notification_id] = delegate;
71 ShowDesktopNotificationHostMsgParams params;
72 params.origin = GURL(origin.string());
74 // TODO(peter): Move the notification_icon_loader to //content/child/ and use
75 // it to download Notification icons here.
76 params.icon = SkBitmap();
77 params.title = notification_data.title;
78 params.body = notification_data.body;
80 // TODO(peter): Remove the usage of the Blink WebTextDirection enumeration for
81 // the text direction of notifications throughout Chrome.
82 params.direction = blink::WebTextDirectionLeftToRight;
83 params.replace_id = notification_data.tag;
85 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show(
86 notification_id, params));
89 void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
90 auto iter = active_notifications_.begin();
91 for (; iter != active_notifications_.end(); ++iter) {
92 if (iter->second != delegate)
95 thread_safe_sender_->Send(
96 new PlatformNotificationHostMsg_Close(iter->first));
97 active_notifications_.erase(iter);
99 delegate->dispatchCloseEvent();
103 // It should not be possible for Blink to call close() on a Notification which
104 // does not exist anymore in the manager.
108 void NotificationManager::notifyDelegateDestroyed(
109 blink::WebNotificationDelegate* delegate) {
110 auto iter = active_notifications_.begin();
111 for (; iter != active_notifications_.end(); ++iter) {
112 if (iter->second != delegate)
115 active_notifications_.erase(iter);
120 WebNotificationPermission NotificationManager::checkPermission(
121 const blink::WebSerializedOrigin& origin) {
122 WebNotificationPermission permission =
123 blink::WebNotificationPermissionAllowed;
124 thread_safe_sender_->Send(new PlatformNotificationHostMsg_CheckPermission(
125 GURL(origin.string()), &permission));
130 bool NotificationManager::OnMessageReceived(const IPC::Message& message) {
132 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message)
133 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnShow);
134 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnClose);
135 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnClick);
136 IPC_MESSAGE_UNHANDLED(handled = false)
137 IPC_END_MESSAGE_MAP()
142 void NotificationManager::OnShow(int id) {
143 const auto& iter = active_notifications_.find(id);
144 if (iter == active_notifications_.end())
147 iter->second->dispatchShowEvent();
150 void NotificationManager::OnClose(int id) {
151 const auto& iter = active_notifications_.find(id);
152 if (iter == active_notifications_.end())
155 iter->second->dispatchCloseEvent();
156 active_notifications_.erase(iter);
159 void NotificationManager::OnClick(int id) {
160 const auto& iter = active_notifications_.find(id);
161 if (iter == active_notifications_.end())
164 iter->second->dispatchClickEvent();
167 } // namespace content