ContentProcessManager.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef mozilla_dom_ContentProcessManager_h
  6. #define mozilla_dom_ContentProcessManager_h
  7. #include <map>
  8. #include <set>
  9. #include "mozilla/StaticPtr.h"
  10. #include "mozilla/dom/TabContext.h"
  11. #include "mozilla/dom/ipc/IdType.h"
  12. #include "nsTArray.h"
  13. namespace mozilla {
  14. namespace dom {
  15. class ContentParent;
  16. struct RemoteFrameInfo
  17. {
  18. TabId mOpenerTabId;
  19. TabContext mContext;
  20. };
  21. struct ContentProcessInfo
  22. {
  23. ContentParent* mCp;
  24. ContentParentId mParentCpId;
  25. std::set<ContentParentId> mChildrenCpId;
  26. std::map<TabId, RemoteFrameInfo> mRemoteFrames;
  27. };
  28. class ContentProcessManager final
  29. {
  30. public:
  31. static ContentProcessManager* GetSingleton();
  32. ~ContentProcessManager() {MOZ_COUNT_DTOR(ContentProcessManager);};
  33. /**
  34. * Add a new content process into the map.
  35. * If aParentCpId is not 0, it's a nested content process.
  36. */
  37. void AddContentProcess(ContentParent* aChildCp,
  38. const ContentParentId& aParentCpId = ContentParentId(0));
  39. /**
  40. * Remove the content process by id.
  41. */
  42. void RemoveContentProcess(const ContentParentId& aChildCpId);
  43. /**
  44. * Add a grandchild content process into the map.
  45. * aParentCpId must be already added in the map by AddContentProcess().
  46. */
  47. bool AddGrandchildProcess(const ContentParentId& aParentCpId,
  48. const ContentParentId& aChildCpId);
  49. /**
  50. * Get the parent process's id by child process's id.
  51. * Used to check if a child really belongs to the parent.
  52. */
  53. bool GetParentProcessId(const ContentParentId& aChildCpId,
  54. /*out*/ ContentParentId* aParentCpId);
  55. /**
  56. * Return the ContentParent pointer by id.
  57. */
  58. ContentParent* GetContentProcessById(const ContentParentId& aChildCpId);
  59. /**
  60. * Return a list of all child process's id.
  61. */
  62. nsTArray<ContentParentId>
  63. GetAllChildProcessById(const ContentParentId& aParentCpId);
  64. /**
  65. * Allocate a tab id for the given content process's id.
  66. * Used when a content process wants to create a new tab. aOpenerTabId and
  67. * aContext are saved in RemoteFrameInfo, which is a part of
  68. * ContentProcessInfo. We can use the tab id and process id to locate the
  69. * TabContext for future use.
  70. */
  71. TabId AllocateTabId(const TabId& aOpenerTabId,
  72. const IPCTabContext& aContext,
  73. const ContentParentId& aChildCpId);
  74. /**
  75. * Remove the RemoteFrameInfo by the given process and tab id.
  76. */
  77. void DeallocateTabId(const ContentParentId& aChildCpId,
  78. const TabId& aChildTabId);
  79. /**
  80. * Get the TabContext by the given content process and tab id.
  81. */
  82. bool
  83. GetTabContextByProcessAndTabId(const ContentParentId& aChildCpId,
  84. const TabId& aChildTabId,
  85. /*out*/ TabContext* aTabContext);
  86. /**
  87. * Get all TabContext which are inside the given content process.
  88. * Used for AppProcessChecker to cehck app status.
  89. */
  90. nsTArray<TabContext>
  91. GetTabContextByContentProcess(const ContentParentId& aChildCpId);
  92. /**
  93. * Query a tab's opener id by the given process and tab id.
  94. * XXX Currently not used. Plan to be used for bug 1020179.
  95. */
  96. bool GetRemoteFrameOpenerTabId(const ContentParentId& aChildCpId,
  97. const TabId& aChildTabId,
  98. /*out*/ TabId* aOpenerTabId);
  99. /**
  100. * Get all TabParents' Ids managed by the givent content process.
  101. * Return empty array when TabParent couldn't be found via aChildCpId
  102. */
  103. nsTArray<TabId>
  104. GetTabParentsByProcessId(const ContentParentId& aChildCpId);
  105. /**
  106. * Get the TabParent by the given content process and tab id.
  107. * Return nullptr when TabParent couldn't be found via aChildCpId
  108. * and aChildTabId.
  109. * (or probably because the TabParent is not in the chrome process)
  110. */
  111. already_AddRefed<TabParent>
  112. GetTabParentByProcessAndTabId(const ContentParentId& aChildCpId,
  113. const TabId& aChildTabId);
  114. /**
  115. * Get the TabParent on top level by the given content process and tab id.
  116. *
  117. * This function return the TabParent belong to the chrome process,
  118. * called top-level TabParent here, by given aChildCpId and aChildTabId.
  119. * The given aChildCpId and aChildTabId are related to a content process
  120. * and a tab respectively. In nested-oop, the top-level TabParent isn't
  121. * always the opener tab of the given tab in content process. This function
  122. * will call GetTabParentByProcessAndTabId iteratively until the Tab returned
  123. * is belong to the chrome process.
  124. */
  125. already_AddRefed<TabParent>
  126. GetTopLevelTabParentByProcessAndTabId(const ContentParentId& aChildCpId,
  127. const TabId& aChildTabId);
  128. /**
  129. * Return appId by given TabId and ContentParentId.
  130. * It will return nsIScriptSecurityManager::NO_APP_ID
  131. * if the given tab is not an app.
  132. */
  133. uint32_t
  134. GetAppIdByProcessAndTabId(const ContentParentId& aChildCpId,
  135. const TabId& aChildTabId);
  136. private:
  137. static StaticAutoPtr<ContentProcessManager> sSingleton;
  138. TabId mUniqueId;
  139. std::map<ContentParentId, ContentProcessInfo> mContentParentMap;
  140. ContentProcessManager() {MOZ_COUNT_CTOR(ContentProcessManager);};
  141. };
  142. } // namespace dom
  143. } // namespace mozilla
  144. #endif // mozilla_dom_ContentProcessManager_h