blob: 36e67591c835d82193a334b58b1284b6ce38477c [file] [log] [blame]
[email protected]b6cb3a842011-06-24 18:28:411// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]90e908552009-10-05 01:40:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]230b7ef2011-03-16 22:30:195#include "content/renderer/notification_provider.h"
[email protected]90e908552009-10-05 01:40:126
[email protected]0ce3f5982010-01-28 23:04:277#include "base/string_util.h"
[email protected]542bdfe2010-11-30 03:55:478#include "base/task.h"
[email protected]e7c21b812011-03-19 18:03:309#include "content/common/desktop_notification_messages.h"
[email protected]2c5569662011-03-22 20:45:0210#include "content/common/view_messages.h"
[email protected]310ebd6302011-10-10 19:06:2811#include "content/renderer/render_view_impl.h"
[email protected]8bd0fe62011-01-17 06:44:3712#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]e6e90dc2011-12-03 00:01:3715#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
[email protected]8bd0fe62011-01-17 06:44:3716#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]90e908552009-10-05 01:40:1217
[email protected]0ce3f5982010-01-28 23:04:2718using WebKit::WebDocument;
[email protected]90e908552009-10-05 01:40:1219using WebKit::WebNotification;
20using WebKit::WebNotificationPresenter;
21using WebKit::WebNotificationPermissionCallback;
[email protected]d5f614f2010-04-06 18:44:2822using WebKit::WebSecurityOrigin;
[email protected]90e908552009-10-05 01:40:1223using WebKit::WebString;
[email protected]0ce3f5982010-01-28 23:04:2724using WebKit::WebURL;
[email protected]90e908552009-10-05 01:40:1225
[email protected]310ebd6302011-10-10 19:06:2826NotificationProvider::NotificationProvider(RenderViewImpl* render_view)
[email protected]3a034ebb2011-10-03 19:19:4427 : content::RenderViewObserver(render_view) {
[email protected]90e908552009-10-05 01:40:1228}
29
[email protected]8a2a6a22010-08-17 00:37:2930NotificationProvider::~NotificationProvider() {
31 manager_.DetachAll();
32}
33
[email protected]90e908552009-10-05 01:40:1234bool 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
42void NotificationProvider::cancel(const WebNotification& notification) {
43 int id;
44 bool id_found = manager_.GetId(notification, id);
[email protected]b52448bb2009-11-04 21:29:1145 // Won't be found if the notification has already been closed by the user.
[email protected]90e908552009-10-05 01:40:1246 if (id_found)
[email protected]e7c21b812011-03-19 18:03:3047 Send(new DesktopNotificationHostMsg_Cancel(routing_id(), id));
[email protected]90e908552009-10-05 01:40:1248}
49
50void NotificationProvider::objectDestroyed(
51 const WebNotification& notification) {
52 int id;
53 bool id_found = manager_.GetId(notification, id);
[email protected]b52448bb2009-11-04 21:29:1154 // Won't be found if the notification has already been closed by the user.
[email protected]90e908552009-10-05 01:40:1255 if (id_found)
56 manager_.UnregisterNotification(id);
57}
58
59WebNotificationPresenter::Permission NotificationProvider::checkPermission(
[email protected]bc6095e02011-11-04 22:10:0260 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]90e908552009-10-05 01:40:1269void NotificationProvider::requestPermission(
[email protected]d5f614f2010-04-06 18:44:2870 const WebSecurityOrigin& origin,
71 WebNotificationPermissionCallback* callback) {
[email protected]4440a58b2009-11-13 22:04:5872 // We only request permission in response to a user gesture.
[email protected]a2ef54c2011-10-10 16:20:3173 if (!render_view()->GetWebView()->mainFrame()->isProcessingUserGesture())
[email protected]4440a58b2009-11-13 22:04:5874 return;
75
[email protected]90e908552009-10-05 01:40:1276 int id = manager_.RegisterPermissionRequest(callback);
77
[email protected]e7c21b812011-03-19 18:03:3078 Send(new DesktopNotificationHostMsg_RequestPermission(
79 routing_id(), GURL(origin.toString()), id));
[email protected]90e908552009-10-05 01:40:1280}
81
[email protected]b6849bda2009-10-14 23:59:2682bool NotificationProvider::OnMessageReceived(const IPC::Message& message) {
83 bool handled = true;
84 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message)
[email protected]e7c21b812011-03-19 18:03:3085 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]b6849bda2009-10-14 23:59:2690 OnPermissionRequestComplete);
91 IPC_MESSAGE_UNHANDLED(handled = false)
92 IPC_END_MESSAGE_MAP()
[email protected]b6849bda2009-10-14 23:59:2693
[email protected]676126f72011-01-15 00:03:5194 if (message.type() == ViewMsg_Navigate::ID)
95 OnNavigate(); // Don't want to swallow the message.
96
97 return handled;
[email protected]422197532010-01-25 17:38:2398}
99
[email protected]90e908552009-10-05 01:40:12100bool NotificationProvider::ShowHTML(const WebNotification& notification,
101 int id) {
[email protected]90e908552009-10-05 01:40:12102 DCHECK(notification.isHTML());
[email protected]0ee57e22011-11-12 01:59:17103 content::ShowDesktopNotificationHostMsgParams params;
[email protected]a2ef54c2011-10-10 16:20:31104 WebDocument document = render_view()->GetWebView()->mainFrame()->document();
[email protected]b6cb3a842011-06-24 18:28:41105 params.origin = GURL(document.securityOrigin().toString());
[email protected]04b0c9332010-07-12 17:13:49106 params.is_html = true;
107 params.contents_url = notification.url();
108 params.notification_id = id;
109 params.replace_id = notification.replaceId();
[email protected]e7c21b812011-03-19 18:03:30110 return Send(new DesktopNotificationHostMsg_Show(routing_id(), params));
[email protected]90e908552009-10-05 01:40:12111}
112
113bool NotificationProvider::ShowText(const WebNotification& notification,
114 int id) {
115 DCHECK(!notification.isHTML());
[email protected]0ee57e22011-11-12 01:59:17116 content::ShowDesktopNotificationHostMsgParams params;
[email protected]04b0c9332010-07-12 17:13:49117 params.is_html = false;
[email protected]a2ef54c2011-10-10 16:20:31118 WebDocument document = render_view()->GetWebView()->mainFrame()->document();
[email protected]b6cb3a842011-06-24 18:28:41119 params.origin = GURL(document.securityOrigin().toString());
[email protected]04b0c9332010-07-12 17:13:49120 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]e7c21b812011-03-19 18:03:30126 return Send(new DesktopNotificationHostMsg_Show(routing_id(), params));
[email protected]90e908552009-10-05 01:40:12127}
128
129void NotificationProvider::OnDisplay(int id) {
[email protected]90e908552009-10-05 01:40:12130 WebNotification notification;
131 bool found = manager_.GetNotification(id, &notification);
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]b6849bda2009-10-14 23:59:26138void NotificationProvider::OnError(int id, const WebString& message) {
[email protected]90e908552009-10-05 01:40:12139 WebNotification notification;
140 bool found = manager_.GetNotification(id, &notification);
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]b6849bda2009-10-14 23:59:26147void NotificationProvider::OnClose(int id, bool by_user) {
[email protected]90e908552009-10-05 01:40:12148 WebNotification notification;
149 bool found = manager_.GetNotification(id, &notification);
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]422197532010-01-25 17:38:23152 if (found) {
[email protected]90e908552009-10-05 01:40:12153 notification.dispatchCloseEvent(by_user);
[email protected]422197532010-01-25 17:38:23154 manager_.UnregisterNotification(id);
155 }
[email protected]90e908552009-10-05 01:40:12156}
157
[email protected]0ff62862010-09-01 05:50:35158void NotificationProvider::OnClick(int id) {
159 WebNotification notification;
160 bool found = manager_.GetNotification(id, &notification);
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]b6849bda2009-10-14 23:59:26167void NotificationProvider::OnPermissionRequestComplete(int id) {
[email protected]90e908552009-10-05 01:40:12168 WebNotificationPermissionCallback* callback = manager_.GetCallback(id);
169 DCHECK(callback);
170 callback->permissionRequestComplete();
171 manager_.OnPermissionRequestComplete(id);
172}
173
[email protected]676126f72011-01-15 00:03:51174void NotificationProvider::OnNavigate() {
175 manager_.Clear();
[email protected]90e908552009-10-05 01:40:12176}