WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / src / NotificationPresenterImpl.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 "config.h"
32 #include "NotificationPresenterImpl.h"
33
34 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
35
36 #include "KURL.h"
37 #include "Notification.h"
38 #include "ScriptExecutionContext.h"
39 #include "SecurityOrigin.h"
40
41 #include "WebNotification.h"
42 #include "WebNotificationPermissionCallback.h"
43 #include "WebNotificationPresenter.h"
44 #include <public/WebURL.h>
45
46 #include <wtf/PassRefPtr.h>
47
48 using namespace WebCore;
49
50 namespace WebKit {
51
52 #if ENABLE(LEGACY_NOTIFICATIONS)
53 class VoidCallbackClient : public WebNotificationPermissionCallback {
54 public:
55     explicit VoidCallbackClient(PassRefPtr<VoidCallback> callback)
56         : m_callback(callback)
57     {
58     }
59
60     virtual void permissionRequestComplete()
61     {
62         if (m_callback)
63             m_callback->handleEvent();
64         delete this;
65     }
66
67 private:
68     virtual ~VoidCallbackClient() { }
69
70     RefPtr<VoidCallback> m_callback;
71 };
72 #endif // ENABLE(LEGACY_NOTIFICATIONS)
73
74 #if ENABLE(NOTIFICATIONS)
75 class NotificationPermissionCallbackClient : public WebNotificationPermissionCallback {
76 public:
77     NotificationPermissionCallbackClient(WebNotificationPresenter* presenter, PassRefPtr<SecurityOrigin> securityOrigin, PassRefPtr<NotificationPermissionCallback> callback)
78         : m_presenter(presenter)
79         , m_securityOrigin(securityOrigin)
80         , m_callback(callback)
81     {
82     }
83
84     virtual void permissionRequestComplete()
85     {
86         if (m_callback)
87             m_callback->handleEvent(Notification::permissionString(static_cast<NotificationClient::Permission>(m_presenter->checkPermission(WebSecurityOrigin(m_securityOrigin)))));
88         delete this;
89     }
90
91 private:
92     virtual ~NotificationPermissionCallbackClient() { }
93
94     WebNotificationPresenter* m_presenter;
95     RefPtr<SecurityOrigin> m_securityOrigin;
96     RefPtr<NotificationPermissionCallback> m_callback;
97 };
98 #endif // ENABLE(NOTIFICATIONS)
99
100 void NotificationPresenterImpl::initialize(WebNotificationPresenter* presenter)
101 {
102     m_presenter = presenter;
103 }
104
105 bool NotificationPresenterImpl::isInitialized()
106 {
107     return !!m_presenter;
108 }
109
110 bool NotificationPresenterImpl::show(Notification* notification)
111 {
112     return m_presenter->show(PassRefPtr<Notification>(notification));
113 }
114
115 void NotificationPresenterImpl::cancel(Notification* notification)
116 {
117     m_presenter->cancel(PassRefPtr<Notification>(notification));
118 }
119
120 void NotificationPresenterImpl::notificationObjectDestroyed(Notification* notification)
121 {
122     m_presenter->objectDestroyed(PassRefPtr<Notification>(notification));
123 }
124
125 void NotificationPresenterImpl::notificationControllerDestroyed()
126 {
127 }
128
129 NotificationClient::Permission NotificationPresenterImpl::checkPermission(ScriptExecutionContext* context)
130 {
131     int result = m_presenter->checkPermission(WebSecurityOrigin(context->securityOrigin()));
132     return static_cast<NotificationClient::Permission>(result);
133 }
134
135 #if ENABLE(LEGACY_NOTIFICATIONS)
136 void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, PassRefPtr<VoidCallback> callback)
137 {
138     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new VoidCallbackClient(callback));
139 }
140 #endif // ENABLE(LEGACY_NOTIFICATIONS)
141
142 #if ENABLE(NOTIFICATIONS)
143 void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, WTF::PassRefPtr<NotificationPermissionCallback> callback)
144 {
145     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new NotificationPermissionCallbackClient(m_presenter, context->securityOrigin(), callback));
146 }
147 #endif // ENABLE(NOTIFICATIONS)
148
149 } // namespace WebKit
150
151 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)