[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 1 | // Copyright (c) 2009 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 "chrome/renderer/notification_provider.h" |
| 6 | |
| 7 | #include "base/task.h" |
| 8 | #include "chrome/common/render_messages.h" |
| 9 | #include "chrome/renderer/render_thread.h" |
| 10 | #include "chrome/renderer/render_view.h" |
| 11 | #include "webkit/api/public/WebNotificationPermissionCallback.h" |
| 12 | |
| 13 | using WebKit::WebNotification; |
| 14 | using WebKit::WebNotificationPresenter; |
| 15 | using WebKit::WebNotificationPermissionCallback; |
| 16 | using WebKit::WebString; |
| 17 | |
| 18 | NotificationProvider::NotificationProvider(RenderView* view) |
| 19 | : view_(view) { |
| 20 | } |
| 21 | |
| 22 | bool NotificationProvider::show(const WebNotification& notification) { |
| 23 | int notification_id = manager_.RegisterNotification(notification); |
| 24 | if (notification.isHTML()) |
| 25 | return ShowHTML(notification, notification_id); |
| 26 | else |
| 27 | return ShowText(notification, notification_id); |
| 28 | } |
| 29 | |
| 30 | void NotificationProvider::cancel(const WebNotification& notification) { |
| 31 | int id; |
| 32 | bool id_found = manager_.GetId(notification, id); |
| 33 | DCHECK(id_found); |
| 34 | if (id_found) |
| 35 | Send(new ViewHostMsg_CancelDesktopNotification(view_->routing_id(), id)); |
| 36 | } |
| 37 | |
| 38 | void NotificationProvider::objectDestroyed( |
| 39 | const WebNotification& notification) { |
| 40 | int id; |
| 41 | bool id_found = manager_.GetId(notification, id); |
| 42 | DCHECK(id_found); |
| 43 | if (id_found) |
| 44 | manager_.UnregisterNotification(id); |
| 45 | } |
| 46 | |
| 47 | WebNotificationPresenter::Permission NotificationProvider::checkPermission( |
| 48 | const WebString& origin) { |
| 49 | int permission; |
[email protected] | 4bb33630 | 2009-10-12 05:44:26 | [diff] [blame] | 50 | Send(new ViewHostMsg_CheckNotificationPermission(view_->routing_id(), |
| 51 | GURL(origin), &permission)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 52 | return static_cast<WebNotificationPresenter::Permission>(permission); |
| 53 | } |
| 54 | |
| 55 | void NotificationProvider::requestPermission( |
| 56 | const WebString& origin, WebNotificationPermissionCallback* callback) { |
| 57 | int id = manager_.RegisterPermissionRequest(callback); |
| 58 | |
| 59 | Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(), |
[email protected] | 4bb33630 | 2009-10-12 05:44:26 | [diff] [blame] | 60 | GURL(origin), id)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame^] | 63 | bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { |
| 64 | bool handled = true; |
| 65 | IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) |
| 66 | IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay); |
| 67 | IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError); |
| 68 | IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose); |
| 69 | IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone, |
| 70 | OnPermissionRequestComplete); |
| 71 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 72 | IPC_END_MESSAGE_MAP() |
| 73 | return handled; |
| 74 | } |
| 75 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 76 | bool NotificationProvider::ShowHTML(const WebNotification& notification, |
| 77 | int id) { |
| 78 | DCHECK(notification.isHTML()); |
| 79 | return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), |
| 80 | GURL(view_->webview()->mainFrame()->url()), |
| 81 | notification.url(), id)); |
| 82 | } |
| 83 | |
| 84 | bool NotificationProvider::ShowText(const WebNotification& notification, |
| 85 | int id) { |
| 86 | DCHECK(!notification.isHTML()); |
| 87 | return Send(new ViewHostMsg_ShowDesktopNotificationText(view_->routing_id(), |
| 88 | GURL(view_->webview()->mainFrame()->url()), |
| 89 | GURL(notification.icon()), |
| 90 | notification.title(), notification.body(), id)); |
| 91 | } |
| 92 | |
| 93 | void NotificationProvider::OnDisplay(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 94 | WebNotification notification; |
| 95 | bool found = manager_.GetNotification(id, ¬ification); |
| 96 | // |found| may be false if the WebNotification went out of scope in |
| 97 | // the page before it was actually displayed to the user. |
| 98 | if (found) |
| 99 | notification.dispatchDisplayEvent(); |
| 100 | } |
| 101 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame^] | 102 | void NotificationProvider::OnError(int id, const WebString& message) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 103 | WebNotification notification; |
| 104 | bool found = manager_.GetNotification(id, ¬ification); |
| 105 | // |found| may be false if the WebNotification went out of scope in |
| 106 | // the page before the error occurred. |
| 107 | if (found) |
| 108 | notification.dispatchErrorEvent(message); |
| 109 | } |
| 110 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame^] | 111 | void NotificationProvider::OnClose(int id, bool by_user) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 112 | WebNotification notification; |
| 113 | bool found = manager_.GetNotification(id, ¬ification); |
| 114 | // |found| may be false if the WebNotification went out of scope in |
| 115 | // the page before the associated toast was closed by the user. |
| 116 | if (found) |
| 117 | notification.dispatchCloseEvent(by_user); |
| 118 | manager_.UnregisterNotification(id); |
| 119 | } |
| 120 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame^] | 121 | void NotificationProvider::OnPermissionRequestComplete(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 122 | WebNotificationPermissionCallback* callback = manager_.GetCallback(id); |
| 123 | DCHECK(callback); |
| 124 | callback->permissionRequestComplete(); |
| 125 | manager_.OnPermissionRequestComplete(id); |
| 126 | } |
| 127 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 128 | bool NotificationProvider::Send(IPC::Message* message) { |
| 129 | return RenderThread::current()->Send(message); |
| 130 | } |