OLD | NEW |
---|---|
(Empty) | |
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 | |
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 | |
13 using WebKit::WebNotification; | |
14 using WebKit::WebNotificationPresenter; | |
15 using WebKit::WebNotificationPermissionCallback; | |
16 using WebKit::WebString; | |
17 | |
18 NotificationProvider::NotificationProvider(RenderView* view) | |
19 : view_(view) { | |
20 } | |
21 | |
22 bool 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 | |
30 void 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 | |
38 void 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 | |
47 WebNotificationPresenter::Permission NotificationProvider::checkPermission( | |
48 const WebString& origin) { | |
49 int permission; | |
50 Send(new ViewHostMsg_CheckNotificationPermission(view_->routing_id(), origin, | |
51 &permission)); | |
52 return static_cast<WebNotificationPresenter::Permission>(permission); | |
53 } | |
54 | |
55 void NotificationProvider::requestPermission( | |
56 const WebString& origin, WebNotificationPermissionCallback* callback) { | |
57 int id = manager_.RegisterPermissionRequest(callback); | |
58 | |
59 Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(), | |
60 origin, id)); | |
61 } | |
62 | |
63 bool NotificationProvider::ShowHTML(const WebNotification& notification, | |
64 int id) { | |
65 DCHECK(notification.isHTML()); | |
66 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), | |
67 GURL(view_->webview()->mainFrame()->url()), | |
68 notification.url(), id)); | |
69 } | |
70 | |
71 bool NotificationProvider::ShowText(const WebNotification& notification, | |
72 int id) { | |
73 DCHECK(!notification.isHTML()); | |
74 return Send(new ViewHostMsg_ShowDesktopNotificationText(view_->routing_id(), | |
75 GURL(view_->webview()->mainFrame()->url()), | |
76 GURL(notification.icon()), | |
77 notification.title(), notification.body(), id)); | |
78 } | |
79 | |
80 void NotificationProvider::OnDisplay(int id) { | |
81 RenderProcess::current()->main_thread()->message_loop()->PostTask(FROM_HERE, | |
82 NewRunnableMethod(this, &NotificationProvider::HandleOnDisplay, id)); | |
83 } | |
84 | |
85 void NotificationProvider::OnError(int id, const WebString& message) { | |
86 RenderProcess::current()->main_thread()->message_loop()->PostTask(FROM_HERE, | |
87 NewRunnableMethod(this, &NotificationProvider::HandleOnError, | |
88 id, message)); | |
89 } | |
90 | |
91 void NotificationProvider::OnClose(int id, bool by_user) { | |
92 RenderProcess::current()->main_thread()->message_loop()->PostTask(FROM_HERE, | |
93 NewRunnableMethod(this, &NotificationProvider::HandleOnClose, | |
94 id, by_user)); | |
95 } | |
96 | |
97 void NotificationProvider::OnPermissionRequestComplete(int id) { | |
98 RenderProcess::current()->main_thread()->message_loop()->PostTask(FROM_HERE, | |
99 NewRunnableMethod(this, | |
100 &NotificationProvider::HandleOnPermissionRequestComplete, id)); | |
101 } | |
102 | |
103 void NotificationProvider::HandleOnDisplay(int id) { | |
104 DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); | |
105 WebNotification notification; | |
106 bool found = manager_.GetNotification(id, ¬ification); | |
107 // |found| may be false if the WebNotification went out of scope in | |
108 // the page before it was actually displayed to the user. | |
109 if (found) | |
110 notification.dispatchDisplayEvent(); | |
111 } | |
112 | |
113 void NotificationProvider::HandleOnError(int id, const WebString& message) { | |
114 DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); | |
115 WebNotification notification; | |
116 bool found = manager_.GetNotification(id, ¬ification); | |
117 // |found| may be false if the WebNotification went out of scope in | |
118 // the page before the error occurred. | |
119 if (found) | |
120 notification.dispatchErrorEvent(message); | |
121 } | |
122 | |
123 void NotificationProvider::HandleOnClose(int id, bool by_user) { | |
124 DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); | |
125 WebNotification notification; | |
126 bool found = manager_.GetNotification(id, ¬ification); | |
127 // |found| may be false if the WebNotification went out of scope in | |
128 // the page before the associated toast was closed by the user. | |
129 if (found) | |
130 notification.dispatchCloseEvent(by_user); | |
131 manager_.UnregisterNotification(id); | |
132 } | |
133 | |
134 void NotificationProvider::HandleOnPermissionRequestComplete(int id) { | |
135 DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); | |
136 WebNotificationPermissionCallback* callback = manager_.GetCallback(id); | |
137 DCHECK(callback); | |
138 callback->permissionRequestComplete(); | |
139 manager_.OnPermissionRequestComplete(id); | |
140 } | |
141 | |
142 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { | |
143 if (message.routing_id() != view_->routing_id()) | |
jam
2009/10/14 20:33:28
This isn't thread safe. IPC::ChannelProxy::Messag
| |
144 return false; | |
145 | |
146 bool handled = true; | |
147 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) | |
148 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay); | |
149 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError); | |
150 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose); | |
151 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone, | |
152 OnPermissionRequestComplete); | |
153 IPC_MESSAGE_UNHANDLED(handled = false) | |
154 IPC_END_MESSAGE_MAP() | |
155 return handled; | |
156 } | |
157 | |
158 bool NotificationProvider::Send(IPC::Message* message) { | |
159 return RenderThread::current()->Send(message); | |
160 } | |
OLD | NEW |