1 /* 2 * Copyright (C) 2013 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 #ifndef Notification_h 32 #define Notification_h 33 34 #include "core/dom/ActiveDOMObject.h" 35 #include "modules/EventTargetModules.h" 36 #include "modules/notifications/NotificationClient.h" 37 #include "platform/AsyncMethodRunner.h" 38 #include "platform/heap/Handle.h" 39 #include "platform/text/TextDirection.h" 40 #include "platform/weborigin/KURL.h" 41 #include "wtf/PassOwnPtr.h" 42 #include "wtf/RefCounted.h" 43 44 namespace blink { 45 46 class ExecutionContext; 47 class NotificationOptions; 48 class NotificationPermissionCallback; 49 50 class Notification : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<Notification>, public ActiveDOMObject, public EventTargetWithInlineData { 51 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<Notification>); 52 DEFINE_WRAPPERTYPEINFO(); 53 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(Notification); 54 public: 55 static Notification* create(ExecutionContext*, const String& title, const NotificationOptions&); 56 57 virtual ~Notification(); 58 59 void close(); 60 61 DEFINE_ATTRIBUTE_EVENT_LISTENER(click); 62 DEFINE_ATTRIBUTE_EVENT_LISTENER(show); 63 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 64 DEFINE_ATTRIBUTE_EVENT_LISTENER(close); 65 66 void dispatchShowEvent(); 67 void dispatchClickEvent(); 68 void dispatchErrorEvent(); 69 void dispatchCloseEvent(); 70 title()71 String title() const { return m_title; } dir()72 String dir() const { return m_dir; } lang()73 String lang() const { return m_lang; } body()74 String body() const { return m_body; } tag()75 String tag() const { return m_tag; } icon()76 String icon() const { return m_iconUrl; } 77 78 TextDirection direction() const; iconURL()79 KURL iconURL() const { return m_iconUrl; } 80 81 static const String& permissionString(NotificationClient::Permission); 82 static const String& permission(ExecutionContext*); 83 static void requestPermission(ExecutionContext*, NotificationPermissionCallback* = nullptr); 84 85 // EventTarget interface. executionContext()86 virtual ExecutionContext* executionContext() const OVERRIDE FINAL { return ActiveDOMObject::executionContext(); } 87 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE FINAL; 88 virtual const AtomicString& interfaceName() const OVERRIDE; 89 90 // ActiveDOMObject interface. 91 virtual void stop() OVERRIDE; 92 virtual bool hasPendingActivity() const OVERRIDE; 93 94 private: 95 Notification(const String& title, ExecutionContext*, NotificationClient*); 96 97 // Calling show() may start asynchronous operation. If this object has 98 // a V8 wrapper, hasPendingActivity() prevents the wrapper from being 99 // collected while m_state is Showing, and so this instance stays alive 100 // until the operation completes. Otherwise, you need to hold a ref on this 101 // instance until the operation completes. 102 void show(); 103 setDir(const String & dir)104 void setDir(const String& dir) { m_dir = dir; } setLang(const String & lang)105 void setLang(const String& lang) { m_lang = lang; } setBody(const String & body)106 void setBody(const String& body) { m_body = body; } setIconUrl(KURL iconUrl)107 void setIconUrl(KURL iconUrl) { m_iconUrl = iconUrl; } setTag(const String & tag)108 void setTag(const String& tag) { m_tag = tag; } 109 110 private: 111 String m_title; 112 String m_dir; 113 String m_lang; 114 String m_body; 115 String m_tag; 116 117 KURL m_iconUrl; 118 119 enum NotificationState { 120 NotificationStateIdle, 121 NotificationStateShowing, 122 NotificationStateClosed 123 }; 124 125 NotificationState m_state; 126 127 NotificationClient* m_client; 128 129 AsyncMethodRunner<Notification> m_asyncRunner; 130 }; 131 132 } // namespace blink 133 134 #endif // Notification_h 135