81ff1b1b75e0ff2fb04b74f0f075d6471da3382a
[WebKit-https.git] / Source / WebKit2 / WebProcess / Notifications / NotificationPermissionRequestManager.cpp
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) || ENABLE(LEGACY_NOTIFICATIONS)
40 #include "WebNotificationManager.h"
41 #endif
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
48 static uint64_t generateRequestID()
49 {
50     static uint64_t uniqueRequestID = 1;
51     return uniqueRequestID++;
52 }
53 #endif
54
55 Ref<NotificationPermissionRequestManager> NotificationPermissionRequestManager::create(WebPage* page)
56 {
57     return adoptRef(*new NotificationPermissionRequestManager(page));
58 }
59
60 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
61 NotificationPermissionRequestManager::NotificationPermissionRequestManager(WebPage* page)
62     : m_page(page)
63 {
64 }
65 #else
66 NotificationPermissionRequestManager::NotificationPermissionRequestManager(WebPage*)
67 {
68 }
69 #endif
70
71 #if ENABLE(NOTIFICATIONS)
72 void NotificationPermissionRequestManager::startRequest(SecurityOrigin* origin, RefPtr<NotificationPermissionCallback>&& callback)
73 {
74     NotificationClient::Permission permission = permissionLevel(origin);
75     if (permission != NotificationClient::PermissionNotAllowed) {
76         if (callback)
77             callback->handleEvent(Notification::permissionString(permission));
78         return;
79     }
80
81     uint64_t requestID = generateRequestID();
82     m_originToIDMap.set(origin, requestID);
83     m_idToOriginMap.set(requestID, origin);
84     m_idToCallbackMap.set(requestID, WTFMove(callback));
85     m_page->send(Messages::WebPageProxy::RequestNotificationPermission(requestID, origin->toString()));
86 }
87 #endif
88
89 #if ENABLE(LEGACY_NOTIFICATIONS)
90 void NotificationPermissionRequestManager::startRequest(SecurityOrigin* origin, RefPtr<WebCore::VoidCallback>&& callback)
91 {
92     NotificationClient::Permission permission = permissionLevel(origin);
93     if (permission != NotificationClient::PermissionNotAllowed) {
94         if (callback)
95             callback->handleEvent();
96         return;
97     }
98     
99     uint64_t requestID = generateRequestID();
100     m_originToIDMap.set(origin, requestID);
101     m_idToOriginMap.set(requestID, origin);
102     m_idToVoidCallbackMap.set(requestID, WTFMove(callback));
103     m_page->send(Messages::WebPageProxy::RequestNotificationPermission(requestID, origin->toString()));
104 }
105 #endif
106
107 void NotificationPermissionRequestManager::cancelRequest(SecurityOrigin* origin)
108 {
109 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
110     uint64_t id = m_originToIDMap.take(origin);
111     if (!id)
112         return;
113     
114     m_idToOriginMap.remove(id);
115 #if ENABLE(NOTIFICATIONS)
116     m_idToCallbackMap.remove(id);
117 #endif
118 #if ENABLE(LEGACY_NOTIFICATIONS)
119     m_idToVoidCallbackMap.remove(id);
120 #endif
121 #else
122     UNUSED_PARAM(origin);
123 #endif
124 }
125
126 bool NotificationPermissionRequestManager::hasPendingPermissionRequests(SecurityOrigin* origin) const
127 {
128 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
129     return m_originToIDMap.contains(origin);
130 #else
131     UNUSED_PARAM(origin);
132     return false;
133 #endif
134 }
135
136 NotificationClient::Permission NotificationPermissionRequestManager::permissionLevel(SecurityOrigin* securityOrigin)
137 {
138 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
139     if (!m_page->corePage()->settings().notificationsEnabled())
140         return NotificationClient::PermissionDenied;
141     
142     return WebProcess::singleton().supplement<WebNotificationManager>()->policyForOrigin(securityOrigin);
143 #else
144     UNUSED_PARAM(securityOrigin);
145     return NotificationClient::PermissionDenied;
146 #endif
147 }
148
149 void NotificationPermissionRequestManager::setPermissionLevelForTesting(const String& originString, bool allowed)
150 {
151 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
152     WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(originString, allowed);
153 #else
154     UNUSED_PARAM(originString);
155     UNUSED_PARAM(allowed);
156 #endif
157 }
158
159 void NotificationPermissionRequestManager::removeAllPermissionsForTesting()
160 {
161 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
162     WebProcess::singleton().supplement<WebNotificationManager>()->removeAllPermissionsForTesting();
163 #endif
164 }
165
166 void NotificationPermissionRequestManager::didReceiveNotificationPermissionDecision(uint64_t requestID, bool allowed)
167 {
168 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
169     if (!isRequestIDValid(requestID))
170         return;
171
172     RefPtr<WebCore::SecurityOrigin> origin = m_idToOriginMap.take(requestID);
173     if (!origin)
174         return;
175
176     m_originToIDMap.remove(origin);
177
178     WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(origin->toString(), allowed);
179
180 #if ENABLE(LEGACY_NOTIFICATIONS)
181     RefPtr<WebCore::VoidCallback> voidCallback = m_idToVoidCallbackMap.take(requestID);
182     if (voidCallback) {
183         voidCallback->handleEvent();
184         return;
185     }
186 #endif
187
188 #if ENABLE(NOTIFICATIONS)
189     RefPtr<NotificationPermissionCallback> callback = m_idToCallbackMap.take(requestID);
190     if (!callback)
191         return;
192     
193     callback->handleEvent(Notification::permissionString(allowed ? NotificationClient::PermissionAllowed : NotificationClient::PermissionDenied));
194 #endif
195
196 #else
197     UNUSED_PARAM(requestID);
198     UNUSED_PARAM(allowed);
199 #endif    
200 }
201
202 } // namespace WebKit