blob: 0c1b789be66f0375fdfef09a338d973dc60439f9 [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
7#include "base/task.h"
8#include "chrome/common/render_messages.h"
9#include "chrome/renderer/render_thread.h"
10#include "chrome/renderer/render_view.h"
11#include "webkit/api/public/WebNotificationPermissionCallback.h"
12
13using WebKit::WebNotification;
14using WebKit::WebNotificationPresenter;
15using WebKit::WebNotificationPermissionCallback;
16using WebKit::WebString;
17
18NotificationProvider::NotificationProvider(RenderView* view)
19 : view_(view) {
20}
21
22bool NotificationProvider::show(const WebNotification& notification) {
23 int notification_id = manager_.RegisterNotification(notification);
24 if (notification.isHTML())
25 return ShowHTML(notification, notification_id);
26 else
27 return ShowText(notification, notification_id);
28}
29
30void NotificationProvider::cancel(const WebNotification& notification) {
31 int id;
32 bool id_found = manager_.GetId(notification, id);
33 DCHECK(id_found);
34 if (id_found)
35 Send(new ViewHostMsg_CancelDesktopNotification(view_->routing_id(), id));
36}
37
38void NotificationProvider::objectDestroyed(
39 const WebNotification& notification) {
40 int id;
41 bool id_found = manager_.GetId(notification, id);
42 DCHECK(id_found);
43 if (id_found)
44 manager_.UnregisterNotification(id);
45}
46
47WebNotificationPresenter::Permission NotificationProvider::checkPermission(
48 const WebString& origin) {
49 int permission;
[email protected]4bb336302009-10-12 05:44:2650 Send(new ViewHostMsg_CheckNotificationPermission(view_->routing_id(),
51 GURL(origin), &permission));
[email protected]90e908552009-10-05 01:40:1252 return static_cast<WebNotificationPresenter::Permission>(permission);
53}
54
55void NotificationProvider::requestPermission(
56 const WebString& origin, WebNotificationPermissionCallback* callback) {
57 int id = manager_.RegisterPermissionRequest(callback);
58
59 Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(),
[email protected]4bb336302009-10-12 05:44:2660 GURL(origin), id));
[email protected]90e908552009-10-05 01:40:1261}
62
[email protected]b6849bda2009-10-14 23:59:2663bool NotificationProvider::OnMessageReceived(const IPC::Message& message) {
64 bool handled = true;
65 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message)
66 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay);
67 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError);
68 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose);
69 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone,
70 OnPermissionRequestComplete);
71 IPC_MESSAGE_UNHANDLED(handled = false)
72 IPC_END_MESSAGE_MAP()
73 return handled;
74}
75
[email protected]90e908552009-10-05 01:40:1276bool NotificationProvider::ShowHTML(const WebNotification& notification,
77 int id) {
78 DCHECK(notification.isHTML());
79 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(),
80 GURL(view_->webview()->mainFrame()->url()),
81 notification.url(), id));
82}
83
84bool NotificationProvider::ShowText(const WebNotification& notification,
85 int id) {
86 DCHECK(!notification.isHTML());
87 return Send(new ViewHostMsg_ShowDesktopNotificationText(view_->routing_id(),
88 GURL(view_->webview()->mainFrame()->url()),
89 GURL(notification.icon()),
90 notification.title(), notification.body(), id));
91}
92
93void NotificationProvider::OnDisplay(int id) {
[email protected]90e908552009-10-05 01:40:1294 WebNotification notification;
95 bool found = manager_.GetNotification(id, &notification);
96 // |found| may be false if the WebNotification went out of scope in
97 // the page before it was actually displayed to the user.
98 if (found)
99 notification.dispatchDisplayEvent();
100}
101
[email protected]b6849bda2009-10-14 23:59:26102void NotificationProvider::OnError(int id, const WebString& message) {
[email protected]90e908552009-10-05 01:40:12103 WebNotification notification;
104 bool found = manager_.GetNotification(id, &notification);
105 // |found| may be false if the WebNotification went out of scope in
106 // the page before the error occurred.
107 if (found)
108 notification.dispatchErrorEvent(message);
109}
110
[email protected]b6849bda2009-10-14 23:59:26111void NotificationProvider::OnClose(int id, bool by_user) {
[email protected]90e908552009-10-05 01:40:12112 WebNotification notification;
113 bool found = manager_.GetNotification(id, &notification);
114 // |found| may be false if the WebNotification went out of scope in
115 // the page before the associated toast was closed by the user.
116 if (found)
117 notification.dispatchCloseEvent(by_user);
118 manager_.UnregisterNotification(id);
119}
120
[email protected]b6849bda2009-10-14 23:59:26121void NotificationProvider::OnPermissionRequestComplete(int id) {
[email protected]90e908552009-10-05 01:40:12122 WebNotificationPermissionCallback* callback = manager_.GetCallback(id);
123 DCHECK(callback);
124 callback->permissionRequestComplete();
125 manager_.OnPermissionRequestComplete(id);
126}
127
[email protected]90e908552009-10-05 01:40:12128bool NotificationProvider::Send(IPC::Message* message) {
129 return RenderThread::current()->Send(message);
130}