Home | History | Annotate | Download | only in web
      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 "WebNotification.h"
     33 
     34 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     35 
     36 #include "WebTextDirection.h"
     37 #include "core/dom/Event.h"
     38 #include "core/dom/UserGestureIndicator.h"
     39 #include "core/page/WindowFocusAllowedIndicator.h"
     40 #include "modules/notifications/Notification.h"
     41 #include "public/platform/WebString.h"
     42 #include "public/platform/WebURL.h"
     43 #include "wtf/PassRefPtr.h"
     44 
     45 using namespace WebCore;
     46 
     47 namespace WebKit {
     48 
     49 class WebNotificationPrivate : public Notification {
     50 };
     51 
     52 void WebNotification::reset()
     53 {
     54     assign(0);
     55 }
     56 
     57 void WebNotification::assign(const WebNotification& other)
     58 {
     59     WebNotificationPrivate* p = const_cast<WebNotificationPrivate*>(other.m_private);
     60     if (p)
     61         p->ref(); // FIXME: We should use WebPrivatePtr.
     62     assign(p);
     63 }
     64 
     65 bool WebNotification::lessThan(const WebNotification& other) const
     66 {
     67     return reinterpret_cast<uintptr_t>(m_private) < reinterpret_cast<uintptr_t>(other.m_private);
     68 }
     69 
     70 bool WebNotification::isHTML() const
     71 {
     72     return false;
     73 }
     74 
     75 WebURL WebNotification::url() const
     76 {
     77     return WebURL();
     78 }
     79 
     80 WebURL WebNotification::iconURL() const
     81 {
     82     return m_private->iconURL();
     83 }
     84 
     85 WebString WebNotification::title() const
     86 {
     87     return m_private->title();
     88 }
     89 
     90 WebString WebNotification::body() const
     91 {
     92     return m_private->body();
     93 }
     94 
     95 WebTextDirection WebNotification::direction() const
     96 {
     97     return (m_private->direction() == RTL) ?
     98         WebTextDirectionRightToLeft :
     99         WebTextDirectionLeftToRight;
    100 }
    101 
    102 WebString WebNotification::replaceId() const
    103 {
    104     return m_private->tag();
    105 }
    106 
    107 void WebNotification::detachPresenter()
    108 {
    109     m_private->detachPresenter();
    110 }
    111 
    112 void WebNotification::dispatchDisplayEvent()
    113 {
    114 #if ENABLE(LEGACY_NOTIFICATIONS)
    115     dispatchEvent("display");
    116 #endif
    117     dispatchEvent("show");
    118 }
    119 
    120 void WebNotification::dispatchErrorEvent(const WebKit::WebString& /* errorMessage */)
    121 {
    122     // FIXME: errorMessage not supported by WebCore yet
    123     dispatchEvent(eventNames().errorEvent);
    124 }
    125 
    126 void WebNotification::dispatchCloseEvent(bool /* byUser */)
    127 {
    128     // FIXME: byUser flag not supported by WebCore yet
    129     dispatchEvent(eventNames().closeEvent);
    130 }
    131 
    132 void WebNotification::dispatchClickEvent()
    133 {
    134     UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
    135     WindowFocusAllowedIndicator windowFocusAllowed;
    136     dispatchEvent(eventNames().clickEvent);
    137 }
    138 
    139 void WebNotification::dispatchEvent(const WTF::AtomicString& type)
    140 {
    141     // Do not dispatch if the context is gone.
    142     if (!m_private->scriptExecutionContext())
    143         return;
    144 
    145     RefPtr<Event> event = Event::create(type, false, true);
    146     m_private->dispatchEvent(event.release());
    147 }
    148 
    149 WebNotification::WebNotification(const WTF::PassRefPtr<Notification>& notification)
    150     : m_private(static_cast<WebNotificationPrivate*>(notification.leakRef()))
    151 {
    152 }
    153 
    154 WebNotification& WebNotification::operator=(const WTF::PassRefPtr<Notification>& notification)
    155 {
    156     assign(static_cast<WebNotificationPrivate*>(notification.leakRef()));
    157     return *this;
    158 }
    159 
    160 WebNotification::operator WTF::PassRefPtr<Notification>() const
    161 {
    162     return WTF::PassRefPtr<Notification>(const_cast<WebNotificationPrivate*>(m_private));
    163 }
    164 
    165 void WebNotification::assign(WebNotificationPrivate* p)
    166 {
    167     // p is already ref'd for us by the caller
    168     if (m_private)
    169         m_private->deref();
    170     m_private = p;
    171 }
    172 
    173 } // namespace WebKit
    174 
    175 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
    176