WebBrowserPersistDocumentParent.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "WebBrowserPersistDocumentParent.h"
  7. #include "mozilla/ipc/InputStreamUtils.h"
  8. #include "nsIInputStream.h"
  9. #include "nsThreadUtils.h"
  10. #include "WebBrowserPersistResourcesParent.h"
  11. #include "WebBrowserPersistSerializeParent.h"
  12. #include "WebBrowserPersistRemoteDocument.h"
  13. namespace mozilla {
  14. WebBrowserPersistDocumentParent::WebBrowserPersistDocumentParent()
  15. : mReflection(nullptr)
  16. {
  17. }
  18. void
  19. WebBrowserPersistDocumentParent::SetOnReady(nsIWebBrowserPersistDocumentReceiver* aOnReady)
  20. {
  21. MOZ_ASSERT(aOnReady);
  22. MOZ_ASSERT(!mOnReady);
  23. MOZ_ASSERT(!mReflection);
  24. mOnReady = aOnReady;
  25. }
  26. void
  27. WebBrowserPersistDocumentParent::ActorDestroy(ActorDestroyReason aWhy)
  28. {
  29. if (mReflection) {
  30. mReflection->ActorDestroy();
  31. mReflection = nullptr;
  32. }
  33. if (mOnReady) {
  34. // Bug 1202887: If this is part of a subtree destruction, then
  35. // anything which could cause another actor in that subtree to
  36. // be Send__delete__()ed will cause use-after-free -- such as
  37. // dropping the last reference to another document's
  38. // WebBrowserPersistRemoteDocument. To avoid that, defer the
  39. // callback until after the entire subtree is destroyed.
  40. nsCOMPtr<nsIRunnable> errorLater = NewRunnableMethod
  41. <nsresult>(mOnReady, &nsIWebBrowserPersistDocumentReceiver::OnError,
  42. NS_ERROR_FAILURE);
  43. NS_DispatchToCurrentThread(errorLater);
  44. mOnReady = nullptr;
  45. }
  46. }
  47. WebBrowserPersistDocumentParent::~WebBrowserPersistDocumentParent()
  48. {
  49. MOZ_RELEASE_ASSERT(!mReflection);
  50. MOZ_ASSERT(!mOnReady);
  51. }
  52. bool
  53. WebBrowserPersistDocumentParent::RecvAttributes(const Attrs& aAttrs,
  54. const OptionalInputStreamParams& aPostData,
  55. nsTArray<FileDescriptor>&& aPostFiles)
  56. {
  57. // Deserialize the postData unconditionally so that fds aren't leaked.
  58. nsCOMPtr<nsIInputStream> postData =
  59. ipc::DeserializeInputStream(aPostData, aPostFiles);
  60. if (!mOnReady || mReflection) {
  61. return false;
  62. }
  63. mReflection = new WebBrowserPersistRemoteDocument(this, aAttrs, postData);
  64. RefPtr<WebBrowserPersistRemoteDocument> reflection = mReflection;
  65. mOnReady->OnDocumentReady(reflection);
  66. mOnReady = nullptr;
  67. return true;
  68. }
  69. bool
  70. WebBrowserPersistDocumentParent::RecvInitFailure(const nsresult& aFailure)
  71. {
  72. if (!mOnReady || mReflection) {
  73. return false;
  74. }
  75. mOnReady->OnError(aFailure);
  76. mOnReady = nullptr;
  77. // Warning: Send__delete__ deallocates this object.
  78. return Send__delete__(this);
  79. }
  80. PWebBrowserPersistResourcesParent*
  81. WebBrowserPersistDocumentParent::AllocPWebBrowserPersistResourcesParent()
  82. {
  83. MOZ_CRASH("Don't use this; construct the actor directly and AddRef.");
  84. return nullptr;
  85. }
  86. bool
  87. WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistResourcesParent(PWebBrowserPersistResourcesParent* aActor)
  88. {
  89. // Turn the ref held by IPC back into an nsRefPtr.
  90. RefPtr<WebBrowserPersistResourcesParent> actor =
  91. already_AddRefed<WebBrowserPersistResourcesParent>(
  92. static_cast<WebBrowserPersistResourcesParent*>(aActor));
  93. return true;
  94. }
  95. PWebBrowserPersistSerializeParent*
  96. WebBrowserPersistDocumentParent::AllocPWebBrowserPersistSerializeParent(
  97. const WebBrowserPersistURIMap& aMap,
  98. const nsCString& aRequestedContentType,
  99. const uint32_t& aEncoderFlags,
  100. const uint32_t& aWrapColumn)
  101. {
  102. MOZ_CRASH("Don't use this; construct the actor directly.");
  103. return nullptr;
  104. }
  105. bool
  106. WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistSerializeParent(PWebBrowserPersistSerializeParent* aActor)
  107. {
  108. delete aActor;
  109. return true;
  110. }
  111. } // namespace mozilla