1/*
2 * Copyright (C) 2011, 2012, 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#ifndef WebNotificationManager_h
27#define WebNotificationManager_h
28
29#include "MessageReceiver.h"
30#include "WebProcessSupplement.h"
31#include <WebCore/NotificationClient.h>
32#include <wtf/HashMap.h>
33#include <wtf/Noncopyable.h>
34#include <wtf/RefPtr.h>
35#include <wtf/Vector.h>
36#include <wtf/text/StringHash.h>
37
38namespace WebCore {
39class Notification;
40class SecurityOrigin;
41}
42
43namespace WebKit {
44
45class WebPage;
46class WebProcess;
47
48class WebNotificationManager : public WebProcessSupplement, public IPC::MessageReceiver {
49    WTF_MAKE_NONCOPYABLE(WebNotificationManager);
50public:
51    explicit WebNotificationManager(WebProcess*);
52    ~WebNotificationManager();
53
54    static const char* supplementName();
55
56    bool show(WebCore::Notification*, WebPage*);
57    void cancel(WebCore::Notification*, WebPage*);
58    void clearNotifications(WebCore::ScriptExecutionContext*, WebPage*);
59    // This callback comes from WebCore, not messaged from the UI process.
60    void didDestroyNotification(WebCore::Notification*, WebPage*);
61
62    void didUpdateNotificationDecision(const String& originString, bool allowed);
63
64    // Looks in local cache for permission. If not found, returns DefaultDenied.
65    WebCore::NotificationClient::Permission policyForOrigin(WebCore::SecurityOrigin*) const;
66
67    void removeAllPermissionsForTesting();
68    uint64_t notificationIDForTesting(WebCore::Notification*);
69
70private:
71    // WebProcessSupplement
72    virtual void initialize(const WebProcessCreationParameters&) override;
73
74    // IPC::MessageReceiver
75    // Implemented in generated WebNotificationManagerMessageReceiver.cpp
76    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
77
78    void didShowNotification(uint64_t notificationID);
79    void didClickNotification(uint64_t notificationID);
80    void didCloseNotifications(const Vector<uint64_t>& notificationIDs);
81    void didRemoveNotificationDecisions(const Vector<String>& originStrings);
82
83#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
84    void removeNotificationFromContextMap(uint64_t notificationID, WebCore::Notification*);
85#endif
86
87    WebProcess* m_process;
88
89#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
90    typedef HashMap<RefPtr<WebCore::Notification>, uint64_t> NotificationMap;
91    NotificationMap m_notificationMap;
92
93    typedef HashMap<uint64_t, RefPtr<WebCore::Notification>> NotificationIDMap;
94    NotificationIDMap m_notificationIDMap;
95
96    typedef HashMap<RefPtr<WebCore::ScriptExecutionContext>, Vector<uint64_t>> NotificationContextMap;
97    NotificationContextMap m_notificationContextMap;
98
99    HashMap<String, bool> m_permissionsMap;
100#endif
101};
102
103} // namespace WebKit
104
105#endif
106