DRTDesktopNotificationPresenter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "config.h"
  31. #include "DRTDesktopNotificationPresenter.h"
  32. #include "DumpRenderTree.h"
  33. #include "TestRunner.h"
  34. #include <JavaScriptCore/JSStringRef.h>
  35. #include <JavaScriptCore/JSStringRefBSTR.h>
  36. #include <WebCore/NotificationClient.h>
  37. DRTDesktopNotificationPresenter::DRTDesktopNotificationPresenter()
  38. : m_refCount(1) {}
  39. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::QueryInterface(REFIID riid, void** ppvObject)
  40. {
  41. *ppvObject = 0;
  42. if (IsEqualGUID(riid, IID_IUnknown))
  43. *ppvObject = static_cast<DRTDesktopNotificationPresenter*>(this);
  44. else if (IsEqualGUID(riid, IID_IWebDesktopNotificationsDelegate))
  45. *ppvObject = static_cast<DRTDesktopNotificationPresenter*>(this);
  46. else
  47. return E_NOINTERFACE;
  48. AddRef();
  49. return S_OK;
  50. }
  51. ULONG STDMETHODCALLTYPE DRTDesktopNotificationPresenter::AddRef()
  52. {
  53. return ++m_refCount;
  54. }
  55. ULONG STDMETHODCALLTYPE DRTDesktopNotificationPresenter::Release()
  56. {
  57. ULONG newRef = --m_refCount;
  58. if (!newRef)
  59. delete(this);
  60. return newRef;
  61. }
  62. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::showDesktopNotification(
  63. /* [in] */ IWebDesktopNotification* notification)
  64. {
  65. BSTR title, text, url;
  66. BOOL html;
  67. if (!notification->isHTML(&html) && html) {
  68. notification->contentsURL(&url);
  69. printf("DESKTOP NOTIFICATION: contents at %S\n", url ? url : L"");
  70. } else {
  71. notification->iconURL(&url);
  72. notification->title(&title);
  73. notification->text(&text);
  74. printf("DESKTOP NOTIFICATION: icon %S, title %S, text %S\n",
  75. url ? url : L"",
  76. title ? title : L"",
  77. text ? text : L"");
  78. }
  79. // In this stub implementation, the notification is displayed immediately;
  80. // we dispatch the display event to mimic that.
  81. notification->notifyDisplay();
  82. return S_OK;
  83. }
  84. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::cancelDesktopNotification(
  85. /* [in] */ IWebDesktopNotification* notification)
  86. {
  87. BSTR identifier;
  88. BOOL html;
  89. notification->isHTML(&html);
  90. if (html)
  91. notification->contentsURL(&identifier);
  92. else
  93. notification->title(&identifier);
  94. printf("DESKTOP NOTIFICATION CLOSED: %S\n", identifier ? identifier : L"");
  95. notification->notifyClose(false);
  96. return S_OK;
  97. }
  98. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::notificationDestroyed(
  99. /* [in] */ IWebDesktopNotification* notification)
  100. {
  101. // Since in these tests events happen immediately, we don't hold on to
  102. // Notification pointers. So there's no cleanup to do.
  103. return S_OK;
  104. }
  105. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::checkNotificationPermission(
  106. /* [in] */ BSTR origin,
  107. /* [out, retval] */ int* result)
  108. {
  109. #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
  110. JSStringRef jsOrigin = JSStringCreateWithBSTR(origin);
  111. bool allowed = ::gTestRunner->checkDesktopNotificationPermission(jsOrigin);
  112. if (allowed)
  113. *result = WebCore::NotificationClient::PermissionAllowed;
  114. else
  115. *result = WebCore::NotificationClient::PermissionDenied;
  116. JSStringRelease(jsOrigin);
  117. #endif
  118. return S_OK;
  119. }
  120. HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::requestNotificationPermission(
  121. /* [in] */ BSTR origin)
  122. {
  123. printf("DESKTOP NOTIFICATION PERMISSION REQUESTED: %S\n", origin ? origin : L"");
  124. return S_OK;
  125. }