Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 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 "NotificationPresenterImpl.h"
     33 
     34 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     35 
     36 #include "WebNotification.h"
     37 #include "WebNotificationPermissionCallback.h"
     38 #include "WebNotificationPresenter.h"
     39 #include "core/dom/ScriptExecutionContext.h"
     40 #include "modules/notifications/Notification.h"
     41 #include "weborigin/SecurityOrigin.h"
     42 #include "wtf/PassRefPtr.h"
     43 
     44 using namespace WebCore;
     45 
     46 namespace WebKit {
     47 
     48 #if ENABLE(LEGACY_NOTIFICATIONS)
     49 class VoidCallbackClient : public WebNotificationPermissionCallback {
     50 public:
     51     explicit VoidCallbackClient(PassRefPtr<VoidCallback> callback)
     52         : m_callback(callback)
     53     {
     54     }
     55 
     56     virtual void permissionRequestComplete()
     57     {
     58         if (m_callback)
     59             m_callback->handleEvent();
     60         delete this;
     61     }
     62 
     63 private:
     64     virtual ~VoidCallbackClient() { }
     65 
     66     RefPtr<VoidCallback> m_callback;
     67 };
     68 #endif // ENABLE(LEGACY_NOTIFICATIONS)
     69 
     70 #if ENABLE(NOTIFICATIONS)
     71 class NotificationPermissionCallbackClient : public WebNotificationPermissionCallback {
     72 public:
     73     NotificationPermissionCallbackClient(WebNotificationPresenter* presenter, PassRefPtr<SecurityOrigin> securityOrigin, PassRefPtr<NotificationPermissionCallback> callback)
     74         : m_presenter(presenter)
     75         , m_securityOrigin(securityOrigin)
     76         , m_callback(callback)
     77     {
     78     }
     79 
     80     virtual void permissionRequestComplete()
     81     {
     82         if (m_callback)
     83             m_callback->handleEvent(Notification::permissionString(static_cast<NotificationClient::Permission>(m_presenter->checkPermission(WebSecurityOrigin(m_securityOrigin)))));
     84         delete this;
     85     }
     86 
     87 private:
     88     virtual ~NotificationPermissionCallbackClient() { }
     89 
     90     WebNotificationPresenter* m_presenter;
     91     RefPtr<SecurityOrigin> m_securityOrigin;
     92     RefPtr<NotificationPermissionCallback> m_callback;
     93 };
     94 #endif // ENABLE(NOTIFICATIONS)
     95 
     96 void NotificationPresenterImpl::initialize(WebNotificationPresenter* presenter)
     97 {
     98     m_presenter = presenter;
     99 }
    100 
    101 bool NotificationPresenterImpl::isInitialized()
    102 {
    103     return !!m_presenter;
    104 }
    105 
    106 bool NotificationPresenterImpl::show(Notification* notification)
    107 {
    108     return m_presenter->show(PassRefPtr<Notification>(notification));
    109 }
    110 
    111 void NotificationPresenterImpl::cancel(Notification* notification)
    112 {
    113     m_presenter->cancel(PassRefPtr<Notification>(notification));
    114 }
    115 
    116 void NotificationPresenterImpl::notificationObjectDestroyed(Notification* notification)
    117 {
    118     m_presenter->objectDestroyed(PassRefPtr<Notification>(notification));
    119 }
    120 
    121 void NotificationPresenterImpl::notificationControllerDestroyed()
    122 {
    123 }
    124 
    125 NotificationClient::Permission NotificationPresenterImpl::checkPermission(ScriptExecutionContext* context)
    126 {
    127     int result = m_presenter->checkPermission(WebSecurityOrigin(context->securityOrigin()));
    128     return static_cast<NotificationClient::Permission>(result);
    129 }
    130 
    131 #if ENABLE(LEGACY_NOTIFICATIONS)
    132 void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, PassRefPtr<VoidCallback> callback)
    133 {
    134     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new VoidCallbackClient(callback));
    135 }
    136 #endif // ENABLE(LEGACY_NOTIFICATIONS)
    137 
    138 #if ENABLE(NOTIFICATIONS)
    139 void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, WTF::PassRefPtr<NotificationPermissionCallback> callback)
    140 {
    141     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new NotificationPermissionCallbackClient(m_presenter, context->securityOrigin(), callback));
    142 }
    143 #endif // ENABLE(NOTIFICATIONS)
    144 
    145 } // namespace WebKit
    146 
    147 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
    148