Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef NotificationPresenterClientQt_h
     33 #define NotificationPresenterClientQt_h
     34 
     35 #include "Notification.h"
     36 #include "NotificationPresenter.h"
     37 #include "QtPlatformPlugin.h"
     38 #include "Timer.h"
     39 
     40 #include "qwebkitplatformplugin.h"
     41 
     42 #include <QMultiHash>
     43 #include <QSystemTrayIcon>
     44 
     45 class QWebFrame;
     46 class QWebPage;
     47 
     48 namespace WebCore {
     49 
     50 class Document;
     51 class Frame;
     52 class ScriptExecutionContext;
     53 
     54 class NotificationWrapper : public QObject, public QWebNotificationData {
     55     Q_OBJECT
     56 public:
     57     NotificationWrapper();
     58     ~NotificationWrapper() {}
     59 
     60     void close();
     61     void close(Timer<NotificationWrapper>*);
     62     const QString title() const;
     63     const QString message() const;
     64     const QByteArray iconData() const;
     65     const QUrl openerPageUrl() const;
     66 
     67 public Q_SLOTS:
     68     void notificationClosed();
     69     void notificationClicked();
     70 
     71 public:
     72 #ifndef QT_NO_SYSTEMTRAYICON
     73     OwnPtr<QSystemTrayIcon> m_notificationIcon;
     74 #endif
     75 
     76     OwnPtr<QWebNotificationPresenter> m_presenter;
     77     Timer<NotificationWrapper> m_closeTimer;
     78 };
     79 
     80 #if ENABLE(NOTIFICATIONS)
     81 
     82 typedef QHash <Notification*, NotificationWrapper*> NotificationsQueue;
     83 
     84 class NotificationPresenterClientQt : public NotificationPresenter {
     85 public:
     86     NotificationPresenterClientQt();
     87     ~NotificationPresenterClientQt();
     88 
     89     /* WebCore::NotificationPresenter interface */
     90     virtual bool show(Notification*);
     91     virtual void cancel(Notification*);
     92     virtual void notificationObjectDestroyed(Notification*);
     93     virtual void requestPermission(ScriptExecutionContext*, PassRefPtr<VoidCallback>);
     94     virtual NotificationPresenter::Permission checkPermission(ScriptExecutionContext*);
     95     virtual void cancelRequestsForPermission(ScriptExecutionContext*);
     96 
     97     void cancel(NotificationWrapper*);
     98 
     99     void allowNotificationForFrame(Frame*);
    100 
    101     static bool dumpNotification;
    102 
    103     void addClient() { m_clientCount++; }
    104     void removeClient();
    105     static NotificationPresenterClientQt* notificationPresenter();
    106 
    107     Notification* notificationForWrapper(const NotificationWrapper*) const;
    108     void notificationClicked(NotificationWrapper*);
    109     void notificationClicked(const QString& title);
    110 
    111 private:
    112     void sendEvent(Notification*, const AtomicString& eventName);
    113     void displayNotification(Notification*, const QByteArray&);
    114     void removeReplacedNotificationFromQueue(Notification*);
    115     void detachNotification(Notification*);
    116     void dumpReplacedIdText(Notification*);
    117     void dumpShowText(Notification*);
    118     QWebPage* toPage(ScriptExecutionContext*);
    119     QWebFrame* toFrame(ScriptExecutionContext*);
    120 
    121     int m_clientCount;
    122     struct CallbacksInfo {
    123         QWebFrame* m_frame;
    124         QList<RefPtr<VoidCallback> > m_callbacks;
    125     };
    126     QHash<ScriptExecutionContext*,  CallbacksInfo > m_pendingPermissionRequests;
    127     QHash<ScriptExecutionContext*, NotificationPresenter::Permission> m_cachedPermissions;
    128 
    129     NotificationsQueue m_notifications;
    130     QtPlatformPlugin m_platformPlugin;
    131 };
    132 
    133 #endif
    134 
    135 }
    136 
    137 #endif
    138