#include "config.h"
#if ENABLE(NOTIFICATIONS)
#include "Notification.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "NotificationClient.h"
#include "NotificationController.h"
#include "NotificationPermissionCallback.h"
#include "WindowFocusAllowedIndicator.h"
namespace WebCore {
Ref<Notification> Notification::create(Document& context, const String& title, const Options& options)
{
auto notification = adoptRef(*new Notification(context, title, options));
notification->suspendIfNeeded();
return notification;
}
Notification::Notification(Document& document, const String& title, const Options& options)
: ActiveDOMObject(&document)
, m_title(title)
, m_direction(options.dir)
, m_lang(options.lang)
, m_body(options.body)
, m_tag(options.tag)
, m_state(Idle)
, m_taskTimer(std::make_unique<Timer>([this] () { show(); }))
{
if (!options.icon.isEmpty()) {
auto iconURL = document.completeURL(options.icon);
if (iconURL.isValid())
m_icon = iconURL;
}
m_taskTimer->startOneShot(0_s);
}
Notification::~Notification() = default;
void Notification::show()
{
if (m_state != Idle)
return;
auto* page = downcast<Document>(*scriptExecutionContext()).page();
if (!page)
return;
auto& client = NotificationController::from(page)->client();
if (client.checkPermission(scriptExecutionContext()) != Permission::Granted) {
dispatchErrorEvent();
return;
}
if (client.show(this)) {
m_state = Showing;
setPendingActivity(this);
}
}
void Notification::close()
{
switch (m_state) {
case Idle:
break;
case Showing: {
auto* page = downcast<Document>(*scriptExecutionContext()).page();
if (page)
NotificationController::from(page)->client().cancel(this);
break;
}
case Closed:
break;
}
}
const char* Notification::activeDOMObjectName() const
{
return "Notification";
}
bool Notification::canSuspendForDocumentSuspension() const
{
return m_state == Idle || m_state == Closed;
}
void Notification::stop()
{
ActiveDOMObject::stop();
auto* page = downcast<Document>(*scriptExecutionContext()).page();
if (page)
NotificationController::from(page)->client().notificationObjectDestroyed(this);
}
void Notification::finalize()
{
if (m_state == Closed)
return;
m_state = Closed;
unsetPendingActivity(this);
}
void Notification::dispatchShowEvent()
{
dispatchEvent(Event::create(eventNames().showEvent, false, false));
}
void Notification::dispatchClickEvent()
{
WindowFocusAllowedIndicator windowFocusAllowed;
dispatchEvent(Event::create(eventNames().clickEvent, false, false));
}
void Notification::dispatchCloseEvent()
{
dispatchEvent(Event::create(eventNames().closeEvent, false, false));
finalize();
}
void Notification::dispatchErrorEvent()
{
dispatchEvent(Event::create(eventNames().errorEvent, false, false));
}
auto Notification::permission(Document& document) -> Permission
{
return NotificationController::from(document.page())->client().checkPermission(&document);
}
void Notification::requestPermission(Document& document, RefPtr<NotificationPermissionCallback>&& callback)
{
NotificationController::from(document.page())->client().requestPermission(&document, WTFMove(callback));
}
}
#endif // ENABLE(NOTIFICATIONS)