1/*
2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20
21#if ENABLE(NOTIFICATIONS)
22
23#include "NotificationPresenterImpl.h"
24
25#include "Event.h"
26#include "Notification.h"
27#include "NotificationPresenterBlackBerry.h"
28#include "ScriptExecutionContext.h"
29#include "StringHash.h"
30#include "UUID.h"
31#include <wtf/text/CString.h>
32
33using namespace WebCore;
34
35namespace WebKit {
36
37NotificationPresenterImpl* NotificationPresenterImpl::m_instance = 0;
38
39NotificationPresenter* NotificationPresenterImpl::instance()
40{
41 if (!m_instance)
42 m_instance = new NotificationPresenterImpl();
43 return m_instance;
44}
45
46NotificationPresenterImpl::NotificationPresenterImpl()
47{
48 m_platformPresenter = adoptPtr(BlackBerry::Platform::NotificationPresenterBlackBerry::create(this));
49}
50
51NotificationPresenterImpl::~NotificationPresenterImpl()
52{
53}
54
55
56bool NotificationPresenterImpl::show(Notification* n)
57{
58 if (NotificationPresenter::PermissionAllowed != checkPermission(n->scriptExecutionContext()))
59 return false;
60
61 RefPtr<Notification> notification = n;
62 if (m_notifications.contains(notification))
63 return false;
64
65 String uuid = createCanonicalUUIDString();
66 m_notifications.add(notification, uuid);
67 String message;
68 if (notification->isHTML()) {
69 // FIXME: load and display HTML content
70 message = notification->url().string();
71 } else {
72 // FIXME: strip to one line
73 message = notification->contents().title() + ": " + notification->contents().body();
74 }
75
76 m_platformPresenter->show(std::string(uuid.utf8().data()), std::string(message.utf8().data()));
77 return true;
78}
79
80void NotificationPresenterImpl::cancel(Notification* n)
81{
82 RefPtr<Notification> notification = n;
83 if (m_notifications.contains(notification)) {
84 m_platformPresenter->cancel(std::string(m_notifications.get(notification).utf8().data()));
85 m_notifications.remove(notification);
86 }
87}
88
89void NotificationPresenterImpl::notificationObjectDestroyed(Notification* notification)
90{
91 cancel(notification);
92}
93
94void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, PassRefPtr<VoidCallback> callback)
95{
96 m_permissionRequests.add(context, callback);
97 m_platformPresenter->requestPermission(std::string(context->url().host().utf8().data()));
98}
99
100void NotificationPresenterImpl::onPermission(const std::string& domainStr, bool isAllowed)
101{
102 String domain = String::fromUTF8(domainStr.c_str());
103 PermissionRequestMap::iterator iter = m_permissionRequests.begin();
104 for (; iter != m_permissionRequests.end(); ++iter) {
105 if (iter->first->url().host() == domain) {
106 if (isAllowed) {
107 m_allowedDomains.add(domain);
108 iter->second->handleEvent();
109 } else
110 m_allowedDomains.remove(domain);
111
112 m_permissionRequests.remove(iter);
113 break;
114 }
115 }
116}
117
118void NotificationPresenterImpl::cancelRequestsForPermission(ScriptExecutionContext*)
119{
120 // Because we are using a modal dialog to request permission, it's probably impossible to cancel it.
121}
122
123NotificationPresenter::Permission NotificationPresenterImpl::checkPermission(ScriptExecutionContext* context)
124{
125 // FIXME: Should store the permission information into file permenently instead of in m_allowedDomains.
126 // The suggested place to do this is in m_platformPresenter->checkPermission().
127
128 if (m_allowedDomains.contains(context->url().host()))
129 return NotificationPresenter::PermissionAllowed;
130 return NotificationPresenter::PermissionNotAllowed;
131
132}
133
134// This function will be called by platform side.
135void NotificationPresenterImpl::notificationClicked(const std::string& idStr)
136{
137 String id = String::fromUTF8(idStr.c_str());
138 NotificationMap::iterator iter = m_notifications.begin();
139 for (; iter != m_notifications.end(); ++iter)
140 if (iter->second == id && iter->first->scriptExecutionContext()) {
141 iter->first->dispatchEvent(Event::create(eventNames().clickEvent, false, true));
142 m_notifications.remove(iter);
143 }
144}
145
146} // namespace WebKit
147
148#endif // ENABLE(NOTIFICATIONS)