Notification.cpp 6.61 KB
Newer Older
1 2
/*
 * Copyright (C) 2009 Google Inc. All rights reserved.
3
 * Copyright (C) 2009 Apple Inc. All rights reserved.
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(NOTIFICATIONS)

#include "Notification.h"

#include "Document.h"
#include "EventNames.h"
40 41 42
#include "NotificationCenter.h"
#include "NotificationContents.h"
#include "NotificationPresenter.h"
43 44 45
#include "ResourceRequest.h"
#include "ResourceResponse.h"
#include "ThreadableLoader.h"
46
#include "WorkerContext.h"
47 48 49

namespace WebCore {

50
Notification::Notification(const KURL& url, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
51 52
    : ActiveDOMObject(context, this)
    , m_isHTML(true)
53
    , m_state(Idle)
54
    , m_notificationCenter(provider)
55
{
56 57
    ASSERT(m_notificationCenter->presenter());
    if (m_notificationCenter->presenter()->checkPermission(context) != NotificationPresenter::PermissionAllowed) {
58 59 60 61
        ec = SECURITY_ERR;
        return;
    }

62
    if (url.isEmpty() || !url.isValid()) {
63 64 65
        ec = SYNTAX_ERR;
        return;
    }
66 67

    m_notificationURL = url;
68 69
}

70
Notification::Notification(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider)
71 72 73
    : ActiveDOMObject(context, this)
    , m_isHTML(false)
    , m_contents(contents)
74
    , m_state(Idle)
75
    , m_notificationCenter(provider)
76
{
77 78
    ASSERT(m_notificationCenter->presenter());
    if (m_notificationCenter->presenter()->checkPermission(context) != NotificationPresenter::PermissionAllowed) {
79 80 81
        ec = SECURITY_ERR;
        return;
    }
82

83
    if (!contents.icon().isEmpty() && !contents.icon().isValid()) {
84 85 86 87 88 89 90
        ec = SYNTAX_ERR;
        return;
    }
}

Notification::~Notification() 
{
91 92 93 94
    if (m_state == Loading) {
        ASSERT_NOT_REACHED();
        cancel();
    }
95 96
}

97 98 99 100 101 102 103 104 105 106
PassRefPtr<Notification> Notification::create(const KURL& url, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider) 
{ 
    return adoptRef(new Notification(url, context, ec, provider));
}

PassRefPtr<Notification> Notification::create(const NotificationContents& contents, ScriptExecutionContext* context, ExceptionCode& ec, PassRefPtr<NotificationCenter> provider) 
{ 
    return adoptRef(new Notification(contents, context, ec, provider));
}

107 108
void Notification::show() 
{
109 110 111 112 113 114
#if PLATFORM(QT)
    if (iconURL().isEmpty()) {
        // Set the state before actually showing, because
        // handling of ondisplay may rely on that.
        if (m_state == Idle) {
            m_state = Showing;
115 116
            if (m_notificationCenter->presenter())
                m_notificationCenter->presenter()->show(this);
117 118 119 120
        }
    } else
        startLoading();
#else
121
    // prevent double-showing
122
    if (m_state == Idle && m_notificationCenter->presenter() && m_notificationCenter->presenter()->show(this))
123 124
        m_state = Showing;
#endif
125 126 127 128
}

void Notification::cancel() 
{
129 130 131 132 133 134 135 136
    switch (m_state) {
    case Idle:
        break;
    case Loading:
        m_state = Cancelled;
        stopLoading();
        break;
    case Showing:
137 138
        if (m_notificationCenter->presenter())
            m_notificationCenter->presenter()->cancel(this);
139 140 141 142
        break;
    case Cancelled:
        break;
    }
143 144
}

145
EventTargetData* Notification::eventTargetData()
146
{
147
    return &m_eventTargetData;
148 149
}

150
EventTargetData* Notification::ensureEventTargetData()
151
{
152
    return &m_eventTargetData;
153 154
}

155 156 157
void Notification::contextDestroyed()
{
    ActiveDOMObject::contextDestroyed();
158 159
    if (m_notificationCenter->presenter())
        m_notificationCenter->presenter()->notificationObjectDestroyed(this);
160
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

void Notification::startLoading()
{
    if (m_state != Idle)
        return;
    setPendingActivity(this);
    m_state = Loading;
    ThreadableLoaderOptions options;
    options.sendLoadCallbacks = false;
    options.sniffContent = false;
    options.forcePreflight = false;
    options.allowCredentials = AllowStoredCredentials;
    options.crossOriginRequestPolicy = AllowCrossOriginRequests;
    m_loader = ThreadableLoader::create(scriptExecutionContext(), this, ResourceRequest(iconURL()), options);
}

void Notification::stopLoading()
{
    m_iconData = 0;
    RefPtr<ThreadableLoader> protect(m_loader);
    m_loader->cancel();
}

void Notification::didReceiveResponse(const ResourceResponse& response)
{
    int status = response.httpStatusCode();
    if (status && (status < 200 || status > 299)) {
        stopLoading();
        return;
    }
    m_iconData = SharedBuffer::create();
}

194
void Notification::didReceiveData(const char* data, int dataLength)
195
{
196
    m_iconData->append(data, dataLength);
197 198
}

199
void Notification::didFinishLoading(unsigned long, double)
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
    finishLoading();
}

void Notification::didFail(const ResourceError&)
{
    finishLoading();
}

void Notification::didFailRedirectCheck()
{
    finishLoading();
}

void Notification::didReceiveAuthenticationCancellation(const ResourceResponse&)
{
    finishLoading();
}

void Notification::finishLoading()
{
    if (m_state == Loading) {
222
        if (m_notificationCenter->presenter() && m_notificationCenter->presenter()->show(this))
223 224 225 226 227
            m_state = Showing;
    }
    unsetPendingActivity(this);
}

228 229 230
} // namespace WebCore

#endif // ENABLE(NOTIFICATIONS)