This source file includes following definitions.
- create
- m_asyncRunner
- show
- close
- dispatchShowEvent
- dispatchClickEvent
- dispatchErrorEvent
- dispatchCloseEvent
- direction
- permissionString
- permission
- requestPermission
- dispatchEvent
- interfaceName
- stop
- hasPendingActivity
#include "config.h"
#include "modules/notifications/Notification.h"
#include "bindings/v8/Dictionary.h"
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/Document.h"
#include "core/page/WindowFocusAllowedIndicator.h"
#include "modules/notifications/NotificationClient.h"
#include "modules/notifications/NotificationController.h"
namespace WebCore {
PassRefPtrWillBeRawPtr<Notification> Notification::create(ExecutionContext* context, const String& title, const Dictionary& options)
{
NotificationClient* client = NotificationController::clientFrom(toDocument(context)->page());
RefPtrWillBeRawPtr<Notification> notification = adoptRefWillBeRefCountedGarbageCollected(new Notification(title, context, client));
String argument;
if (options.get("body", argument))
notification->setBody(argument);
if (options.get("tag", argument))
notification->setTag(argument);
if (options.get("lang", argument))
notification->setLang(argument);
if (options.get("dir", argument))
notification->setDir(argument);
if (options.get("icon", argument)) {
KURL iconUrl = argument.isEmpty() ? KURL() : context->completeURL(argument);
if (!iconUrl.isEmpty() && iconUrl.isValid())
notification->setIconUrl(iconUrl);
}
notification->suspendIfNeeded();
return notification.release();
}
Notification::Notification(const String& title, ExecutionContext* context, NotificationClient* client)
: ActiveDOMObject(context)
, m_title(title)
, m_dir("auto")
, m_state(Idle)
, m_client(client)
, m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notification::show)))
{
ASSERT(m_client);
ScriptWrappable::init(this);
m_asyncRunner->runAsync();
}
Notification::~Notification()
{
}
void Notification::show()
{
ASSERT(m_state == Idle);
if (!toDocument(executionContext())->page())
return;
if (NotificationController::from(toDocument(executionContext())->page())->client()->checkPermission(executionContext()) != NotificationClient::PermissionAllowed) {
dispatchErrorEvent();
return;
}
if (m_client->show(this))
m_state = Showing;
}
void Notification::close()
{
switch (m_state) {
case Idle:
break;
case Showing:
m_client->close(this);
break;
case Closed:
break;
}
}
void Notification::dispatchShowEvent()
{
dispatchEvent(Event::create(EventTypeNames::show));
}
void Notification::dispatchClickEvent()
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
WindowFocusAllowedIndicator windowFocusAllowed;
dispatchEvent(Event::create(EventTypeNames::click));
}
void Notification::dispatchErrorEvent()
{
dispatchEvent(Event::create(EventTypeNames::error));
}
void Notification::dispatchCloseEvent()
{
dispatchEvent(Event::create(EventTypeNames::close));
m_state = Closed;
}
TextDirection Notification::direction() const
{
return dir() == "rtl" ? RTL : LTR;
}
const String& Notification::permissionString(NotificationClient::Permission permission)
{
DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted"));
DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied"));
DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default"));
switch (permission) {
case NotificationClient::PermissionAllowed:
return allowedPermission;
case NotificationClient::PermissionDenied:
return deniedPermission;
case NotificationClient::PermissionNotAllowed:
return defaultPermission;
}
ASSERT_NOT_REACHED();
return deniedPermission;
}
const String& Notification::permission(ExecutionContext* context)
{
ASSERT(toDocument(context)->page());
return permissionString(NotificationController::from(toDocument(context)->page())->client()->checkPermission(context));
}
void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<NotificationPermissionCallback> callback)
{
ASSERT(toDocument(context)->page());
NotificationController::from(toDocument(context)->page())->client()->requestPermission(context, callback);
}
bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
{
ASSERT(m_state != Closed);
return EventTarget::dispatchEvent(event);
}
const AtomicString& Notification::interfaceName() const
{
return EventTargetNames::Notification;
}
void Notification::stop()
{
if (m_client)
m_client->notificationObjectDestroyed(this);
if (m_asyncRunner)
m_asyncRunner->stop();
m_client = 0;
m_state = Closed;
}
bool Notification::hasPendingActivity() const
{
return m_state == Showing || (m_asyncRunner && m_asyncRunner->isActive());
}
}