nsIContentChild.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "nsIContentChild.h"
  6. #include "mozilla/dom/ContentChild.h"
  7. #include "mozilla/dom/DOMTypes.h"
  8. #include "mozilla/dom/File.h"
  9. #include "mozilla/dom/PermissionMessageUtils.h"
  10. #include "mozilla/dom/TabChild.h"
  11. #include "mozilla/dom/ipc/BlobChild.h"
  12. #include "mozilla/dom/ipc/StructuredCloneData.h"
  13. #include "mozilla/ipc/FileDescriptorSetChild.h"
  14. #include "mozilla/ipc/InputStreamUtils.h"
  15. #include "mozilla/ipc/SendStream.h"
  16. #include "nsPrintfCString.h"
  17. #include "xpcpublic.h"
  18. using namespace mozilla::ipc;
  19. using namespace mozilla::jsipc;
  20. namespace mozilla {
  21. namespace dom {
  22. PJavaScriptChild*
  23. nsIContentChild::AllocPJavaScriptChild()
  24. {
  25. return NewJavaScriptChild();
  26. }
  27. bool
  28. nsIContentChild::DeallocPJavaScriptChild(PJavaScriptChild* aChild)
  29. {
  30. ReleaseJavaScriptChild(aChild);
  31. return true;
  32. }
  33. PBrowserChild*
  34. nsIContentChild::AllocPBrowserChild(const TabId& aTabId,
  35. const IPCTabContext& aContext,
  36. const uint32_t& aChromeFlags,
  37. const ContentParentId& aCpID,
  38. const bool& aIsForApp,
  39. const bool& aIsForBrowser)
  40. {
  41. // We'll happily accept any kind of IPCTabContext here; we don't need to
  42. // check that it's of a certain type for security purposes, because we
  43. // believe whatever the parent process tells us.
  44. MaybeInvalidTabContext tc(aContext);
  45. if (!tc.IsValid()) {
  46. NS_ERROR(nsPrintfCString("Received an invalid TabContext from "
  47. "the parent process. (%s) Crashing...",
  48. tc.GetInvalidReason()).get());
  49. MOZ_CRASH("Invalid TabContext received from the parent process.");
  50. }
  51. RefPtr<TabChild> child =
  52. TabChild::Create(this, aTabId, tc.GetTabContext(), aChromeFlags);
  53. // The ref here is released in DeallocPBrowserChild.
  54. return child.forget().take();
  55. }
  56. bool
  57. nsIContentChild::DeallocPBrowserChild(PBrowserChild* aIframe)
  58. {
  59. TabChild* child = static_cast<TabChild*>(aIframe);
  60. NS_RELEASE(child);
  61. return true;
  62. }
  63. PBlobChild*
  64. nsIContentChild::AllocPBlobChild(const BlobConstructorParams& aParams)
  65. {
  66. return BlobChild::Create(this, aParams);
  67. }
  68. bool
  69. nsIContentChild::DeallocPBlobChild(PBlobChild* aActor)
  70. {
  71. BlobChild::Destroy(aActor);
  72. return true;
  73. }
  74. BlobChild*
  75. nsIContentChild::GetOrCreateActorForBlob(Blob* aBlob)
  76. {
  77. MOZ_ASSERT(NS_IsMainThread());
  78. MOZ_ASSERT(aBlob);
  79. RefPtr<BlobImpl> blobImpl = aBlob->Impl();
  80. MOZ_ASSERT(blobImpl);
  81. return GetOrCreateActorForBlobImpl(blobImpl);
  82. }
  83. BlobChild*
  84. nsIContentChild::GetOrCreateActorForBlobImpl(BlobImpl* aImpl)
  85. {
  86. MOZ_ASSERT(NS_IsMainThread());
  87. MOZ_ASSERT(aImpl);
  88. BlobChild* actor = BlobChild::GetOrCreate(this, aImpl);
  89. NS_ENSURE_TRUE(actor, nullptr);
  90. return actor;
  91. }
  92. PSendStreamChild*
  93. nsIContentChild::AllocPSendStreamChild()
  94. {
  95. MOZ_CRASH("PSendStreamChild actors should be manually constructed!");
  96. }
  97. bool
  98. nsIContentChild::DeallocPSendStreamChild(PSendStreamChild* aActor)
  99. {
  100. delete aActor;
  101. return true;
  102. }
  103. PFileDescriptorSetChild*
  104. nsIContentChild::AllocPFileDescriptorSetChild(const FileDescriptor& aFD)
  105. {
  106. return new FileDescriptorSetChild(aFD);
  107. }
  108. bool
  109. nsIContentChild::DeallocPFileDescriptorSetChild(PFileDescriptorSetChild* aActor)
  110. {
  111. delete static_cast<FileDescriptorSetChild*>(aActor);
  112. return true;
  113. }
  114. bool
  115. nsIContentChild::RecvAsyncMessage(const nsString& aMsg,
  116. InfallibleTArray<CpowEntry>&& aCpows,
  117. const IPC::Principal& aPrincipal,
  118. const ClonedMessageData& aData)
  119. {
  120. RefPtr<nsFrameMessageManager> cpm = nsFrameMessageManager::GetChildProcessManager();
  121. if (cpm) {
  122. ipc::StructuredCloneData data;
  123. ipc::UnpackClonedMessageDataForChild(aData, data);
  124. CrossProcessCpowHolder cpows(this, aCpows);
  125. cpm->ReceiveMessage(static_cast<nsIContentFrameMessageManager*>(cpm.get()), nullptr,
  126. aMsg, false, &data, &cpows, aPrincipal, nullptr);
  127. }
  128. return true;
  129. }
  130. } // namespace dom
  131. } // namespace mozilla