OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "content/renderer/notification_provider.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 #include "content/common/desktop_notification_messages.h" | |
11 #include "content/common/frame_messages.h" | |
12 #include "content/renderer/notification_icon_loader.h" | |
13 #include "content/renderer/render_frame_impl.h" | |
14 #include "third_party/WebKit/public/web/WebDocument.h" | |
15 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
16 | |
17 using blink::WebDocument; | |
18 using blink::WebNotification; | |
19 using blink::WebNotificationPresenter; | |
20 using blink::WebSecurityOrigin; | |
21 using blink::WebString; | |
22 | |
23 namespace content { | |
24 | |
25 NotificationProvider::NotificationProvider(RenderFrame* render_frame) | |
26 : RenderFrameObserver(render_frame) {} | |
27 | |
28 NotificationProvider::~NotificationProvider() {} | |
29 | |
30 bool NotificationProvider::show(const WebNotification& notification) { | |
31 int notification_id = manager_.RegisterNotification(notification); | |
32 if (notification.iconURL().isEmpty()) { | |
33 DisplayNotification(notification_id, SkBitmap()); | |
34 return true; | |
35 } | |
36 | |
37 scoped_ptr<NotificationIconLoader> loader( | |
38 new NotificationIconLoader( | |
39 notification_id, | |
40 base::Bind(&NotificationProvider::DisplayNotification, | |
41 base::Unretained(this)))); | |
42 | |
43 loader->Start(notification.iconURL()); | |
44 | |
45 pending_notifications_.push_back(loader.release()); | |
46 return true; | |
47 } | |
48 | |
49 void NotificationProvider::DisplayNotification(int notification_id, | |
50 const SkBitmap& icon) { | |
51 WebDocument document = render_frame()->GetWebFrame()->document(); | |
52 WebNotification notification; | |
53 | |
54 if (!manager_.GetNotification(notification_id, ¬ification)) { | |
55 NOTREACHED(); | |
56 return; | |
57 } | |
58 | |
59 RemovePendingNotification(notification_id); | |
60 | |
61 ShowDesktopNotificationHostMsgParams params; | |
62 params.origin = GURL(document.securityOrigin().toString()); | |
63 params.icon = icon; | |
64 params.title = notification.title(); | |
65 params.body = notification.body(); | |
66 params.direction = notification.direction(); | |
67 params.replace_id = notification.replaceId(); | |
68 | |
69 Send(new DesktopNotificationHostMsg_Show(routing_id(), | |
70 notification_id, | |
71 params)); | |
72 } | |
73 | |
74 bool NotificationProvider::RemovePendingNotification(int notification_id) { | |
75 PendingNotifications::iterator iter = pending_notifications_.begin(); | |
76 for (; iter != pending_notifications_.end(); ++iter) { | |
77 if ((*iter)->notification_id() != notification_id) | |
78 continue; | |
79 | |
80 pending_notifications_.erase(iter); | |
81 return true; | |
82 } | |
83 | |
84 return false; | |
85 } | |
86 | |
87 void NotificationProvider::cancel(const WebNotification& notification) { | |
88 int id; | |
89 bool id_found = manager_.GetId(notification, id); | |
90 // Won't be found if the notification has already been closed by the user, | |
91 // or if the notification's icon is still being requested. | |
92 if (id_found && !RemovePendingNotification(id)) | |
93 Send(new DesktopNotificationHostMsg_Cancel(routing_id(), id)); | |
94 } | |
95 | |
96 void NotificationProvider::objectDestroyed( | |
97 const WebNotification& notification) { | |
98 int id; | |
99 bool id_found = manager_.GetId(notification, id); | |
100 // Won't be found if the notification has already been closed by the user. | |
101 if (id_found) { | |
102 RemovePendingNotification(id); | |
103 manager_.UnregisterNotification(id); | |
104 } | |
105 } | |
106 | |
107 WebNotificationPresenter::Permission NotificationProvider::checkPermission( | |
108 const WebSecurityOrigin& origin) { | |
109 int permission = WebNotificationPresenter::PermissionNotAllowed; | |
110 Send(new DesktopNotificationHostMsg_CheckPermission( | |
111 routing_id(), | |
112 GURL(origin.toString()), | |
113 &permission)); | |
114 return static_cast<WebNotificationPresenter::Permission>(permission); | |
115 } | |
116 | |
117 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { | |
118 bool handled = true; | |
119 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) | |
120 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostDisplay, OnDisplay); | |
121 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClose, OnClose); | |
122 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClick, OnClick); | |
123 IPC_MESSAGE_UNHANDLED(handled = false) | |
124 IPC_END_MESSAGE_MAP() | |
125 | |
126 if (message.type() == FrameMsg_Navigate::ID) | |
127 OnNavigate(); // Don't want to swallow the message. | |
128 | |
129 return handled; | |
130 } | |
131 | |
132 void NotificationProvider::OnDisplay(int id) { | |
133 WebNotification notification; | |
134 bool found = manager_.GetNotification(id, ¬ification); | |
135 // |found| may be false if the WebNotification went out of scope in | |
136 // the page before it was actually displayed to the user. | |
137 if (found) | |
138 notification.dispatchDisplayEvent(); | |
139 } | |
140 | |
141 void NotificationProvider::OnClose(int id, bool by_user) { | |
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 the associated toast was closed by the user. | |
146 if (found) { | |
147 notification.dispatchCloseEvent(by_user); | |
148 manager_.UnregisterNotification(id); | |
149 } | |
150 } | |
151 | |
152 void NotificationProvider::OnClick(int id) { | |
153 WebNotification notification; | |
154 bool found = manager_.GetNotification(id, ¬ification); | |
155 // |found| may be false if the WebNotification went out of scope in | |
156 // the page before the associated toast was clicked on. | |
157 if (found) | |
158 notification.dispatchClickEvent(); | |
159 } | |
160 | |
161 void NotificationProvider::OnNavigate() { | |
162 manager_.Clear(); | |
163 } | |
164 | |
165 } // namespace content | |
OLD | NEW |