blob: fde326f3b1ca3bd365cdc0280a23c39fcc3cbef6 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2012 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#include "config.h"
33
34#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
35
36#include "modules/notifications/NotificationCenter.h"
37
38#include "core/dom/Document.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039#include "core/workers/WorkerContext.h"
40#include "modules/notifications/NotificationClient.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010041#include "weborigin/SecurityOrigin.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042
43namespace WebCore {
44
45PassRefPtr<NotificationCenter> NotificationCenter::create(ScriptExecutionContext* context, NotificationClient* client)
46{
47 RefPtr<NotificationCenter> notificationCenter(adoptRef(new NotificationCenter(context, client)));
48 notificationCenter->suspendIfNeeded();
49 return notificationCenter.release();
50}
51
52NotificationCenter::NotificationCenter(ScriptExecutionContext* context, NotificationClient* client)
53 : ActiveDOMObject(context)
54 , m_client(client)
55{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010056 ScriptWrappable::init(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057}
58
59#if ENABLE(LEGACY_NOTIFICATIONS)
60int NotificationCenter::checkPermission()
61{
62 if (!client() || !scriptExecutionContext())
63 return NotificationClient::PermissionDenied;
64
65 switch (scriptExecutionContext()->securityOrigin()->canShowNotifications()) {
66 case SecurityOrigin::AlwaysAllow:
67 return NotificationClient::PermissionAllowed;
68 case SecurityOrigin::AlwaysDeny:
69 return NotificationClient::PermissionDenied;
70 case SecurityOrigin::Ask:
71 return m_client->checkPermission(scriptExecutionContext());
72 }
73
74 ASSERT_NOT_REACHED();
75 return m_client->checkPermission(scriptExecutionContext());
76}
77#endif
78
79#if ENABLE(LEGACY_NOTIFICATIONS)
80void NotificationCenter::requestPermission(PassRefPtr<VoidCallback> callback)
81{
82 if (!client() || !scriptExecutionContext())
83 return;
84
85 switch (scriptExecutionContext()->securityOrigin()->canShowNotifications()) {
86 case SecurityOrigin::AlwaysAllow:
87 case SecurityOrigin::AlwaysDeny: {
88 m_callbacks.add(NotificationRequestCallback::createAndStartTimer(this, callback));
89 return;
90 }
91 case SecurityOrigin::Ask:
92 return m_client->requestPermission(scriptExecutionContext(), callback);
93 }
94
95 ASSERT_NOT_REACHED();
96 m_client->requestPermission(scriptExecutionContext(), callback);
97}
98#endif
99
100void NotificationCenter::stop()
101{
102 if (!m_client)
103 return;
104 m_client->cancelRequestsForPermission(scriptExecutionContext());
105 m_client->clearNotifications(scriptExecutionContext());
106 m_client = 0;
107}
108
109void NotificationCenter::requestTimedOut(NotificationCenter::NotificationRequestCallback* request)
110{
111 m_callbacks.remove(request);
112}
113
114PassRefPtr<NotificationCenter::NotificationRequestCallback> NotificationCenter::NotificationRequestCallback::createAndStartTimer(NotificationCenter* center, PassRefPtr<VoidCallback> callback)
115{
116 RefPtr<NotificationCenter::NotificationRequestCallback> requestCallback = adoptRef(new NotificationCenter::NotificationRequestCallback(center, callback));
117 requestCallback->startTimer();
118 return requestCallback.release();
119}
120
121NotificationCenter::NotificationRequestCallback::NotificationRequestCallback(NotificationCenter* center, PassRefPtr<VoidCallback> callback)
122 : m_notificationCenter(center)
123 , m_timer(this, &NotificationCenter::NotificationRequestCallback::timerFired)
124 , m_callback(callback)
125{
126}
127
128void NotificationCenter::NotificationRequestCallback::startTimer()
129{
130 m_timer.startOneShot(0);
131}
132
133void NotificationCenter::NotificationRequestCallback::timerFired(Timer<NotificationCenter::NotificationRequestCallback>*)
134{
135 if (m_callback)
136 m_callback->handleEvent();
137 m_notificationCenter->requestTimedOut(this);
138}
139
140} // namespace WebCore
141
142#endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)