blob: 9a1edab08cb17ef70066b5b3764073e834834fee [file] [log] [blame]
[email protected]90e908552009-10-05 01:40:121// 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
[email protected]0ce3f5982010-01-28 23:04:277#include "base/string_util.h"
[email protected]90e908552009-10-05 01:40:128#include "base/task.h"
9#include "chrome/common/render_messages.h"
[email protected]cd065c0112010-01-13 05:57:0910#include "chrome/common/url_constants.h"
[email protected]90e908552009-10-05 01:40:1211#include "chrome/renderer/render_thread.h"
12#include "chrome/renderer/render_view.h"
[email protected]0ce3f5982010-01-28 23:04:2713#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
[email protected]418ed5ab2009-11-12 01:14:4914#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
15#include "third_party/WebKit/WebKit/chromium/public/WebNotificationPermissionCallback.h"
[email protected]0ce3f5982010-01-28 23:04:2716#include "third_party/WebKit/WebKit/chromium/public/WebURL.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;
22using WebKit::WebString;
[email protected]0ce3f5982010-01-28 23:04:2723using WebKit::WebURL;
[email protected]90e908552009-10-05 01:40:1224
25NotificationProvider::NotificationProvider(RenderView* view)
26 : view_(view) {
27}
28
29bool NotificationProvider::show(const WebNotification& notification) {
30 int notification_id = manager_.RegisterNotification(notification);
31 if (notification.isHTML())
32 return ShowHTML(notification, notification_id);
33 else
34 return ShowText(notification, notification_id);
35}
36
37void NotificationProvider::cancel(const WebNotification& notification) {
38 int id;
39 bool id_found = manager_.GetId(notification, id);
[email protected]b52448bb2009-11-04 21:29:1140 // Won't be found if the notification has already been closed by the user.
[email protected]90e908552009-10-05 01:40:1241 if (id_found)
42 Send(new ViewHostMsg_CancelDesktopNotification(view_->routing_id(), id));
43}
44
45void NotificationProvider::objectDestroyed(
46 const WebNotification& notification) {
47 int id;
48 bool id_found = manager_.GetId(notification, id);
[email protected]b52448bb2009-11-04 21:29:1149 // Won't be found if the notification has already been closed by the user.
[email protected]90e908552009-10-05 01:40:1250 if (id_found)
51 manager_.UnregisterNotification(id);
52}
53
54WebNotificationPresenter::Permission NotificationProvider::checkPermission(
[email protected]0ce3f5982010-01-28 23:04:2755 const WebURL& url, WebDocument* document) {
[email protected]90e908552009-10-05 01:40:1256 int permission;
[email protected]0ce3f5982010-01-28 23:04:2757 Send(new ViewHostMsg_CheckNotificationPermission(
58 view_->routing_id(),
59 url,
60 document ? UTF16ToASCII(document->applicationID()) : "",
61 &permission));
[email protected]90e908552009-10-05 01:40:1262 return static_cast<WebNotificationPresenter::Permission>(permission);
63}
64
65void NotificationProvider::requestPermission(
66 const WebString& origin, WebNotificationPermissionCallback* callback) {
[email protected]4440a58b2009-11-13 22:04:5867 // We only request permission in response to a user gesture.
68 if (!view_->webview()->mainFrame()->isProcessingUserGesture())
69 return;
70
[email protected]90e908552009-10-05 01:40:1271 int id = manager_.RegisterPermissionRequest(callback);
72
73 Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(),
[email protected]4bb336302009-10-12 05:44:2674 GURL(origin), id));
[email protected]90e908552009-10-05 01:40:1275}
76
[email protected]b6849bda2009-10-14 23:59:2677bool NotificationProvider::OnMessageReceived(const IPC::Message& message) {
78 bool handled = true;
79 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message)
80 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay);
81 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError);
82 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose);
83 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone,
84 OnPermissionRequestComplete);
85 IPC_MESSAGE_UNHANDLED(handled = false)
86 IPC_END_MESSAGE_MAP()
87 return handled;
88}
89
[email protected]422197532010-01-25 17:38:2390void NotificationProvider::OnNavigate() {
[email protected]3db53702010-03-03 22:38:1391 manager_.Clear();
[email protected]422197532010-01-25 17:38:2392}
93
[email protected]90e908552009-10-05 01:40:1294bool NotificationProvider::ShowHTML(const WebNotification& notification,
95 int id) {
[email protected]cd065c0112010-01-13 05:57:0996 // Disallow HTML notifications from non-HTTP schemes.
97 GURL url = notification.url();
[email protected]13347d152010-01-23 01:37:5498 if (!url.SchemeIs(chrome::kHttpScheme) &&
99 !url.SchemeIs(chrome::kHttpsScheme) &&
100 !url.SchemeIs(chrome::kExtensionScheme))
[email protected]cd065c0112010-01-13 05:57:09101 return false;
102
[email protected]90e908552009-10-05 01:40:12103 DCHECK(notification.isHTML());
104 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(),
[email protected]9429e972009-11-10 06:27:38105 GURL(view_->webview()->mainFrame()->url()).GetOrigin(),
[email protected]90e908552009-10-05 01:40:12106 notification.url(), id));
107}
108
109bool NotificationProvider::ShowText(const WebNotification& notification,
110 int id) {
111 DCHECK(!notification.isHTML());
112 return Send(new ViewHostMsg_ShowDesktopNotificationText(view_->routing_id(),
[email protected]9429e972009-11-10 06:27:38113 GURL(view_->webview()->mainFrame()->url()).GetOrigin(),
[email protected]90e908552009-10-05 01:40:12114 GURL(notification.icon()),
115 notification.title(), notification.body(), id));
116}
117
118void NotificationProvider::OnDisplay(int id) {
[email protected]90e908552009-10-05 01:40:12119 WebNotification notification;
120 bool found = manager_.GetNotification(id, &notification);
121 // |found| may be false if the WebNotification went out of scope in
122 // the page before it was actually displayed to the user.
123 if (found)
124 notification.dispatchDisplayEvent();
125}
126
[email protected]b6849bda2009-10-14 23:59:26127void NotificationProvider::OnError(int id, const WebString& message) {
[email protected]90e908552009-10-05 01:40:12128 WebNotification notification;
129 bool found = manager_.GetNotification(id, &notification);
130 // |found| may be false if the WebNotification went out of scope in
131 // the page before the error occurred.
132 if (found)
133 notification.dispatchErrorEvent(message);
134}
135
[email protected]b6849bda2009-10-14 23:59:26136void NotificationProvider::OnClose(int id, bool by_user) {
[email protected]90e908552009-10-05 01:40:12137 WebNotification notification;
138 bool found = manager_.GetNotification(id, &notification);
139 // |found| may be false if the WebNotification went out of scope in
140 // the page before the associated toast was closed by the user.
[email protected]422197532010-01-25 17:38:23141 if (found) {
[email protected]90e908552009-10-05 01:40:12142 notification.dispatchCloseEvent(by_user);
[email protected]422197532010-01-25 17:38:23143 manager_.UnregisterNotification(id);
144 }
[email protected]90e908552009-10-05 01:40:12145}
146
[email protected]b6849bda2009-10-14 23:59:26147void NotificationProvider::OnPermissionRequestComplete(int id) {
[email protected]90e908552009-10-05 01:40:12148 WebNotificationPermissionCallback* callback = manager_.GetCallback(id);
149 DCHECK(callback);
150 callback->permissionRequestComplete();
151 manager_.OnPermissionRequestComplete(id);
152}
153
[email protected]90e908552009-10-05 01:40:12154bool NotificationProvider::Send(IPC::Message* message) {
155 return RenderThread::current()->Send(message);
156}