Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsDOMFileReader_h__ |
michael@0 | 7 | #define nsDOMFileReader_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Attributes.h" |
michael@0 | 10 | #include "nsISupportsUtils.h" |
michael@0 | 11 | #include "nsString.h" |
michael@0 | 12 | #include "nsWeakReference.h" |
michael@0 | 13 | #include "nsIStreamListener.h" |
michael@0 | 14 | #include "nsIInterfaceRequestor.h" |
michael@0 | 15 | #include "nsJSUtils.h" |
michael@0 | 16 | #include "nsTArray.h" |
michael@0 | 17 | #include "nsIJSNativeInitializer.h" |
michael@0 | 18 | #include "prtime.h" |
michael@0 | 19 | #include "nsITimer.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "nsIDOMFile.h" |
michael@0 | 22 | #include "nsIDOMFileReader.h" |
michael@0 | 23 | #include "nsIDOMFileList.h" |
michael@0 | 24 | #include "nsIInputStream.h" |
michael@0 | 25 | #include "nsCOMPtr.h" |
michael@0 | 26 | #include "nsIStreamLoader.h" |
michael@0 | 27 | #include "nsIChannel.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "FileIOObject.h" |
michael@0 | 30 | |
michael@0 | 31 | class nsDOMFileReader : public mozilla::dom::FileIOObject, |
michael@0 | 32 | public nsIDOMFileReader, |
michael@0 | 33 | public nsIInterfaceRequestor, |
michael@0 | 34 | public nsSupportsWeakReference |
michael@0 | 35 | { |
michael@0 | 36 | typedef mozilla::ErrorResult ErrorResult; |
michael@0 | 37 | typedef mozilla::dom::GlobalObject GlobalObject; |
michael@0 | 38 | public: |
michael@0 | 39 | nsDOMFileReader(); |
michael@0 | 40 | virtual ~nsDOMFileReader(); |
michael@0 | 41 | |
michael@0 | 42 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 43 | |
michael@0 | 44 | NS_DECL_NSIDOMFILEREADER |
michael@0 | 45 | |
michael@0 | 46 | NS_REALLY_FORWARD_NSIDOMEVENTTARGET(mozilla::DOMEventTargetHelper) |
michael@0 | 47 | |
michael@0 | 48 | // nsIInterfaceRequestor |
michael@0 | 49 | NS_DECL_NSIINTERFACEREQUESTOR |
michael@0 | 50 | |
michael@0 | 51 | // FileIOObject overrides |
michael@0 | 52 | virtual void DoAbort(nsAString& aEvent) MOZ_OVERRIDE; |
michael@0 | 53 | NS_IMETHOD DoOnStopRequest(nsIRequest* aRequest, nsISupports* aContext, |
michael@0 | 54 | nsresult aStatus, nsAString& aSuccessEvent, |
michael@0 | 55 | nsAString& aTerminationEvent) MOZ_OVERRIDE; |
michael@0 | 56 | NS_IMETHOD DoOnDataAvailable(nsIRequest* aRequest, nsISupports* aContext, |
michael@0 | 57 | nsIInputStream* aInputStream, uint64_t aOffset, |
michael@0 | 58 | uint32_t aCount) MOZ_OVERRIDE; |
michael@0 | 59 | |
michael@0 | 60 | nsPIDOMWindow* GetParentObject() const |
michael@0 | 61 | { |
michael@0 | 62 | return GetOwner(); |
michael@0 | 63 | } |
michael@0 | 64 | virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 65 | |
michael@0 | 66 | // WebIDL |
michael@0 | 67 | static already_AddRefed<nsDOMFileReader> |
michael@0 | 68 | Constructor(const GlobalObject& aGlobal, ErrorResult& aRv); |
michael@0 | 69 | void ReadAsArrayBuffer(JSContext* aCx, nsIDOMBlob* aBlob, ErrorResult& aRv) |
michael@0 | 70 | { |
michael@0 | 71 | MOZ_ASSERT(aBlob); |
michael@0 | 72 | ReadFileContent(aCx, aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv); |
michael@0 | 73 | } |
michael@0 | 74 | void ReadAsText(nsIDOMBlob* aBlob, const nsAString& aLabel, ErrorResult& aRv) |
michael@0 | 75 | { |
michael@0 | 76 | MOZ_ASSERT(aBlob); |
michael@0 | 77 | ReadFileContent(nullptr, aBlob, aLabel, FILE_AS_TEXT, aRv); |
michael@0 | 78 | } |
michael@0 | 79 | void ReadAsDataURL(nsIDOMBlob* aBlob, ErrorResult& aRv) |
michael@0 | 80 | { |
michael@0 | 81 | MOZ_ASSERT(aBlob); |
michael@0 | 82 | ReadFileContent(nullptr, aBlob, EmptyString(), FILE_AS_DATAURL, aRv); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | using FileIOObject::Abort; |
michael@0 | 86 | |
michael@0 | 87 | // Inherited ReadyState(). |
michael@0 | 88 | |
michael@0 | 89 | void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, |
michael@0 | 90 | ErrorResult& aRv); |
michael@0 | 91 | |
michael@0 | 92 | using FileIOObject::GetError; |
michael@0 | 93 | |
michael@0 | 94 | IMPL_EVENT_HANDLER(loadstart) |
michael@0 | 95 | using FileIOObject::GetOnprogress; |
michael@0 | 96 | using FileIOObject::SetOnprogress; |
michael@0 | 97 | IMPL_EVENT_HANDLER(load) |
michael@0 | 98 | using FileIOObject::GetOnabort; |
michael@0 | 99 | using FileIOObject::SetOnabort; |
michael@0 | 100 | using FileIOObject::GetOnerror; |
michael@0 | 101 | using FileIOObject::SetOnerror; |
michael@0 | 102 | IMPL_EVENT_HANDLER(loadend) |
michael@0 | 103 | |
michael@0 | 104 | void ReadAsBinaryString(nsIDOMBlob* aBlob, ErrorResult& aRv) |
michael@0 | 105 | { |
michael@0 | 106 | MOZ_ASSERT(aBlob); |
michael@0 | 107 | ReadFileContent(nullptr, aBlob, EmptyString(), FILE_AS_BINARY, aRv); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | nsresult Init(); |
michael@0 | 112 | |
michael@0 | 113 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(nsDOMFileReader, |
michael@0 | 114 | FileIOObject) |
michael@0 | 115 | void RootResultArrayBuffer(); |
michael@0 | 116 | |
michael@0 | 117 | protected: |
michael@0 | 118 | enum eDataFormat { |
michael@0 | 119 | FILE_AS_ARRAYBUFFER, |
michael@0 | 120 | FILE_AS_BINARY, |
michael@0 | 121 | FILE_AS_TEXT, |
michael@0 | 122 | FILE_AS_DATAURL |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | void ReadFileContent(JSContext* aCx, nsIDOMBlob* aBlob, |
michael@0 | 126 | const nsAString &aCharset, eDataFormat aDataFormat, |
michael@0 | 127 | ErrorResult& aRv); |
michael@0 | 128 | nsresult GetAsText(nsIDOMBlob *aFile, const nsACString &aCharset, |
michael@0 | 129 | const char *aFileData, uint32_t aDataLen, nsAString &aResult); |
michael@0 | 130 | nsresult GetAsDataURL(nsIDOMBlob *aFile, const char *aFileData, uint32_t aDataLen, nsAString &aResult); |
michael@0 | 131 | |
michael@0 | 132 | void FreeFileData() { |
michael@0 | 133 | moz_free(mFileData); |
michael@0 | 134 | mFileData = nullptr; |
michael@0 | 135 | mDataLen = 0; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | char *mFileData; |
michael@0 | 139 | nsCOMPtr<nsIDOMBlob> mFile; |
michael@0 | 140 | nsCString mCharset; |
michael@0 | 141 | uint32_t mDataLen; |
michael@0 | 142 | |
michael@0 | 143 | eDataFormat mDataFormat; |
michael@0 | 144 | |
michael@0 | 145 | nsString mResult; |
michael@0 | 146 | nsCOMPtr<nsIPrincipal> mPrincipal; |
michael@0 | 147 | |
michael@0 | 148 | JS::Heap<JSObject*> mResultArrayBuffer; |
michael@0 | 149 | }; |
michael@0 | 150 | |
michael@0 | 151 | #endif |