ContentBridgeParent.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "mozilla/dom/ContentBridgeParent.h"
  6. #include "mozilla/dom/TabParent.h"
  7. #include "mozilla/jsipc/CrossProcessObjectWrappers.h"
  8. #include "nsXULAppAPI.h"
  9. #include "nsIObserverService.h"
  10. #include "base/task.h"
  11. using namespace mozilla::ipc;
  12. using namespace mozilla::jsipc;
  13. namespace mozilla {
  14. namespace dom {
  15. NS_IMPL_ISUPPORTS(ContentBridgeParent,
  16. nsIContentParent,
  17. nsIObserver)
  18. ContentBridgeParent::ContentBridgeParent(Transport* aTransport)
  19. : mTransport(aTransport)
  20. {}
  21. ContentBridgeParent::~ContentBridgeParent()
  22. {
  23. }
  24. void
  25. ContentBridgeParent::ActorDestroy(ActorDestroyReason aWhy)
  26. {
  27. nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
  28. if (os) {
  29. os->RemoveObserver(this, "content-child-shutdown");
  30. }
  31. MessageLoop::current()->PostTask(NewRunnableMethod(this, &ContentBridgeParent::DeferredDestroy));
  32. }
  33. /*static*/ ContentBridgeParent*
  34. ContentBridgeParent::Create(Transport* aTransport, ProcessId aOtherPid)
  35. {
  36. RefPtr<ContentBridgeParent> bridge =
  37. new ContentBridgeParent(aTransport);
  38. bridge->mSelfRef = bridge;
  39. DebugOnly<bool> ok = bridge->Open(aTransport, aOtherPid,
  40. XRE_GetIOMessageLoop());
  41. MOZ_ASSERT(ok);
  42. nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
  43. if (os) {
  44. os->AddObserver(bridge, "content-child-shutdown", false);
  45. }
  46. // Initialize the message manager (and load delayed scripts) now that we
  47. // have established communications with the child.
  48. bridge->mMessageManager->InitWithCallback(bridge);
  49. return bridge.get();
  50. }
  51. void
  52. ContentBridgeParent::DeferredDestroy()
  53. {
  54. mSelfRef = nullptr;
  55. // |this| was just destroyed, hands off
  56. }
  57. bool
  58. ContentBridgeParent::RecvSyncMessage(const nsString& aMsg,
  59. const ClonedMessageData& aData,
  60. InfallibleTArray<jsipc::CpowEntry>&& aCpows,
  61. const IPC::Principal& aPrincipal,
  62. nsTArray<StructuredCloneData>* aRetvals)
  63. {
  64. return nsIContentParent::RecvSyncMessage(aMsg, aData, Move(aCpows),
  65. aPrincipal, aRetvals);
  66. }
  67. bool
  68. ContentBridgeParent::RecvAsyncMessage(const nsString& aMsg,
  69. InfallibleTArray<jsipc::CpowEntry>&& aCpows,
  70. const IPC::Principal& aPrincipal,
  71. const ClonedMessageData& aData)
  72. {
  73. return nsIContentParent::RecvAsyncMessage(aMsg, Move(aCpows),
  74. aPrincipal, aData);
  75. }
  76. PBlobParent*
  77. ContentBridgeParent::SendPBlobConstructor(PBlobParent* actor,
  78. const BlobConstructorParams& params)
  79. {
  80. return PContentBridgeParent::SendPBlobConstructor(actor, params);
  81. }
  82. PBrowserParent*
  83. ContentBridgeParent::SendPBrowserConstructor(PBrowserParent* aActor,
  84. const TabId& aTabId,
  85. const IPCTabContext& aContext,
  86. const uint32_t& aChromeFlags,
  87. const ContentParentId& aCpID,
  88. const bool& aIsForApp,
  89. const bool& aIsForBrowser)
  90. {
  91. return PContentBridgeParent::SendPBrowserConstructor(aActor,
  92. aTabId,
  93. aContext,
  94. aChromeFlags,
  95. aCpID,
  96. aIsForApp,
  97. aIsForBrowser);
  98. }
  99. PBlobParent*
  100. ContentBridgeParent::AllocPBlobParent(const BlobConstructorParams& aParams)
  101. {
  102. return nsIContentParent::AllocPBlobParent(aParams);
  103. }
  104. bool
  105. ContentBridgeParent::DeallocPBlobParent(PBlobParent* aActor)
  106. {
  107. return nsIContentParent::DeallocPBlobParent(aActor);
  108. }
  109. mozilla::jsipc::PJavaScriptParent *
  110. ContentBridgeParent::AllocPJavaScriptParent()
  111. {
  112. return nsIContentParent::AllocPJavaScriptParent();
  113. }
  114. bool
  115. ContentBridgeParent::DeallocPJavaScriptParent(PJavaScriptParent *parent)
  116. {
  117. return nsIContentParent::DeallocPJavaScriptParent(parent);
  118. }
  119. PBrowserParent*
  120. ContentBridgeParent::AllocPBrowserParent(const TabId& aTabId,
  121. const IPCTabContext &aContext,
  122. const uint32_t& aChromeFlags,
  123. const ContentParentId& aCpID,
  124. const bool& aIsForApp,
  125. const bool& aIsForBrowser)
  126. {
  127. return nsIContentParent::AllocPBrowserParent(aTabId,
  128. aContext,
  129. aChromeFlags,
  130. aCpID,
  131. aIsForApp,
  132. aIsForBrowser);
  133. }
  134. bool
  135. ContentBridgeParent::DeallocPBrowserParent(PBrowserParent* aParent)
  136. {
  137. return nsIContentParent::DeallocPBrowserParent(aParent);
  138. }
  139. void
  140. ContentBridgeParent::NotifyTabDestroyed()
  141. {
  142. int32_t numLiveTabs = ManagedPBrowserParent().Count();
  143. if (numLiveTabs == 1) {
  144. MessageLoop::current()->PostTask(NewRunnableMethod(this, &ContentBridgeParent::Close));
  145. }
  146. }
  147. // This implementation is identical to ContentParent::GetCPOWManager but we can't
  148. // move it to nsIContentParent because it calls ManagedPJavaScriptParent() which
  149. // only exists in PContentParent and PContentBridgeParent.
  150. jsipc::CPOWManager*
  151. ContentBridgeParent::GetCPOWManager()
  152. {
  153. if (PJavaScriptParent* p = LoneManagedOrNullAsserts(ManagedPJavaScriptParent())) {
  154. return CPOWManagerFor(p);
  155. }
  156. return nullptr;
  157. }
  158. NS_IMETHODIMP
  159. ContentBridgeParent::Observe(nsISupports* aSubject,
  160. const char* aTopic,
  161. const char16_t* aData)
  162. {
  163. if (!strcmp(aTopic, "content-child-shutdown")) {
  164. Close();
  165. }
  166. return NS_OK;
  167. }
  168. PFileDescriptorSetParent*
  169. ContentBridgeParent::AllocPFileDescriptorSetParent(const FileDescriptor& aFD)
  170. {
  171. return nsIContentParent::AllocPFileDescriptorSetParent(aFD);
  172. }
  173. bool
  174. ContentBridgeParent::DeallocPFileDescriptorSetParent(PFileDescriptorSetParent* aActor)
  175. {
  176. return nsIContentParent::DeallocPFileDescriptorSetParent(aActor);
  177. }
  178. PSendStreamParent*
  179. ContentBridgeParent::AllocPSendStreamParent()
  180. {
  181. return nsIContentParent::AllocPSendStreamParent();
  182. }
  183. bool
  184. ContentBridgeParent::DeallocPSendStreamParent(PSendStreamParent* aActor)
  185. {
  186. return nsIContentParent::DeallocPSendStreamParent(aActor);
  187. }
  188. } // namespace dom
  189. } // namespace mozilla