Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKitLegacy / win / WebCoreSupport / WebDesktopNotificationsDelegate.cpp
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
31 #include "WebDesktopNotificationsDelegate.h"
32 #include "WebSecurityOrigin.h"
33 #include "WebView.h"
34 #include <WebCore/BString.h>
35 #include <WebCore/Document.h>
36 #include <wtf/URL.h>
37
38 #if ENABLE(NOTIFICATIONS)
39
40 using namespace WebCore;
41
42 class NotificationCOMWrapper : public IWebDesktopNotification {
43 public:
44     static NotificationCOMWrapper* create(Notification* inner) { return new NotificationCOMWrapper(inner); }
45    
46     // IUnknown
47     virtual HRESULT STDMETHODCALLTYPE QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject);
48     virtual ULONG STDMETHODCALLTYPE AddRef();
49     virtual ULONG STDMETHODCALLTYPE Release();
50
51     // IWebDesktopNotification
52     HRESULT STDMETHODCALLTYPE isHTML(BOOL* result);
53     HRESULT STDMETHODCALLTYPE contentsURL(BSTR* result);
54     HRESULT STDMETHODCALLTYPE iconURL(BSTR* result);
55     HRESULT STDMETHODCALLTYPE title(BSTR* result);
56     HRESULT STDMETHODCALLTYPE text(BSTR* result);
57     HRESULT STDMETHODCALLTYPE notifyDisplay();
58     HRESULT STDMETHODCALLTYPE notifyError();
59     HRESULT STDMETHODCALLTYPE notifyClose(BOOL xplicit);
60
61 private:
62     NotificationCOMWrapper(Notification* inner) : m_refCount(1), m_inner(inner) {}
63
64     int m_refCount;
65     Notification* m_inner;
66 };
67
68 HRESULT NotificationCOMWrapper::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
69 {
70     if (!ppvObject)
71         return E_POINTER;
72     *ppvObject = nullptr;
73     if (IsEqualGUID(riid, IID_IUnknown))
74         *ppvObject = static_cast<NotificationCOMWrapper*>(this);
75     else if (IsEqualGUID(riid, IID_IWebDesktopNotification))
76         *ppvObject = static_cast<NotificationCOMWrapper*>(this);
77     else
78         return E_NOINTERFACE;
79
80     AddRef();
81     return S_OK;
82 }
83
84 ULONG NotificationCOMWrapper::AddRef()
85
86     return ++m_refCount; 
87 }
88
89 ULONG NotificationCOMWrapper::Release()
90 {
91     ULONG newRef = --m_refCount;
92     if (!newRef)
93         delete(this);
94     return newRef;
95 }
96
97 HRESULT NotificationCOMWrapper::isHTML(BOOL* result)
98 {
99     *result = m_inner->isHTML();
100     return S_OK;
101 }
102
103 HRESULT NotificationCOMWrapper::contentsURL(BSTR* result)
104 {
105     *result = BString(m_inner->url()).release();
106     return S_OK;
107 }
108
109 HRESULT NotificationCOMWrapper::iconURL(BSTR* result)
110 {
111     *result = BString(m_inner->contents().icon()).release();
112     return S_OK;
113 }
114
115 HRESULT NotificationCOMWrapper::title(BSTR* result)
116 {
117     *result = BString(m_inner->contents().title()).release();
118     return S_OK;
119 }
120
121 HRESULT NotificationCOMWrapper::text(BSTR* result)
122 {
123     *result = BString(m_inner->contents().body()).release();
124     return S_OK;
125 }
126
127 HRESULT NotificationCOMWrapper::notifyDisplay()
128 {
129     m_inner->dispatchDisplayEvent();
130     return S_OK;
131 }
132
133 HRESULT NotificationCOMWrapper::notifyError()
134 {
135     m_inner->dispatchErrorEvent();
136     return S_OK;
137 }
138
139 HRESULT NotificationCOMWrapper::notifyClose(BOOL xplicit)
140 {
141     m_inner->dispatchCloseEvent();
142     return S_OK;
143 }
144
145 WebDesktopNotificationsDelegate::WebDesktopNotificationsDelegate(WebView* webView)
146     : m_webView(webView)
147 {
148 }
149
150 bool WebDesktopNotificationsDelegate::show(Notification* object)
151 {
152     if (hasNotificationDelegate())
153         notificationDelegate()->showDesktopNotification(NotificationCOMWrapper::create(object));
154     return true;
155 }
156
157 void WebDesktopNotificationsDelegate::cancel(Notification* object)
158 {
159     if (hasNotificationDelegate())
160         notificationDelegate()->cancelDesktopNotification(NotificationCOMWrapper::create(object));
161 }
162
163 void WebDesktopNotificationsDelegate::notificationObjectDestroyed(Notification* object)
164 {
165     if (hasNotificationDelegate())
166         notificationDelegate()->notificationDestroyed(NotificationCOMWrapper::create(object));
167 }
168
169 void WebDesktopNotificationsDelegate::notificationControllerDestroyed()
170 {
171 }
172
173 void WebDesktopNotificationsDelegate::requestPermission(SecurityOrigin* origin, RefPtr<VoidCallback>&&)
174 {
175     BString org(origin->toString());
176     if (hasNotificationDelegate())
177         notificationDelegate()->requestNotificationPermission(org);
178 }
179
180 void WebDesktopNotificationsDelegate::requestPermission(SecurityOrigin*, RefPtr<NotificationPermissionCallback>&&)
181 {
182 }
183
184 void WebDesktopNotificationsDelegate::cancelRequestsForPermission(ScriptExecutionContext* context)
185 {
186 }
187
188 bool hasPendingPermissionRequests(ScriptExecutionContext*) const
189 {
190     // We can safely return false here because our implementation for requestPermission() never calls
191     // the completion callback.
192     return false;
193 }
194
195 NotificationClient::Permission WebDesktopNotificationsDelegate::checkPermission(const URL& url)
196 {
197     int out = 0;
198     BString org(SecurityOrigin::create(url)->toString());
199     if (hasNotificationDelegate())
200         notificationDelegate()->checkNotificationPermission(org, &out);
201     return (NotificationClient::Permission) out;
202 }
203
204 bool WebDesktopNotificationsDelegate::hasNotificationDelegate()
205 {
206     COMPtr<IWebUIDelegate> ui;
207     m_webView->uiDelegate(&ui);
208
209     COMPtr<IWebUIDelegate2> ui2;
210     return SUCCEEDED(ui->QueryInterface(IID_IWebUIDelegate2, (void**) &ui2));
211 }
212
213 COMPtr<IWebDesktopNotificationsDelegate> WebDesktopNotificationsDelegate::notificationDelegate()
214 {
215     COMPtr<IWebUIDelegate> ui;
216     m_webView->uiDelegate(&ui);
217
218     COMPtr<IWebUIDelegate2> ui2;
219     COMPtr<IWebDesktopNotificationsDelegate> delegate;
220     if (SUCCEEDED(ui->QueryInterface(IID_IWebUIDelegate2, (void**) &ui2)))
221         ui2->desktopNotificationsDelegate(&delegate);
222
223     return delegate;
224 }
225
226 #endif // ENABLE(NOTIFICATIONS)