static void
MarkChildMessageManagers(nsIMessageBroadcaster* aMM)
{
  aMM->MarkForCC();

  uint32_t tabChildCount = 0;
  aMM->GetChildCount(&tabChildCount);
  for (uint32_t j = 0; j < tabChildCount; ++j) {
    nsCOMPtr<nsIMessageListenerManager> childMM;
    aMM->GetChildAt(j, getter_AddRefs(childMM));
    if (!childMM) {
      continue;
    }

    nsCOMPtr<nsIMessageBroadcaster> strongNonLeafMM = do_QueryInterface(childMM);
    nsIMessageBroadcaster* nonLeafMM = strongNonLeafMM;

    nsCOMPtr<nsIMessageSender> strongTabMM = do_QueryInterface(childMM);
    nsIMessageSender* tabMM = strongTabMM;

    strongNonLeafMM = nullptr;
    strongTabMM = nullptr;
    childMM = nullptr;

    if (nonLeafMM) {
      MarkChildMessageManagers(nonLeafMM);
      continue;
    }

    tabMM->MarkForCC();

    //XXX hack warning, but works, since we know that
    //    callback is frameloader.
    mozilla::dom::ipc::MessageManagerCallback* cb =
      static_cast<nsFrameMessageManager*>(tabMM)->GetCallback();
    if (cb) {
      nsFrameLoader* fl = static_cast<nsFrameLoader*>(cb);
      EventTarget* et = fl->GetTabChildGlobalAsEventTarget();
      if (!et) {
        continue;
      }
      static_cast<nsInProcessTabChildGlobal*>(et)->MarkForCC();
      EventListenerManager* elm = et->GetExistingListenerManager();
      if (elm) {
        elm->MarkForCC();
      }
    }
  }
}
static void
MarkChildMessageManagers(ChromeMessageBroadcaster* aMM)
{
  aMM->MarkForCC();

  uint32_t tabChildCount = aMM->ChildCount();
  for (uint32_t j = 0; j < tabChildCount; ++j) {
    RefPtr<MessageListenerManager> childMM = aMM->GetChildAt(j);
    if (!childMM) {
      continue;
    }

    RefPtr<ChromeMessageBroadcaster> strongNonLeafMM =
      ChromeMessageBroadcaster::From(childMM);
    ChromeMessageBroadcaster* nonLeafMM = strongNonLeafMM;

    MessageListenerManager* tabMM = childMM;

    strongNonLeafMM = nullptr;
    childMM = nullptr;

    if (nonLeafMM) {
      MarkChildMessageManagers(nonLeafMM);
      continue;
    }

    tabMM->MarkForCC();

    //XXX hack warning, but works, since we know that
    //    callback is frameloader.
    mozilla::dom::ipc::MessageManagerCallback* cb = tabMM->GetCallback();
    if (cb) {
      nsFrameLoader* fl = static_cast<nsFrameLoader*>(cb);
      EventTarget* et = fl->GetTabChildGlobalAsEventTarget();
      if (!et) {
        continue;
      }
      static_cast<nsInProcessTabChildGlobal*>(et)->MarkForCC();
      EventListenerManager* elm = et->GetExistingListenerManager();
      if (elm) {
        elm->MarkForCC();
      }
    }
  }
}
static void
MarkMessageManagers()
{
  // The global message manager only exists in the root process.
  if (XRE_GetProcessType() != GeckoProcessType_Default) {
    return;
  }
  nsCOMPtr<nsIMessageBroadcaster> strongGlobalMM =
    do_GetService("@mozilla.org/globalmessagemanager;1");
  if (!strongGlobalMM) {
    return;
  }
  nsIMessageBroadcaster* globalMM = strongGlobalMM;
  strongGlobalMM = nullptr;

  globalMM->MarkForCC();
  uint32_t childCount = 0;
  globalMM->GetChildCount(&childCount);
  for (uint32_t i = 0; i < childCount; ++i) {
    nsCOMPtr<nsIMessageListenerManager> childMM;
    globalMM->GetChildAt(i, getter_AddRefs(childMM));
    if (!childMM) {
      continue;
    }
    nsCOMPtr<nsIMessageBroadcaster> strongWindowMM = do_QueryInterface(childMM);
    nsIMessageBroadcaster* windowMM = strongWindowMM;
    childMM = nullptr;
    strongWindowMM = nullptr;
    windowMM->MarkForCC();
    uint32_t tabChildCount = 0;
    windowMM->GetChildCount(&tabChildCount);
    for (uint32_t j = 0; j < tabChildCount; ++j) {
      nsCOMPtr<nsIMessageListenerManager> childMM;
      windowMM->GetChildAt(j, getter_AddRefs(childMM));
      if (!childMM) {
        continue;
      }
      nsCOMPtr<nsIMessageSender> strongTabMM = do_QueryInterface(childMM);
      nsIMessageSender* tabMM = strongTabMM;
      childMM = nullptr;
      strongTabMM = nullptr;
      tabMM->MarkForCC();
      //XXX hack warning, but works, since we know that
      //    callback is frameloader.
      mozilla::dom::ipc::MessageManagerCallback* cb =
        static_cast<nsFrameMessageManager*>(tabMM)->GetCallback();
      if (cb) {
        nsFrameLoader* fl = static_cast<nsFrameLoader*>(cb);
        EventTarget* et = fl->GetTabChildGlobalAsEventTarget();
        if (!et) {
          continue;
        }
        static_cast<nsInProcessTabChildGlobal*>(et)->MarkForCC();
        nsEventListenerManager* elm = et->GetExistingListenerManager();
        if (elm) {
          elm->MarkForCC();
        }
      }
    }
  }
  if (nsFrameMessageManager::sParentProcessManager) {
    nsFrameMessageManager::sParentProcessManager->MarkForCC();
    uint32_t childCount = 0;
    nsFrameMessageManager::sParentProcessManager->GetChildCount(&childCount);
    for (uint32_t i = 0; i < childCount; ++i) {
      nsCOMPtr<nsIMessageListenerManager> childMM;
      nsFrameMessageManager::sParentProcessManager->
        GetChildAt(i, getter_AddRefs(childMM));
      if (!childMM) {
        continue;
      }
      nsIMessageListenerManager* child = childMM;
      childMM = nullptr;
      child->MarkForCC();
    }
  }
  if (nsFrameMessageManager::sSameProcessParentManager) {
    nsFrameMessageManager::sSameProcessParentManager->MarkForCC();
  }
  if (nsFrameMessageManager::sChildProcessManager) {
    nsFrameMessageManager::sChildProcessManager->MarkForCC();
  }
}