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#ifndef WebNotificationManagerProxy_h
27#define WebNotificationManagerProxy_h
28
29#include "APIObject.h"
30#include "MessageReceiver.h"
31#include "WebContextSupplement.h"
32#include "WebNotificationProvider.h"
33#include <WebCore/NotificationClient.h>
34#include <wtf/HashMap.h>
35#include <wtf/OwnPtr.h>
36#include <wtf/PassRefPtr.h>
37#include <wtf/text/StringHash.h>
38
39namespace WebKit {
40
41class ImmutableArray;
42class WebContext;
43class WebPageProxy;
44class WebSecurityOrigin;
45
46class WebNotificationManagerProxy : public TypedAPIObject<APIObject::TypeNotificationManager>, public WebContextSupplement {
47public:
48
49    static const char* supplementName();
50
51    static PassRefPtr<WebNotificationManagerProxy> create(WebContext*);
52
53    void initializeProvider(const WKNotificationProvider*);
54    void populateCopyOfNotificationPermissions(HashMap<String, bool>&);
55
56    void show(WebPageProxy*, const String& title, const String& body, const String& iconURL, const String& tag, const String& lang, const String& dir, const String& originString, uint64_t pageNotificationID);
57    void cancel(WebPageProxy*, uint64_t pageNotificationID);
58    void clearNotifications(WebPageProxy*);
59    void clearNotifications(WebPageProxy*, const Vector<uint64_t>& pageNotificationIDs);
60    void didDestroyNotification(WebPageProxy*, uint64_t pageNotificationID);
61
62    void providerDidShowNotification(uint64_t notificationID);
63    void providerDidClickNotification(uint64_t notificationID);
64    void providerDidCloseNotifications(ImmutableArray* notificationIDs);
65    void providerDidUpdateNotificationPolicy(const WebSecurityOrigin*, bool allowed);
66    void providerDidRemoveNotificationPolicies(ImmutableArray* origins);
67
68    using APIObject::ref;
69    using APIObject::deref;
70
71private:
72    explicit WebNotificationManagerProxy(WebContext*);
73
74    typedef bool (*NotificationFilterFunction)(uint64_t pageID, uint64_t pageNotificationID, uint64_t desiredPageID, const Vector<uint64_t>& desiredPageNotificationIDs);
75    void clearNotifications(WebPageProxy*, const Vector<uint64_t>& pageNotificationIDs, NotificationFilterFunction);
76
77    // WebContextSupplement
78    virtual void contextDestroyed() OVERRIDE;
79    virtual void refWebContextSupplement() OVERRIDE;
80    virtual void derefWebContextSupplement() OVERRIDE;
81
82    WebNotificationProvider m_provider;
83    // Pair comprised of web page ID and the web process's notification ID
84    HashMap<uint64_t, pair<uint64_t, uint64_t>> m_globalNotificationMap;
85    // Key pair comprised of web page ID and the web process's notification ID; value pair comprised of global notification ID, and notification object
86    HashMap<pair<uint64_t, uint64_t>, pair<uint64_t, RefPtr<WebNotification>>> m_notifications;
87};
88
89} // namespace WebKit
90
91#endif // WebNotificationManagerProxy_h
92