2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009, 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #if ENABLE(NOTIFICATIONS)
36 #include "Notification.h"
39 #include "EventNames.h"
40 #include "NotificationCenter.h"
41 #include "NotificationContents.h"
42 #include "NotificationPresenter.h"
43 #include "ResourceRequest.h"
44 #include "ResourceResponse.h"
45 #include "ThreadableLoader.h"
46 #include "WorkerContext.h"
50 Notification::Notification()
51 : ActiveDOMObject(0, this)
55 Notification::Notification(const KURL& url, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
56 : ActiveDOMObject(context, this)
59 , m_notificationCenter(provider)
61 ASSERT(m_notificationCenter->presenter());
62 if (m_notificationCenter->presenter()->checkPermission(context) != NotificationPresenter::PermissionAllowed) {
67 if (url.isEmpty() || !url.isValid()) {
72 m_notificationURL = url;
75 Notification::Notification(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
76 : ActiveDOMObject(context, this)
78 , m_contents(contents)
80 , m_notificationCenter(provider)
82 ASSERT(m_notificationCenter->presenter());
83 if (m_notificationCenter->presenter()->checkPermission(context) != NotificationPresenter::PermissionAllowed) {
88 if (!contents.icon.isEmpty() && !contents.icon.isValid()) {
94 Notification::~Notification()
96 if (m_state == Loading) {
102 PassRefPtr<Notification> Notification::create(const KURL& url, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
104 return adoptRef(new Notification(url, context, ec, provider));
107 PassRefPtr<Notification> Notification::create(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
109 return adoptRef(new Notification(contents, context, ec, provider));
112 const AtomicString& Notification::interfaceName() const
114 return eventNames().interfaceForNotification;
117 void Notification::show()
120 if (iconURL().isEmpty()) {
121 // Set the state before actually showing, because
122 // handling of ondisplay may rely on that.
123 if (m_state == Idle) {
125 if (m_notificationCenter->presenter())
126 m_notificationCenter->presenter()->show(this);
131 // prevent double-showing
132 if (m_state == Idle && m_notificationCenter->presenter() && m_notificationCenter->presenter()->show(this))
137 void Notification::cancel()
147 if (m_notificationCenter->presenter())
148 m_notificationCenter->presenter()->cancel(this);
155 EventTargetData* Notification::eventTargetData()
157 return &m_eventTargetData;
160 EventTargetData* Notification::ensureEventTargetData()
162 return &m_eventTargetData;
165 void Notification::contextDestroyed()
167 ActiveDOMObject::contextDestroyed();
168 if (m_notificationCenter->presenter())
169 m_notificationCenter->presenter()->notificationObjectDestroyed(this);
172 void Notification::startLoading()
176 setPendingActivity(this);
178 ThreadableLoaderOptions options;
179 options.sendLoadCallbacks = DoNotSendCallbacks;
180 options.sniffContent = DoNotSniffContent;
181 options.preflightPolicy = ConsiderPreflight;
182 options.allowCredentials = AllowStoredCredentials;
183 options.crossOriginRequestPolicy = AllowCrossOriginRequests;
184 m_loader = ThreadableLoader::create(scriptExecutionContext(), this, ResourceRequest(iconURL()), options);
187 void Notification::stopLoading()
190 RefPtr<ThreadableLoader> protect(m_loader);
194 void Notification::didReceiveResponse(unsigned long, const ResourceResponse& response)
196 int status = response.httpStatusCode();
197 if (status && (status < 200 || status > 299)) {
201 m_iconData = SharedBuffer::create();
204 void Notification::didReceiveData(const char* data, int dataLength)
206 m_iconData->append(data, dataLength);
209 void Notification::didFinishLoading(unsigned long, double)
214 void Notification::didFail(const ResourceError&)
219 void Notification::didFailRedirectCheck()
224 void Notification::finishLoading()
226 if (m_state == Loading) {
227 if (m_notificationCenter->presenter() && m_notificationCenter->presenter()->show(this))
230 unsetPendingActivity(this);
233 } // namespace WebCore
235 #endif // ENABLE(NOTIFICATIONS)