Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / notifications / Notification.cpp
1 /*
2  * Copyright (C) 2013 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 #include "modules/notifications/Notification.h"
33
34 #include "bindings/core/v8/ScriptWrappable.h"
35 #include "core/dom/Document.h"
36 #include "core/events/Event.h"
37 #include "core/frame/UseCounter.h"
38 #include "core/page/WindowFocusAllowedIndicator.h"
39 #include "modules/notifications/NotificationClient.h"
40 #include "modules/notifications/NotificationController.h"
41 #include "modules/notifications/NotificationOptions.h"
42 #include "modules/notifications/NotificationPermissionClient.h"
43
44 namespace blink {
45
46 Notification* Notification::create(ExecutionContext* context, const String& title, const NotificationOptions& options)
47 {
48     NotificationClient& client = NotificationController::clientFrom(context);
49     Notification* notification = new Notification(title, context, &client);
50
51     notification->setBody(options.body());
52     notification->setTag(options.tag());
53     notification->setLang(options.lang());
54     notification->setDir(options.dir());
55     if (options.hasIcon()) {
56         KURL iconUrl = options.icon().isEmpty() ? KURL() : context->completeURL(options.icon());
57         if (!iconUrl.isEmpty() && iconUrl.isValid())
58             notification->setIconUrl(iconUrl);
59     }
60
61     String insecureOriginMessage;
62     UseCounter::Feature feature = context->securityOrigin()->canAccessFeatureRequiringSecureOrigin(insecureOriginMessage)
63         ? UseCounter::NotificationSecureOrigin : UseCounter::NotificationInsecureOrigin;
64     UseCounter::count(context, feature);
65
66     notification->suspendIfNeeded();
67     return notification;
68 }
69
70 Notification::Notification(const String& title, ExecutionContext* context, NotificationClient* client)
71     : ActiveDOMObject(context)
72     , m_title(title)
73     , m_dir("auto")
74     , m_state(NotificationStateIdle)
75     , m_client(client)
76     , m_asyncRunner(this, &Notification::show)
77 {
78     ASSERT(m_client);
79
80     m_asyncRunner.runAsync();
81 }
82
83 Notification::~Notification()
84 {
85 }
86
87 void Notification::show()
88 {
89     ASSERT(m_state == NotificationStateIdle);
90     if (!toDocument(executionContext())->page())
91         return;
92
93     if (m_client->checkPermission(executionContext()) != NotificationClient::PermissionAllowed) {
94         dispatchErrorEvent();
95         return;
96     }
97
98     if (m_client->show(this))
99         m_state = NotificationStateShowing;
100 }
101
102 void Notification::close()
103 {
104     switch (m_state) {
105     case NotificationStateIdle:
106         break;
107     case NotificationStateShowing:
108         m_client->close(this);
109         break;
110     case NotificationStateClosed:
111         break;
112     }
113 }
114
115 void Notification::dispatchShowEvent()
116 {
117     dispatchEvent(Event::create(EventTypeNames::show));
118 }
119
120 void Notification::dispatchClickEvent()
121 {
122     UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
123     WindowFocusAllowedIndicator windowFocusAllowed;
124     dispatchEvent(Event::create(EventTypeNames::click));
125 }
126
127 void Notification::dispatchErrorEvent()
128 {
129     dispatchEvent(Event::create(EventTypeNames::error));
130 }
131
132 void Notification::dispatchCloseEvent()
133 {
134     dispatchEvent(Event::create(EventTypeNames::close));
135     m_state = NotificationStateClosed;
136 }
137
138 TextDirection Notification::direction() const
139 {
140     // FIXME: Resolve dir()=="auto" against the document.
141     return dir() == "rtl" ? RTL : LTR;
142 }
143
144 const String& Notification::permissionString(NotificationClient::Permission permission)
145 {
146     DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted"));
147     DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied"));
148     DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default"));
149
150     switch (permission) {
151     case NotificationClient::PermissionAllowed:
152         return allowedPermission;
153     case NotificationClient::PermissionDenied:
154         return deniedPermission;
155     case NotificationClient::PermissionNotAllowed:
156         return defaultPermission;
157     }
158
159     ASSERT_NOT_REACHED();
160     return deniedPermission;
161 }
162
163 const String& Notification::permission(ExecutionContext* context)
164 {
165     return permissionString(NotificationController::clientFrom(context).checkPermission(context));
166 }
167
168 void Notification::requestPermission(ExecutionContext* context, NotificationPermissionCallback* callback)
169 {
170     // FIXME: Assert that this code-path will only be reached for Document environments
171     // when Blink supports [Exposed] annotations on class members in IDL definitions.
172     if (NotificationPermissionClient* permissionClient = NotificationPermissionClient::from(context)) {
173         permissionClient->requestPermission(context, callback);
174         return;
175     }
176 }
177
178 bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
179 {
180     ASSERT(m_state != NotificationStateClosed);
181
182     return EventTarget::dispatchEvent(event);
183 }
184
185 const AtomicString& Notification::interfaceName() const
186 {
187     return EventTargetNames::Notification;
188 }
189
190 void Notification::stop()
191 {
192     m_state = NotificationStateClosed;
193
194     if (m_client) {
195         m_client->notificationObjectDestroyed(this);
196         m_client = 0;
197     }
198
199     m_asyncRunner.stop();
200 }
201
202 bool Notification::hasPendingActivity() const
203 {
204     return m_state == NotificationStateShowing || m_asyncRunner.isActive();
205 }
206
207 } // namespace blink