FileHandleBase.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 "FileHandleBase.h"
  6. #include "ActorsChild.h"
  7. #include "BackgroundChildImpl.h"
  8. #include "FileRequestBase.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/ErrorResult.h"
  11. #include "mozilla/dom/File.h"
  12. #include "mozilla/dom/PBackgroundFileHandle.h"
  13. #include "mozilla/dom/UnionConversions.h"
  14. #include "mozilla/ipc/BackgroundChild.h"
  15. #include "MutableFileBase.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsDebug.h"
  18. #include "nsError.h"
  19. #include "nsString.h"
  20. namespace mozilla {
  21. namespace dom {
  22. using namespace mozilla::ipc;
  23. FileHandleBase::FileHandleBase(DEBUGONLY(PRThread* aOwningThread,)
  24. FileMode aMode)
  25. : RefCountedThreadObject(DEBUGONLY(aOwningThread))
  26. , mBackgroundActor(nullptr)
  27. , mLocation(0)
  28. , mPendingRequestCount(0)
  29. , mReadyState(INITIAL)
  30. , mMode(aMode)
  31. , mAborted(false)
  32. , mCreating(false)
  33. DEBUGONLY(, mSentFinishOrAbort(false))
  34. DEBUGONLY(, mFiredCompleteOrAbort(false))
  35. {
  36. AssertIsOnOwningThread();
  37. }
  38. FileHandleBase::~FileHandleBase()
  39. {
  40. AssertIsOnOwningThread();
  41. MOZ_ASSERT(!mPendingRequestCount);
  42. MOZ_ASSERT(!mCreating);
  43. MOZ_ASSERT(mSentFinishOrAbort);
  44. MOZ_ASSERT_IF(mBackgroundActor, mFiredCompleteOrAbort);
  45. if (mBackgroundActor) {
  46. mBackgroundActor->SendDeleteMeInternal();
  47. MOZ_ASSERT(!mBackgroundActor, "SendDeleteMeInternal should have cleared!");
  48. }
  49. }
  50. // static
  51. FileHandleBase*
  52. FileHandleBase::GetCurrent()
  53. {
  54. MOZ_ASSERT(BackgroundChild::GetForCurrentThread());
  55. BackgroundChildImpl::ThreadLocal* threadLocal =
  56. BackgroundChildImpl::GetThreadLocalForCurrentThread();
  57. MOZ_ASSERT(threadLocal);
  58. return threadLocal->mCurrentFileHandle;
  59. }
  60. void
  61. FileHandleBase::SetBackgroundActor(BackgroundFileHandleChild* aActor)
  62. {
  63. AssertIsOnOwningThread();
  64. MOZ_ASSERT(aActor);
  65. MOZ_ASSERT(!mBackgroundActor);
  66. mBackgroundActor = aActor;
  67. }
  68. void
  69. FileHandleBase::StartRequest(FileRequestBase* aFileRequest,
  70. const FileRequestParams& aParams)
  71. {
  72. AssertIsOnOwningThread();
  73. MOZ_ASSERT(aFileRequest);
  74. MOZ_ASSERT(aParams.type() != FileRequestParams::T__None);
  75. BackgroundFileRequestChild* actor =
  76. new BackgroundFileRequestChild(DEBUGONLY(mBackgroundActor->OwningThread(),)
  77. aFileRequest);
  78. mBackgroundActor->SendPBackgroundFileRequestConstructor(actor, aParams);
  79. // Balanced in BackgroundFileRequestChild::Recv__delete__().
  80. OnNewRequest();
  81. }
  82. void
  83. FileHandleBase::OnNewRequest()
  84. {
  85. AssertIsOnOwningThread();
  86. if (!mPendingRequestCount) {
  87. MOZ_ASSERT(mReadyState == INITIAL);
  88. mReadyState = LOADING;
  89. }
  90. ++mPendingRequestCount;
  91. }
  92. void
  93. FileHandleBase::OnRequestFinished(bool aActorDestroyedNormally)
  94. {
  95. AssertIsOnOwningThread();
  96. MOZ_ASSERT(mPendingRequestCount);
  97. --mPendingRequestCount;
  98. if (!mPendingRequestCount && !MutableFile()->IsInvalidated()) {
  99. mReadyState = FINISHING;
  100. if (aActorDestroyedNormally) {
  101. if (!mAborted) {
  102. SendFinish();
  103. } else {
  104. SendAbort();
  105. }
  106. } else {
  107. // Don't try to send any more messages to the parent if the request actor
  108. // was killed.
  109. #ifdef DEBUG
  110. MOZ_ASSERT(!mSentFinishOrAbort);
  111. mSentFinishOrAbort = true;
  112. #endif
  113. }
  114. }
  115. }
  116. bool
  117. FileHandleBase::IsOpen() const
  118. {
  119. AssertIsOnOwningThread();
  120. // If we haven't started anything then we're open.
  121. if (mReadyState == INITIAL) {
  122. return true;
  123. }
  124. // If we've already started then we need to check to see if we still have the
  125. // mCreating flag set. If we do (i.e. we haven't returned to the event loop
  126. // from the time we were created) then we are open. Otherwise check the
  127. // currently running file handles to see if it's the same. We only allow other
  128. // requests to be made if this file handle is currently running.
  129. if (mReadyState == LOADING && (mCreating || GetCurrent() == this)) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. void
  135. FileHandleBase::Abort()
  136. {
  137. AssertIsOnOwningThread();
  138. if (IsFinishingOrDone()) {
  139. // Already started (and maybe finished) the finish or abort so there is
  140. // nothing to do here.
  141. return;
  142. }
  143. const bool isInvalidated = MutableFile()->IsInvalidated();
  144. bool needToSendAbort = mReadyState == INITIAL && !isInvalidated;
  145. #ifdef DEBUG
  146. if (isInvalidated) {
  147. mSentFinishOrAbort = true;
  148. }
  149. #endif
  150. mAborted = true;
  151. mReadyState = DONE;
  152. // Fire the abort event if there are no outstanding requests. Otherwise the
  153. // abort event will be fired when all outstanding requests finish.
  154. if (needToSendAbort) {
  155. SendAbort();
  156. }
  157. }
  158. already_AddRefed<FileRequestBase>
  159. FileHandleBase::Read(uint64_t aSize, bool aHasEncoding,
  160. const nsAString& aEncoding, ErrorResult& aRv)
  161. {
  162. AssertIsOnOwningThread();
  163. // State and argument checking for read
  164. if (!CheckStateAndArgumentsForRead(aSize, aRv)) {
  165. return nullptr;
  166. }
  167. // Do nothing if the window is closed
  168. if (!CheckWindow()) {
  169. return nullptr;
  170. }
  171. FileRequestReadParams params;
  172. params.offset() = mLocation;
  173. params.size() = aSize;
  174. RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
  175. if (aHasEncoding) {
  176. fileRequest->SetEncoding(aEncoding);
  177. }
  178. StartRequest(fileRequest, params);
  179. mLocation += aSize;
  180. return fileRequest.forget();
  181. }
  182. already_AddRefed<FileRequestBase>
  183. FileHandleBase::Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv)
  184. {
  185. AssertIsOnOwningThread();
  186. // State checking for write
  187. if (!CheckStateForWrite(aRv)) {
  188. return nullptr;
  189. }
  190. // Getting location and additional state checking for truncate
  191. uint64_t location;
  192. if (aSize.WasPassed()) {
  193. // Just in case someone calls us from C++
  194. MOZ_ASSERT(aSize.Value() != UINT64_MAX, "Passed wrong size!");
  195. location = aSize.Value();
  196. } else {
  197. if (mLocation == UINT64_MAX) {
  198. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
  199. return nullptr;
  200. }
  201. location = mLocation;
  202. }
  203. // Do nothing if the window is closed
  204. if (!CheckWindow()) {
  205. return nullptr;
  206. }
  207. FileRequestTruncateParams params;
  208. params.offset() = location;
  209. RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
  210. StartRequest(fileRequest, params);
  211. if (aSize.WasPassed()) {
  212. mLocation = aSize.Value();
  213. }
  214. return fileRequest.forget();
  215. }
  216. already_AddRefed<FileRequestBase>
  217. FileHandleBase::Flush(ErrorResult& aRv)
  218. {
  219. AssertIsOnOwningThread();
  220. // State checking for write
  221. if (!CheckStateForWrite(aRv)) {
  222. return nullptr;
  223. }
  224. // Do nothing if the window is closed
  225. if (!CheckWindow()) {
  226. return nullptr;
  227. }
  228. FileRequestFlushParams params;
  229. RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
  230. StartRequest(fileRequest, params);
  231. return fileRequest.forget();
  232. }
  233. void
  234. FileHandleBase::Abort(ErrorResult& aRv)
  235. {
  236. AssertIsOnOwningThread();
  237. // This method is special enough for not using generic state checking methods.
  238. if (IsFinishingOrDone()) {
  239. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
  240. return;
  241. }
  242. Abort();
  243. }
  244. void
  245. FileHandleBase::HandleCompleteOrAbort(bool aAborted)
  246. {
  247. AssertIsOnOwningThread();
  248. MOZ_ASSERT(!mFiredCompleteOrAbort);
  249. mReadyState = DONE;
  250. DEBUGONLY(mFiredCompleteOrAbort = true;)
  251. }
  252. void
  253. FileHandleBase::OnReturnToEventLoop()
  254. {
  255. AssertIsOnOwningThread();
  256. // We're back at the event loop, no longer newborn.
  257. mCreating = false;
  258. // Maybe finish if there were no requests generated.
  259. if (mReadyState == INITIAL) {
  260. mReadyState = DONE;
  261. SendFinish();
  262. }
  263. }
  264. bool
  265. FileHandleBase::CheckState(ErrorResult& aRv)
  266. {
  267. if (!IsOpen()) {
  268. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_INACTIVE_ERR);
  269. return false;
  270. }
  271. return true;
  272. }
  273. bool
  274. FileHandleBase::CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv)
  275. {
  276. // Common state checking
  277. if (!CheckState(aRv)) {
  278. return false;
  279. }
  280. // Additional state checking for read
  281. if (mLocation == UINT64_MAX) {
  282. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
  283. return false;
  284. }
  285. // Argument checking for read
  286. if (!aSize) {
  287. aRv.ThrowTypeError<MSG_INVALID_READ_SIZE>();
  288. return false;
  289. }
  290. return true;
  291. }
  292. bool
  293. FileHandleBase::CheckStateForWrite(ErrorResult& aRv)
  294. {
  295. // Common state checking
  296. if (!CheckState(aRv)) {
  297. return false;
  298. }
  299. // Additional state checking for write
  300. if (mMode != FileMode::Readwrite) {
  301. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR);
  302. return false;
  303. }
  304. return true;
  305. }
  306. bool
  307. FileHandleBase::CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv)
  308. {
  309. // State checking for write
  310. if (!CheckStateForWrite(aRv)) {
  311. return false;
  312. }
  313. // Additional state checking for write
  314. if (!aAppend && mLocation == UINT64_MAX) {
  315. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
  316. return false;
  317. }
  318. return true;
  319. }
  320. already_AddRefed<FileRequestBase>
  321. FileHandleBase::WriteOrAppend(
  322. const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
  323. bool aAppend,
  324. ErrorResult& aRv)
  325. {
  326. AssertIsOnOwningThread();
  327. if (aValue.IsString()) {
  328. return WriteOrAppend(aValue.GetAsString(), aAppend, aRv);
  329. }
  330. if (aValue.IsArrayBuffer()) {
  331. return WriteOrAppend(aValue.GetAsArrayBuffer(), aAppend, aRv);
  332. }
  333. if (aValue.IsArrayBufferView()) {
  334. return WriteOrAppend(aValue.GetAsArrayBufferView(), aAppend, aRv);
  335. }
  336. MOZ_ASSERT(aValue.IsBlob());
  337. return WriteOrAppend(aValue.GetAsBlob(), aAppend, aRv);
  338. }
  339. already_AddRefed<FileRequestBase>
  340. FileHandleBase::WriteOrAppend(const nsAString& aValue,
  341. bool aAppend,
  342. ErrorResult& aRv)
  343. {
  344. AssertIsOnOwningThread();
  345. // State checking for write or append
  346. if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
  347. return nullptr;
  348. }
  349. NS_ConvertUTF16toUTF8 cstr(aValue);
  350. uint64_t dataLength = cstr.Length();;
  351. if (!dataLength) {
  352. return nullptr;
  353. }
  354. FileRequestStringData stringData(cstr);
  355. // Do nothing if the window is closed
  356. if (!CheckWindow()) {
  357. return nullptr;
  358. }
  359. return WriteInternal(stringData, dataLength, aAppend, aRv);
  360. }
  361. already_AddRefed<FileRequestBase>
  362. FileHandleBase::WriteOrAppend(const ArrayBuffer& aValue,
  363. bool aAppend,
  364. ErrorResult& aRv)
  365. {
  366. AssertIsOnOwningThread();
  367. // State checking for write or append
  368. if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
  369. return nullptr;
  370. }
  371. aValue.ComputeLengthAndData();
  372. uint64_t dataLength = aValue.Length();;
  373. if (!dataLength) {
  374. return nullptr;
  375. }
  376. const char* data = reinterpret_cast<const char*>(aValue.Data());
  377. FileRequestStringData stringData;
  378. if (NS_WARN_IF(!stringData.string().Assign(data, aValue.Length(),
  379. fallible_t()))) {
  380. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  381. return nullptr;
  382. }
  383. // Do nothing if the window is closed
  384. if (!CheckWindow()) {
  385. return nullptr;
  386. }
  387. return WriteInternal(stringData, dataLength, aAppend, aRv);
  388. }
  389. already_AddRefed<FileRequestBase>
  390. FileHandleBase::WriteOrAppend(const ArrayBufferView& aValue,
  391. bool aAppend,
  392. ErrorResult& aRv)
  393. {
  394. AssertIsOnOwningThread();
  395. // State checking for write or append
  396. if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
  397. return nullptr;
  398. }
  399. aValue.ComputeLengthAndData();
  400. uint64_t dataLength = aValue.Length();;
  401. if (!dataLength) {
  402. return nullptr;
  403. }
  404. const char* data = reinterpret_cast<const char*>(aValue.Data());
  405. FileRequestStringData stringData;
  406. if (NS_WARN_IF(!stringData.string().Assign(data, aValue.Length(),
  407. fallible_t()))) {
  408. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  409. return nullptr;
  410. }
  411. // Do nothing if the window is closed
  412. if (!CheckWindow()) {
  413. return nullptr;
  414. }
  415. return WriteInternal(stringData, dataLength, aAppend, aRv);
  416. }
  417. already_AddRefed<FileRequestBase>
  418. FileHandleBase::WriteOrAppend(Blob& aValue,
  419. bool aAppend,
  420. ErrorResult& aRv)
  421. {
  422. AssertIsOnOwningThread();
  423. // State checking for write or append
  424. if (!CheckStateForWriteOrAppend(aAppend, aRv)) {
  425. return nullptr;
  426. }
  427. ErrorResult rv;
  428. uint64_t dataLength = aValue.GetSize(rv);
  429. if (NS_WARN_IF(rv.Failed())) {
  430. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  431. return nullptr;
  432. }
  433. if (!dataLength) {
  434. return nullptr;
  435. }
  436. PBackgroundChild* backgroundActor = BackgroundChild::GetForCurrentThread();
  437. MOZ_ASSERT(backgroundActor);
  438. PBlobChild* blobActor =
  439. BackgroundChild::GetOrCreateActorForBlob(backgroundActor, &aValue);
  440. if (NS_WARN_IF(!blobActor)) {
  441. aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  442. return nullptr;
  443. }
  444. FileRequestBlobData blobData;
  445. blobData.blobChild() = blobActor;
  446. // Do nothing if the window is closed
  447. if (!CheckWindow()) {
  448. return nullptr;
  449. }
  450. return WriteInternal(blobData, dataLength, aAppend, aRv);
  451. }
  452. already_AddRefed<FileRequestBase>
  453. FileHandleBase::WriteInternal(const FileRequestData& aData,
  454. uint64_t aDataLength,
  455. bool aAppend,
  456. ErrorResult& aRv)
  457. {
  458. AssertIsOnOwningThread();
  459. DebugOnly<ErrorResult> error;
  460. MOZ_ASSERT(CheckStateForWrite(error));
  461. MOZ_ASSERT_IF(!aAppend, mLocation != UINT64_MAX);
  462. MOZ_ASSERT(aDataLength);
  463. MOZ_ASSERT(CheckWindow());
  464. FileRequestWriteParams params;
  465. params.offset() = aAppend ? UINT64_MAX : mLocation;
  466. params.data() = aData;
  467. params.dataLength() = aDataLength;
  468. RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();
  469. MOZ_ASSERT(fileRequest);
  470. StartRequest(fileRequest, params);
  471. if (aAppend) {
  472. mLocation = UINT64_MAX;
  473. }
  474. else {
  475. mLocation += aDataLength;
  476. }
  477. return fileRequest.forget();
  478. }
  479. void
  480. FileHandleBase::SendFinish()
  481. {
  482. AssertIsOnOwningThread();
  483. MOZ_ASSERT(!mAborted);
  484. MOZ_ASSERT(IsFinishingOrDone());
  485. MOZ_ASSERT(!mSentFinishOrAbort);
  486. MOZ_ASSERT(!mPendingRequestCount);
  487. MOZ_ASSERT(mBackgroundActor);
  488. mBackgroundActor->SendFinish();
  489. DEBUGONLY(mSentFinishOrAbort = true;)
  490. }
  491. void
  492. FileHandleBase::SendAbort()
  493. {
  494. AssertIsOnOwningThread();
  495. MOZ_ASSERT(mAborted);
  496. MOZ_ASSERT(IsFinishingOrDone());
  497. MOZ_ASSERT(!mSentFinishOrAbort);
  498. MOZ_ASSERT(mBackgroundActor);
  499. mBackgroundActor->SendAbort();
  500. DEBUGONLY(mSentFinishOrAbort = true;)
  501. }
  502. } // namespace dom
  503. } // namespace mozilla