Remove support for legacy Notifications
[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)
40 #include "WebNotificationManager.h"
41 #endif
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 #if ENABLE(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)
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 void NotificationPermissionRequestManager::cancelRequest(SecurityOrigin* origin)
90 {
91 #if ENABLE(NOTIFICATIONS)
92     uint64_t id = m_originToIDMap.take(origin);
93     if (!id)
94         return;
95     
96     m_idToOriginMap.remove(id);
97     m_idToCallbackMap.remove(id);
98 #else
99     UNUSED_PARAM(origin);
100 #endif
101 }
102
103 bool NotificationPermissionRequestManager::hasPendingPermissionRequests(SecurityOrigin* origin) const
104 {
105 #if ENABLE(NOTIFICATIONS)
106     return m_originToIDMap.contains(origin);
107 #else
108     UNUSED_PARAM(origin);
109     return false;
110 #endif
111 }
112
113 NotificationClient::Permission NotificationPermissionRequestManager::permissionLevel(SecurityOrigin* securityOrigin)
114 {
115 #if ENABLE(NOTIFICATIONS)
116     if (!m_page->corePage()->settings().notificationsEnabled())
117         return NotificationClient::PermissionDenied;
118     
119     return WebProcess::singleton().supplement<WebNotificationManager>()->policyForOrigin(securityOrigin);
120 #else
121     UNUSED_PARAM(securityOrigin);
122     return NotificationClient::PermissionDenied;
123 #endif
124 }
125
126 void NotificationPermissionRequestManager::setPermissionLevelForTesting(const String& originString, bool allowed)
127 {
128 #if ENABLE(NOTIFICATIONS)
129     WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(originString, allowed);
130 #else
131     UNUSED_PARAM(originString);
132     UNUSED_PARAM(allowed);
133 #endif
134 }
135
136 void NotificationPermissionRequestManager::removeAllPermissionsForTesting()
137 {
138 #if ENABLE(NOTIFICATIONS)
139     WebProcess::singleton().supplement<WebNotificationManager>()->removeAllPermissionsForTesting();
140 #endif
141 }
142
143 void NotificationPermissionRequestManager::didReceiveNotificationPermissionDecision(uint64_t requestID, bool allowed)
144 {
145 #if ENABLE(NOTIFICATIONS)
146     if (!isRequestIDValid(requestID))
147         return;
148
149     RefPtr<WebCore::SecurityOrigin> origin = m_idToOriginMap.take(requestID);
150     if (!origin)
151         return;
152
153     m_originToIDMap.remove(origin);
154
155     WebProcess::singleton().supplement<WebNotificationManager>()->didUpdateNotificationDecision(origin->toString(), allowed);
156
157     RefPtr<NotificationPermissionCallback> callback = m_idToCallbackMap.take(requestID);
158     if (!callback)
159         return;
160     
161     callback->handleEvent(Notification::permissionString(allowed ? NotificationClient::PermissionAllowed : NotificationClient::PermissionDenied));
162 #else
163     UNUSED_PARAM(requestID);
164     UNUSED_PARAM(allowed);
165 #endif    
166 }
167
168 } // namespace WebKit