1 /* 2 * Copyright (C) 2008, 2009 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef DOMApplicationCache_h 27 #define DOMApplicationCache_h 28 29 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 30 31 #include "ApplicationCacheHost.h" 32 #include "AtomicStringHash.h" 33 #include "EventTarget.h" 34 #include "EventListener.h" 35 #include <wtf/HashMap.h> 36 #include <wtf/PassRefPtr.h> 37 #include <wtf/RefCounted.h> 38 #include <wtf/Vector.h> 39 40 namespace WebCore { 41 42 class AtomicStringImpl; 43 class Frame; 44 class KURL; 45 class String; 46 47 class DOMApplicationCache : public RefCounted<DOMApplicationCache>, public EventTarget { 48 public: create(Frame * frame)49 static PassRefPtr<DOMApplicationCache> create(Frame* frame) { return adoptRef(new DOMApplicationCache(frame)); } ~DOMApplicationCache()50 ~DOMApplicationCache() { ASSERT(!m_frame); } 51 52 void disconnectFrame(); 53 54 unsigned short status() const; 55 void update(ExceptionCode&); 56 void swapCache(ExceptionCode&); 57 58 // Event listener attributes by EventID 59 setAttributeEventListener(ApplicationCacheHost::EventID id,PassRefPtr<EventListener> eventListener)60 void setAttributeEventListener(ApplicationCacheHost::EventID id, PassRefPtr<EventListener> eventListener) { m_attributeEventListeners[id] = eventListener; } getAttributeEventListener(ApplicationCacheHost::EventID id)61 EventListener* getAttributeEventListener(ApplicationCacheHost::EventID id) const { return m_attributeEventListeners[id].get(); } clearAttributeEventListener(ApplicationCacheHost::EventID id)62 void clearAttributeEventListener(ApplicationCacheHost::EventID id) { m_attributeEventListeners[id] = 0; } callEventListener(ApplicationCacheHost::EventID id)63 void callEventListener(ApplicationCacheHost::EventID id) { callListener(toEventType(id), getAttributeEventListener(id)); } 64 65 // EventTarget impl 66 67 virtual void addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture); 68 virtual void removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture); 69 virtual bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&); 70 typedef Vector<RefPtr<EventListener> > ListenerVector; 71 typedef HashMap<AtomicString, ListenerVector> EventListenersMap; eventListeners()72 EventListenersMap& eventListeners() { return m_eventListeners; } 73 74 using RefCounted<DOMApplicationCache>::ref; 75 using RefCounted<DOMApplicationCache>::deref; 76 77 // Explicitly named attribute event listener helpers 78 setOnchecking(PassRefPtr<EventListener> listener)79 void setOnchecking(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::CHECKING_EVENT, listener); } onchecking()80 EventListener* onchecking() const { return getAttributeEventListener(ApplicationCacheHost::CHECKING_EVENT); } 81 setOnerror(PassRefPtr<EventListener> listener)82 void setOnerror(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::ERROR_EVENT, listener);} onerror()83 EventListener* onerror() const { return getAttributeEventListener(ApplicationCacheHost::ERROR_EVENT); } 84 setOnnoupdate(PassRefPtr<EventListener> listener)85 void setOnnoupdate(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::NOUPDATE_EVENT, listener); } onnoupdate()86 EventListener* onnoupdate() const { return getAttributeEventListener(ApplicationCacheHost::NOUPDATE_EVENT); } 87 setOndownloading(PassRefPtr<EventListener> listener)88 void setOndownloading(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::DOWNLOADING_EVENT, listener); } ondownloading()89 EventListener* ondownloading() const { return getAttributeEventListener(ApplicationCacheHost::DOWNLOADING_EVENT); } 90 setOnprogress(PassRefPtr<EventListener> listener)91 void setOnprogress(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::PROGRESS_EVENT, listener); } onprogress()92 EventListener* onprogress() const { return getAttributeEventListener(ApplicationCacheHost::PROGRESS_EVENT); } 93 setOnupdateready(PassRefPtr<EventListener> listener)94 void setOnupdateready(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::UPDATEREADY_EVENT, listener); } onupdateready()95 EventListener* onupdateready() const { return getAttributeEventListener(ApplicationCacheHost::UPDATEREADY_EVENT); } 96 setOncached(PassRefPtr<EventListener> listener)97 void setOncached(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::CACHED_EVENT, listener); } oncached()98 EventListener* oncached() const { return getAttributeEventListener(ApplicationCacheHost::CACHED_EVENT); } 99 setOnobsolete(PassRefPtr<EventListener> listener)100 void setOnobsolete(PassRefPtr<EventListener> listener) { setAttributeEventListener(ApplicationCacheHost::OBSOLETE_EVENT, listener); } onobsolete()101 EventListener* onobsolete() const { return getAttributeEventListener(ApplicationCacheHost::OBSOLETE_EVENT); } 102 103 virtual ScriptExecutionContext* scriptExecutionContext() const; toDOMApplicationCache()104 DOMApplicationCache* toDOMApplicationCache() { return this; } 105 106 static const AtomicString& toEventType(ApplicationCacheHost::EventID id); 107 static ApplicationCacheHost::EventID toEventID(const AtomicString& eventType); 108 109 private: 110 DOMApplicationCache(Frame*); 111 112 void callListener(const AtomicString& eventType, EventListener*); 113 refEventTarget()114 virtual void refEventTarget() { ref(); } derefEventTarget()115 virtual void derefEventTarget() { deref(); } 116 117 ApplicationCacheHost* applicationCacheHost() const; 118 bool swapCache(); 119 120 RefPtr<EventListener> m_attributeEventListeners[ApplicationCacheHost::OBSOLETE_EVENT + 1]; 121 122 EventListenersMap m_eventListeners; 123 124 Frame* m_frame; 125 }; 126 127 } // namespace WebCore 128 129 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS) 130 131 #endif // DOMApplicationCache_h 132