• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "WebNotification.h"
35 #include "WebNotificationPermissionCallback.h"
36 #include "WebNotificationPresenter.h"
37 #include "core/dom/ExecutionContext.h"
38 #include "modules/notifications/NotificationBase.h"
39 #include "platform/weborigin/SecurityOrigin.h"
40 
41 using namespace WebCore;
42 
43 namespace blink {
44 
45 #if ENABLE(LEGACY_NOTIFICATIONS)
46 class VoidCallbackClient : public WebNotificationPermissionCallback {
47 public:
VoidCallbackClient(PassOwnPtr<VoidCallback> callback)48     explicit VoidCallbackClient(PassOwnPtr<VoidCallback> callback)
49         : m_callback(callback)
50     {
51     }
52 
permissionRequestComplete()53     virtual void permissionRequestComplete()
54     {
55         if (m_callback)
56             m_callback->handleEvent();
57         delete this;
58     }
59 
60 private:
~VoidCallbackClient()61     virtual ~VoidCallbackClient() { }
62 
63     OwnPtr<VoidCallback> m_callback;
64 };
65 #endif // ENABLE(LEGACY_NOTIFICATIONS)
66 
67 class NotificationPermissionCallbackClient : public WebNotificationPermissionCallback {
68 public:
NotificationPermissionCallbackClient(WebNotificationPresenter * presenter,PassRefPtr<SecurityOrigin> securityOrigin,PassOwnPtr<NotificationPermissionCallback> callback)69     NotificationPermissionCallbackClient(WebNotificationPresenter* presenter, PassRefPtr<SecurityOrigin> securityOrigin, PassOwnPtr<NotificationPermissionCallback> callback)
70         : m_presenter(presenter)
71         , m_securityOrigin(securityOrigin)
72         , m_callback(callback)
73     {
74     }
75 
permissionRequestComplete()76     virtual void permissionRequestComplete()
77     {
78         if (m_callback)
79             m_callback->handleEvent(NotificationBase::permissionString(static_cast<NotificationClient::Permission>(m_presenter->checkPermission(WebSecurityOrigin(m_securityOrigin)))));
80         delete this;
81     }
82 
83 private:
~NotificationPermissionCallbackClient()84     virtual ~NotificationPermissionCallbackClient() { }
85 
86     WebNotificationPresenter* m_presenter;
87     RefPtr<SecurityOrigin> m_securityOrigin;
88     OwnPtr<NotificationPermissionCallback> m_callback;
89 };
90 
initialize(WebNotificationPresenter * presenter)91 void NotificationPresenterImpl::initialize(WebNotificationPresenter* presenter)
92 {
93     m_presenter = presenter;
94 }
95 
isInitialized()96 bool NotificationPresenterImpl::isInitialized()
97 {
98     return !!m_presenter;
99 }
100 
show(NotificationBase * notification)101 bool NotificationPresenterImpl::show(NotificationBase* notification)
102 {
103     return m_presenter->show(PassRefPtr<NotificationBase>(notification));
104 }
105 
cancel(NotificationBase * notification)106 void NotificationPresenterImpl::cancel(NotificationBase* notification)
107 {
108     m_presenter->cancel(PassRefPtr<NotificationBase>(notification));
109 }
110 
notificationObjectDestroyed(NotificationBase * notification)111 void NotificationPresenterImpl::notificationObjectDestroyed(NotificationBase* notification)
112 {
113     m_presenter->objectDestroyed(PassRefPtr<NotificationBase>(notification));
114 }
115 
checkPermission(ExecutionContext * context)116 NotificationClient::Permission NotificationPresenterImpl::checkPermission(ExecutionContext* context)
117 {
118     int result = m_presenter->checkPermission(WebSecurityOrigin(context->securityOrigin()));
119     return static_cast<NotificationClient::Permission>(result);
120 }
121 
122 #if ENABLE(LEGACY_NOTIFICATIONS)
requestPermission(ExecutionContext * context,PassOwnPtr<VoidCallback> callback)123 void NotificationPresenterImpl::requestPermission(ExecutionContext* context, PassOwnPtr<VoidCallback> callback)
124 {
125     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new VoidCallbackClient(callback));
126 }
127 #endif // ENABLE(LEGACY_NOTIFICATIONS)
128 
requestPermission(ExecutionContext * context,WTF::PassOwnPtr<NotificationPermissionCallback> callback)129 void NotificationPresenterImpl::requestPermission(ExecutionContext* context, WTF::PassOwnPtr<NotificationPermissionCallback> callback)
130 {
131     m_presenter->requestPermission(WebSecurityOrigin(context->securityOrigin()), new NotificationPermissionCallbackClient(m_presenter, context->securityOrigin(), callback));
132 }
133 
134 } // namespace blink
135