2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/notifications/Notification.h"
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"
46 Notification* Notification::create(ExecutionContext* context, const String& title, const NotificationOptions& options)
48 NotificationClient& client = NotificationController::clientFrom(context);
49 Notification* notification = new Notification(title, context, &client);
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);
61 String insecureOriginMessage;
62 UseCounter::Feature feature = context->securityOrigin()->canAccessFeatureRequiringSecureOrigin(insecureOriginMessage)
63 ? UseCounter::NotificationSecureOrigin : UseCounter::NotificationInsecureOrigin;
64 UseCounter::count(context, feature);
66 notification->suspendIfNeeded();
70 Notification::Notification(const String& title, ExecutionContext* context, NotificationClient* client)
71 : ActiveDOMObject(context)
74 , m_state(NotificationStateIdle)
76 , m_asyncRunner(this, &Notification::show)
80 m_asyncRunner.runAsync();
83 Notification::~Notification()
87 void Notification::show()
89 ASSERT(m_state == NotificationStateIdle);
90 if (!toDocument(executionContext())->page())
93 if (m_client->checkPermission(executionContext()) != NotificationClient::PermissionAllowed) {
98 if (m_client->show(this))
99 m_state = NotificationStateShowing;
102 void Notification::close()
105 case NotificationStateIdle:
107 case NotificationStateShowing:
108 m_client->close(this);
110 case NotificationStateClosed:
115 void Notification::dispatchShowEvent()
117 dispatchEvent(Event::create(EventTypeNames::show));
120 void Notification::dispatchClickEvent()
122 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
123 WindowFocusAllowedIndicator windowFocusAllowed;
124 dispatchEvent(Event::create(EventTypeNames::click));
127 void Notification::dispatchErrorEvent()
129 dispatchEvent(Event::create(EventTypeNames::error));
132 void Notification::dispatchCloseEvent()
134 dispatchEvent(Event::create(EventTypeNames::close));
135 m_state = NotificationStateClosed;
138 TextDirection Notification::direction() const
140 // FIXME: Resolve dir()=="auto" against the document.
141 return dir() == "rtl" ? RTL : LTR;
144 const String& Notification::permissionString(NotificationClient::Permission permission)
146 DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted"));
147 DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied"));
148 DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default"));
150 switch (permission) {
151 case NotificationClient::PermissionAllowed:
152 return allowedPermission;
153 case NotificationClient::PermissionDenied:
154 return deniedPermission;
155 case NotificationClient::PermissionNotAllowed:
156 return defaultPermission;
159 ASSERT_NOT_REACHED();
160 return deniedPermission;
163 const String& Notification::permission(ExecutionContext* context)
165 return permissionString(NotificationController::clientFrom(context).checkPermission(context));
168 void Notification::requestPermission(ExecutionContext* context, NotificationPermissionCallback* callback)
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);
178 bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
180 ASSERT(m_state != NotificationStateClosed);
182 return EventTarget::dispatchEvent(event);
185 const AtomicString& Notification::interfaceName() const
187 return EventTargetNames::Notification;
190 void Notification::stop()
192 m_state = NotificationStateClosed;
195 m_client->notificationObjectDestroyed(this);
199 m_asyncRunner.stop();
202 bool Notification::hasPendingActivity() const
204 return m_state == NotificationStateShowing || m_asyncRunner.isActive();