diff options
Diffstat (limited to 'chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp')
-rw-r--r-- | chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp | 130 |
1 files changed, 110 insertions, 20 deletions
diff --git a/chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp b/chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp index d2c9a0bd4b7..4030151d7cb 100644 --- a/chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp +++ b/chromium/third_party/WebKit/Source/modules/notifications/Notification.cpp @@ -1,6 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. All rights reserved. - * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved. + * Copyright (C) 2013 Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -35,15 +34,17 @@ #include "bindings/v8/Dictionary.h" #include "bindings/v8/ScriptWrappable.h" #include "core/dom/Document.h" -#include "core/dom/ExecutionContext.h" +#include "core/frame/UseCounter.h" +#include "core/page/WindowFocusAllowedIndicator.h" +#include "modules/notifications/NotificationClient.h" #include "modules/notifications/NotificationController.h" namespace WebCore { -PassRefPtr<Notification> Notification::create(ExecutionContext* context, const String& title, const Dictionary& options) +Notification* Notification::create(ExecutionContext* context, const String& title, const Dictionary& options) { - NotificationClient* client = NotificationController::clientFrom(toDocument(context)->page()); - RefPtr<Notification> notification(adoptRef(new Notification(context, title, client))); + NotificationClient& client = NotificationController::clientFrom(toDocument(context)->frame()); + Notification* notification = adoptRefCountedGarbageCollectedWillBeNoop(new Notification(title, context, &client)); String argument; if (options.get("body", argument)) @@ -61,13 +62,18 @@ PassRefPtr<Notification> Notification::create(ExecutionContext* context, const S } notification->suspendIfNeeded(); - return notification.release(); + return notification; } -Notification::Notification(ExecutionContext* context, const String& title, NotificationClient* client) - : NotificationBase(title, context, client) - , m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notification::showSoon))) +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(); @@ -77,16 +83,101 @@ Notification::~Notification() { } +void Notification::show() +{ + ASSERT(m_state == Idle); + if (!toDocument(executionContext())->page()) + return; + + if (m_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 +{ + // FIXME: Resolve dir()=="auto" against the document. + 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)); + + UseCounter::count(context, UseCounter::NotificationPermission); + return permissionString(NotificationController::clientFrom(toDocument(context)->frame()).checkPermission(context)); } void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<NotificationPermissionCallback> callback) { ASSERT(toDocument(context)->page()); - NotificationController::from(toDocument(context)->page())->client()->requestPermission(context, callback); + NotificationController::clientFrom(toDocument(context)->frame()).requestPermission(context, callback); +} + +bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event) +{ + ASSERT(m_state != Closed); + + return EventTarget::dispatchEvent(event); } const AtomicString& Notification::interfaceName() const @@ -96,20 +187,19 @@ const AtomicString& Notification::interfaceName() const void Notification::stop() { - NotificationBase::stop(); + if (m_client) + m_client->notificationObjectDestroyed(this); + if (m_asyncRunner) m_asyncRunner->stop(); -} -bool Notification::hasPendingActivity() const -{ - return NotificationBase::hasPendingActivity() || (m_asyncRunner && m_asyncRunner->isActive()); + m_client = 0; + m_state = Closed; } -void Notification::showSoon() +bool Notification::hasPendingActivity() const { - ASSERT(executionContext()->isDocument()); - show(); + return m_state == Showing || (m_asyncRunner && m_asyncRunner->isActive()); } } // namespace WebCore |