1 | /* |
2 | * Copyright (C) 2011, 2012 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "NotificationPermissionRequestManager.h" |
28 | |
29 | #include "WebCoreArgumentCoders.h" |
30 | #include "WebPage.h" |
31 | #include "WebPageProxyMessages.h" |
32 | #include "WebProcess.h" |
33 | #include <WebCore/Notification.h> |
34 | #include <WebCore/Page.h> |
35 | #include <WebCore/ScriptExecutionContext.h> |
36 | #include <WebCore/SecurityOrigin.h> |
37 | #include <WebCore/Settings.h> |
38 | |
39 | #if ENABLE(NOTIFICATIONS) |
40 | #include "WebNotificationManager.h" |
41 | #endif |
42 | |
43 | namespace WebKit { |
44 | using namespace WebCore; |
45 | |
46 | #if ENABLE(NOTIFICATIONS) |
47 | static uint64_t generateRequestID() |
48 | { |
49 | static uint64_t uniqueRequestID = 1; |
50 | return uniqueRequestID++; |
51 | } |
52 | #endif |
53 | |
54 | Ref<NotificationPermissionRequestManager> NotificationPermissionRequestManager::create(WebPage* page) |
55 | { |
56 | return adoptRef(*new NotificationPermissionRequestManager(page)); |
57 | } |
58 | |
59 | #if ENABLE(NOTIFICATIONS) |
60 | NotificationPermissionRequestManager::NotificationPermissionRequestManager(WebPage* page) |
61 | : m_page(page) |
62 | { |
63 | } |
64 | #else |
65 | NotificationPermissionRequestManager::NotificationPermissionRequestManager(WebPage*) |
66 | { |
67 | } |
68 | #endif |
69 | |
70 | #if ENABLE(NOTIFICATIONS) |
71 | void NotificationPermissionRequestManager::startRequest(SecurityOrigin* origin, RefPtr<NotificationPermissionCallback>&& callback) |
72 | { |
73 | auto permission = permissionLevel(origin); |
74 | if (permission != NotificationClient::Permission::Default) { |
75 | if (callback) |
76 | callback->handleEvent(permission); |
77 | return; |
78 | } |
79 | |
80 | uint64_t requestID = generateRequestID(); |
81 | m_originToIDMap.set(origin, requestID); |
82 | m_idToOriginMap.set(requestID, origin); |
83 | m_idToCallbackMap.set(requestID, WTFMove(callback)); |
84 | m_page->send(Messages::WebPageProxy::RequestNotificationPermission(requestID, origin->toString())); |
85 | } |
86 | #endif |
87 | |
88 | void NotificationPermissionRequestManager::cancelRequest(SecurityOrigin* origin) |
89 | { |
90 | #if ENABLE(NOTIFICATIONS) |
91 | uint64_t id = m_originToIDMap.take(origin); |
92 | if (!id) |
93 | return; |
94 | |
95 | m_idToOriginMap.remove(id); |
96 | m_idToCallbackMap.remove(id); |
97 | #else |
98 | UNUSED_PARAM(origin); |
99 | #endif |
100 | } |
101 | |
102 | bool NotificationPermissionRequestManager::hasPendingPermissionRequests(SecurityOrigin* origin) const |
103 | { |
104 | #if ENABLE(NOTIFICATIONS) |
105 | return m_originToIDMap.contains(origin); |
106 | #else |
107 | UNUSED_PARAM(origin); |
108 | return false; |
109 | #endif |
110 | } |
111 | |
112 | NotificationClient::Permission NotificationPermissionRequestManager::permissionLevel(SecurityOrigin* securityOrigin) |
113 | { |
114 | #if ENABLE(NOTIFICATIONS) |
115 | if (!m_page->corePage()->settings().notificationsEnabled()) |
116 | return NotificationClient::Permission::Denied; |
117 | |
118 | return WebProcess::singleton().supplement<WebNotificationManager>()->policyForOrigin(securityOrigin); |
119 | #else |
120 | UNUSED_PARAM(securityOrigin); |
121 | return NotificationClient::Permission::Denied; |
122 | #endif |
123 | } |
124 | |
125 | void NotificationPermissionRequestManager::setPermissionLevelForTesting(const String& originString, bool allowed) |
126 | { |
127 | #if ENABLE(NOTIFICATIONS) |
128 | WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(originString, allowed); |
129 | #else |
130 | UNUSED_PARAM(originString); |
131 | UNUSED_PARAM(allowed); |
132 | #endif |
133 | } |
134 | |
135 | void NotificationPermissionRequestManager::removeAllPermissionsForTesting() |
136 | { |
137 | #if ENABLE(NOTIFICATIONS) |
138 | WebProcess::singleton().supplement<WebNotificationManager>()->removeAllPermissionsForTesting(); |
139 | #endif |
140 | } |
141 | |
142 | void NotificationPermissionRequestManager::didReceiveNotificationPermissionDecision(uint64_t requestID, bool allowed) |
143 | { |
144 | #if ENABLE(NOTIFICATIONS) |
145 | if (!isRequestIDValid(requestID)) |
146 | return; |
147 | |
148 | RefPtr<WebCore::SecurityOrigin> origin = m_idToOriginMap.take(requestID); |
149 | if (!origin) |
150 | return; |
151 | |
152 | m_originToIDMap.remove(origin); |
153 | |
154 | WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(origin->toString(), allowed); |
155 | |
156 | RefPtr<NotificationPermissionCallback> callback = m_idToCallbackMap.take(requestID); |
157 | if (!callback) |
158 | return; |
159 | |
160 | callback->handleEvent(allowed ? NotificationClient::Permission::Granted : NotificationClient::Permission::Denied); |
161 | #else |
162 | UNUSED_PARAM(requestID); |
163 | UNUSED_PARAM(allowed); |
164 | #endif |
165 | } |
166 | |
167 | } // namespace WebKit |
168 | |