[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 | |
[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] | cd065c011 | 2010-01-13 05:57:09 | [diff] [blame] | 9 | #include "chrome/common/url_constants.h" |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 10 | #include "chrome/common/render_messages.h" |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 11 | #include "chrome/renderer/render_thread.h" |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 12 | #include "content/common/desktop_notification_messages.h" |
[email protected] | 6091604 | 2011-03-19 00:43:36 | [diff] [blame] | 13 | #include "content/renderer/render_view.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPermissionCallback.h" |
| 17 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 18 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 19 | |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 20 | using WebKit::WebDocument; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 21 | using WebKit::WebNotification; |
| 22 | using WebKit::WebNotificationPresenter; |
| 23 | using WebKit::WebNotificationPermissionCallback; |
[email protected] | d5f614f | 2010-04-06 18:44:28 | [diff] [blame] | 24 | using WebKit::WebSecurityOrigin; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 25 | using WebKit::WebString; |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 26 | using WebKit::WebURL; |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 27 | |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 28 | NotificationProvider::NotificationProvider(RenderView* render_view) |
| 29 | : RenderViewObserver(render_view) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 8a2a6a2 | 2010-08-17 00:37:29 | [diff] [blame] | 32 | NotificationProvider::~NotificationProvider() { |
| 33 | manager_.DetachAll(); |
| 34 | } |
| 35 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 36 | bool NotificationProvider::show(const WebNotification& notification) { |
| 37 | int notification_id = manager_.RegisterNotification(notification); |
| 38 | if (notification.isHTML()) |
| 39 | return ShowHTML(notification, notification_id); |
| 40 | else |
| 41 | return ShowText(notification, notification_id); |
| 42 | } |
| 43 | |
| 44 | void NotificationProvider::cancel(const WebNotification& notification) { |
| 45 | int id; |
| 46 | bool id_found = manager_.GetId(notification, id); |
[email protected] | b52448bb | 2009-11-04 21:29:11 | [diff] [blame] | 47 | // 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] | 48 | if (id_found) |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 49 | Send(new DesktopNotificationHostMsg_Cancel(routing_id(), id)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void NotificationProvider::objectDestroyed( |
| 53 | const WebNotification& notification) { |
| 54 | int id; |
| 55 | bool id_found = manager_.GetId(notification, id); |
[email protected] | b52448bb | 2009-11-04 21:29:11 | [diff] [blame] | 56 | // 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] | 57 | if (id_found) |
| 58 | manager_.UnregisterNotification(id); |
| 59 | } |
| 60 | |
| 61 | WebNotificationPresenter::Permission NotificationProvider::checkPermission( |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 62 | const WebURL& url) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 63 | int permission; |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 64 | Send(new DesktopNotificationHostMsg_CheckPermission( |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 65 | routing_id(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 66 | url, |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 67 | &permission)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 68 | return static_cast<WebNotificationPresenter::Permission>(permission); |
| 69 | } |
| 70 | |
| 71 | void NotificationProvider::requestPermission( |
[email protected] | d5f614f | 2010-04-06 18:44:28 | [diff] [blame] | 72 | const WebSecurityOrigin& origin, |
| 73 | WebNotificationPermissionCallback* callback) { |
[email protected] | 4440a58b | 2009-11-13 22:04:58 | [diff] [blame] | 74 | // We only request permission in response to a user gesture. |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 75 | if (!render_view()->webview()->mainFrame()->isProcessingUserGesture()) |
[email protected] | 4440a58b | 2009-11-13 22:04:58 | [diff] [blame] | 76 | return; |
| 77 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 78 | int id = manager_.RegisterPermissionRequest(callback); |
| 79 | |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 80 | Send(new DesktopNotificationHostMsg_RequestPermission( |
| 81 | routing_id(), GURL(origin.toString()), id)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 84 | bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { |
| 85 | bool handled = true; |
| 86 | IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 87 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostDisplay, OnDisplay); |
| 88 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostError, OnError); |
| 89 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClose, OnClose); |
| 90 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClick, OnClick); |
| 91 | IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PermissionRequestDone, |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 92 | OnPermissionRequestComplete); |
| 93 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 94 | IPC_END_MESSAGE_MAP() |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 95 | |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 96 | if (message.type() == ViewMsg_Navigate::ID) |
| 97 | OnNavigate(); // Don't want to swallow the message. |
| 98 | |
| 99 | return handled; |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 102 | bool NotificationProvider::ShowHTML(const WebNotification& notification, |
| 103 | int id) { |
[email protected] | 1ec4e04 | 2010-06-09 21:47:02 | [diff] [blame] | 104 | // Disallow HTML notifications from unwanted schemes. javascript: |
| 105 | // in particular allows unwanted cross-domain access. |
[email protected] | cd065c011 | 2010-01-13 05:57:09 | [diff] [blame] | 106 | GURL url = notification.url(); |
[email protected] | 13347d15 | 2010-01-23 01:37:54 | [diff] [blame] | 107 | if (!url.SchemeIs(chrome::kHttpScheme) && |
| 108 | !url.SchemeIs(chrome::kHttpsScheme) && |
[email protected] | 1ec4e04 | 2010-06-09 21:47:02 | [diff] [blame] | 109 | !url.SchemeIs(chrome::kExtensionScheme) && |
| 110 | !url.SchemeIs(chrome::kDataScheme)) |
[email protected] | cd065c011 | 2010-01-13 05:57:09 | [diff] [blame] | 111 | return false; |
| 112 | |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 113 | DCHECK(notification.isHTML()); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 114 | DesktopNotificationHostMsg_Show_Params params; |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 115 | params.origin = |
| 116 | GURL(render_view()->webview()->mainFrame()->url()).GetOrigin(); |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 117 | params.is_html = true; |
| 118 | params.contents_url = notification.url(); |
| 119 | params.notification_id = id; |
| 120 | params.replace_id = notification.replaceId(); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 121 | return Send(new DesktopNotificationHostMsg_Show(routing_id(), params)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool NotificationProvider::ShowText(const WebNotification& notification, |
| 125 | int id) { |
| 126 | DCHECK(!notification.isHTML()); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 127 | DesktopNotificationHostMsg_Show_Params params; |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 128 | params.is_html = false; |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 129 | params.origin = GURL( |
| 130 | render_view()->webview()->mainFrame()->url()).GetOrigin(); |
[email protected] | 04b0c933 | 2010-07-12 17:13:49 | [diff] [blame] | 131 | params.icon_url = notification.iconURL(); |
| 132 | params.title = notification.title(); |
| 133 | params.body = notification.body(); |
| 134 | params.direction = notification.direction(); |
| 135 | params.notification_id = id; |
| 136 | params.replace_id = notification.replaceId(); |
[email protected] | e7c21b81 | 2011-03-19 18:03:30 | [diff] [blame^] | 137 | return Send(new DesktopNotificationHostMsg_Show(routing_id(), params)); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void NotificationProvider::OnDisplay(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 141 | WebNotification notification; |
| 142 | bool found = manager_.GetNotification(id, ¬ification); |
| 143 | // |found| may be false if the WebNotification went out of scope in |
| 144 | // the page before it was actually displayed to the user. |
| 145 | if (found) |
| 146 | notification.dispatchDisplayEvent(); |
| 147 | } |
| 148 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 149 | void NotificationProvider::OnError(int id, const WebString& message) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 150 | WebNotification notification; |
| 151 | bool found = manager_.GetNotification(id, ¬ification); |
| 152 | // |found| may be false if the WebNotification went out of scope in |
| 153 | // the page before the error occurred. |
| 154 | if (found) |
| 155 | notification.dispatchErrorEvent(message); |
| 156 | } |
| 157 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 158 | void NotificationProvider::OnClose(int id, bool by_user) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 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 closed by the user. |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 163 | if (found) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 164 | notification.dispatchCloseEvent(by_user); |
[email protected] | 42219753 | 2010-01-25 17:38:23 | [diff] [blame] | 165 | manager_.UnregisterNotification(id); |
| 166 | } |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 0ff6286 | 2010-09-01 05:50:35 | [diff] [blame] | 169 | void NotificationProvider::OnClick(int id) { |
| 170 | WebNotification notification; |
| 171 | bool found = manager_.GetNotification(id, ¬ification); |
| 172 | // |found| may be false if the WebNotification went out of scope in |
| 173 | // the page before the associated toast was clicked on. |
| 174 | if (found) |
| 175 | notification.dispatchClickEvent(); |
| 176 | } |
| 177 | |
[email protected] | b6849bda | 2009-10-14 23:59:26 | [diff] [blame] | 178 | void NotificationProvider::OnPermissionRequestComplete(int id) { |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 179 | WebNotificationPermissionCallback* callback = manager_.GetCallback(id); |
| 180 | DCHECK(callback); |
| 181 | callback->permissionRequestComplete(); |
| 182 | manager_.OnPermissionRequestComplete(id); |
| 183 | } |
| 184 | |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 185 | void NotificationProvider::OnNavigate() { |
| 186 | manager_.Clear(); |
[email protected] | 90e90855 | 2009-10-05 01:40:12 | [diff] [blame] | 187 | } |