WebNotificationProvider.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2012 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebNotificationProvider.h"
  27. #include <WebKit2/WKMutableArray.h>
  28. #include <WebKit2/WKNotification.h>
  29. #include <WebKit2/WKNumber.h>
  30. #include <WebKit2/WKSecurityOrigin.h>
  31. #include <wtf/Assertions.h>
  32. namespace WTR {
  33. static void showWebNotification(WKPageRef page, WKNotificationRef notification, const void* clientInfo)
  34. {
  35. static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->showWebNotification(page, notification);
  36. }
  37. static void closeWebNotification(WKNotificationRef notification, const void* clientInfo)
  38. {
  39. static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->closeWebNotification(notification);
  40. }
  41. static void addNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
  42. {
  43. static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->addNotificationManager(manager);
  44. }
  45. static void removeNotificationManager(WKNotificationManagerRef manager, const void* clientInfo)
  46. {
  47. static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->removeNotificationManager(manager);
  48. }
  49. static WKDictionaryRef notificationPermissions(const void* clientInfo)
  50. {
  51. return static_cast<WebNotificationProvider*>(const_cast<void*>(clientInfo))->notificationPermissions();
  52. }
  53. WebNotificationProvider::WebNotificationProvider()
  54. {
  55. }
  56. WebNotificationProvider::~WebNotificationProvider()
  57. {
  58. WKNotificationManagerSetProvider(m_notificationManager.get(), 0);
  59. }
  60. WKNotificationProvider WebNotificationProvider::provider()
  61. {
  62. WKNotificationProvider notificationProvider = {
  63. kWKNotificationProviderCurrentVersion,
  64. this,
  65. WTR::showWebNotification,
  66. WTR::closeWebNotification,
  67. 0, // didDestroyNotification
  68. WTR::addNotificationManager,
  69. WTR::removeNotificationManager,
  70. WTR::notificationPermissions,
  71. 0, // clearNotifications
  72. };
  73. return notificationProvider;
  74. }
  75. void WebNotificationProvider::showWebNotification(WKPageRef, WKNotificationRef notification)
  76. {
  77. if (!m_notificationManager)
  78. return;
  79. uint64_t id = WKNotificationGetID(notification);
  80. ASSERT(!m_shownNotifications.contains(id));
  81. m_shownNotifications.add(id);
  82. WKNotificationManagerProviderDidShowNotification(m_notificationManager.get(), WKNotificationGetID(notification));
  83. }
  84. void WebNotificationProvider::closeWebNotification(WKNotificationRef notification)
  85. {
  86. if (!m_notificationManager)
  87. return;
  88. uint64_t id = WKNotificationGetID(notification);
  89. WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(id);
  90. WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
  91. WKArrayAppendItem(array.get(), wkID.get());
  92. m_shownNotifications.remove(id);
  93. WKNotificationManagerProviderDidCloseNotifications(m_notificationManager.get(), array.get());
  94. }
  95. void WebNotificationProvider::addNotificationManager(WKNotificationManagerRef manager)
  96. {
  97. // We assume there is only one for testing.
  98. ASSERT(!m_notificationManager);
  99. m_notificationManager = manager;
  100. }
  101. void WebNotificationProvider::removeNotificationManager(WKNotificationManagerRef manager)
  102. {
  103. // We assume there is only one for testing.
  104. ASSERT(m_notificationManager);
  105. ASSERT(m_notificationManager.get() == manager);
  106. m_notificationManager = 0;
  107. }
  108. WKDictionaryRef WebNotificationProvider::notificationPermissions()
  109. {
  110. // Initial permissions are always empty.
  111. return WKMutableDictionaryCreate();
  112. }
  113. void WebNotificationProvider::simulateWebNotificationClick(uint64_t notificationID)
  114. {
  115. if (!m_notificationManager)
  116. return;
  117. ASSERT(m_shownNotifications.contains(notificationID));
  118. WKNotificationManagerProviderDidClickNotification(m_notificationManager.get(), notificationID);
  119. }
  120. void WebNotificationProvider::reset()
  121. {
  122. if (!m_notificationManager) {
  123. m_shownNotifications.clear();
  124. return;
  125. }
  126. WKRetainPtr<WKMutableArrayRef> array(AdoptWK, WKMutableArrayCreate());
  127. HashSet<uint64_t>::const_iterator itEnd = m_shownNotifications.end();
  128. for (HashSet<uint64_t>::const_iterator it = m_shownNotifications.begin(); it != itEnd; ++it) {
  129. WKRetainPtr<WKUInt64Ref> wkID = WKUInt64Create(*it);
  130. WKArrayAppendItem(array.get(), wkID.get());
  131. }
  132. m_shownNotifications.clear();
  133. WKNotificationManagerProviderDidCloseNotifications(m_notificationManager.get(), array.get());
  134. }
  135. } // namespace WTR