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.
5 #include "content/renderer/notification_provider.h"
7 #include "base/strings/string_util.h"
8 #include "content/common/desktop_notification_messages.h"
9 #include "content/common/frame_messages.h"
10 #include "content/renderer/render_frame_impl.h"
11 #include "third_party/WebKit/public/platform/WebURL.h"
12 #include "third_party/WebKit/public/web/WebDocument.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebNotificationPermissionCallback.h"
15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
17 using blink::WebDocument
;
18 using blink::WebNotification
;
19 using blink::WebNotificationPresenter
;
20 using blink::WebNotificationPermissionCallback
;
21 using blink::WebSecurityOrigin
;
22 using blink::WebString
;
24 using blink::WebUserGestureIndicator
;
29 NotificationProvider::NotificationProvider(RenderFrame
* render_frame
)
30 : RenderFrameObserver(render_frame
) {
33 NotificationProvider::~NotificationProvider() {
36 bool NotificationProvider::show(const WebNotification
& notification
) {
37 WebDocument document
= render_frame()->GetWebFrame()->document();
38 int notification_id
= manager_
.RegisterNotification(notification
);
40 ShowDesktopNotificationHostMsgParams params
;
41 params
.origin
= GURL(document
.securityOrigin().toString());
42 params
.icon_url
= notification
.iconURL();
43 params
.title
= notification
.title();
44 params
.body
= notification
.body();
45 params
.direction
= notification
.direction();
46 params
.replace_id
= notification
.replaceId();
47 return Send(new DesktopNotificationHostMsg_Show(
48 routing_id(), notification_id
, params
));
51 void NotificationProvider::cancel(const WebNotification
& notification
) {
53 bool id_found
= manager_
.GetId(notification
, id
);
54 // Won't be found if the notification has already been closed by the user.
56 Send(new DesktopNotificationHostMsg_Cancel(routing_id(), id
));
59 void NotificationProvider::objectDestroyed(
60 const WebNotification
& notification
) {
62 bool id_found
= manager_
.GetId(notification
, id
);
63 // Won't be found if the notification has already been closed by the user.
65 manager_
.UnregisterNotification(id
);
68 WebNotificationPresenter::Permission
NotificationProvider::checkPermission(
69 const WebSecurityOrigin
& origin
) {
71 Send(new DesktopNotificationHostMsg_CheckPermission(
73 GURL(origin
.toString()),
75 return static_cast<WebNotificationPresenter::Permission
>(permission
);
78 void NotificationProvider::requestPermission(
79 const WebSecurityOrigin
& origin
,
80 WebNotificationPermissionCallback
* callback
) {
81 // We only request permission in response to a user gesture.
82 if (!WebUserGestureIndicator::isProcessingUserGesture())
85 int id
= manager_
.RegisterPermissionRequest(callback
);
87 Send(new DesktopNotificationHostMsg_RequestPermission(
88 routing_id(), GURL(origin
.toString()), id
));
91 bool NotificationProvider::OnMessageReceived(const IPC::Message
& message
) {
93 IPC_BEGIN_MESSAGE_MAP(NotificationProvider
, message
)
94 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostDisplay
, OnDisplay
);
95 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostError
, OnError
);
96 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClose
, OnClose
);
97 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PostClick
, OnClick
);
98 IPC_MESSAGE_HANDLER(DesktopNotificationMsg_PermissionRequestDone
,
99 OnPermissionRequestComplete
);
100 IPC_MESSAGE_UNHANDLED(handled
= false)
101 IPC_END_MESSAGE_MAP()
103 if (message
.type() == FrameMsg_Navigate::ID
)
104 OnNavigate(); // Don't want to swallow the message.
109 void NotificationProvider::OnDisplay(int id
) {
110 WebNotification notification
;
111 bool found
= manager_
.GetNotification(id
, ¬ification
);
112 // |found| may be false if the WebNotification went out of scope in
113 // the page before it was actually displayed to the user.
115 notification
.dispatchDisplayEvent();
118 void NotificationProvider::OnError(int id
) {
119 WebNotification notification
;
120 bool found
= manager_
.GetNotification(id
, ¬ification
);
121 // |found| may be false if the WebNotification went out of scope in
122 // the page before the error occurred.
124 notification
.dispatchErrorEvent(WebString());
127 void NotificationProvider::OnClose(int id
, bool by_user
) {
128 WebNotification notification
;
129 bool found
= manager_
.GetNotification(id
, ¬ification
);
130 // |found| may be false if the WebNotification went out of scope in
131 // the page before the associated toast was closed by the user.
133 notification
.dispatchCloseEvent(by_user
);
134 manager_
.UnregisterNotification(id
);
138 void NotificationProvider::OnClick(int id
) {
139 WebNotification notification
;
140 bool found
= manager_
.GetNotification(id
, ¬ification
);
141 // |found| may be false if the WebNotification went out of scope in
142 // the page before the associated toast was clicked on.
144 notification
.dispatchClickEvent();
147 void NotificationProvider::OnPermissionRequestComplete(int id
) {
148 WebNotificationPermissionCallback
* callback
= manager_
.GetCallback(id
);
150 callback
->permissionRequestComplete();
151 manager_
.OnPermissionRequestComplete(id
);
154 void NotificationProvider::OnNavigate() {
158 } // namespace content