[email protected] | b6cb3a84 | 2011-06-24 18:28:41 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 230b7ef | 2011-03-16 22:30:19 | [diff] [blame] | 5 | #include "content/renderer/notification_provider.h" |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 6 | |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 7 | #include "base/string_util.h" |
[email protected] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 8 | #include "base/task.h" |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 9 | #include "content/common/desktop_notification_messages.h" |
[email protected] | 2c556966 | 2011-03-22 20:45:02 | [diff] [blame] | 10 | #include "content/common/view_messages.h" |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 11 | #include "content/renderer/render_view_impl.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 12 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPermissionCallback.h" |
[email protected] | e6e90dc | 2011-12-03 00:01:37 | [diff] [blame] | 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 17 | |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 18 | using WebKit::WebDocument; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 19 | using WebKit::WebNotification; |
| 20 | using WebKit::WebNotificationPresenter; |
| 21 | using WebKit::WebNotificationPermissionCallback; |
[email protected] | d5f614f | 2010-04-06 18:44:28 | [diff] [blame] | 22 | using WebKit::WebSecurityOrigin; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 23 | using WebKit::WebString; |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 24 | using WebKit::WebURL; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 25 | |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 26 | NotificationProvider::NotificationProvider(RenderViewImpl* render_view) |
[email protected] | 3a034ebb | 2011-10-03 19:19:44 | [diff] [blame] | 27 | : content::RenderViewObserver(render_view) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | 8a2a6a2 | 2010-08-17 00:37:29 | [diff] [blame] | 30 | NotificationProvider::~NotificationProvider() { |
| 31 | manager_.DetachAll(); |
| 32 | } |
| 33 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 34 | bool NotificationProvider::show(const WebNotification& notification) { |
| 35 | int notification_id = manager_.RegisterNotification(notification); |
| 36 | if (notification.isHTML()) |
| 37 | return ShowHTML(notification, notification_id); |
| 38 | else |
| 39 | return ShowText(notification, notification_id); |
| 40 | } |
| 41 | |
| 42 | void NotificationProvider::cancel(const WebNotification& notification) { |
| 43 | int id; |
| 44 | bool id_found = manager_.GetId(notification, id); |
[email protected] | b52448bb | 2009-11-04 21:29:11 | [diff] [blame] | 45 | // Won't be found if the notification has already been closed by the user. |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 46 | if (id_found) |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 47 | Send(new DesktopNotificationHostMsg_Cancel(routing_id(), id)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void NotificationProvider::objectDestroyed( |
| 51 | const WebNotification& notification) { |
| 52 | int id; |
| 53 | bool id_found = manager_.GetId(notification, id); |
[email protected] | b52448bb | 2009-11-04 21:29:11 | [diff] [blame] | 54 | // Won't be found if the notification has already been closed by the user. |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 55 | if (id_found) |
| 56 | manager_.UnregisterNotification(id); |
| 57 | } |
| 58 | |
| 59 | WebNotificationPresenter::Permission NotificationProvider::checkPermission( |
[email protected] | bc6095e0 | 2011-11-04 22:10:02 | [diff] [blame] | 60 | const WebSecurityOrigin& origin) { |
| 61 | int permission; |
| 62 | Send(new DesktopNotificationHostMsg_CheckPermission( |
| 63 | routing_id(), |
| 64 | GURL(origin.toString()), |
| 65 | &permission)); |
| 66 | return static_cast<WebNotificationPresenter::Permission>(permission); |
| 67 | } |
| 68 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 69 | void NotificationProvider::requestPermission( |
[email protected] | d5f614f | 2010-04-06 18:44:28 | [diff] [blame] | 70 | const WebSecurityOrigin& origin, |
| 71 | WebNotificationPermissionCallback* callback) { |
[email protected] | 4440a58b | 2009-11-13 22:04:58 | [diff] [blame] | 72 | // We only request permission in response to a user gesture. |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 73 | if (!render_view()->GetWebView()->mainFrame()->isProcessingUserGesture()) |
[email protected] | 4440a58b | 2009-11-13 22:04:58 | [diff] [blame] | 74 | return; |
| 75 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 76 | int id = manager_.RegisterPermissionRequest(callback); |
| 77 | |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 78 | Send(new DesktopNotificationHostMsg_RequestPermission( |
| 79 | routing_id(), GURL(origin.toString()), id)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 82 | bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { |
| 83 | bool handled = true; |
| 84 | IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 85 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostDisplay, OnDisplay); |
| 86 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostError, OnError); |
| 87 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClose, OnClose); |
| 88 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClick, OnClick); |
| 89 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PermissionRequestDone, |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 90 | OnPermissionRequestComplete); |
| 91 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 92 | IPC_END_MESSAGE_MAP() |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 93 | |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 94 | if (message.type() == ViewMsg_Navigate::ID) |
| 95 | OnNavigate(); // Don't want to swallow the message. |
| 96 | |
| 97 | return handled; |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 100 | bool NotificationProvider::ShowHTML(const WebNotification& notification, |
| 101 | int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 102 | DCHECK(notification.isHTML()); |
[email protected] | 0ee57e2 | 2011-11-12 01:59:17 | [diff] [blame] | 103 | content::ShowDesktopNotificationHostMsgParams params; |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 104 | WebDocument document = render_view()->GetWebView()->mainFrame()->document(); |
[email protected] | b6cb3a84 | 2011-06-24 18:28:41 | [diff] [blame] | 105 | params.origin = GURL(document.securityOrigin().toString()); |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 106 | params.is_html = true; |
| 107 | params.contents_url = notification.url(); |
| 108 | params.notification_id = id; |
| 109 | params.replace_id = notification.replaceId(); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 110 | return Send(new DesktopNotificationHostMsg_Show(routing_id(), params)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool NotificationProvider::ShowText(const WebNotification& notification, |
| 114 | int id) { |
| 115 | DCHECK(!notification.isHTML()); |
[email protected] | 0ee57e2 | 2011-11-12 01:59:17 | [diff] [blame] | 116 | content::ShowDesktopNotificationHostMsgParams params; |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 117 | params.is_html = false; |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 118 | WebDocument document = render_view()->GetWebView()->mainFrame()->document(); |
[email protected] | b6cb3a84 | 2011-06-24 18:28:41 | [diff] [blame] | 119 | params.origin = GURL(document.securityOrigin().toString()); |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 120 | params.icon_url = notification.iconURL(); |
| 121 | params.title = notification.title(); |
| 122 | params.body = notification.body(); |
| 123 | params.direction = notification.direction(); |
| 124 | params.notification_id = id; |
| 125 | params.replace_id = notification.replaceId(); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame] | 126 | return Send(new DesktopNotificationHostMsg_Show(routing_id(), params)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void NotificationProvider::OnDisplay(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 130 | WebNotification notification; |
| 131 | bool found = manager_.GetNotification(id, ¬ification); |
| 132 | // |found| may be false if the WebNotification went out of scope in |
| 133 | // the page before it was actually displayed to the user. |
| 134 | if (found) |
| 135 | notification.dispatchDisplayEvent(); |
| 136 | } |
| 137 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 138 | void NotificationProvider::OnError(int id, const WebString& message) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 139 | WebNotification notification; |
| 140 | bool found = manager_.GetNotification(id, ¬ification); |
| 141 | // |found| may be false if the WebNotification went out of scope in |
| 142 | // the page before the error occurred. |
| 143 | if (found) |
| 144 | notification.dispatchErrorEvent(message); |
| 145 | } |
| 146 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 147 | void NotificationProvider::OnClose(int id, bool by_user) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 148 | WebNotification notification; |
| 149 | bool found = manager_.GetNotification(id, ¬ification); |
| 150 | // |found| may be false if the WebNotification went out of scope in |
| 151 | // the page before the associated toast was closed by the user. |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 152 | if (found) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 153 | notification.dispatchCloseEvent(by_user); |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 154 | manager_.UnregisterNotification(id); |
| 155 | } |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 0ff6286 | 2010-09-01 05:50:35 | [diff] [blame] | 158 | void NotificationProvider::OnClick(int id) { |
| 159 | WebNotification notification; |
| 160 | bool found = manager_.GetNotification(id, ¬ification); |
| 161 | // |found| may be false if the WebNotification went out of scope in |
| 162 | // the page before the associated toast was clicked on. |
| 163 | if (found) |
| 164 | notification.dispatchClickEvent(); |
| 165 | } |
| 166 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 167 | void NotificationProvider::OnPermissionRequestComplete(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 168 | WebNotificationPermissionCallback* callback = manager_.GetCallback(id); |
| 169 | DCHECK(callback); |
| 170 | callback->permissionRequestComplete(); |
| 171 | manager_.OnPermissionRequestComplete(id); |
| 172 | } |
| 173 | |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 174 | void NotificationProvider::OnNavigate() { |
| 175 | manager_.Clear(); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 176 | } |