Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / content / browser / notifications / notification_message_filter.cc
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.
4
5 #include "content/browser/notifications/notification_message_filter.h"
6
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"
15
16 namespace content {
17
18 NotificationMessageFilter::NotificationMessageFilter(
19     int process_id,
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) {}
26
27 NotificationMessageFilter::~NotificationMessageFilter() {}
28
29 void NotificationMessageFilter::DidCloseNotification(int notification_id) {
30   close_closures_.erase(notification_id);
31 }
32
33 bool NotificationMessageFilter::OnMessageReceived(const IPC::Message& message) {
34   bool handled = true;
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)
43   IPC_END_MESSAGE_MAP()
44
45   return handled;
46 }
47
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;
53 }
54
55 void NotificationMessageFilter::OnCheckNotificationPermission(
56     const GURL& origin, blink::WebNotificationPermission* permission) {
57   *permission =
58       GetContentClient()->browser()->CheckDesktopNotificationPermission(
59           origin,
60           resource_context_,
61           process_id_);
62 }
63
64 void NotificationMessageFilter::OnShowPlatformNotification(
65     int notification_id, const ShowDesktopNotificationHostMsgParams& params) {
66   scoped_ptr<DesktopNotificationDelegate> delegate(
67       new PageNotificationDelegate(process_id_, notification_id));
68
69   base::Closure close_closure;
70   GetContentClient()->browser()->ShowDesktopNotification(params,
71                                                          browser_context_,
72                                                          process_id_,
73                                                          delegate.Pass(),
74                                                          &close_closure);
75
76   if (!close_closure.is_null())
77     close_closures_[notification_id] = close_closure;
78 }
79
80 void NotificationMessageFilter::OnClosePlatformNotification(
81     int notification_id) {
82   if (!close_closures_.count(notification_id))
83     return;
84
85   close_closures_[notification_id].Run();
86   close_closures_.erase(notification_id);
87 }
88
89 }  // namespace content