1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/FileIOObject.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef FileIOObject_h__ 1.10 +#define FileIOObject_h__ 1.11 + 1.12 +#include "mozilla/DOMEventTargetHelper.h" 1.13 +#include "nsIChannel.h" 1.14 +#include "nsIFile.h" 1.15 +#include "nsIDOMFile.h" 1.16 +#include "nsIStreamListener.h" 1.17 +#include "nsITimer.h" 1.18 +#include "nsCOMPtr.h" 1.19 + 1.20 +#include "mozilla/dom/DOMError.h" 1.21 + 1.22 +#define NS_PROGRESS_EVENT_INTERVAL 50 1.23 + 1.24 +namespace mozilla { 1.25 + 1.26 +class ErrorResult; 1.27 + 1.28 +namespace dom { 1.29 + 1.30 +extern const uint64_t kUnknownSize; 1.31 + 1.32 +// A common base class for FileReader and FileSaver 1.33 + 1.34 +class FileIOObject : public DOMEventTargetHelper, 1.35 + public nsIStreamListener, 1.36 + public nsITimerCallback 1.37 +{ 1.38 +public: 1.39 + FileIOObject(); 1.40 + 1.41 + NS_DECL_ISUPPORTS_INHERITED 1.42 + 1.43 + // Common methods 1.44 + void Abort(ErrorResult& aRv); 1.45 + uint16_t ReadyState() const 1.46 + { 1.47 + return mReadyState; 1.48 + } 1.49 + DOMError* GetError() const 1.50 + { 1.51 + return mError; 1.52 + } 1.53 + 1.54 + NS_METHOD GetOnabort(JSContext* aCx, JS::MutableHandle<JS::Value> aValue); 1.55 + NS_METHOD SetOnabort(JSContext* aCx, JS::Handle<JS::Value> aValue); 1.56 + NS_METHOD GetOnerror(JSContext* aCx, JS::MutableHandle<JS::Value> aValue); 1.57 + NS_METHOD SetOnerror(JSContext* aCx, JS::Handle<JS::Value> aValue); 1.58 + NS_METHOD GetOnprogress(JSContext* aCx, JS::MutableHandle<JS::Value> aValue); 1.59 + NS_METHOD SetOnprogress(JSContext* aCx, JS::Handle<JS::Value> aValue); 1.60 + 1.61 + IMPL_EVENT_HANDLER(abort) 1.62 + IMPL_EVENT_HANDLER(error) 1.63 + IMPL_EVENT_HANDLER(progress) 1.64 + 1.65 + NS_DECL_NSITIMERCALLBACK 1.66 + 1.67 + NS_DECL_NSISTREAMLISTENER 1.68 + 1.69 + NS_DECL_NSIREQUESTOBSERVER 1.70 + 1.71 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileIOObject, DOMEventTargetHelper) 1.72 + 1.73 +protected: 1.74 + // Implemented by the derived class to do whatever it needs to do for abort 1.75 + virtual void DoAbort(nsAString& aEvent) = 0; 1.76 + // for onStartRequest (this has a default impl since FileReader doesn't need 1.77 + // special handling 1.78 + NS_IMETHOD DoOnStartRequest(nsIRequest *aRequest, nsISupports *aContext); 1.79 + // for onStopRequest 1.80 + NS_IMETHOD DoOnStopRequest(nsIRequest *aRequest, nsISupports *aContext, 1.81 + nsresult aStatus, nsAString& aSuccessEvent, 1.82 + nsAString& aTerminationEvent) = 0; 1.83 + // and for onDataAvailable 1.84 + NS_IMETHOD DoOnDataAvailable(nsIRequest *aRequest, nsISupports *aContext, 1.85 + nsIInputStream *aInputStream, uint64_t aOffset, 1.86 + uint32_t aCount) = 0; 1.87 + 1.88 + void StartProgressEventTimer(); 1.89 + void ClearProgressEventTimer(); 1.90 + void DispatchError(nsresult rv, nsAString& finalEvent); 1.91 + nsresult DispatchProgressEvent(const nsAString& aType); 1.92 + 1.93 + nsCOMPtr<nsITimer> mProgressNotifier; 1.94 + bool mProgressEventWasDelayed; 1.95 + bool mTimerIsActive; 1.96 + 1.97 + nsRefPtr<DOMError> mError; 1.98 + nsCOMPtr<nsIChannel> mChannel; 1.99 + 1.100 + uint16_t mReadyState; 1.101 + 1.102 + uint64_t mTotal; 1.103 + uint64_t mTransferred; 1.104 +}; 1.105 + 1.106 +} // namespace dom 1.107 +} // namespace mozilla 1.108 + 1.109 +#endif