IDBFileHandle.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_idbfilehandle_h__
  6. #define mozilla_dom_idbfilehandle_h__
  7. #include "IDBFileRequest.h"
  8. #include "js/TypeDecls.h"
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/dom/FileHandleBase.h"
  11. #include "mozilla/DOMEventTargetHelper.h"
  12. #include "nsCycleCollectionParticipant.h"
  13. #include "nsIRunnable.h"
  14. #include "nsWeakReference.h"
  15. class nsPIDOMWindowInner;
  16. namespace mozilla {
  17. namespace dom {
  18. struct IDBFileMetadataParameters;
  19. class IDBFileRequest;
  20. class IDBMutableFile;
  21. class IDBFileHandle final
  22. : public DOMEventTargetHelper
  23. , public nsIRunnable
  24. , public FileHandleBase
  25. , public nsSupportsWeakReference
  26. {
  27. RefPtr<IDBMutableFile> mMutableFile;
  28. public:
  29. static already_AddRefed<IDBFileHandle>
  30. Create(IDBMutableFile* aMutableFile,
  31. FileMode aMode);
  32. // WebIDL
  33. nsPIDOMWindowInner*
  34. GetParentObject() const
  35. {
  36. AssertIsOnOwningThread();
  37. return GetOwner();
  38. }
  39. IDBMutableFile*
  40. GetMutableFile() const
  41. {
  42. AssertIsOnOwningThread();
  43. return mMutableFile;
  44. }
  45. IDBMutableFile*
  46. GetFileHandle() const
  47. {
  48. AssertIsOnOwningThread();
  49. return GetMutableFile();
  50. }
  51. already_AddRefed<IDBFileRequest>
  52. GetMetadata(const IDBFileMetadataParameters& aParameters, ErrorResult& aRv);
  53. already_AddRefed<IDBFileRequest>
  54. ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv)
  55. {
  56. AssertIsOnOwningThread();
  57. return Read(aSize, false, NullString(), aRv).downcast<IDBFileRequest>();
  58. }
  59. already_AddRefed<IDBFileRequest>
  60. ReadAsText(uint64_t aSize, const nsAString& aEncoding, ErrorResult& aRv)
  61. {
  62. AssertIsOnOwningThread();
  63. return Read(aSize, true, aEncoding, aRv).downcast<IDBFileRequest>();
  64. }
  65. already_AddRefed<IDBFileRequest>
  66. Write(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
  67. ErrorResult& aRv)
  68. {
  69. AssertIsOnOwningThread();
  70. return WriteOrAppend(aValue, false, aRv).downcast<IDBFileRequest>();
  71. }
  72. already_AddRefed<IDBFileRequest>
  73. Append(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
  74. ErrorResult& aRv)
  75. {
  76. AssertIsOnOwningThread();
  77. return WriteOrAppend(aValue, true, aRv).downcast<IDBFileRequest>();
  78. }
  79. already_AddRefed<IDBFileRequest>
  80. Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv)
  81. {
  82. AssertIsOnOwningThread();
  83. return FileHandleBase::Truncate(aSize, aRv).downcast<IDBFileRequest>();
  84. }
  85. already_AddRefed<IDBFileRequest>
  86. Flush(ErrorResult& aRv)
  87. {
  88. AssertIsOnOwningThread();
  89. return FileHandleBase::Flush(aRv).downcast<IDBFileRequest>();
  90. }
  91. IMPL_EVENT_HANDLER(complete)
  92. IMPL_EVENT_HANDLER(abort)
  93. IMPL_EVENT_HANDLER(error)
  94. NS_DECL_ISUPPORTS_INHERITED
  95. NS_DECL_NSIRUNNABLE
  96. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileHandle, DOMEventTargetHelper)
  97. // nsIDOMEventTarget
  98. virtual nsresult
  99. GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  100. // WrapperCache
  101. virtual JSObject*
  102. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  103. // FileHandleBase
  104. virtual MutableFileBase*
  105. MutableFile() const override;
  106. virtual void
  107. HandleCompleteOrAbort(bool aAborted) override;
  108. private:
  109. IDBFileHandle(FileMode aMode,
  110. IDBMutableFile* aMutableFile);
  111. ~IDBFileHandle();
  112. // FileHandleBase
  113. virtual bool
  114. CheckWindow() override;
  115. virtual already_AddRefed<FileRequestBase>
  116. GenerateFileRequest() override;
  117. };
  118. } // namespace dom
  119. } // namespace mozilla
  120. #endif // mozilla_dom_idbfilehandle_h__