WebDesktopNotificationsDelegate.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "WebDesktopNotificationsDelegate.h"
  32. #include "WebSecurityOrigin.h"
  33. #include "WebView.h"
  34. #include <WebCore/BString.h>
  35. #include <WebCore/Document.h>
  36. #include <WebCore/KURL.h>
  37. #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
  38. using namespace WebCore;
  39. class NotificationCOMWrapper : public IWebDesktopNotification {
  40. public:
  41. static NotificationCOMWrapper* create(Notification* inner) { return new NotificationCOMWrapper(inner); }
  42. // IUnknown
  43. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
  44. virtual ULONG STDMETHODCALLTYPE AddRef();
  45. virtual ULONG STDMETHODCALLTYPE Release();
  46. // IWebDesktopNotification
  47. HRESULT STDMETHODCALLTYPE isHTML(BOOL* result);
  48. HRESULT STDMETHODCALLTYPE contentsURL(BSTR* result);
  49. HRESULT STDMETHODCALLTYPE iconURL(BSTR* result);
  50. HRESULT STDMETHODCALLTYPE title(BSTR* result);
  51. HRESULT STDMETHODCALLTYPE text(BSTR* result);
  52. HRESULT STDMETHODCALLTYPE notifyDisplay();
  53. HRESULT STDMETHODCALLTYPE notifyError();
  54. HRESULT STDMETHODCALLTYPE notifyClose(BOOL xplicit);
  55. private:
  56. NotificationCOMWrapper(Notification* inner) : m_refCount(1), m_inner(inner) {}
  57. int m_refCount;
  58. Notification* m_inner;
  59. };
  60. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::QueryInterface(REFIID riid, void** ppvObject)
  61. {
  62. *ppvObject = 0;
  63. if (IsEqualGUID(riid, IID_IUnknown))
  64. *ppvObject = static_cast<NotificationCOMWrapper*>(this);
  65. else if (IsEqualGUID(riid, IID_IWebDesktopNotification))
  66. *ppvObject = static_cast<NotificationCOMWrapper*>(this);
  67. else
  68. return E_NOINTERFACE;
  69. AddRef();
  70. return S_OK;
  71. }
  72. ULONG STDMETHODCALLTYPE NotificationCOMWrapper::AddRef()
  73. {
  74. return ++m_refCount;
  75. }
  76. ULONG STDMETHODCALLTYPE NotificationCOMWrapper::Release()
  77. {
  78. ULONG newRef = --m_refCount;
  79. if (!newRef)
  80. delete(this);
  81. return newRef;
  82. }
  83. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::isHTML(BOOL* result)
  84. {
  85. *result = m_inner->isHTML();
  86. return S_OK;
  87. }
  88. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::contentsURL(BSTR* result)
  89. {
  90. *result = BString(m_inner->url()).release();
  91. return S_OK;
  92. }
  93. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::iconURL(BSTR* result)
  94. {
  95. *result = BString(m_inner->contents().icon()).release();
  96. return S_OK;
  97. }
  98. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::title(BSTR* result)
  99. {
  100. *result = BString(m_inner->contents().title()).release();
  101. return S_OK;
  102. }
  103. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::text(BSTR* result)
  104. {
  105. *result = BString(m_inner->contents().body()).release();
  106. return S_OK;
  107. }
  108. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::notifyDisplay()
  109. {
  110. m_inner->dispatchDisplayEvent();
  111. return S_OK;
  112. }
  113. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::notifyError()
  114. {
  115. m_inner->dispatchErrorEvent();
  116. return S_OK;
  117. }
  118. HRESULT STDMETHODCALLTYPE NotificationCOMWrapper::notifyClose(BOOL xplicit)
  119. {
  120. m_inner->dispatchCloseEvent();
  121. return S_OK;
  122. }
  123. WebDesktopNotificationsDelegate::WebDesktopNotificationsDelegate(WebView* webView)
  124. : m_webView(webView)
  125. {
  126. }
  127. bool WebDesktopNotificationsDelegate::show(Notification* object)
  128. {
  129. if (hasNotificationDelegate())
  130. notificationDelegate()->showDesktopNotification(NotificationCOMWrapper::create(object));
  131. return true;
  132. }
  133. void WebDesktopNotificationsDelegate::cancel(Notification* object)
  134. {
  135. if (hasNotificationDelegate())
  136. notificationDelegate()->cancelDesktopNotification(NotificationCOMWrapper::create(object));
  137. }
  138. void WebDesktopNotificationsDelegate::notificationObjectDestroyed(Notification* object)
  139. {
  140. if (hasNotificationDelegate())
  141. notificationDelegate()->notificationDestroyed(NotificationCOMWrapper::create(object));
  142. }
  143. void WebDesktopNotificationsDelegate::notificationControllerDestroyed()
  144. {
  145. }
  146. void WebDesktopNotificationsDelegate::requestPermission(SecurityOrigin* origin, PassRefPtr<VoidCallback> callback)
  147. {
  148. BString org(origin->toString());
  149. if (hasNotificationDelegate())
  150. notificationDelegate()->requestNotificationPermission(org);
  151. }
  152. void WebDesktopNotificationsDelegate::requestPermission(SecurityOrigin*, PassRefPtr<NotificationPermissionCallback>)
  153. {
  154. }
  155. void WebDesktopNotificationsDelegate::cancelRequestsForPermission(ScriptExecutionContext* context)
  156. {
  157. }
  158. NotificationClient::Permission WebDesktopNotificationsDelegate::checkPermission(const KURL& url)
  159. {
  160. int out = 0;
  161. BString org(SecurityOrigin::create(url)->toString());
  162. if (hasNotificationDelegate())
  163. notificationDelegate()->checkNotificationPermission(org, &out);
  164. return (NotificationClient::Permission) out;
  165. }
  166. bool WebDesktopNotificationsDelegate::hasNotificationDelegate()
  167. {
  168. COMPtr<IWebUIDelegate> ui;
  169. m_webView->uiDelegate(&ui);
  170. COMPtr<IWebUIDelegate2> ui2;
  171. return SUCCEEDED(ui->QueryInterface(IID_IWebUIDelegate2, (void**) &ui2));
  172. }
  173. COMPtr<IWebDesktopNotificationsDelegate> WebDesktopNotificationsDelegate::notificationDelegate()
  174. {
  175. COMPtr<IWebUIDelegate> ui;
  176. m_webView->uiDelegate(&ui);
  177. COMPtr<IWebUIDelegate2> ui2;
  178. COMPtr<IWebDesktopNotificationsDelegate> delegate;
  179. if (SUCCEEDED(ui->QueryInterface(IID_IWebUIDelegate2, (void**) &ui2)))
  180. ui2->desktopNotificationsDelegate(&delegate);
  181. return delegate;
  182. }
  183. #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)