1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#if ENABLE(NOTIFICATIONS)
34
35#include "Notification.h"
36#include "NotificationContents.h"
37
38#include "Document.h"
39#include "EventNames.h"
40#include "WorkerContext.h"
41
42namespace WebCore {
43
44Notification::Notification(const String& url, ScriptExecutionContext* context, ExceptionCode& ec, NotificationProvider* provider)
45 : ActiveDOMObject(context, this)
46 , m_isHTML(true)
47 , m_isShowing(false)
48 , m_provider(provider)
49{
50 if (m_provider->checkPermission(context->securityOrigin()) != NotificationProvider::PERMISSION_ALLOWED) {
51 ec = SECURITY_ERR;
52 return;
53 }
54
55 m_notificationURL = context->completeURL(url);
56 if (url.isEmpty() || !m_notificationURL.isValid()) {
57 ec = SYNTAX_ERR;
58 return;
59 }
60}
61
62Notification::Notification(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, NotificationProvider* provider)
63 : ActiveDOMObject(context, this)
64 , m_isHTML(false)
65 , m_isShowing(false)
66 , m_contents(contents)
67 , m_provider(provider)
68{
69 if (m_provider->checkPermission(context->securityOrigin()) != NotificationProvider::PERMISSION_ALLOWED) {
70 ec = SECURITY_ERR;
71 return;
72 }
73
74 KURL icon = context->completeURL(contents.icon());
75 if (!icon.isEmpty() && !icon.isValid()) {
76 ec = SYNTAX_ERR;
77 return;
78 }
79}
80
81Notification::~Notification()
82{
83}
84
85void Notification::show()
86{
87 // prevent double-showing
88 if (!m_isShowing)
89 m_isShowing = m_provider->show(this);
90}
91
92void Notification::cancel()
93{
94 if (m_isShowing)
95 m_provider->cancel(this);
96}
97
98void Notification::dispatchDisplayEvent()
99{
100 RefPtr<Event> event = Event::create("display", false, true);
101
102 if (m_onDisplayListener) {
103 event->setTarget(this);
104 event->setCurrentTarget(this);
105 m_onDisplayListener->handleEvent(event.get(), true);
106 }
107
108 ExceptionCode ec = 0;
109 dispatchEvent(event.release(), ec);
110 ASSERT(!ec);
111}
112
113void Notification::dispatchErrorEvent()
114{
115 RefPtr<Event> event = Event::create(eventNames().errorEvent, false, true);
116
117 if (m_onErrorListener) {
118 event->setTarget(this);
119 event->setCurrentTarget(this);
120 m_onErrorListener->handleEvent(event.get(), true);
121 }
122
123 ExceptionCode ec = 0;
124 dispatchEvent(event.release(), ec);
125 ASSERT(!ec);
126}
127
128void Notification::dispatchCloseEvent()
129{
130 RefPtr<Event> event = Event::create(eventNames().closeEvent, false, true);
131
132 if (m_onCloseListener) {
133 event->setTarget(this);
134 event->setCurrentTarget(this);
135 m_onCloseListener->handleEvent(event.get(), true);
136 }
137
138 ExceptionCode ec = 0;
139 dispatchEvent(event.release(), ec);
140 ASSERT(!ec);
141
142}
143
144void Notification::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
145{
146 RefPtr<RegisteredEventListener> registeredListener = RegisteredEventListener::create(eventType, listener, useCapture);
147 m_eventListeners.append(registeredListener);
148}
149
150void Notification::removeEventListener(const AtomicString& eventType, EventListener* listener, bool useCapture)
151{
152 size_t size = m_eventListeners.size();
153 for (size_t i = 0; i < size; ++i) {
154 RegisteredEventListener& r = *m_eventListeners[i];
155 if (r.eventType() == eventType && r.listener() == listener && r.useCapture() == useCapture) {
156 r.setRemoved(true);
157 m_eventListeners.remove(i);
158 return;
159 }
160 }
161}
162
163bool Notification::dispatchEvent(PassRefPtr<Event> event, ExceptionCode& ec)
164{
165 RegisteredEventListenerVector listenersCopy = m_eventListeners;
166 size_t size = listenersCopy.size();
167 for (size_t i = 0; i < size; ++i) {
168 RegisteredEventListener& r = *listenersCopy[i];
169 if (r.eventType() == event->type() && !r.removed())
170 r.listener()->handleEvent(event.get());
171 }
172
173 return true;
174}
175
176void Notification::display()
177{
178 dispatchDisplayEvent();
179}
180
181void Notification::error()
182{
183 dispatchErrorEvent();
184}
185
186void Notification::close()
187{
188 dispatchCloseEvent();
189}
190
191} // namespace WebCore
192
193#endif // ENABLE(NOTIFICATIONS)