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