| 1 | /* |
| 2 | * Copyright (C) 2011 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 "WebNotificationProvider.h" |
| 28 | |
| 29 | #include "APIArray.h" |
| 30 | #include "APIDictionary.h" |
| 31 | #include "APINumber.h" |
| 32 | #include "APISecurityOrigin.h" |
| 33 | #include "WKAPICast.h" |
| 34 | #include "WebNotification.h" |
| 35 | #include "WebNotificationManagerProxy.h" |
| 36 | #include "WebPageProxy.h" |
| 37 | |
| 38 | namespace WebKit { |
| 39 | |
| 40 | WebNotificationProvider::WebNotificationProvider(const WKNotificationProviderBase* provider) |
| 41 | { |
| 42 | initialize(provider); |
| 43 | } |
| 44 | |
| 45 | void WebNotificationProvider::show(WebPageProxy& page, WebNotification& notification) |
| 46 | { |
| 47 | if (!m_client.show) |
| 48 | return; |
| 49 | |
| 50 | m_client.show(toAPI(&page), toAPI(¬ification), m_client.base.clientInfo); |
| 51 | } |
| 52 | |
| 53 | void WebNotificationProvider::cancel(WebNotification& notification) |
| 54 | { |
| 55 | if (!m_client.cancel) |
| 56 | return; |
| 57 | |
| 58 | m_client.cancel(toAPI(¬ification), m_client.base.clientInfo); |
| 59 | } |
| 60 | |
| 61 | void WebNotificationProvider::didDestroyNotification(WebNotification& notification) |
| 62 | { |
| 63 | if (!m_client.didDestroyNotification) |
| 64 | return; |
| 65 | |
| 66 | m_client.didDestroyNotification(toAPI(¬ification), m_client.base.clientInfo); |
| 67 | } |
| 68 | |
| 69 | void WebNotificationProvider::clearNotifications(const Vector<uint64_t>& notificationIDs) |
| 70 | { |
| 71 | if (!m_client.clearNotifications) |
| 72 | return; |
| 73 | |
| 74 | Vector<RefPtr<API::Object>> arrayIDs; |
| 75 | arrayIDs.reserveInitialCapacity(notificationIDs.size()); |
| 76 | |
| 77 | for (const auto& notificationID : notificationIDs) |
| 78 | arrayIDs.uncheckedAppend(API::UInt64::create(notificationID)); |
| 79 | |
| 80 | m_client.clearNotifications(toAPI(API::Array::create(WTFMove(arrayIDs)).ptr()), m_client.base.clientInfo); |
| 81 | } |
| 82 | |
| 83 | void WebNotificationProvider::addNotificationManager(WebNotificationManagerProxy& manager) |
| 84 | { |
| 85 | if (!m_client.addNotificationManager) |
| 86 | return; |
| 87 | |
| 88 | m_client.addNotificationManager(toAPI(&manager), m_client.base.clientInfo); |
| 89 | } |
| 90 | |
| 91 | void WebNotificationProvider::removeNotificationManager(WebNotificationManagerProxy& manager) |
| 92 | { |
| 93 | if (!m_client.removeNotificationManager) |
| 94 | return; |
| 95 | |
| 96 | m_client.removeNotificationManager(toAPI(&manager), m_client.base.clientInfo); |
| 97 | } |
| 98 | |
| 99 | HashMap<WTF::String, bool> WebNotificationProvider::notificationPermissions() |
| 100 | { |
| 101 | HashMap<WTF::String, bool> permissions; |
| 102 | if (!m_client.notificationPermissions) |
| 103 | return permissions; |
| 104 | |
| 105 | RefPtr<API::Dictionary> knownPermissions = adoptRef(toImpl(m_client.notificationPermissions(m_client.base.clientInfo))); |
| 106 | if (!knownPermissions) |
| 107 | return permissions; |
| 108 | |
| 109 | Ref<API::Array> knownOrigins = knownPermissions->keys(); |
| 110 | for (size_t i = 0; i < knownOrigins->size(); ++i) { |
| 111 | API::String* origin = knownOrigins->at<API::String>(i); |
| 112 | permissions.set(origin->string(), knownPermissions->get<API::Boolean>(origin->string())->value()); |
| 113 | } |
| 114 | return permissions; |
| 115 | } |
| 116 | |
| 117 | } // namespace WebKit |
| 118 | |