NotificationPresenterClientQt.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef NotificationPresenterClientQt_h
  32. #define NotificationPresenterClientQt_h
  33. #include "Notification.h"
  34. #include "NotificationClient.h"
  35. #include "QtPlatformPlugin.h"
  36. #include "Timer.h"
  37. #include "qwebkitplatformplugin.h"
  38. #include <QMultiHash>
  39. #include <QScopedPointer>
  40. class QWebFrameAdapter;
  41. class QWebPageAdapter;
  42. namespace WebCore {
  43. class Document;
  44. class Frame;
  45. class ScriptExecutionContext;
  46. class NotificationWrapper : public QObject, public QWebNotificationData {
  47. Q_OBJECT
  48. public:
  49. NotificationWrapper();
  50. ~NotificationWrapper() { }
  51. void close();
  52. void close(Timer<NotificationWrapper>*);
  53. void sendDisplayEvent(Timer<NotificationWrapper>*);
  54. const QString title() const;
  55. const QString message() const;
  56. const QUrl iconUrl() const;
  57. const QUrl openerPageUrl() const;
  58. public Q_SLOTS:
  59. void notificationClosed();
  60. void notificationClicked();
  61. private:
  62. OwnPtr<QWebNotificationPresenter> m_presenter;
  63. Timer<NotificationWrapper> m_closeTimer;
  64. Timer<NotificationWrapper> m_displayEventTimer;
  65. friend class NotificationPresenterClientQt;
  66. };
  67. #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
  68. typedef QHash <Notification*, NotificationWrapper*> NotificationsQueue;
  69. class NotificationPresenterClientQt : public NotificationClient {
  70. public:
  71. NotificationPresenterClientQt();
  72. ~NotificationPresenterClientQt();
  73. /* WebCore::NotificationClient interface */
  74. virtual bool show(Notification*);
  75. virtual void cancel(Notification*);
  76. virtual void notificationObjectDestroyed(Notification*);
  77. virtual void notificationControllerDestroyed();
  78. #if ENABLE(LEGACY_NOTIFICATIONS)
  79. virtual void requestPermission(ScriptExecutionContext*, PassRefPtr<VoidCallback>);
  80. #endif
  81. #if ENABLE(NOTIFICATIONS)
  82. virtual void requestPermission(ScriptExecutionContext*, PassRefPtr<NotificationPermissionCallback>);
  83. #endif
  84. virtual NotificationClient::Permission checkPermission(ScriptExecutionContext*);
  85. virtual void cancelRequestsForPermission(ScriptExecutionContext*);
  86. void cancel(NotificationWrapper*);
  87. void setNotificationsAllowedForFrame(Frame*, bool allowed);
  88. static bool dumpNotification;
  89. void addClient() { m_clientCount++; }
  90. #ifndef QT_NO_SYSTEMTRAYICON
  91. bool hasSystemTrayIcon() const { return !m_systemTrayIcon.isNull(); }
  92. void setSystemTrayIcon(QObject* icon) { m_systemTrayIcon.reset(icon); }
  93. #endif
  94. void removeClient();
  95. static NotificationPresenterClientQt* notificationPresenter();
  96. Notification* notificationForWrapper(const NotificationWrapper*) const;
  97. void notificationClicked(NotificationWrapper*);
  98. void notificationClicked(const QString& title);
  99. void sendDisplayEvent(NotificationWrapper*);
  100. void clearCachedPermissions();
  101. private:
  102. void sendEvent(Notification*, const AtomicString& eventName);
  103. void displayNotification(Notification*);
  104. void removeReplacedNotificationFromQueue(Notification*);
  105. void detachNotification(Notification*);
  106. void dumpReplacedIdText(Notification*);
  107. void dumpShowText(Notification*);
  108. QWebPageAdapter* toPage(ScriptExecutionContext*);
  109. QWebFrameAdapter* toFrame(ScriptExecutionContext*);
  110. int m_clientCount;
  111. struct CallbacksInfo {
  112. QWebFrameAdapter* m_frame;
  113. #if ENABLE(LEGACY_NOTIFICATIONS)
  114. QList<RefPtr<VoidCallback> > m_voidCallbacks;
  115. #endif
  116. #if ENABLE(NOTIFICATIONS)
  117. QList<RefPtr<NotificationPermissionCallback> > m_callbacks;
  118. #endif
  119. };
  120. QHash<ScriptExecutionContext*, CallbacksInfo > m_pendingPermissionRequests;
  121. QHash<ScriptExecutionContext*, NotificationClient::Permission> m_cachedPermissions;
  122. NotificationsQueue m_notifications;
  123. QtPlatformPlugin m_platformPlugin;
  124. #ifndef QT_NO_SYSTEMTRAYICON
  125. QScopedPointer<QObject> m_systemTrayIcon;
  126. #endif
  127. };
  128. #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
  129. }
  130. #endif // NotificationPresenterClientQt_h