2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2012 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(LEGACY_NOTIFICATIONS)
36 #include "modules/notifications/NotificationCenter.h"
38 #include "core/dom/Document.h"
39 #include "core/workers/WorkerGlobalScope.h"
40 #include "modules/notifications/NotificationClient.h"
41 #include "platform/weborigin/SecurityOrigin.h"
45 DEFINE_GC_INFO(NotificationCenter);
46 DEFINE_GC_INFO(NotificationCenter::NotificationRequestCallback);
48 PassRefPtrWillBeRawPtr<NotificationCenter> NotificationCenter::create(ExecutionContext* context, NotificationClient* client)
50 RefPtrWillBeRawPtr<NotificationCenter> notificationCenter = adoptRefWillBeNoop(new NotificationCenter(context, client));
51 notificationCenter->suspendIfNeeded();
52 return notificationCenter.release();
55 NotificationCenter::NotificationCenter(ExecutionContext* context, NotificationClient* client)
56 : ActiveDOMObject(context)
59 ScriptWrappable::init(this);
62 int NotificationCenter::checkPermission()
64 if (!client() || !executionContext())
65 return NotificationClient::PermissionDenied;
67 switch (executionContext()->securityOrigin()->canShowNotifications()) {
68 case SecurityOrigin::AlwaysAllow:
69 return NotificationClient::PermissionAllowed;
70 case SecurityOrigin::AlwaysDeny:
71 return NotificationClient::PermissionDenied;
72 case SecurityOrigin::Ask:
73 return m_client->checkPermission(executionContext());
77 return m_client->checkPermission(executionContext());
80 void NotificationCenter::requestPermission(PassOwnPtr<VoidCallback> callback)
82 if (!client() || !executionContext())
85 switch (executionContext()->securityOrigin()->canShowNotifications()) {
86 case SecurityOrigin::AlwaysAllow:
87 case SecurityOrigin::AlwaysDeny: {
88 m_callbacks.add(NotificationRequestCallback::createAndStartTimer(this, callback));
91 case SecurityOrigin::Ask:
92 return m_client->requestPermission(executionContext(), callback);
96 m_client->requestPermission(executionContext(), callback);
99 void NotificationCenter::stop()
104 void NotificationCenter::requestTimedOut(NotificationCenter::NotificationRequestCallback* request)
106 m_callbacks.remove(request);
109 void NotificationCenter::trace(Visitor* visitor)
111 visitor->trace(m_callbacks);
114 PassRefPtrWillBeRawPtr<NotificationCenter::NotificationRequestCallback> NotificationCenter::NotificationRequestCallback::createAndStartTimer(NotificationCenter* center, PassOwnPtr<VoidCallback> callback)
116 RefPtrWillBeRawPtr<NotificationCenter::NotificationRequestCallback> requestCallback = adoptRefWillBeNoop(new NotificationCenter::NotificationRequestCallback(center, callback));
117 requestCallback->startTimer();
118 return requestCallback.release();
121 NotificationCenter::NotificationRequestCallback::NotificationRequestCallback(NotificationCenter* center, PassOwnPtr<VoidCallback> callback)
122 : m_notificationCenter(center)
123 , m_timer(this, &NotificationCenter::NotificationRequestCallback::timerFired)
124 , m_callback(callback)
128 void NotificationCenter::NotificationRequestCallback::startTimer()
130 m_timer.startOneShot(0);
133 void NotificationCenter::NotificationRequestCallback::timerFired(Timer<NotificationCenter::NotificationRequestCallback>*)
136 m_callback->handleEvent();
137 m_notificationCenter->requestTimedOut(this);
140 void NotificationCenter::NotificationRequestCallback::trace(Visitor* visitor)
142 visitor->trace(m_notificationCenter);
145 } // namespace WebCore
147 #endif // ENABLE(LEGACY_NOTIFICATIONS)