IDBFileHandle.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "IDBFileHandle.h"
  6. #include "IDBEvents.h"
  7. #include "IDBMutableFile.h"
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/dom/IDBFileHandleBinding.h"
  10. #include "mozilla/dom/filehandle/ActorsChild.h"
  11. #include "mozilla/EventDispatcher.h"
  12. #include "nsContentUtils.h"
  13. #include "nsQueryObject.h"
  14. #include "nsServiceManagerUtils.h"
  15. #include "nsWidgetsCID.h"
  16. namespace mozilla {
  17. namespace dom {
  18. using namespace mozilla::dom::indexedDB;
  19. IDBFileHandle::IDBFileHandle(FileMode aMode,
  20. IDBMutableFile* aMutableFile)
  21. : FileHandleBase(DEBUGONLY(aMutableFile->OwningThread(),)
  22. aMode)
  23. , mMutableFile(aMutableFile)
  24. {
  25. AssertIsOnOwningThread();
  26. }
  27. IDBFileHandle::~IDBFileHandle()
  28. {
  29. AssertIsOnOwningThread();
  30. mMutableFile->UnregisterFileHandle(this);
  31. }
  32. // static
  33. already_AddRefed<IDBFileHandle>
  34. IDBFileHandle::Create(IDBMutableFile* aMutableFile,
  35. FileMode aMode)
  36. {
  37. MOZ_ASSERT(aMutableFile);
  38. aMutableFile->AssertIsOnOwningThread();
  39. MOZ_ASSERT(aMode == FileMode::Readonly || aMode == FileMode::Readwrite);
  40. RefPtr<IDBFileHandle> fileHandle =
  41. new IDBFileHandle(aMode, aMutableFile);
  42. fileHandle->BindToOwner(aMutableFile);
  43. // XXX Fix!
  44. MOZ_ASSERT(NS_IsMainThread(), "This won't work on non-main threads!");
  45. nsCOMPtr<nsIRunnable> runnable = do_QueryObject(fileHandle);
  46. nsContentUtils::RunInMetastableState(runnable.forget());
  47. fileHandle->SetCreating();
  48. aMutableFile->RegisterFileHandle(fileHandle);
  49. return fileHandle.forget();
  50. }
  51. already_AddRefed<IDBFileRequest>
  52. IDBFileHandle::GetMetadata(const IDBFileMetadataParameters& aParameters,
  53. ErrorResult& aRv)
  54. {
  55. AssertIsOnOwningThread();
  56. // Common state checking
  57. if (!CheckState(aRv)) {
  58. return nullptr;
  59. }
  60. // Argument checking for get metadata.
  61. if (!aParameters.mSize && !aParameters.mLastModified) {
  62. aRv.ThrowTypeError<MSG_METADATA_NOT_CONFIGURED>();
  63. return nullptr;
  64. }
  65. // Do nothing if the window is closed
  66. if (!CheckWindow()) {
  67. return nullptr;
  68. }
  69. FileRequestGetMetadataParams params;
  70. params.size() = aParameters.mSize;
  71. params.lastModified() = aParameters.mLastModified;
  72. RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
  73. StartRequest(fileRequest, params);
  74. return fileRequest.forget().downcast<IDBFileRequest>();
  75. }
  76. NS_IMPL_ADDREF_INHERITED(IDBFileHandle, DOMEventTargetHelper)
  77. NS_IMPL_RELEASE_INHERITED(IDBFileHandle, DOMEventTargetHelper)
  78. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileHandle)
  79. NS_INTERFACE_MAP_ENTRY(nsIRunnable)
  80. NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  81. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  82. NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFileHandle)
  83. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBFileHandle,
  84. DOMEventTargetHelper)
  85. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMutableFile)
  86. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  87. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBFileHandle,
  88. DOMEventTargetHelper)
  89. // Don't unlink mMutableFile!
  90. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  91. NS_IMETHODIMP
  92. IDBFileHandle::Run()
  93. {
  94. AssertIsOnOwningThread();
  95. OnReturnToEventLoop();
  96. return NS_OK;
  97. }
  98. nsresult
  99. IDBFileHandle::GetEventTargetParent(EventChainPreVisitor& aVisitor)
  100. {
  101. AssertIsOnOwningThread();
  102. aVisitor.mCanHandle = true;
  103. aVisitor.SetParentTarget(mMutableFile, false);
  104. return NS_OK;
  105. }
  106. // virtual
  107. JSObject*
  108. IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  109. {
  110. AssertIsOnOwningThread();
  111. return IDBFileHandleBinding::Wrap(aCx, this, aGivenProto);
  112. }
  113. mozilla::dom::MutableFileBase*
  114. IDBFileHandle::MutableFile() const
  115. {
  116. AssertIsOnOwningThread();
  117. return mMutableFile;
  118. }
  119. void
  120. IDBFileHandle::HandleCompleteOrAbort(bool aAborted)
  121. {
  122. AssertIsOnOwningThread();
  123. FileHandleBase::HandleCompleteOrAbort(aAborted);
  124. nsCOMPtr<nsIDOMEvent> event;
  125. if (aAborted) {
  126. event = CreateGenericEvent(this, nsDependentString(kAbortEventType),
  127. eDoesBubble, eNotCancelable);
  128. } else {
  129. event = CreateGenericEvent(this, nsDependentString(kCompleteEventType),
  130. eDoesNotBubble, eNotCancelable);
  131. }
  132. if (NS_WARN_IF(!event)) {
  133. return;
  134. }
  135. bool dummy;
  136. if (NS_FAILED(DispatchEvent(event, &dummy))) {
  137. NS_WARNING("DispatchEvent failed!");
  138. }
  139. }
  140. bool
  141. IDBFileHandle::CheckWindow()
  142. {
  143. AssertIsOnOwningThread();
  144. return GetOwner();
  145. }
  146. already_AddRefed<mozilla::dom::FileRequestBase>
  147. IDBFileHandle::GenerateFileRequest()
  148. {
  149. AssertIsOnOwningThread();
  150. return IDBFileRequest::Create(GetOwner(), this,
  151. /* aWrapAsDOMRequest */ false);
  152. }
  153. } // namespace dom
  154. } // namespace mozilla