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