summaryrefslogtreecommitdiff
path: root/Source/WebKit/blackberry/WebCoreSupport/NotificationPresenterImpl.cpp
blob: 16c451ec364f4cf45816899346f307a04a8824b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "NotificationPresenterImpl.h"

#if ENABLE(NOTIFICATIONS)
#include <Event.h>
#include <Notification.h>
#include <NotificationPresenterBlackBerry.h>
#include <ScriptExecutionContext.h>
#include <UUID.h>


namespace WebCore {

NotificationClient* NotificationPresenterImpl::instance()
{
    static NotificationPresenterImpl* s_instance = 0;
    if (!s_instance)
        s_instance = new NotificationPresenterImpl;
    return s_instance;
}

NotificationPresenterImpl::NotificationPresenterImpl()
{
    m_platformPresenter = adoptPtr(BlackBerry::Platform::NotificationPresenterBlackBerry::create(this));
}

NotificationPresenterImpl::~NotificationPresenterImpl()
{
}

bool NotificationPresenterImpl::show(Notification* notification)
{
    ASSERT(notification);
    ASSERT(notification->scriptExecutionContext());
    RefPtr<Notification> n = notification;
    if (m_notifications.contains(n))
        return false;

    if (checkPermission(notification->scriptExecutionContext()) != NotificationClient::PermissionAllowed)
        return false;

    String uuid = createCanonicalUUIDString();
    m_notifications.add(n, uuid);
    String message;
    if (n->isHTML()) {
        // FIXME: Load and display HTML content.
        message = n->url().string();
    } else {
        // FIXME: Strip the content into one line.
        message = n->contents().title() + ": " + n->contents().body();
    }

    m_platformPresenter->show(std::string(uuid.utf8().data()), std::string(message.utf8().data()));
    return true;
}

void NotificationPresenterImpl::cancel(Notification* notification)
{
    ASSERT(notification);
    RefPtr<Notification> n = notification;

    NotificationMap::iterator it = m_notifications.find(n);
    if (it == m_notifications.end())
        return;

    m_platformPresenter->cancel(std::string(it->second.utf8().data()));
    m_notifications.remove(it);
}

void NotificationPresenterImpl::notificationObjectDestroyed(Notification* notification)
{
    ASSERT(notification);
    cancel(notification);
}

void NotificationPresenterImpl::notificationControllerDestroyed()
{
}

void NotificationPresenterImpl::requestPermission(ScriptExecutionContext* context, PassRefPtr<VoidCallback> callback)
{
    ASSERT(context);
    m_permissionRequests.add(context, callback);
    m_platformPresenter->requestPermission(std::string(context->url().host().utf8().data()));
}

void NotificationPresenterImpl::onPermission(const std::string& domain, bool isAllowed)
{
    ASSERT(!domain.empty());
    String domainString = String::fromUTF8(domain.c_str());
    PermissionRequestMap::iterator it = m_permissionRequests.begin();
    for (; it != m_permissionRequests.end(); ++it) {
        if (it->first->url().host() != domainString)
            continue;

        if (isAllowed) {
            m_allowedDomains.add(domainString);
            it->second->handleEvent();
        } else
            m_allowedDomains.remove(domainString);

        m_permissionRequests.remove(it);
        return;
    }
}

void NotificationPresenterImpl::cancelRequestsForPermission(ScriptExecutionContext*)
{
    // Because we are using modal dialogs to request permission, it's impossible to cancel them.
}

NotificationClient::Permission NotificationPresenterImpl::checkPermission(ScriptExecutionContext* context)
{
    ASSERT(context);
    // FIXME: Should store the permission information into file permanently instead of in m_allowedDomains.
    // The suggested place to do this is in m_platformPresenter->checkPermission().
    if (m_allowedDomains.contains(context->url().host()))
        return NotificationClient::PermissionAllowed;
    return NotificationClient::PermissionNotAllowed;
}

// This function is called in platform side.
void NotificationPresenterImpl::notificationClicked(const std::string& id)
{
    ASSERT(!id.empty());
    String idString = String::fromUTF8(id.c_str());
    NotificationMap::iterator it = m_notifications.begin();
    for (; it != m_notifications.end(); ++it) {
        if (it->second == idString && it->first->scriptExecutionContext()) {
            RefPtr<Notification> notification = it->first;
            it->first->dispatchEvent(Event::create(eventNames().clickEvent, false, true));
            m_notifications.remove(notification);
            return;
        }
    }
}

} // namespace WebKit

#endif // ENABLE(NOTIFICATIONS)