1/*
2 * Copyright (C) 2011, 2013 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#pragma once
27
28#include "APIObject.h"
29#include "MessageReceiver.h"
30#include "WebContextSupplement.h"
31#include <WebCore/NotificationClient.h>
32#include <WebCore/PageIdentifier.h>
33#include <wtf/HashMap.h>
34#include <wtf/text/StringHash.h>
35
36namespace WebCore {
37enum class NotificationDirection : uint8_t;
38}
39
40namespace API {
41class Array;
42class NotificationProvider;
43class SecurityOrigin;
44}
45
46namespace WebKit {
47
48class WebNotification;
49class WebPageProxy;
50class WebProcessPool;
51
52class WebNotificationManagerProxy : public API::ObjectImpl<API::Object::Type::NotificationManager>, public WebContextSupplement {
53public:
54
55 static const char* supplementName();
56
57 static Ref<WebNotificationManagerProxy> create(WebProcessPool*);
58
59 void setProvider(std::unique_ptr<API::NotificationProvider>&&);
60 HashMap<String, bool> notificationPermissions();
61
62 void show(WebPageProxy*, const String& title, const String& body, const String& iconURL, const String& tag, const String& lang, WebCore::NotificationDirection, const String& originString, uint64_t pageNotificationID);
63 void cancel(WebPageProxy*, uint64_t pageNotificationID);
64 void clearNotifications(WebPageProxy*);
65 void clearNotifications(WebPageProxy*, const Vector<uint64_t>& pageNotificationIDs);
66 void didDestroyNotification(WebPageProxy*, uint64_t pageNotificationID);
67
68 void providerDidShowNotification(uint64_t notificationID);
69 void providerDidClickNotification(uint64_t notificationID);
70 void providerDidCloseNotifications(API::Array* notificationIDs);
71 void providerDidUpdateNotificationPolicy(const API::SecurityOrigin*, bool allowed);
72 void providerDidRemoveNotificationPolicies(API::Array* origins);
73
74 uint64_t notificationLocalIDForTesting(WebNotification*);
75
76 using API::Object::ref;
77 using API::Object::deref;
78
79private:
80 explicit WebNotificationManagerProxy(WebProcessPool*);
81
82 typedef bool (*NotificationFilterFunction)(WebCore::PageIdentifier pageID, uint64_t pageNotificationID, WebCore::PageIdentifier desiredPageID, const Vector<uint64_t>& desiredPageNotificationIDs);
83 void clearNotifications(WebPageProxy*, const Vector<uint64_t>& pageNotificationIDs, NotificationFilterFunction);
84
85 // WebContextSupplement
86 void processPoolDestroyed() override;
87 void refWebContextSupplement() override;
88 void derefWebContextSupplement() override;
89
90 std::unique_ptr<API::NotificationProvider> m_provider;
91 // Pair comprised of web page ID and the web process's notification ID
92 HashMap<uint64_t, std::pair<WebCore::PageIdentifier, uint64_t>> m_globalNotificationMap;
93 // Key pair comprised of web page ID and the web process's notification ID; value pair comprised of global notification ID, and notification object
94 HashMap<std::pair<WebCore::PageIdentifier, uint64_t>, std::pair<uint64_t, RefPtr<WebNotification>>> m_notifications;
95};
96
97} // namespace WebKit
98