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/browser/notifications/notification_message_filter.h"
7 #include "base/callback.h"
8 #include "content/browser/notifications/page_notification_delegate.h"
9 #include "content/common/platform_notification_messages.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/desktop_notification_delegate.h"
14 #include "content/public/common/content_client.h"
18 NotificationMessageFilter::NotificationMessageFilter(
20 ResourceContext* resource_context,
21 BrowserContext* browser_context)
22 : BrowserMessageFilter(PlatformNotificationMsgStart),
23 process_id_(process_id),
24 resource_context_(resource_context),
25 browser_context_(browser_context) {}
27 NotificationMessageFilter::~NotificationMessageFilter() {}
29 void NotificationMessageFilter::DidCloseNotification(int notification_id) {
30 close_closures_.erase(notification_id);
33 bool NotificationMessageFilter::OnMessageReceived(const IPC::Message& message) {
35 IPC_BEGIN_MESSAGE_MAP(NotificationMessageFilter, message)
36 IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_CheckPermission,
37 OnCheckNotificationPermission)
38 IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_Show,
39 OnShowPlatformNotification)
40 IPC_MESSAGE_HANDLER(PlatformNotificationHostMsg_Close,
41 OnClosePlatformNotification)
42 IPC_MESSAGE_UNHANDLED(handled = false)
48 void NotificationMessageFilter::OverrideThreadForMessage(
49 const IPC::Message& message, content::BrowserThread::ID* thread) {
50 if (message.type() == PlatformNotificationHostMsg_Show::ID ||
51 message.type() == PlatformNotificationHostMsg_Close::ID)
52 *thread = BrowserThread::UI;
55 void NotificationMessageFilter::OnCheckNotificationPermission(
56 const GURL& origin, blink::WebNotificationPermission* permission) {
58 GetContentClient()->browser()->CheckDesktopNotificationPermission(
64 void NotificationMessageFilter::OnShowPlatformNotification(
65 int notification_id, const ShowDesktopNotificationHostMsgParams& params) {
66 scoped_ptr<DesktopNotificationDelegate> delegate(
67 new PageNotificationDelegate(process_id_, notification_id));
69 base::Closure close_closure;
70 GetContentClient()->browser()->ShowDesktopNotification(params,
76 if (!close_closure.is_null())
77 close_closures_[notification_id] = close_closure;
80 void NotificationMessageFilter::OnClosePlatformNotification(
81 int notification_id) {
82 if (!close_closures_.count(notification_id))
85 close_closures_[notification_id].Run();
86 close_closures_.erase(notification_id);
89 } // namespace content