WebBrowserPersistSerializeParent.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "WebBrowserPersistSerializeParent.h"
  7. #include "nsReadableUtils.h"
  8. #include "nsThreadUtils.h"
  9. namespace mozilla {
  10. WebBrowserPersistSerializeParent::WebBrowserPersistSerializeParent(
  11. nsIWebBrowserPersistDocument* aDocument,
  12. nsIOutputStream* aStream,
  13. nsIWebBrowserPersistWriteCompletion* aFinish)
  14. : mDocument(aDocument)
  15. , mStream(aStream)
  16. , mFinish(aFinish)
  17. , mOutputError(NS_OK)
  18. {
  19. MOZ_ASSERT(aDocument);
  20. MOZ_ASSERT(aStream);
  21. MOZ_ASSERT(aFinish);
  22. }
  23. WebBrowserPersistSerializeParent::~WebBrowserPersistSerializeParent()
  24. {
  25. }
  26. bool
  27. WebBrowserPersistSerializeParent::RecvWriteData(nsTArray<uint8_t>&& aData)
  28. {
  29. if (NS_FAILED(mOutputError)) {
  30. return true;
  31. }
  32. uint32_t written = 0;
  33. static_assert(sizeof(char) == sizeof(uint8_t),
  34. "char must be (at least?) 8 bits");
  35. const char* data = reinterpret_cast<const char*>(aData.Elements());
  36. // nsIOutputStream::Write is allowed to return short writes.
  37. while (written < aData.Length()) {
  38. uint32_t writeReturn;
  39. nsresult rv = mStream->Write(data + written,
  40. aData.Length() - written,
  41. &writeReturn);
  42. if (NS_FAILED(rv)) {
  43. mOutputError = rv;
  44. return true;
  45. }
  46. written += writeReturn;
  47. }
  48. return true;
  49. }
  50. bool
  51. WebBrowserPersistSerializeParent::Recv__delete__(const nsCString& aContentType,
  52. const nsresult& aStatus)
  53. {
  54. if (NS_SUCCEEDED(mOutputError)) {
  55. mOutputError = aStatus;
  56. }
  57. mFinish->OnFinish(mDocument,
  58. mStream,
  59. aContentType,
  60. mOutputError);
  61. mFinish = nullptr;
  62. return true;
  63. }
  64. void
  65. WebBrowserPersistSerializeParent::ActorDestroy(ActorDestroyReason aWhy)
  66. {
  67. if (mFinish) {
  68. MOZ_ASSERT(aWhy != Deletion);
  69. // See comment in WebBrowserPersistDocumentParent::ActorDestroy
  70. // (or bug 1202887) for why this is deferred.
  71. nsCOMPtr<nsIRunnable> errorLater = NewRunnableMethod
  72. <nsCOMPtr<nsIWebBrowserPersistDocument>, nsCOMPtr<nsIOutputStream>,
  73. nsCString, nsresult>
  74. (mFinish, &nsIWebBrowserPersistWriteCompletion::OnFinish,
  75. mDocument, mStream, EmptyCString(), NS_ERROR_FAILURE);
  76. NS_DispatchToCurrentThread(errorLater);
  77. mFinish = nullptr;
  78. }
  79. }
  80. } // namespace mozilla