1 // Copyright 2014 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_permission_dispatcher.h"
8 #include "content/public/common/service_registry.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "third_party/WebKit/public/platform/WebString.h"
11 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
12 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
13 #include "third_party/WebKit/public/web/modules/notifications/WebNotificationPermissionCallback.h"
17 NotificationPermissionDispatcher::NotificationPermissionDispatcher(
18 RenderFrame
* render_frame
) : RenderFrameObserver(render_frame
) {
21 NotificationPermissionDispatcher::~NotificationPermissionDispatcher() {
24 void NotificationPermissionDispatcher::RequestPermission(
25 const blink::WebSecurityOrigin
& origin
,
26 blink::WebNotificationPermissionCallback
* callback
) {
27 if (!permission_service_
.get()) {
28 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
29 mojo::GetProxy(&permission_service_
));
32 int request_id
= pending_requests_
.Add(callback
);
34 permission_service_
->RequestPermission(
35 PERMISSION_NAME_NOTIFICATIONS
,
36 origin
.toString().utf8(),
37 blink::WebUserGestureIndicator::isProcessingUserGesture(),
38 base::Bind(&NotificationPermissionDispatcher::OnPermissionRequestComplete
,
39 base::Unretained(this),
43 void NotificationPermissionDispatcher::OnPermissionRequestComplete(
44 int request_id
, PermissionStatus status
) {
45 blink::WebNotificationPermissionCallback
* callback
=
46 pending_requests_
.Lookup(request_id
);
49 blink::WebNotificationPermission permission
=
50 blink::WebNotificationPermissionDefault
;
52 case PERMISSION_STATUS_GRANTED
:
53 permission
= blink::WebNotificationPermissionAllowed
;
55 case PERMISSION_STATUS_DENIED
:
56 permission
= blink::WebNotificationPermissionDenied
;
58 case PERMISSION_STATUS_ASK
:
59 permission
= blink::WebNotificationPermissionDefault
;
63 callback
->permissionRequestComplete(permission
);
64 pending_requests_
.Remove(request_id
);
67 } // namespace content