IDBMutableFile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "IDBMutableFile.h"
  6. #include "ActorsChild.h"
  7. #include "FileInfo.h"
  8. #include "FileSnapshot.h"
  9. #include "IDBDatabase.h"
  10. #include "IDBFactory.h"
  11. #include "IDBFileHandle.h"
  12. #include "IDBFileRequest.h"
  13. #include "IndexedDatabaseManager.h"
  14. #include "MainThreadUtils.h"
  15. #include "mozilla/Assertions.h"
  16. #include "mozilla/ErrorResult.h"
  17. #include "mozilla/dom/File.h"
  18. #include "mozilla/dom/IDBMutableFileBinding.h"
  19. #include "mozilla/dom/filehandle/ActorsChild.h"
  20. #include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
  21. #include "mozilla/dom/quota/FileStreams.h"
  22. #include "mozilla/dom/quota/QuotaManager.h"
  23. #include "mozilla/ipc/BackgroundUtils.h"
  24. #include "mozilla/ipc/PBackgroundSharedTypes.h"
  25. #include "nsContentUtils.h"
  26. #include "nsDebug.h"
  27. #include "nsError.h"
  28. #include "nsIInputStream.h"
  29. #include "nsIPrincipal.h"
  30. #include "ReportInternalError.h"
  31. namespace mozilla {
  32. namespace dom {
  33. using namespace mozilla::dom::indexedDB;
  34. using namespace mozilla::dom::quota;
  35. IDBMutableFile::IDBMutableFile(IDBDatabase* aDatabase,
  36. BackgroundMutableFileChild* aActor,
  37. const nsAString& aName,
  38. const nsAString& aType)
  39. : DOMEventTargetHelper(aDatabase)
  40. , MutableFileBase(DEBUGONLY(aDatabase->OwningThread(),)
  41. aActor)
  42. , mDatabase(aDatabase)
  43. , mName(aName)
  44. , mType(aType)
  45. , mInvalidated(false)
  46. {
  47. AssertIsOnOwningThread();
  48. MOZ_ASSERT(aDatabase);
  49. aDatabase->AssertIsOnOwningThread();
  50. mDatabase->NoteLiveMutableFile(this);
  51. }
  52. IDBMutableFile::~IDBMutableFile()
  53. {
  54. AssertIsOnOwningThread();
  55. mDatabase->NoteFinishedMutableFile(this);
  56. }
  57. int64_t
  58. IDBMutableFile::GetFileId() const
  59. {
  60. AssertIsOnOwningThread();
  61. int64_t fileId;
  62. if (!mBackgroundActor ||
  63. NS_WARN_IF(!mBackgroundActor->SendGetFileId(&fileId))) {
  64. return -1;
  65. }
  66. return fileId;
  67. }
  68. void
  69. IDBMutableFile::Invalidate()
  70. {
  71. AssertIsOnOwningThread();
  72. MOZ_ASSERT(!mInvalidated);
  73. mInvalidated = true;
  74. AbortFileHandles();
  75. }
  76. void
  77. IDBMutableFile::RegisterFileHandle(IDBFileHandle* aFileHandle)
  78. {
  79. AssertIsOnOwningThread();
  80. MOZ_ASSERT(aFileHandle);
  81. aFileHandle->AssertIsOnOwningThread();
  82. MOZ_ASSERT(!mFileHandles.Contains(aFileHandle));
  83. mFileHandles.PutEntry(aFileHandle);
  84. }
  85. void
  86. IDBMutableFile::UnregisterFileHandle(IDBFileHandle* aFileHandle)
  87. {
  88. AssertIsOnOwningThread();
  89. MOZ_ASSERT(aFileHandle);
  90. aFileHandle->AssertIsOnOwningThread();
  91. MOZ_ASSERT(mFileHandles.Contains(aFileHandle));
  92. mFileHandles.RemoveEntry(aFileHandle);
  93. }
  94. void
  95. IDBMutableFile::AbortFileHandles()
  96. {
  97. AssertIsOnOwningThread();
  98. class MOZ_STACK_CLASS Helper final
  99. {
  100. public:
  101. static void
  102. AbortFileHandles(nsTHashtable<nsPtrHashKey<IDBFileHandle>>& aTable)
  103. {
  104. if (!aTable.Count()) {
  105. return;
  106. }
  107. nsTArray<RefPtr<IDBFileHandle>> fileHandlesToAbort;
  108. fileHandlesToAbort.SetCapacity(aTable.Count());
  109. for (auto iter = aTable.Iter(); !iter.Done(); iter.Next()) {
  110. IDBFileHandle* fileHandle = iter.Get()->GetKey();
  111. MOZ_ASSERT(fileHandle);
  112. fileHandle->AssertIsOnOwningThread();
  113. if (!fileHandle->IsDone()) {
  114. fileHandlesToAbort.AppendElement(iter.Get()->GetKey());
  115. }
  116. }
  117. MOZ_ASSERT(fileHandlesToAbort.Length() <= aTable.Count());
  118. if (fileHandlesToAbort.IsEmpty()) {
  119. return;
  120. }
  121. for (RefPtr<IDBFileHandle>& fileHandle : fileHandlesToAbort) {
  122. MOZ_ASSERT(fileHandle);
  123. fileHandle->Abort();
  124. }
  125. }
  126. };
  127. Helper::AbortFileHandles(mFileHandles);
  128. }
  129. IDBDatabase*
  130. IDBMutableFile::Database() const
  131. {
  132. AssertIsOnOwningThread();
  133. return mDatabase;
  134. }
  135. already_AddRefed<IDBFileHandle>
  136. IDBMutableFile::Open(FileMode aMode, ErrorResult& aError)
  137. {
  138. AssertIsOnOwningThread();
  139. if (QuotaManager::IsShuttingDown() ||
  140. mDatabase->IsClosed() ||
  141. !GetOwner()) {
  142. aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  143. return nullptr;
  144. }
  145. RefPtr<IDBFileHandle> fileHandle =
  146. IDBFileHandle::Create(this, aMode);
  147. if (NS_WARN_IF(!fileHandle)) {
  148. aError.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  149. return nullptr;
  150. }
  151. BackgroundFileHandleChild* actor =
  152. new BackgroundFileHandleChild(DEBUGONLY(mBackgroundActor->OwningThread(),)
  153. fileHandle);
  154. MOZ_ALWAYS_TRUE(
  155. mBackgroundActor->SendPBackgroundFileHandleConstructor(actor, aMode));
  156. fileHandle->SetBackgroundActor(actor);
  157. return fileHandle.forget();
  158. }
  159. already_AddRefed<DOMRequest>
  160. IDBMutableFile::GetFile(ErrorResult& aError)
  161. {
  162. RefPtr<IDBFileHandle> fileHandle = Open(FileMode::Readonly, aError);
  163. if (NS_WARN_IF(aError.Failed())) {
  164. return nullptr;
  165. }
  166. FileRequestGetFileParams params;
  167. RefPtr<IDBFileRequest> request =
  168. IDBFileRequest::Create(GetOwner(),
  169. fileHandle,
  170. /* aWrapAsDOMRequest */ true);
  171. fileHandle->StartRequest(request, params);
  172. return request.forget();
  173. }
  174. NS_IMPL_ADDREF_INHERITED(IDBMutableFile, DOMEventTargetHelper)
  175. NS_IMPL_RELEASE_INHERITED(IDBMutableFile, DOMEventTargetHelper)
  176. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBMutableFile)
  177. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  178. NS_IMPL_CYCLE_COLLECTION_CLASS(IDBMutableFile)
  179. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBMutableFile,
  180. DOMEventTargetHelper)
  181. tmp->AssertIsOnOwningThread();
  182. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDatabase)
  183. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  184. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBMutableFile,
  185. DOMEventTargetHelper)
  186. tmp->AssertIsOnOwningThread();
  187. // Don't unlink mDatabase!
  188. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  189. JSObject*
  190. IDBMutableFile::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  191. {
  192. return IDBMutableFileBinding::Wrap(aCx, this, aGivenProto);
  193. }
  194. const nsString&
  195. IDBMutableFile::Name() const
  196. {
  197. AssertIsOnOwningThread();
  198. return mName;
  199. }
  200. const nsString&
  201. IDBMutableFile::Type() const
  202. {
  203. AssertIsOnOwningThread();
  204. return mType;
  205. }
  206. bool
  207. IDBMutableFile::IsInvalidated()
  208. {
  209. AssertIsOnOwningThread();
  210. return mInvalidated;
  211. }
  212. already_AddRefed<File>
  213. IDBMutableFile::CreateFileFor(BlobImpl* aBlobImpl,
  214. FileHandleBase* aFileHandle)
  215. {
  216. AssertIsOnOwningThread();
  217. RefPtr<BlobImpl> blobImplSnapshot =
  218. new BlobImplSnapshot(aBlobImpl, static_cast<IDBFileHandle*>(aFileHandle));
  219. RefPtr<File> file = File::Create(GetOwner(), blobImplSnapshot);
  220. return file.forget();
  221. }
  222. } // namespace dom
  223. } // namespace mozilla