Home | History | Annotate | Download | only in notifications
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef Notification_h
     33 #define Notification_h
     34 
     35 #include "bindings/v8/ScriptWrappable.h"
     36 #include "core/dom/ActiveDOMObject.h"
     37 #include "core/dom/EventNames.h"
     38 #include "core/dom/EventTarget.h"
     39 #include "core/loader/ThreadableLoaderClient.h"
     40 #include "core/platform/SharedBuffer.h"
     41 #include "core/platform/text/TextDirection.h"
     42 #include "modules/notifications/NotificationClient.h"
     43 #include "weborigin/KURL.h"
     44 #include "wtf/OwnPtr.h"
     45 #include "wtf/PassRefPtr.h"
     46 #include "wtf/RefCounted.h"
     47 #include "wtf/RefPtr.h"
     48 #include "wtf/text/AtomicStringHash.h"
     49 
     50 #if ENABLE(NOTIFICATIONS)
     51 #include "core/platform/Timer.h"
     52 #endif
     53 
     54 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     55 namespace WebCore {
     56 
     57 class Dictionary;
     58 class ExceptionState;
     59 class NotificationCenter;
     60 class NotificationPermissionCallback;
     61 class ResourceError;
     62 class ResourceResponse;
     63 class ScriptExecutionContext;
     64 class ThreadableLoader;
     65 
     66 class Notification : public RefCounted<Notification>, public ScriptWrappable, public ActiveDOMObject, public EventTarget {
     67     WTF_MAKE_FAST_ALLOCATED;
     68 public:
     69     Notification();
     70 #if ENABLE(LEGACY_NOTIFICATIONS)
     71     static PassRefPtr<Notification> create(const String& title, const String& body, const String& iconURI, ScriptExecutionContext*, ExceptionState&, PassRefPtr<NotificationCenter> provider);
     72 #endif
     73 #if ENABLE(NOTIFICATIONS)
     74     static PassRefPtr<Notification> create(ScriptExecutionContext*, const String& title, const Dictionary& options);
     75 #endif
     76 
     77     virtual ~Notification();
     78 
     79     void show();
     80 #if ENABLE(LEGACY_NOTIFICATIONS)
     81     void cancel() { close(); }
     82 #endif
     83     void close();
     84 
     85     KURL iconURL() const { return m_icon; }
     86     void setIconURL(const KURL& url) { m_icon = url; }
     87 
     88     String title() const { return m_title; }
     89     String body() const { return m_body; }
     90 
     91     String lang() const { return m_lang; }
     92     void setLang(const String& lang) { m_lang = lang; }
     93 
     94     String dir() const { return m_direction; }
     95     void setDir(const String& dir) { m_direction = dir; }
     96 
     97 #if ENABLE(LEGACY_NOTIFICATIONS)
     98     String replaceId() const { return tag(); }
     99     void setReplaceId(const String& replaceId) { setTag(replaceId); }
    100 #endif
    101 
    102     String tag() const { return m_tag; }
    103     void setTag(const String& tag) { m_tag = tag; }
    104 
    105     TextDirection direction() const { return dir() == "rtl" ? RTL : LTR; }
    106 
    107 #if ENABLE(LEGACY_NOTIFICATIONS)
    108     DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(display, show);
    109 #endif
    110     DEFINE_ATTRIBUTE_EVENT_LISTENER(show);
    111     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
    112     DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
    113     DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
    114 
    115     void dispatchClickEvent();
    116     void dispatchCloseEvent();
    117     void dispatchErrorEvent();
    118     void dispatchShowEvent();
    119 
    120     using RefCounted<Notification>::ref;
    121     using RefCounted<Notification>::deref;
    122 
    123     // EventTarget interface
    124     virtual const AtomicString& interfaceName() const;
    125     virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); }
    126 
    127     // ActiveDOMObject interface
    128     virtual void contextDestroyed();
    129 
    130     void stopLoadingIcon();
    131 
    132     // Deprecated. Use functions from NotificationCenter.
    133     void detachPresenter() { }
    134 
    135     void finalize();
    136 
    137 #if ENABLE(NOTIFICATIONS)
    138     static const String& permission(ScriptExecutionContext*);
    139     static const String& permissionString(NotificationClient::Permission);
    140     static void requestPermission(ScriptExecutionContext*, PassRefPtr<NotificationPermissionCallback> = 0);
    141 #endif
    142 
    143 private:
    144 #if ENABLE(LEGACY_NOTIFICATIONS)
    145     Notification(const String& title, const String& body, const String& iconURI, ScriptExecutionContext*, ExceptionState&, PassRefPtr<NotificationCenter>);
    146 #endif
    147 #if ENABLE(NOTIFICATIONS)
    148     Notification(ScriptExecutionContext*, const String& title);
    149 #endif
    150 
    151     void setBody(const String& body) { m_body = body; }
    152 
    153     // EventTarget interface
    154     virtual void refEventTarget() { ref(); }
    155     virtual void derefEventTarget() { deref(); }
    156     virtual EventTargetData* eventTargetData();
    157     virtual EventTargetData* ensureEventTargetData();
    158 
    159     void startLoadingIcon();
    160     void finishLoadingIcon();
    161 
    162 #if ENABLE(NOTIFICATIONS)
    163     void taskTimerFired(Timer<Notification>*);
    164 #endif
    165 
    166     // Text notifications.
    167     KURL m_icon;
    168     String m_title;
    169     String m_body;
    170 
    171     String m_direction;
    172     String m_lang;
    173     String m_tag;
    174 
    175     enum NotificationState {
    176         Idle = 0,
    177         Showing = 1,
    178         Closed = 2,
    179     };
    180 
    181     NotificationState m_state;
    182 
    183     RefPtr<NotificationCenter> m_notificationCenter;
    184 
    185     EventTargetData m_eventTargetData;
    186 
    187 #if ENABLE(NOTIFICATIONS)
    188     OwnPtr<Timer<Notification> > m_taskTimer;
    189 #endif
    190 };
    191 
    192 } // namespace WebCore
    193 
    194 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
    195 
    196 #endif // Notifications_h
    197