Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / notifications / NotificationCenter.cpp
1 /*
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(LEGACY_NOTIFICATIONS)
35
36 #include "modules/notifications/NotificationCenter.h"
37
38 #include "core/dom/Document.h"
39 #include "core/workers/WorkerGlobalScope.h"
40 #include "modules/notifications/NotificationClient.h"
41 #include "platform/weborigin/SecurityOrigin.h"
42
43 namespace WebCore {
44
45 DEFINE_GC_INFO(NotificationCenter);
46 DEFINE_GC_INFO(NotificationCenter::NotificationRequestCallback);
47
48 PassRefPtrWillBeRawPtr<NotificationCenter> NotificationCenter::create(ExecutionContext* context, NotificationClient* client)
49 {
50     RefPtrWillBeRawPtr<NotificationCenter> notificationCenter = adoptRefWillBeNoop(new NotificationCenter(context, client));
51     notificationCenter->suspendIfNeeded();
52     return notificationCenter.release();
53 }
54
55 NotificationCenter::NotificationCenter(ExecutionContext* context, NotificationClient* client)
56     : ActiveDOMObject(context)
57     , m_client(client)
58 {
59     ScriptWrappable::init(this);
60 }
61
62 int NotificationCenter::checkPermission()
63 {
64     if (!client() || !executionContext())
65         return NotificationClient::PermissionDenied;
66
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());
74     }
75
76     ASSERT_NOT_REACHED();
77     return m_client->checkPermission(executionContext());
78 }
79
80 void NotificationCenter::requestPermission(PassOwnPtr<VoidCallback> callback)
81 {
82     if (!client() || !executionContext())
83         return;
84
85     switch (executionContext()->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(executionContext(), callback);
93     }
94
95     ASSERT_NOT_REACHED();
96     m_client->requestPermission(executionContext(), callback);
97 }
98
99 void NotificationCenter::stop()
100 {
101     m_client = 0;
102 }
103
104 void NotificationCenter::requestTimedOut(NotificationCenter::NotificationRequestCallback* request)
105 {
106     m_callbacks.remove(request);
107 }
108
109 void NotificationCenter::trace(Visitor* visitor)
110 {
111     visitor->trace(m_callbacks);
112 }
113
114 PassRefPtrWillBeRawPtr<NotificationCenter::NotificationRequestCallback> NotificationCenter::NotificationRequestCallback::createAndStartTimer(NotificationCenter* center, PassOwnPtr<VoidCallback> callback)
115 {
116     RefPtrWillBeRawPtr<NotificationCenter::NotificationRequestCallback> requestCallback = adoptRefWillBeNoop(new NotificationCenter::NotificationRequestCallback(center, callback));
117     requestCallback->startTimer();
118     return requestCallback.release();
119 }
120
121 NotificationCenter::NotificationRequestCallback::NotificationRequestCallback(NotificationCenter* center, PassOwnPtr<VoidCallback> callback)
122     : m_notificationCenter(center)
123     , m_timer(this, &NotificationCenter::NotificationRequestCallback::timerFired)
124     , m_callback(callback)
125 {
126 }
127
128 void NotificationCenter::NotificationRequestCallback::startTimer()
129 {
130     m_timer.startOneShot(0);
131 }
132
133 void NotificationCenter::NotificationRequestCallback::timerFired(Timer<NotificationCenter::NotificationRequestCallback>*)
134 {
135     if (m_callback)
136         m_callback->handleEvent();
137     m_notificationCenter->requestTimedOut(this);
138 }
139
140 void NotificationCenter::NotificationRequestCallback::trace(Visitor* visitor)
141 {
142     visitor->trace(m_notificationCenter);
143 }
144
145 } // namespace WebCore
146
147 #endif // ENABLE(LEGACY_NOTIFICATIONS)