| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/notification_provider.h" | 5 #include "chrome/renderer/notification_provider.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/task.h" | 8 #include "base/task.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/renderer/render_thread.h" | 12 #include "chrome/renderer/render_thread.h" |
| 13 #include "chrome/renderer/render_view.h" | 13 #include "chrome/renderer/render_view.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPermissionCal
lback.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPermissionCal
lback.h" |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" |
| 18 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" |
| 19 | 19 |
| 20 using WebKit::WebDocument; | 20 using WebKit::WebDocument; |
| 21 using WebKit::WebNotification; | 21 using WebKit::WebNotification; |
| 22 using WebKit::WebNotificationPresenter; | 22 using WebKit::WebNotificationPresenter; |
| 23 using WebKit::WebNotificationPermissionCallback; | 23 using WebKit::WebNotificationPermissionCallback; |
| 24 using WebKit::WebSecurityOrigin; | 24 using WebKit::WebSecurityOrigin; |
| 25 using WebKit::WebString; | 25 using WebKit::WebString; |
| 26 using WebKit::WebURL; | 26 using WebKit::WebURL; |
| 27 | 27 |
| 28 NotificationProvider::NotificationProvider(RenderView* view) | 28 NotificationProvider::NotificationProvider(RenderView* render_view) |
| 29 : view_(view) { | 29 : RenderViewObserver(render_view) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 NotificationProvider::~NotificationProvider() { | 32 NotificationProvider::~NotificationProvider() { |
| 33 manager_.DetachAll(); | 33 manager_.DetachAll(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool NotificationProvider::show(const WebNotification& notification) { | 36 bool NotificationProvider::show(const WebNotification& notification) { |
| 37 int notification_id = manager_.RegisterNotification(notification); | 37 int notification_id = manager_.RegisterNotification(notification); |
| 38 if (notification.isHTML()) | 38 if (notification.isHTML()) |
| 39 return ShowHTML(notification, notification_id); | 39 return ShowHTML(notification, notification_id); |
| 40 else | 40 else |
| 41 return ShowText(notification, notification_id); | 41 return ShowText(notification, notification_id); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void NotificationProvider::cancel(const WebNotification& notification) { | 44 void NotificationProvider::cancel(const WebNotification& notification) { |
| 45 int id; | 45 int id; |
| 46 bool id_found = manager_.GetId(notification, id); | 46 bool id_found = manager_.GetId(notification, id); |
| 47 // Won't be found if the notification has already been closed by the user. | 47 // Won't be found if the notification has already been closed by the user. |
| 48 if (id_found) | 48 if (id_found) |
| 49 Send(new ViewHostMsg_CancelDesktopNotification(view_->routing_id(), id)); | 49 Send(new ViewHostMsg_CancelDesktopNotification(routing_id(), id)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void NotificationProvider::objectDestroyed( | 52 void NotificationProvider::objectDestroyed( |
| 53 const WebNotification& notification) { | 53 const WebNotification& notification) { |
| 54 int id; | 54 int id; |
| 55 bool id_found = manager_.GetId(notification, id); | 55 bool id_found = manager_.GetId(notification, id); |
| 56 // Won't be found if the notification has already been closed by the user. | 56 // Won't be found if the notification has already been closed by the user. |
| 57 if (id_found) | 57 if (id_found) |
| 58 manager_.UnregisterNotification(id); | 58 manager_.UnregisterNotification(id); |
| 59 } | 59 } |
| 60 | 60 |
| 61 WebNotificationPresenter::Permission NotificationProvider::checkPermission( | 61 WebNotificationPresenter::Permission NotificationProvider::checkPermission( |
| 62 const WebURL& url) { | 62 const WebURL& url) { |
| 63 int permission; | 63 int permission; |
| 64 Send(new ViewHostMsg_CheckNotificationPermission( | 64 Send(new ViewHostMsg_CheckNotificationPermission( |
| 65 view_->routing_id(), | 65 routing_id(), |
| 66 url, | 66 url, |
| 67 &permission)); | 67 &permission)); |
| 68 return static_cast<WebNotificationPresenter::Permission>(permission); | 68 return static_cast<WebNotificationPresenter::Permission>(permission); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void NotificationProvider::requestPermission( | 71 void NotificationProvider::requestPermission( |
| 72 const WebSecurityOrigin& origin, | 72 const WebSecurityOrigin& origin, |
| 73 WebNotificationPermissionCallback* callback) { | 73 WebNotificationPermissionCallback* callback) { |
| 74 // We only request permission in response to a user gesture. | 74 // We only request permission in response to a user gesture. |
| 75 if (!view_->webview()->mainFrame()->isProcessingUserGesture()) | 75 if (!render_view()->webview()->mainFrame()->isProcessingUserGesture()) |
| 76 return; | 76 return; |
| 77 | 77 |
| 78 int id = manager_.RegisterPermissionRequest(callback); | 78 int id = manager_.RegisterPermissionRequest(callback); |
| 79 | 79 |
| 80 Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(), | 80 Send(new ViewHostMsg_RequestNotificationPermission(routing_id(), |
| 81 GURL(origin.toString()), | 81 GURL(origin.toString()), |
| 82 id)); | 82 id)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { | 85 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { |
| 86 bool handled = true; | 86 bool handled = true; |
| 87 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) | 87 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) |
| 88 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay); | 88 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay); |
| 89 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError); | 89 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError); |
| 90 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose); | 90 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose); |
| 91 IPC_MESSAGE_HANDLER(ViewMsg_PostClickToNotificationObject, OnClick); | 91 IPC_MESSAGE_HANDLER(ViewMsg_PostClickToNotificationObject, OnClick); |
| 92 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone, | 92 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone, |
| 93 OnPermissionRequestComplete); | 93 OnPermissionRequestComplete); |
| 94 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
| 95 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
| 96 |
| 97 if (message.type() == ViewMsg_Navigate::ID) |
| 98 OnNavigate(); // Don't want to swallow the message. |
| 99 |
| 96 return handled; | 100 return handled; |
| 97 } | 101 } |
| 98 | 102 |
| 99 void NotificationProvider::OnNavigate() { | |
| 100 manager_.Clear(); | |
| 101 } | |
| 102 | |
| 103 bool NotificationProvider::ShowHTML(const WebNotification& notification, | 103 bool NotificationProvider::ShowHTML(const WebNotification& notification, |
| 104 int id) { | 104 int id) { |
| 105 // Disallow HTML notifications from unwanted schemes. javascript: | 105 // Disallow HTML notifications from unwanted schemes. javascript: |
| 106 // in particular allows unwanted cross-domain access. | 106 // in particular allows unwanted cross-domain access. |
| 107 GURL url = notification.url(); | 107 GURL url = notification.url(); |
| 108 if (!url.SchemeIs(chrome::kHttpScheme) && | 108 if (!url.SchemeIs(chrome::kHttpScheme) && |
| 109 !url.SchemeIs(chrome::kHttpsScheme) && | 109 !url.SchemeIs(chrome::kHttpsScheme) && |
| 110 !url.SchemeIs(chrome::kExtensionScheme) && | 110 !url.SchemeIs(chrome::kExtensionScheme) && |
| 111 !url.SchemeIs(chrome::kDataScheme)) | 111 !url.SchemeIs(chrome::kDataScheme)) |
| 112 return false; | 112 return false; |
| 113 | 113 |
| 114 DCHECK(notification.isHTML()); | 114 DCHECK(notification.isHTML()); |
| 115 ViewHostMsg_ShowNotification_Params params; | 115 ViewHostMsg_ShowNotification_Params params; |
| 116 params.origin = GURL(view_->webview()->mainFrame()->url()).GetOrigin(); | 116 params.origin = |
| 117 GURL(render_view()->webview()->mainFrame()->url()).GetOrigin(); |
| 117 params.is_html = true; | 118 params.is_html = true; |
| 118 params.contents_url = notification.url(); | 119 params.contents_url = notification.url(); |
| 119 params.notification_id = id; | 120 params.notification_id = id; |
| 120 params.replace_id = notification.replaceId(); | 121 params.replace_id = notification.replaceId(); |
| 121 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), | 122 return Send(new ViewHostMsg_ShowDesktopNotification(routing_id(), params)); |
| 122 params)); | |
| 123 } | 123 } |
| 124 | 124 |
| 125 bool NotificationProvider::ShowText(const WebNotification& notification, | 125 bool NotificationProvider::ShowText(const WebNotification& notification, |
| 126 int id) { | 126 int id) { |
| 127 DCHECK(!notification.isHTML()); | 127 DCHECK(!notification.isHTML()); |
| 128 ViewHostMsg_ShowNotification_Params params; | 128 ViewHostMsg_ShowNotification_Params params; |
| 129 params.is_html = false; | 129 params.is_html = false; |
| 130 params.origin = GURL(view_->webview()->mainFrame()->url()).GetOrigin(); | 130 params.origin = GURL( |
| 131 render_view()->webview()->mainFrame()->url()).GetOrigin(); |
| 131 params.icon_url = notification.iconURL(); | 132 params.icon_url = notification.iconURL(); |
| 132 params.title = notification.title(); | 133 params.title = notification.title(); |
| 133 params.body = notification.body(); | 134 params.body = notification.body(); |
| 134 params.direction = notification.direction(); | 135 params.direction = notification.direction(); |
| 135 params.notification_id = id; | 136 params.notification_id = id; |
| 136 params.replace_id = notification.replaceId(); | 137 params.replace_id = notification.replaceId(); |
| 137 | 138 |
| 138 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), | 139 return Send(new ViewHostMsg_ShowDesktopNotification(routing_id(), params)); |
| 139 params)); | |
| 140 } | 140 } |
| 141 | 141 |
| 142 void NotificationProvider::OnDisplay(int id) { | 142 void NotificationProvider::OnDisplay(int id) { |
| 143 WebNotification notification; | 143 WebNotification notification; |
| 144 bool found = manager_.GetNotification(id, ¬ification); | 144 bool found = manager_.GetNotification(id, ¬ification); |
| 145 // |found| may be false if the WebNotification went out of scope in | 145 // |found| may be false if the WebNotification went out of scope in |
| 146 // the page before it was actually displayed to the user. | 146 // the page before it was actually displayed to the user. |
| 147 if (found) | 147 if (found) |
| 148 notification.dispatchDisplayEvent(); | 148 notification.dispatchDisplayEvent(); |
| 149 } | 149 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 177 notification.dispatchClickEvent(); | 177 notification.dispatchClickEvent(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void NotificationProvider::OnPermissionRequestComplete(int id) { | 180 void NotificationProvider::OnPermissionRequestComplete(int id) { |
| 181 WebNotificationPermissionCallback* callback = manager_.GetCallback(id); | 181 WebNotificationPermissionCallback* callback = manager_.GetCallback(id); |
| 182 DCHECK(callback); | 182 DCHECK(callback); |
| 183 callback->permissionRequestComplete(); | 183 callback->permissionRequestComplete(); |
| 184 manager_.OnPermissionRequestComplete(id); | 184 manager_.OnPermissionRequestComplete(id); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool NotificationProvider::Send(IPC::Message* message) { | 187 void NotificationProvider::OnNavigate() { |
| 188 return RenderThread::current()->Send(message); | 188 manager_.Clear(); |
| 189 } | 189 } |
| OLD | NEW |