ContentParent.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_ContentParent_h
  6. #define mozilla_dom_ContentParent_h
  7. #include "mozilla/dom/PContentParent.h"
  8. #include "mozilla/dom/nsIContentParent.h"
  9. #include "mozilla/gfx/gfxVarReceiver.h"
  10. #include "mozilla/gfx/GPUProcessListener.h"
  11. #include "mozilla/ipc/GeckoChildProcessHost.h"
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/FileUtils.h"
  14. #include "mozilla/HalTypes.h"
  15. #include "mozilla/LinkedList.h"
  16. #include "mozilla/StaticPtr.h"
  17. #include "mozilla/UniquePtr.h"
  18. #include "nsDataHashtable.h"
  19. #include "nsFrameMessageManager.h"
  20. #include "nsHashKeys.h"
  21. #include "nsIObserver.h"
  22. #include "nsIThreadInternal.h"
  23. #include "nsIDOMGeoPositionCallback.h"
  24. #include "nsIDOMGeoPositionErrorCallback.h"
  25. #include "nsRefPtrHashtable.h"
  26. #include "PermissionMessageUtils.h"
  27. #include "DriverCrashGuard.h"
  28. #define CHILD_PROCESS_SHUTDOWN_MESSAGE NS_LITERAL_STRING("child-process-shutdown")
  29. class mozIApplication;
  30. class nsConsoleService;
  31. class nsICycleCollectorLogSink;
  32. class nsIDumpGCAndCCLogsCallback;
  33. class nsITimer;
  34. class ParentIdleListener;
  35. class nsIWidget;
  36. namespace mozilla {
  37. class PRemoteSpellcheckEngineParent;
  38. namespace embedding {
  39. class PrintingParent;
  40. }
  41. namespace ipc {
  42. class OptionalURIParams;
  43. class PFileDescriptorSetParent;
  44. class URIParams;
  45. class TestShellParent;
  46. } // namespace ipc
  47. namespace jsipc {
  48. class PJavaScriptParent;
  49. } // namespace jsipc
  50. namespace layers {
  51. struct TextureFactoryIdentifier;
  52. } // namespace layers
  53. namespace layout {
  54. class PRenderFrameParent;
  55. } // namespace layout
  56. namespace dom {
  57. class Element;
  58. class TabParent;
  59. class PStorageParent;
  60. class ClonedMessageData;
  61. class MemoryReport;
  62. class TabContext;
  63. class ContentBridgeParent;
  64. class GetFilesHelper;
  65. class ContentParent final : public PContentParent
  66. , public nsIContentParent
  67. , public nsIObserver
  68. , public nsIDOMGeoPositionCallback
  69. , public nsIDOMGeoPositionErrorCallback
  70. , public gfx::gfxVarReceiver
  71. , public mozilla::LinkedListElement<ContentParent>
  72. , public gfx::GPUProcessListener
  73. {
  74. typedef mozilla::ipc::GeckoChildProcessHost GeckoChildProcessHost;
  75. typedef mozilla::ipc::OptionalURIParams OptionalURIParams;
  76. typedef mozilla::ipc::PFileDescriptorSetParent PFileDescriptorSetParent;
  77. typedef mozilla::ipc::TestShellParent TestShellParent;
  78. typedef mozilla::ipc::URIParams URIParams;
  79. typedef mozilla::ipc::PrincipalInfo PrincipalInfo;
  80. typedef mozilla::dom::ClonedMessageData ClonedMessageData;
  81. public:
  82. virtual bool IsContentParent() const override { return true; }
  83. /**
  84. * Start up the content-process machinery. This might include
  85. * scheduling pre-launch tasks.
  86. */
  87. static void StartUp();
  88. /** Shut down the content-process machinery. */
  89. static void ShutDown();
  90. /**
  91. * Ensure that all subprocesses are terminated and their OS
  92. * resources have been reaped. This is synchronous and can be
  93. * very expensive in general. It also bypasses the normal
  94. * shutdown process.
  95. */
  96. static void JoinAllSubprocesses();
  97. static bool PreallocatedProcessReady();
  98. /**
  99. * Get or create a content process for:
  100. * 1. browser iframe
  101. * 2. remote xul <browser>
  102. * 3. normal iframe
  103. */
  104. static already_AddRefed<ContentParent>
  105. GetNewOrUsedBrowserProcess(bool aForBrowserElement = false,
  106. hal::ProcessPriority aPriority =
  107. hal::ProcessPriority::PROCESS_PRIORITY_FOREGROUND,
  108. ContentParent* aOpener = nullptr,
  109. bool aLargeAllocationProcess = false);
  110. /**
  111. * Create a subprocess suitable for use as a preallocated app process.
  112. */
  113. static already_AddRefed<ContentParent> PreallocateAppProcess();
  114. /**
  115. * Get or create a content process for the given TabContext. aFrameElement
  116. * should be the frame/iframe element with which this process will
  117. * associated.
  118. */
  119. static TabParent*
  120. CreateBrowserOrApp(const TabContext& aContext,
  121. Element* aFrameElement,
  122. ContentParent* aOpenerContentParent,
  123. bool aFreshProcess = false);
  124. static void GetAll(nsTArray<ContentParent*>& aArray);
  125. static void GetAllEvenIfDead(nsTArray<ContentParent*>& aArray);
  126. enum CPIteratorPolicy {
  127. eLive,
  128. eAll
  129. };
  130. class ContentParentIterator {
  131. private:
  132. ContentParent* mCurrent;
  133. CPIteratorPolicy mPolicy;
  134. public:
  135. ContentParentIterator(CPIteratorPolicy aPolicy, ContentParent* aCurrent)
  136. : mCurrent(aCurrent),
  137. mPolicy(aPolicy)
  138. {
  139. }
  140. ContentParentIterator begin()
  141. {
  142. // Move the cursor to the first element that matches the policy.
  143. while (mPolicy != eAll && mCurrent && !mCurrent->mIsAlive) {
  144. mCurrent = mCurrent->LinkedListElement<ContentParent>::getNext();
  145. }
  146. return *this;
  147. }
  148. ContentParentIterator end()
  149. {
  150. return ContentParentIterator(mPolicy, nullptr);
  151. }
  152. const ContentParentIterator& operator++()
  153. {
  154. MOZ_ASSERT(mCurrent);
  155. do {
  156. mCurrent = mCurrent->LinkedListElement<ContentParent>::getNext();
  157. } while (mPolicy != eAll && mCurrent && !mCurrent->mIsAlive);
  158. return *this;
  159. }
  160. bool operator!=(const ContentParentIterator& aOther)
  161. {
  162. MOZ_ASSERT(mPolicy == aOther.mPolicy);
  163. return mCurrent != aOther.mCurrent;
  164. }
  165. ContentParent* operator*()
  166. {
  167. return mCurrent;
  168. }
  169. };
  170. static ContentParentIterator AllProcesses(CPIteratorPolicy aPolicy)
  171. {
  172. ContentParent* first =
  173. sContentParents ? sContentParents->getFirst() : nullptr;
  174. return ContentParentIterator(aPolicy, first);
  175. }
  176. static bool IgnoreIPCPrincipal();
  177. static void NotifyUpdatedDictionaries();
  178. #if defined(XP_WIN)
  179. /**
  180. * Windows helper for firing off an update window request to a plugin
  181. * instance.
  182. *
  183. * aWidget - the eWindowType_plugin_ipc_chrome widget associated with
  184. * this plugin window.
  185. */
  186. static void SendAsyncUpdate(nsIWidget* aWidget);
  187. #endif
  188. // Let managees query if it is safe to send messages.
  189. bool IsDestroyed() const { return !mIPCOpen; }
  190. virtual bool RecvCreateChildProcess(const IPCTabContext& aContext,
  191. const hal::ProcessPriority& aPriority,
  192. const TabId& aOpenerTabId,
  193. ContentParentId* aCpId,
  194. bool* aIsForApp,
  195. bool* aIsForBrowser,
  196. TabId* aTabId) override;
  197. virtual bool RecvBridgeToChildProcess(const ContentParentId& aCpId) override;
  198. virtual bool RecvCreateGMPService() override;
  199. virtual bool RecvLoadPlugin(const uint32_t& aPluginId, nsresult* aRv,
  200. uint32_t* aRunID) override;
  201. virtual bool RecvConnectPluginBridge(const uint32_t& aPluginId,
  202. nsresult* aRv) override;
  203. virtual bool RecvGetBlocklistState(const uint32_t& aPluginId,
  204. uint32_t* aIsBlocklisted) override;
  205. virtual bool RecvFindPlugins(const uint32_t& aPluginEpoch,
  206. nsresult* aRv,
  207. nsTArray<PluginTag>* aPlugins,
  208. uint32_t* aNewPluginEpoch) override;
  209. virtual bool RecvUngrabPointer(const uint32_t& aTime) override;
  210. virtual bool RecvRemovePermission(const IPC::Principal& aPrincipal,
  211. const nsCString& aPermissionType,
  212. nsresult* aRv) override;
  213. NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ContentParent, nsIObserver)
  214. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  215. NS_DECL_NSIOBSERVER
  216. NS_DECL_NSIDOMGEOPOSITIONCALLBACK
  217. NS_DECL_NSIDOMGEOPOSITIONERRORCALLBACK
  218. /**
  219. * MessageManagerCallback methods that we override.
  220. */
  221. virtual bool DoLoadMessageManagerScript(const nsAString& aURL,
  222. bool aRunInGlobalScope) override;
  223. virtual nsresult DoSendAsyncMessage(JSContext* aCx,
  224. const nsAString& aMessage,
  225. StructuredCloneData& aData,
  226. JS::Handle<JSObject *> aCpows,
  227. nsIPrincipal* aPrincipal) override;
  228. virtual bool CheckPermission(const nsAString& aPermission) override;
  229. virtual bool CheckManifestURL(const nsAString& aManifestURL) override;
  230. virtual bool CheckAppHasPermission(const nsAString& aPermission) override;
  231. virtual bool CheckAppHasStatus(unsigned short aStatus) override;
  232. virtual bool KillChild() override;
  233. /** Notify that a tab is beginning its destruction sequence. */
  234. static void NotifyTabDestroying(const TabId& aTabId,
  235. const ContentParentId& aCpId);
  236. /** Notify that a tab was destroyed during normal operation. */
  237. void NotifyTabDestroyed(const TabId& aTabId,
  238. bool aNotifiedDestroying);
  239. TestShellParent* CreateTestShell();
  240. bool DestroyTestShell(TestShellParent* aTestShell);
  241. TestShellParent* GetTestShellSingleton();
  242. jsipc::CPOWManager* GetCPOWManager() override;
  243. static TabId
  244. AllocateTabId(const TabId& aOpenerTabId,
  245. const IPCTabContext& aContext,
  246. const ContentParentId& aCpId);
  247. static void
  248. DeallocateTabId(const TabId& aTabId,
  249. const ContentParentId& aCpId,
  250. bool aMarkedDestroying);
  251. void ReportChildAlreadyBlocked();
  252. bool RequestRunToCompletion();
  253. bool IsAlive() const;
  254. virtual bool IsForApp() const override;
  255. virtual bool IsForBrowser() const override
  256. {
  257. return mIsForBrowser;
  258. }
  259. GeckoChildProcessHost* Process() const
  260. {
  261. return mSubprocess;
  262. }
  263. ContentParent* Opener() const
  264. {
  265. return mOpener;
  266. }
  267. bool NeedsPermissionsUpdate() const
  268. {
  269. return mSendPermissionUpdates;
  270. }
  271. /**
  272. * Kill our subprocess and make sure it dies. Should only be used
  273. * in emergency situations since it bypasses the normal shutdown
  274. * process.
  275. *
  276. * WARNING: aReason appears in telemetry, so any new value passed in requires
  277. * data review.
  278. */
  279. void KillHard(const char* aWhy);
  280. ContentParentId ChildID() const override { return mChildID; }
  281. const nsString& AppManifestURL() const { return mAppManifestURL; }
  282. bool IsPreallocated() const;
  283. /**
  284. * Get a user-friendly name for this ContentParent. We make no guarantees
  285. * about this name: It might not be unique, apps can spoof special names,
  286. * etc. So please don't use this name to make any decisions about the
  287. * ContentParent based on the value returned here.
  288. */
  289. void FriendlyName(nsAString& aName, bool aAnonymize = false);
  290. virtual void OnChannelError() override;
  291. virtual PNeckoParent* AllocPNeckoParent() override;
  292. virtual bool RecvPNeckoConstructor(PNeckoParent* aActor) override
  293. {
  294. return PContentParent::RecvPNeckoConstructor(aActor);
  295. }
  296. virtual PPrintingParent* AllocPPrintingParent() override;
  297. virtual bool DeallocPPrintingParent(PPrintingParent* aActor) override;
  298. #if defined(NS_PRINTING)
  299. /**
  300. * @return the PrintingParent for this ContentParent.
  301. */
  302. already_AddRefed<embedding::PrintingParent> GetPrintingParent();
  303. #endif
  304. virtual PSendStreamParent* AllocPSendStreamParent() override;
  305. virtual bool DeallocPSendStreamParent(PSendStreamParent* aActor) override;
  306. virtual PScreenManagerParent*
  307. AllocPScreenManagerParent(uint32_t* aNumberOfScreens,
  308. float* aSystemDefaultScale,
  309. bool* aSuccess) override;
  310. virtual bool
  311. DeallocPScreenManagerParent(PScreenManagerParent* aActor) override;
  312. virtual PHalParent* AllocPHalParent() override;
  313. virtual bool RecvPHalConstructor(PHalParent* aActor) override
  314. {
  315. return PContentParent::RecvPHalConstructor(aActor);
  316. }
  317. virtual PHeapSnapshotTempFileHelperParent*
  318. AllocPHeapSnapshotTempFileHelperParent() override;
  319. virtual PStorageParent* AllocPStorageParent() override;
  320. virtual bool RecvPStorageConstructor(PStorageParent* aActor) override
  321. {
  322. return PContentParent::RecvPStorageConstructor(aActor);
  323. }
  324. virtual PJavaScriptParent*
  325. AllocPJavaScriptParent() override;
  326. virtual bool
  327. RecvPJavaScriptConstructor(PJavaScriptParent* aActor) override
  328. {
  329. return PContentParent::RecvPJavaScriptConstructor(aActor);
  330. }
  331. virtual PRemoteSpellcheckEngineParent* AllocPRemoteSpellcheckEngineParent() override;
  332. virtual bool RecvRecordingDeviceEvents(const nsString& aRecordingStatus,
  333. const nsString& aPageURL,
  334. const bool& aIsAudio,
  335. const bool& aIsVideo) override;
  336. bool CycleCollectWithLogs(bool aDumpAllTraces,
  337. nsICycleCollectorLogSink* aSink,
  338. nsIDumpGCAndCCLogsCallback* aCallback);
  339. virtual PBlobParent*
  340. SendPBlobConstructor(PBlobParent* aActor,
  341. const BlobConstructorParams& aParams) override;
  342. virtual bool RecvAllocateTabId(const TabId& aOpenerTabId,
  343. const IPCTabContext& aContext,
  344. const ContentParentId& aCpId,
  345. TabId* aTabId) override;
  346. virtual bool RecvDeallocateTabId(const TabId& aTabId,
  347. const ContentParentId& aCpId,
  348. const bool& aMarkedDestroying) override;
  349. virtual bool RecvNotifyTabDestroying(const TabId& aTabId,
  350. const ContentParentId& aCpId) override;
  351. nsTArray<TabContext> GetManagedTabContext();
  352. virtual POfflineCacheUpdateParent*
  353. AllocPOfflineCacheUpdateParent(const URIParams& aManifestURI,
  354. const URIParams& aDocumentURI,
  355. const PrincipalInfo& aLoadingPrincipalInfo,
  356. const bool& aStickDocument) override;
  357. virtual bool
  358. RecvPOfflineCacheUpdateConstructor(POfflineCacheUpdateParent* aActor,
  359. const URIParams& aManifestURI,
  360. const URIParams& aDocumentURI,
  361. const PrincipalInfo& aLoadingPrincipal,
  362. const bool& stickDocument) override;
  363. virtual bool
  364. DeallocPOfflineCacheUpdateParent(POfflineCacheUpdateParent* aActor) override;
  365. virtual bool RecvSetOfflinePermission(const IPC::Principal& principal) override;
  366. virtual bool RecvFinishShutdown() override;
  367. void MaybeInvokeDragSession(TabParent* aParent);
  368. virtual PContentPermissionRequestParent*
  369. AllocPContentPermissionRequestParent(const InfallibleTArray<PermissionRequest>& aRequests,
  370. const IPC::Principal& aPrincipal,
  371. const TabId& aTabId) override;
  372. virtual bool
  373. DeallocPContentPermissionRequestParent(PContentPermissionRequestParent* actor) override;
  374. virtual bool HandleWindowsMessages(const Message& aMsg) const override;
  375. void ForkNewProcess(bool aBlocking);
  376. virtual bool RecvCreateWindow(PBrowserParent* aThisTabParent,
  377. PBrowserParent* aOpener,
  378. layout::PRenderFrameParent* aRenderFrame,
  379. const uint32_t& aChromeFlags,
  380. const bool& aCalledFromJS,
  381. const bool& aPositionSpecified,
  382. const bool& aSizeSpecified,
  383. const nsCString& aFeatures,
  384. const nsCString& aBaseURI,
  385. const DocShellOriginAttributes& aOpenerOriginAttributes,
  386. const float& aFullZoom,
  387. nsresult* aResult,
  388. bool* aWindowIsNew,
  389. InfallibleTArray<FrameScriptInfo>* aFrameScripts,
  390. nsCString* aURLToLoad,
  391. layers::TextureFactoryIdentifier* aTextureFactoryIdentifier,
  392. uint64_t* aLayersId) override;
  393. static bool AllocateLayerTreeId(TabParent* aTabParent, uint64_t* aId);
  394. static void
  395. BroadcastBlobURLRegistration(const nsACString& aURI,
  396. BlobImpl* aBlobImpl,
  397. nsIPrincipal* aPrincipal,
  398. ContentParent* aIgnoreThisCP = nullptr);
  399. static void
  400. BroadcastBlobURLUnregistration(const nsACString& aURI,
  401. ContentParent* aIgnoreThisCP = nullptr);
  402. virtual bool
  403. RecvStoreAndBroadcastBlobURLRegistration(const nsCString& aURI,
  404. PBlobParent* aBlobParent,
  405. const Principal& aPrincipal) override;
  406. virtual bool
  407. RecvUnstoreAndBroadcastBlobURLUnregistration(const nsCString& aURI) override;
  408. virtual bool
  409. RecvGetA11yContentId(uint32_t* aContentId) override;
  410. virtual int32_t Pid() const override;
  411. // Use the PHangMonitor channel to ask the child to repaint a tab.
  412. void ForceTabPaint(TabParent* aTabParent, uint64_t aLayerObserverEpoch);
  413. protected:
  414. void OnChannelConnected(int32_t pid) override;
  415. virtual void ActorDestroy(ActorDestroyReason why) override;
  416. bool ShouldContinueFromReplyTimeout() override;
  417. void OnVarChanged(const GfxVarUpdate& aVar) override;
  418. void OnCompositorUnexpectedShutdown() override;
  419. private:
  420. static nsDataHashtable<nsStringHashKey, ContentParent*> *sAppContentParents;
  421. static nsTArray<ContentParent*>* sNonAppContentParents;
  422. static nsTArray<ContentParent*>* sLargeAllocationContentParents;
  423. static nsTArray<ContentParent*>* sPrivateContent;
  424. static StaticAutoPtr<LinkedList<ContentParent> > sContentParents;
  425. static void JoinProcessesIOThread(const nsTArray<ContentParent*>* aProcesses,
  426. Monitor* aMonitor, bool* aDone);
  427. // Take the preallocated process and transform it into a "real" app process,
  428. // for the specified manifest URL. If there is no preallocated process (or
  429. // if it's dead), create a new one and set aTookPreAllocated to false.
  430. static already_AddRefed<ContentParent>
  431. GetNewOrPreallocatedAppProcess(mozIApplication* aApp,
  432. hal::ProcessPriority aInitialPriority,
  433. ContentParent* aOpener,
  434. /*out*/ bool* aTookPreAllocated = nullptr);
  435. static hal::ProcessPriority GetInitialProcessPriority(Element* aFrameElement);
  436. static ContentBridgeParent* CreateContentBridgeParent(const TabContext& aContext,
  437. const hal::ProcessPriority& aPriority,
  438. const TabId& aOpenerTabId,
  439. /*out*/ TabId* aTabId);
  440. // Hide the raw constructor methods since we don't want client code
  441. // using them.
  442. virtual PBrowserParent* SendPBrowserConstructor(
  443. PBrowserParent* actor,
  444. const TabId& aTabId,
  445. const IPCTabContext& context,
  446. const uint32_t& chromeFlags,
  447. const ContentParentId& aCpId,
  448. const bool& aIsForApp,
  449. const bool& aIsForBrowser) override;
  450. using PContentParent::SendPTestShellConstructor;
  451. FORWARD_SHMEM_ALLOCATOR_TO(PContentParent)
  452. // No more than one of !!aApp, aIsForBrowser, and aIsForPreallocated may be
  453. // true.
  454. ContentParent(mozIApplication* aApp,
  455. ContentParent* aOpener,
  456. bool aIsForBrowser,
  457. bool aIsForPreallocated);
  458. // The common initialization for the constructors.
  459. void InitializeMembers();
  460. // Launch the subprocess and associated initialization.
  461. // Returns false if the process fails to start.
  462. bool LaunchSubprocess(hal::ProcessPriority aInitialPriority = hal::PROCESS_PRIORITY_FOREGROUND);
  463. // Common initialization after sub process launch or adoption.
  464. void InitInternal(ProcessPriority aPriority,
  465. bool aSetupOffMainThreadCompositing,
  466. bool aSendRegisteredChrome);
  467. virtual ~ContentParent();
  468. void Init();
  469. // Some information could be sent to content very early, it
  470. // should be send from this function. This function should only be
  471. // called after the process has been transformed to app or browser.
  472. void ForwardKnownInfo();
  473. // Set the child process's priority and then check whether the child is
  474. // still alive. Returns true if the process is still alive, and false
  475. // otherwise. If you pass a FOREGROUND* priority here, it's (hopefully)
  476. // unlikely that the process will be killed after this point.
  477. bool SetPriorityAndCheckIsAlive(hal::ProcessPriority aPriority);
  478. // Transform a pre-allocated app process into a "real" app
  479. // process, for the specified manifest URL.
  480. void TransformPreallocatedIntoApp(ContentParent* aOpener,
  481. const nsAString& aAppManifestURL);
  482. // Transform a pre-allocated app process into a browser process. If this
  483. // returns false, the child process has died.
  484. void TransformPreallocatedIntoBrowser(ContentParent* aOpener);
  485. /**
  486. * Mark this ContentParent as dead for the purposes of Get*().
  487. * This method is idempotent.
  488. */
  489. void MarkAsDead();
  490. /**
  491. * How we will shut down this ContentParent and its subprocess.
  492. */
  493. enum ShutDownMethod
  494. {
  495. // Send a shutdown message and wait for FinishShutdown call back.
  496. SEND_SHUTDOWN_MESSAGE,
  497. // Close the channel ourselves and let the subprocess clean up itself.
  498. CLOSE_CHANNEL,
  499. // Close the channel with error and let the subprocess clean up itself.
  500. CLOSE_CHANNEL_WITH_ERROR,
  501. };
  502. /**
  503. * Exit the subprocess and vamoose. After this call IsAlive()
  504. * will return false and this ContentParent will not be returned
  505. * by the Get*() funtions. However, the shutdown sequence itself
  506. * may be asynchronous.
  507. *
  508. * If aMethod is CLOSE_CHANNEL_WITH_ERROR and this is the first call
  509. * to ShutDownProcess, then we'll close our channel using CloseWithError()
  510. * rather than vanilla Close(). CloseWithError() indicates to IPC that this
  511. * is an abnormal shutdown (e.g. a crash).
  512. */
  513. void ShutDownProcess(ShutDownMethod aMethod);
  514. // Perform any steps necesssary to gracefully shtudown the message
  515. // manager and null out mMessageManager.
  516. void ShutDownMessageManager();
  517. // Start the force-kill timer on shutdown.
  518. void StartForceKillTimer();
  519. static void ForceKillTimerCallback(nsITimer* aTimer, void* aClosure);
  520. static bool AllocateLayerTreeId(ContentParent* aContent,
  521. TabParent* aTopLevel, const TabId& aTabId,
  522. uint64_t* aId);
  523. PGMPServiceParent*
  524. AllocPGMPServiceParent(mozilla::ipc::Transport* aTransport,
  525. base::ProcessId aOtherProcess) override;
  526. PBackgroundParent*
  527. AllocPBackgroundParent(Transport* aTransport, ProcessId aOtherProcess)
  528. override;
  529. PProcessHangMonitorParent*
  530. AllocPProcessHangMonitorParent(Transport* aTransport,
  531. ProcessId aOtherProcess) override;
  532. virtual bool RecvGetProcessAttributes(ContentParentId* aCpId,
  533. bool* aIsForApp,
  534. bool* aIsForBrowser) override;
  535. virtual bool
  536. RecvGetXPCOMProcessAttributes(bool* aIsOffline,
  537. bool* aIsConnected,
  538. int32_t* aCaptivePortalState,
  539. bool* aIsLangRTL,
  540. bool* aHaveBidiKeyboards,
  541. InfallibleTArray<nsString>* dictionaries,
  542. ClipboardCapabilities* clipboardCaps,
  543. DomainPolicyClone* domainPolicy,
  544. StructuredCloneData* initialData,
  545. OptionalURIParams* aUserContentSheetURL) override;
  546. virtual bool
  547. DeallocPJavaScriptParent(mozilla::jsipc::PJavaScriptParent*) override;
  548. virtual bool
  549. DeallocPRemoteSpellcheckEngineParent(PRemoteSpellcheckEngineParent*) override;
  550. virtual PBrowserParent* AllocPBrowserParent(const TabId& aTabId,
  551. const IPCTabContext& aContext,
  552. const uint32_t& aChromeFlags,
  553. const ContentParentId& aCpId,
  554. const bool& aIsForApp,
  555. const bool& aIsForBrowser) override;
  556. virtual bool DeallocPBrowserParent(PBrowserParent* frame) override;
  557. virtual PBlobParent*
  558. AllocPBlobParent(const BlobConstructorParams& aParams) override;
  559. virtual bool DeallocPBlobParent(PBlobParent* aActor) override;
  560. virtual bool
  561. RecvPBlobConstructor(PBlobParent* aActor,
  562. const BlobConstructorParams& params) override;
  563. virtual bool RecvNSSU2FTokenIsCompatibleVersion(const nsString& aVersion,
  564. bool* aIsCompatible) override;
  565. virtual bool RecvNSSU2FTokenIsRegistered(nsTArray<uint8_t>&& aKeyHandle,
  566. bool* aIsValidKeyHandle) override;
  567. virtual bool RecvNSSU2FTokenRegister(nsTArray<uint8_t>&& aApplication,
  568. nsTArray<uint8_t>&& aChallenge,
  569. nsTArray<uint8_t>* aRegistration) override;
  570. virtual bool RecvNSSU2FTokenSign(nsTArray<uint8_t>&& aApplication,
  571. nsTArray<uint8_t>&& aChallenge,
  572. nsTArray<uint8_t>&& aKeyHandle,
  573. nsTArray<uint8_t>* aSignature) override;
  574. virtual bool RecvIsSecureURI(const uint32_t& aType, const URIParams& aURI,
  575. const uint32_t& aFlags, bool* aIsSecureURI) override;
  576. virtual bool DeallocPHalParent(PHalParent*) override;
  577. virtual bool
  578. DeallocPHeapSnapshotTempFileHelperParent(PHeapSnapshotTempFileHelperParent*) override;
  579. virtual PMemoryReportRequestParent*
  580. AllocPMemoryReportRequestParent(const uint32_t& aGeneration,
  581. const bool &aAnonymize,
  582. const bool &aMinimizeMemoryUsage,
  583. const MaybeFileDesc &aDMDFile) override;
  584. virtual bool
  585. DeallocPMemoryReportRequestParent(PMemoryReportRequestParent* actor) override;
  586. virtual PCycleCollectWithLogsParent*
  587. AllocPCycleCollectWithLogsParent(const bool& aDumpAllTraces,
  588. const FileDescriptor& aGCLog,
  589. const FileDescriptor& aCCLog) override;
  590. virtual bool
  591. DeallocPCycleCollectWithLogsParent(PCycleCollectWithLogsParent* aActor) override;
  592. virtual PTestShellParent* AllocPTestShellParent() override;
  593. virtual bool DeallocPTestShellParent(PTestShellParent* shell) override;
  594. virtual bool DeallocPNeckoParent(PNeckoParent* necko) override;
  595. virtual PPSMContentDownloaderParent*
  596. AllocPPSMContentDownloaderParent(const uint32_t& aCertType) override;
  597. virtual bool
  598. DeallocPPSMContentDownloaderParent(PPSMContentDownloaderParent* aDownloader) override;
  599. virtual PExternalHelperAppParent*
  600. AllocPExternalHelperAppParent(const OptionalURIParams& aUri,
  601. const nsCString& aMimeContentType,
  602. const nsCString& aContentDisposition,
  603. const uint32_t& aContentDispositionHint,
  604. const nsString& aContentDispositionFilename,
  605. const bool& aForceSave,
  606. const int64_t& aContentLength,
  607. const bool& aWasFileChannel,
  608. const OptionalURIParams& aReferrer,
  609. PBrowserParent* aBrowser) override;
  610. virtual bool
  611. DeallocPExternalHelperAppParent(PExternalHelperAppParent* aService) override;
  612. virtual PHandlerServiceParent* AllocPHandlerServiceParent() override;
  613. virtual bool DeallocPHandlerServiceParent(PHandlerServiceParent*) override;
  614. virtual PMediaParent* AllocPMediaParent() override;
  615. virtual bool DeallocPMediaParent(PMediaParent* aActor) override;
  616. virtual bool DeallocPStorageParent(PStorageParent* aActor) override;
  617. virtual PSpeechSynthesisParent* AllocPSpeechSynthesisParent() override;
  618. virtual bool
  619. DeallocPSpeechSynthesisParent(PSpeechSynthesisParent* aActor) override;
  620. virtual bool
  621. RecvPSpeechSynthesisConstructor(PSpeechSynthesisParent* aActor) override;
  622. virtual PWebBrowserPersistDocumentParent*
  623. AllocPWebBrowserPersistDocumentParent(PBrowserParent* aBrowser,
  624. const uint64_t& aOuterWindowID) override;
  625. virtual bool
  626. DeallocPWebBrowserPersistDocumentParent(PWebBrowserPersistDocumentParent* aActor) override;
  627. virtual bool RecvReadPrefsArray(InfallibleTArray<PrefSetting>* aPrefs) override;
  628. virtual bool RecvGetGfxVars(InfallibleTArray<GfxVarUpdate>* aVars) override;
  629. virtual bool RecvReadFontList(InfallibleTArray<FontListEntry>* retValue) override;
  630. virtual bool RecvReadDataStorageArray(const nsString& aFilename,
  631. InfallibleTArray<DataStorageItem>* aValues) override;
  632. virtual bool RecvReadPermissions(InfallibleTArray<IPC::Permission>* aPermissions) override;
  633. virtual bool RecvSetClipboard(const IPCDataTransfer& aDataTransfer,
  634. const bool& aIsPrivateData,
  635. const IPC::Principal& aRequestingPrincipal,
  636. const int32_t& aWhichClipboard) override;
  637. virtual bool RecvGetClipboard(nsTArray<nsCString>&& aTypes,
  638. const int32_t& aWhichClipboard,
  639. IPCDataTransfer* aDataTransfer) override;
  640. virtual bool RecvEmptyClipboard(const int32_t& aWhichClipboard) override;
  641. virtual bool RecvClipboardHasType(nsTArray<nsCString>&& aTypes,
  642. const int32_t& aWhichClipboard,
  643. bool* aHasType) override;
  644. virtual bool RecvGetSystemColors(const uint32_t& colorsCount,
  645. InfallibleTArray<uint32_t>* colors) override;
  646. virtual bool RecvGetIconForExtension(const nsCString& aFileExt,
  647. const uint32_t& aIconSize,
  648. InfallibleTArray<uint8_t>* bits) override;
  649. virtual bool RecvGetShowPasswordSetting(bool* showPassword) override;
  650. virtual bool RecvStartVisitedQuery(const URIParams& uri) override;
  651. virtual bool RecvVisitURI(const URIParams& uri,
  652. const OptionalURIParams& referrer,
  653. const uint32_t& flags) override;
  654. virtual bool RecvSetURITitle(const URIParams& uri,
  655. const nsString& title) override;
  656. bool HasNotificationPermission(const IPC::Principal& aPrincipal);
  657. virtual bool RecvShowAlert(const AlertNotificationType& aAlert) override;
  658. virtual bool RecvCloseAlert(const nsString& aName,
  659. const IPC::Principal& aPrincipal) override;
  660. virtual bool RecvDisableNotifications(const IPC::Principal& aPrincipal) override;
  661. virtual bool RecvOpenNotificationSettings(const IPC::Principal& aPrincipal) override;
  662. virtual bool RecvLoadURIExternal(const URIParams& uri,
  663. PBrowserParent* windowContext) override;
  664. virtual bool RecvExtProtocolChannelConnectParent(const uint32_t& registrarId) override;
  665. virtual bool RecvSyncMessage(const nsString& aMsg,
  666. const ClonedMessageData& aData,
  667. InfallibleTArray<CpowEntry>&& aCpows,
  668. const IPC::Principal& aPrincipal,
  669. nsTArray<StructuredCloneData>* aRetvals) override;
  670. virtual bool RecvRpcMessage(const nsString& aMsg,
  671. const ClonedMessageData& aData,
  672. InfallibleTArray<CpowEntry>&& aCpows,
  673. const IPC::Principal& aPrincipal,
  674. nsTArray<StructuredCloneData>* aRetvals) override;
  675. virtual bool RecvAsyncMessage(const nsString& aMsg,
  676. InfallibleTArray<CpowEntry>&& aCpows,
  677. const IPC::Principal& aPrincipal,
  678. const ClonedMessageData& aData) override;
  679. virtual bool RecvAddGeolocationListener(const IPC::Principal& aPrincipal,
  680. const bool& aHighAccuracy) override;
  681. virtual bool RecvRemoveGeolocationListener() override;
  682. virtual bool RecvSetGeolocationHigherAccuracy(const bool& aEnable) override;
  683. virtual bool RecvConsoleMessage(const nsString& aMessage) override;
  684. virtual bool RecvScriptError(const nsString& aMessage,
  685. const nsString& aSourceName,
  686. const nsString& aSourceLine,
  687. const uint32_t& aLineNumber,
  688. const uint32_t& aColNumber,
  689. const uint32_t& aFlags,
  690. const nsCString& aCategory) override;
  691. virtual bool RecvPrivateDocShellsExist(const bool& aExist) override;
  692. virtual bool RecvFirstIdle() override;
  693. virtual bool RecvAudioChannelChangeDefVolChannel(const int32_t& aChannel,
  694. const bool& aHidden) override;
  695. virtual bool RecvAudioChannelServiceStatus(const bool& aTelephonyChannel,
  696. const bool& aContentOrNormalChannel,
  697. const bool& aAnyChannel) override;
  698. virtual bool RecvGetLookAndFeelCache(nsTArray<LookAndFeelInt>* aLookAndFeelIntCache) override;
  699. virtual bool RecvSpeakerManagerGetSpeakerStatus(bool* aValue) override;
  700. virtual bool RecvSpeakerManagerForceSpeaker(const bool& aEnable) override;
  701. virtual bool RecvCreateFakeVolume(const nsString& aFsName,
  702. const nsString& aMountPoint) override;
  703. virtual bool RecvSetFakeVolumeState(const nsString& aFsName,
  704. const int32_t& aFsState) override;
  705. virtual bool RecvRemoveFakeVolume(const nsString& fsName) override;
  706. virtual bool RecvKeywordToURI(const nsCString& aKeyword,
  707. nsString* aProviderName,
  708. OptionalInputStreamParams* aPostData,
  709. OptionalURIParams* aURI) override;
  710. virtual bool RecvNotifyKeywordSearchLoading(const nsString &aProvider,
  711. const nsString &aKeyword) override;
  712. virtual bool RecvCopyFavicon(const URIParams& aOldURI,
  713. const URIParams& aNewURI,
  714. const IPC::Principal& aLoadingPrincipal,
  715. const bool& aInPrivateBrowsing) override;
  716. virtual void ProcessingError(Result aCode, const char* aMsgName) override;
  717. virtual bool RecvAllocateLayerTreeId(const ContentParentId& aCpId,
  718. const TabId& aTabId,
  719. uint64_t* aId) override;
  720. virtual bool RecvDeallocateLayerTreeId(const uint64_t& aId) override;
  721. virtual bool RecvGetGraphicsFeatureStatus(const int32_t& aFeature,
  722. int32_t* aStatus,
  723. nsCString* aFailureId,
  724. bool* aSuccess) override;
  725. virtual bool RecvGraphicsError(const nsCString& aError) override;
  726. virtual bool
  727. RecvBeginDriverCrashGuard(const uint32_t& aGuardType,
  728. bool* aOutCrashed) override;
  729. virtual bool RecvEndDriverCrashGuard(const uint32_t& aGuardType) override;
  730. virtual bool RecvAddIdleObserver(const uint64_t& observerId,
  731. const uint32_t& aIdleTimeInS) override;
  732. virtual bool RecvRemoveIdleObserver(const uint64_t& observerId,
  733. const uint32_t& aIdleTimeInS) override;
  734. virtual bool
  735. RecvBackUpXResources(const FileDescriptor& aXSocketFd) override;
  736. virtual bool
  737. RecvOpenAnonymousTemporaryFile(FileDescOrError* aFD) override;
  738. virtual bool
  739. RecvKeygenProcessValue(const nsString& oldValue, const nsString& challenge,
  740. const nsString& keytype, const nsString& keyparams,
  741. nsString* newValue) override;
  742. virtual bool
  743. RecvKeygenProvideContent(nsString* aAttribute,
  744. nsTArray<nsString>* aContent) override;
  745. virtual PFileDescriptorSetParent*
  746. AllocPFileDescriptorSetParent(const mozilla::ipc::FileDescriptor&) override;
  747. virtual bool
  748. DeallocPFileDescriptorSetParent(PFileDescriptorSetParent*) override;
  749. virtual PWebrtcGlobalParent* AllocPWebrtcGlobalParent() override;
  750. virtual bool DeallocPWebrtcGlobalParent(PWebrtcGlobalParent *aActor) override;
  751. virtual bool RecvUpdateDropEffect(const uint32_t& aDragAction,
  752. const uint32_t& aDropEffect) override;
  753. virtual bool RecvGetGraphicsDeviceInitData(ContentDeviceData* aOut) override;
  754. virtual bool RecvGetAndroidSystemInfo(AndroidSystemInfo* aInfo) override;
  755. virtual bool RecvNotifyBenchmarkResult(const nsString& aCodecName,
  756. const uint32_t& aDecodeFPS) override;
  757. virtual bool RecvNotifyPushObservers(const nsCString& aScope,
  758. const IPC::Principal& aPrincipal,
  759. const nsString& aMessageId) override;
  760. virtual bool RecvNotifyPushObserversWithData(const nsCString& aScope,
  761. const IPC::Principal& aPrincipal,
  762. const nsString& aMessageId,
  763. InfallibleTArray<uint8_t>&& aData) override;
  764. virtual bool RecvNotifyPushSubscriptionChangeObservers(const nsCString& aScope,
  765. const IPC::Principal& aPrincipal) override;
  766. virtual bool RecvNotifyPushSubscriptionModifiedObservers(const nsCString& aScope,
  767. const IPC::Principal& aPrincipal) override;
  768. virtual bool RecvNotifyLowMemory() override;
  769. virtual bool RecvGetFilesRequest(const nsID& aID,
  770. const nsString& aDirectoryPath,
  771. const bool& aRecursiveFlag) override;
  772. virtual bool RecvDeleteGetFilesRequest(const nsID& aID) override;
  773. public:
  774. void SendGetFilesResponseAndForget(const nsID& aID,
  775. const GetFilesResponseResult& aResult);
  776. private:
  777. // If you add strong pointers to cycle collected objects here, be sure to
  778. // release these objects in ShutDownProcess. See the comment there for more
  779. // details.
  780. GeckoChildProcessHost* mSubprocess;
  781. ContentParent* mOpener;
  782. ContentParentId mChildID;
  783. int32_t mGeolocationWatchID;
  784. nsString mAppManifestURL;
  785. nsCString mKillHardAnnotation;
  786. /**
  787. * We cache mAppName instead of looking it up using mAppManifestURL when we
  788. * need it because it turns out that getting an app from the apps service is
  789. * expensive.
  790. */
  791. nsString mAppName;
  792. // After we initiate shutdown, we also start a timer to ensure
  793. // that even content processes that are 100% blocked (say from
  794. // SIGSTOP), are still killed eventually. This task enforces that
  795. // timer.
  796. nsCOMPtr<nsITimer> mForceKillTimer;
  797. // How many tabs we're waiting to finish their destruction
  798. // sequence. Precisely, how many TabParents have called
  799. // NotifyTabDestroying() but not called NotifyTabDestroyed().
  800. int32_t mNumDestroyingTabs;
  801. // True only while this is ready to be used to host remote tabs.
  802. // This must not be used for new purposes after mIsAlive goes to
  803. // false, but some previously scheduled IPC traffic may still pass
  804. // through.
  805. bool mIsAlive;
  806. // True only the if process is already a browser or app or has
  807. // been transformed into one.
  808. bool mMetamorphosed;
  809. bool mSendPermissionUpdates;
  810. bool mIsForBrowser;
  811. // These variables track whether we've called Close() and KillHard() on our
  812. // channel.
  813. bool mCalledClose;
  814. bool mCalledKillHard;
  815. bool mCreatedPairedMinidumps;
  816. bool mShutdownPending;
  817. bool mIPCOpen;
  818. friend class CrashReporterParent;
  819. RefPtr<nsConsoleService> mConsoleService;
  820. nsConsoleService* GetConsoleService();
  821. nsTArray<nsCOMPtr<nsIObserver>> mIdleListeners;
  822. #ifdef MOZ_X11
  823. // Dup of child's X socket, used to scope its resources to this
  824. // object instead of the child process's lifetime.
  825. ScopedClose mChildXSocketFdDup;
  826. #endif
  827. PProcessHangMonitorParent* mHangMonitorActor;
  828. nsCString mProfile;
  829. UniquePtr<gfx::DriverCrashGuard> mDriverCrashGuard;
  830. #ifdef NS_PRINTING
  831. RefPtr<embedding::PrintingParent> mPrintingParent;
  832. #endif
  833. // This hashtable is used to run GetFilesHelper objects in the parent process.
  834. // GetFilesHelper can be aborted by receiving RecvDeleteGetFilesRequest.
  835. nsRefPtrHashtable<nsIDHashKey, GetFilesHelper> mGetFilesPendingRequests;
  836. nsTArray<nsCString> mBlobURLs;
  837. bool mLargeAllocationProcess;
  838. };
  839. } // namespace dom
  840. } // namespace mozilla
  841. class ParentIdleListener : public nsIObserver
  842. {
  843. friend class mozilla::dom::ContentParent;
  844. public:
  845. NS_DECL_ISUPPORTS
  846. NS_DECL_NSIOBSERVER
  847. ParentIdleListener(mozilla::dom::ContentParent* aParent,
  848. uint64_t aObserver, uint32_t aTime)
  849. : mParent(aParent), mObserver(aObserver), mTime(aTime)
  850. {}
  851. private:
  852. virtual ~ParentIdleListener() {}
  853. RefPtr<mozilla::dom::ContentParent> mParent;
  854. uint64_t mObserver;
  855. uint32_t mTime;
  856. };
  857. #endif // mozilla_dom_ContentParent_h