1 /* 2 * Copyright (C) 2008 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 XMLHttpRequestUpload_h 27 #define XMLHttpRequestUpload_h 28 29 #include "AtomicStringHash.h" 30 #include "EventListener.h" 31 #include "EventTarget.h" 32 #include <wtf/HashMap.h> 33 #include <wtf/RefCounted.h> 34 #include <wtf/RefPtr.h> 35 #include <wtf/PassRefPtr.h> 36 #include <wtf/Vector.h> 37 38 namespace WebCore { 39 40 class AtomicStringImpl; 41 class ScriptExecutionContext; 42 class XMLHttpRequest; 43 44 class XMLHttpRequestUpload : public RefCounted<XMLHttpRequestUpload>, public EventTarget { 45 public: create(XMLHttpRequest * xmlHttpRequest)46 static PassRefPtr<XMLHttpRequestUpload> create(XMLHttpRequest* xmlHttpRequest) 47 { 48 return adoptRef(new XMLHttpRequestUpload(xmlHttpRequest)); 49 } 50 toXMLHttpRequestUpload()51 virtual XMLHttpRequestUpload* toXMLHttpRequestUpload() { return this; } 52 associatedXMLHttpRequest()53 XMLHttpRequest* associatedXMLHttpRequest() const { return m_xmlHttpRequest; } disconnectXMLHttpRequest()54 void disconnectXMLHttpRequest() { m_xmlHttpRequest = 0; } 55 56 ScriptExecutionContext* scriptExecutionContext() const; 57 58 void dispatchAbortEvent(); 59 void dispatchErrorEvent(); 60 void dispatchLoadEvent(); 61 void dispatchLoadStartEvent(); 62 void dispatchProgressEvent(long long bytesSent, long long totalBytesToBeSent); 63 setOnabort(PassRefPtr<EventListener> eventListener)64 void setOnabort(PassRefPtr<EventListener> eventListener) { m_onAbortListener = eventListener; } onabort()65 EventListener* onabort() const { return m_onAbortListener.get(); } 66 setOnerror(PassRefPtr<EventListener> eventListener)67 void setOnerror(PassRefPtr<EventListener> eventListener) { m_onErrorListener = eventListener; } onerror()68 EventListener* onerror() const { return m_onErrorListener.get(); } 69 setOnload(PassRefPtr<EventListener> eventListener)70 void setOnload(PassRefPtr<EventListener> eventListener) { m_onLoadListener = eventListener; } onload()71 EventListener* onload() const { return m_onLoadListener.get(); } 72 setOnloadstart(PassRefPtr<EventListener> eventListener)73 void setOnloadstart(PassRefPtr<EventListener> eventListener) { m_onLoadStartListener = eventListener; } onloadstart()74 EventListener* onloadstart() const { return m_onLoadStartListener.get(); } 75 setOnprogress(PassRefPtr<EventListener> eventListener)76 void setOnprogress(PassRefPtr<EventListener> eventListener) { m_onProgressListener = eventListener; } onprogress()77 EventListener* onprogress() const { return m_onProgressListener.get(); } 78 79 typedef Vector<RefPtr<EventListener> > ListenerVector; 80 typedef HashMap<AtomicString, ListenerVector> EventListenersMap; 81 82 virtual void addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture); 83 virtual void removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture); 84 virtual bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&); eventListeners()85 EventListenersMap& eventListeners() { return m_eventListeners; } 86 87 using RefCounted<XMLHttpRequestUpload>::ref; 88 using RefCounted<XMLHttpRequestUpload>::deref; 89 90 private: 91 XMLHttpRequestUpload(XMLHttpRequest*); 92 93 void dispatchXMLHttpRequestProgressEvent(EventListener*, const AtomicString& type, bool lengthComputable, unsigned loaded, unsigned total); 94 refEventTarget()95 virtual void refEventTarget() { ref(); } derefEventTarget()96 virtual void derefEventTarget() { deref(); } 97 98 RefPtr<EventListener> m_onAbortListener; 99 RefPtr<EventListener> m_onErrorListener; 100 RefPtr<EventListener> m_onLoadListener; 101 RefPtr<EventListener> m_onLoadStartListener; 102 RefPtr<EventListener> m_onProgressListener; 103 EventListenersMap m_eventListeners; 104 105 XMLHttpRequest* m_xmlHttpRequest; 106 }; 107 108 } // namespace WebCore 109 110 #endif // XMLHttpRequestUpload_h 111