1/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef Notification_h
33#define Notification_h
34
35#include "ActiveDOMObject.h"
36#include "Event.h"
37#include "EventListener.h"
38#include "EventNames.h"
39#include "EventTarget.h"
40#include "ExceptionCode.h"
41#include "KURL.h"
42#include "NotificationPresenter.h"
43#include "NotificationContents.h"
44#include "RegisteredEventListener.h"
45#include "SharedBuffer.h"
46#include "TextDirection.h"
47#include "ThreadableLoader.h"
48#include "ThreadableLoaderClient.h"
49#include <wtf/OwnPtr.h>
50#include <wtf/PassRefPtr.h>
51#include <wtf/RefCounted.h>
52#include <wtf/RefPtr.h>
53#include <wtf/text/AtomicStringHash.h>
54
55#if ENABLE(NOTIFICATIONS)
56namespace WebCore {
57
58    class NotificationCenter;
59    class WorkerContext;
60
61    class Notification : public RefCounted<Notification>, public ActiveDOMObject, public ThreadableLoaderClient, public EventTarget {
62        WTF_MAKE_FAST_ALLOCATED;
63    public:
64        static PassRefPtr<Notification> create(const KURL& url, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider);
65        static PassRefPtr<Notification> create(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider);
66
67        virtual ~Notification();
68
69        void show();
70        void cancel();
71
72        bool isHTML() { return m_isHTML; }
73        KURL url() { return m_notificationURL; }
74        KURL iconURL() { return m_contents.icon(); }
75        NotificationContents& contents() { return m_contents; }
76
77        String dir() const { return m_direction; }
78        void setDir(const String& dir) { m_direction = dir; }
79        String replaceId() const { return m_replaceId; }
80        void setReplaceId(const String& replaceId) { m_replaceId = replaceId; }
81
82        TextDirection direction() const { return dir() == "rtl" ? RTL : LTR; }
83
84        DEFINE_ATTRIBUTE_EVENT_LISTENER(display);
85        DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
86        DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
87        DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
88
89        using RefCounted<Notification>::ref;
90        using RefCounted<Notification>::deref;
91
92        // EventTarget interface
93        virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
94        virtual Notification* toNotification() { return this; }
95
96        // ActiveDOMObject interface
97        virtual void contextDestroyed();
98
99        void stopLoading();
100
101        SharedBuffer* iconData() { return m_iconData.get(); }
102        void releaseIconData() { m_iconData = 0; }
103
104        // Deprecated. Use functions from NotificationCenter.
105        void detachPresenter() { }
106
107        virtual void didReceiveResponse(const ResourceResponse&);
108        virtual void didReceiveData(const char* data, int dataLength);
109        virtual void didFinishLoading(unsigned long identifier, double finishTime);
110        virtual void didFail(const ResourceError&);
111        virtual void didFailRedirectCheck();
112        virtual void didReceiveAuthenticationCancellation(const ResourceResponse&);
113
114    private:
115        Notification(const KURL&, ScriptExecutionContext*, ExceptionCode&, PassRefPtr<NotificationCenter>);
116        Notification(const NotificationContents&, ScriptExecutionContext*, ExceptionCode&, PassRefPtr<NotificationCenter>);
117
118        // EventTarget interface
119        virtual void refEventTarget() { ref(); }
120        virtual void derefEventTarget() { deref(); }
121        virtual EventTargetData* eventTargetData();
122        virtual EventTargetData* ensureEventTargetData();
123
124        void startLoading();
125        void finishLoading();
126
127        bool m_isHTML;
128        KURL m_notificationURL;
129        NotificationContents m_contents;
130
131        String m_direction;
132        String m_replaceId;
133
134        enum NotificationState {
135            Idle = 0,
136            Loading = 1,
137            Showing = 2,
138            Cancelled = 3
139        };
140
141        NotificationState m_state;
142
143        RefPtr<NotificationCenter> m_notificationCenter;
144
145        EventTargetData m_eventTargetData;
146
147        RefPtr<ThreadableLoader> m_loader;
148        RefPtr<SharedBuffer> m_iconData;
149    };
150
151} // namespace WebCore
152
153#endif // ENABLE(NOTIFICATIONS)
154
155#endif // Notifications_h
156