b2g process: XREMain::XRE_mainRun

本文深入剖析了B2G进程的主要模块与启动流程,详细解释了从XRE_main到XRE_mainRun的各阶段功能,包括XPCOM初始化、线程启动、组件管理、内存管理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

b2g process 中到后面主要的几个大模块:

结构图抄自:https://blog.youkuaiyun.com/zembers/article/details/48495627

|-XRE_main
|
|-XREMain::XRE_main
       |
       |-new ScopedAppData(aAppData);
       |
       |-XREMain::XRE_mainInit
       |
       |-XREMain::XRE_mainStartup
       |
       |-mScopedXPCOM = MakeUnique<ScopedXPCOMStartup>();
       |
       |-mScopedXPCOM->Initialize();          (toolkit/xre/nsAppRunner.cpp)
       |           |
       |           |-NS_InitXPCOM2                       (xpcom/build/XPCOMInit.cpp)
       |                  |
       |                  |-sMessageLoop = new MessageLoopForUI(MessageLoop::TYPE_MOZILLA_UI);
       |                  |
       |                  |-ioThread = MakeUnique<BrowserProcessSubThread>(BrowserProcessSubThread::IO);
       |                  |-ioThread->StartWithOptions
       |                  |
       |                  |-nsThreadManager::get()->Init();
       |                  |
       |                  |-nsTimerImpl::Startup();
       |                  |
       |                  |-nsComponentManagerImpl::gComponentManager = new nsComponentManagerImpl();
       |                  |
       |                  |-nsCycleCollector_init
       |                  |
       |                  |-nsCycleCollector_startup
       |                  |
       |                  |-JS_Init
       |                  |
       |                  |-nsComponentManagerImpl::gComponentManager->Init();(xpcom/components/nsComponentManager.cpp)
       |                  |     |
       |                  |     |-nsComponentManagerImpl::InitializeStaticModules()
       |                  |     |
       |                  |     |-RegisterModule(...)
       |                  |     |
       |                  |     |-greOmnijar =mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
       |                  |     |
       |                  |     |-cl->location.Init(greOmnijar, "chrome.manifest");
       |                  |     |
       |                  |     |-nsComponentManagerImpl::RereadChromeManifests
       |                  |               |
       |                  |               |-nsComponentManagerImpl::RegisterManifest
       |                  |                       |
       |                  |                       |-DoRegisterManifest
       |                  |                                |
       |                  |                                |-ParseManifest
       |                  |                                      |
       |                  |                                      |-...
       |                  |                                      |
       |                  |                                      |-nsComponentManagerImpl::ManifestContract 
       |                  |
       |                  |-XPTInterfaceInfoManager::GetSingleton();
       |                  |
       |                  |-nsDirectoryService::gService->RegisterCategoryProviders();
       |                  |
       |                  |-SharedThreadPool::InitStatics();
       |                  |
       |                  |-AbstractThread::InitStatics();
       |                  |
       |                  |-mozilla::scache::StartupCache::GetSingleton();
       |                  |
       |                  |-mozilla::AvailableMemoryTracker::Activate();
       |                  |
       |                  |-NS_CreateServicesFromCategory(...)
       |                  |
       |                  |-mozilla::HangMonitor::Startup();
       |                  |
       |                  |-mozilla::BackgroundHangMonitor::Startup();
       |                  |
       |                  |-sMainHangMonitor = new mozilla::BackgroundHangMonitor
       |                  
       |-XREMain::XRE_mainRun()
                |
                |-mozilla::ipc::ProcLoaderClientGeckoInit();
                |
                |-mScopedXPCOM->SetWindowCreator(mNativeApp);
                |
                |-startupNotifier->Observe(nullptr, APPSTARTUP_TOPIC, nullptr);
                |
                |-mDirProvider.DoStartup();
                |
                |-cmdLine->Init(...)
                |
                |-obsService->NotifyObservers(cmdLine, "command-line-startup", nullptr);
                |
                |-appStartup->CreateHiddenWindow();
                |
                |-obsService->NotifyObservers(nullptr, "final-ui-startup", nullptr);
                |
                |-cmdLine->Run();     (toolkit/components/commandlines/nsCommandLine.cpp)
                |          |
                |          |- nsCommandLine::EnumerateValidators
                |          |
                |          |-nsCommandLine::EnumerateHandlers
                |                  |
                |                  |-EnumRun
                |                        |
                |                        |-nsICommandLineHandler->Handle   (first page will be loaded in here)        
                |                                  
                |-mNativeApp->Enable();
                |
                |-appStartup->Run();     (toolkit/components/startup/nsAppStartup.cpp)
                          |
                          |-mAppShell->Run();         (widget/gonk/nsAppShell.cpp->widget/nsBaseAppShell.cpp)
                                   |
                                   |-MessageLoop::current()->Run();  // run forever~~~~~

main()->RunProcesses()->b2g_main()->do_main()->XRE_main()->XREMain::XRE_main()->XREMain::XRE_mainRun()

分析XREMain::XRE_mainRun()做的工作:

gecko/toolkit/xre/nsAppRunner.cpp

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICommandLine

comandline 什么鬼?如何使用,有什么用?浏览器上用的?对一些扩展应用进行设置查看???

把默认设置等迁移migration过来?

XRE_mainRun()主要工作:

 

XREMain::XRE_mainRun()
|
|-mozilla::ipc::ProcLoaderClientGeckoInit();//初始化ProcLoaderClientGecko
|
|-mScopedXPCOM->SetWindowCreator(mNativeApp);
|
|-startupNotifier->Observe(nullptr, APPSTARTUP_TOPIC, nullptr);
|
|-mDirProvider.DoStartup();//启动
|
|-cmdLine->Init(...)//cmdLine初始化
|
|-obsService->NotifyObservers(cmdLine, "command-line-startup", nullptr);//启动对commandline通知观察监听,
|
|-appStartup->CreateHiddenWindow();//appStartup 创建window
|
|-obsService->NotifyObservers(nullptr, "final-ui-startup", nullptr);//启动对ui通知观察监听
|
|-cmdLine->Run();     (toolkit/components/commandlines/nsCommandLine.cpp)//运行cmdline
|          |
|          |- nsCommandLine::EnumerateValidators//枚举验证器?
|          |
|          |-nsCommandLine::EnumerateHandlers//枚举处理程序
|                  |
|                  |-EnumRun//运行枚举
|                        |
|                        |-nsICommandLineHandler->Handle   (first page will be loaded in here)      //cmdline处理器处理事情? 
|                                  
|-mNativeApp->Enable();//使能nativeapp
|
|-appStartup->Run();     (toolkit/components/startup/nsAppStartup.cpp)//开始启动窗口?是gaia了吗?
          |
          |-mAppShell->Run();         (widget/gonk/nsAppShell.cpp->widget/nsBaseAppShell.cpp)//获取当前的线程并messageloop?
                   |
                   |-MessageLoop::current()->Run();  // run forever~~~~~

 

widget/nsBaseAppShell.cpp

// nsIAppShell methods:

NS_IMETHODIMP
nsBaseAppShell::Run(void)
{
    //P_LOGI(SEPARATOR_LINE);
  NS_ENSURE_STATE(!mRunning);  // should not call Run twice
  mRunning = true;
//P_LOGI("  nsIThread *thread = NS_GetCurrentThread()--->");
  nsIThread *thread = NS_GetCurrentThread();
//P_LOGI("  MessageLoop::current()->Run()--->");
  MessageLoop::current()->Run();
//P_LOGI("  NS_ProcessPendingEvents(thread)  NS_ProcessPendingEvents(thread)-->");
  NS_ProcessPendingEvents(thread);

  mRunning = false;
  return NS_OK;
}

两次对NS_InitXPCOM2的调用:

一次是b2g ,一次是nuwa??

(2)

gecko/toolkit/xre/nsEmbedFunctions.cpp:172, Fuc:XRE_InitEmbedding2

gecko/toolkit/xre/nsEmbedFunctions.cpp:196, Fuc:XRE_InitEmbedding2    rv = NS_InitXPCOM2(nullptr, aAppDirectory, gDirServiceProvider)-->
gecko/xpcom/build/XPCOMInit.cpp:512, Fuc:NS_InitXPCOM2

 

 

 

 

 

 

ProcLoaderClientGeckoInit()-->

(1)sProcLoaderParent->Open(transport,
                          sProcLoaderPid,
                          XRE_GetIOMessageLoop(),
                          ParentSide);

(2)  sProcLoaderLoop = MessageLoop::current();

 

 

 

gecko/toolkit/xre/nsAppRunner.cpp:1584, Fuc:Initialize  startup xpcom seccessed?? call do_QueryInterface(mServiceManager)-->
gecko/toolkit/xre/nsAppRunner.cpp:4463, Fuc:XRE_main  call XRE_mainRun--->
gecko/toolkit/xre/nsAppRunner.cpp:4060, Fuc:XRE_mainRun  ----------------------------------
gecko/toolkit/xre/nsAppRunner.cpp:4065, Fuc:XRE_mainRun  call mozilla::ipc::ProcLoaderClientGeckoInit()--->
gecko/ipc/glue/ProcessUtils_linux.cpp:231, Fuc:ProcLoaderClientGeckoInit  ----------------------------------
gecko/ipc/glue/ProcessUtils_linux.cpp:251, Fuc:ProcLoaderClientGeckoInit    sProcLoaderParent = new ProcLoaderParent()
gecko/ipc/glue/ProcessUtils_linux.cpp:253, Fuc:ProcLoaderClientGeckoInit  sProcLoaderParent->Open(transport,sProcLoaderPid,XRE_GetIOMessageLoop(),ParentSide)   
gecko/ipc/glue/ProcessUtils_linux.cpp:258, Fuc:ProcLoaderClientGeckoInit  call  sProcLoaderLoop = MessageLoop::current()--->
gecko/toolkit/xre/nsAppRunner.cpp:4090, Fuc:XRE_mainRun  call SetWindowCreator--->
gecko/toolkit/xre/nsAppRunner.cpp:4096, Fuc:XRE_mainRun    nsCOMPtr<nsIPrefService> prefs = do_GetService('@mozilla.org/preferences-service;1', &rv)
gecko/toolkit/xre/nsAppRunner.cpp:4112, Fuc:XRE_mainRun  set of CrashReporter after xpcom Initialization,here? again?
gecko/toolkit/xre/nsAppRunner.cpp:4134, Fuc:XRE_mainRun  startupNotifier ,do_CreateInstance --
gecko/toolkit/xre/nsAppRunner.cpp:4141, Fuc:XRE_mainRun  call appStartup  (do_GetService(NS_APPSTARTUP_CONTRACTID))
gecko/toolkit/xre/nsAppRunner.cpp:4147, Fuc:XRE_mainRun  gDoMigration = true --->
gecko/toolkit/xre/nsAppRunner.cpp:4148, Fuc:XRE_mainRun  do sth about GetAppDir,GetFile(XRE_APP_DISTRIBUTION_DIR),EnableProfileMigrator,ect.
gecko/toolkit/xre/nsAppRunner.cpp:4183, Fuc:XRE_mainRun  ---Profile Migration--- infact not execu
gecko/toolkit/xre/nsAppRunner.cpp:4210, Fuc:XRE_mainRun  call mDirProvider.DoStartup()--->
gecko/toolkit/xre/nsAppRunner.cpp:4223, Fuc:XRE_mainRun  call appStartup->GetShuttingDown()-->
gecko/toolkit/xre/nsAppRunner.cpp:4233, Fuc:XRE_mainRun  mShuttingDown = false,call cmdLine = do_CreateInstance('@mozilla.org/toolkit/command-line;1')
gecko/toolkit/xre/nsAppRunner.cpp:4236, Fuc:XRE_mainRun   call rv = cmdLine->Init(gArgc, gArgv, workingDir)-->
gecko/toolkit/xre/nsAppRunner.cpp:4246, Fuc:XRE_mainRun  obsService = mozilla::services::GetObserverService(), call obsService->NotifyObservers(cmdLine, 'command-line-startup', nullptr)
gecko/toolkit/xre/nsAppRunner.cpp:4262, Fuc:XRE_mainRun    SaveStateForAppInitiatedRestart()--->
gecko/toolkit/xre/nsAppRunner.cpp:4276, Fuc:XRE_mainRun  mShuttingDown = false,call rv = appStartup->CreateHiddenWindow()-->
gecko/toolkit/xre/nsAppRunner.cpp:4307, Fuc:XRE_mainRun  call obsService =mozilla::services::GetObserverService()-->
gecko/toolkit/xre/nsAppRunner.cpp:4312, Fuc:XRE_mainRun  appStartup->DoneStartingUp()-->
gecko/toolkit/xre/nsAppRunner.cpp:4314, Fuc:XRE_mainRun   call   appStartup->GetShuttingDown(&mShuttingDown)
gecko/toolkit/xre/nsAppRunner.cpp:4319, Fuc:XRE_mainRun   mShuttingDown = false,   rv = cmdLine->Run()
gecko/toolkit/xre/nsAppRunner.cpp:4323, Fuc:XRE_mainRun   call   appStartup->GetShuttingDown(&mShuttingDown)
gecko/toolkit/xre/nsAppRunner.cpp:4346, Fuc:XRE_mainRun  not get here?
gecko/toolkit/xre/nsAppRunner.cpp:4355, Fuc:XRE_mainRun  appStartup->Run()-->
gecko/ipc/glue/ProcessUtils_linux.cpp:321, Fuc:ProcLoaderLoad  ----------------------------------
gecko/ipc/glue/ProcessUtils_linux.cpp:627, Fuc:ProcLoaderServiceRun  call BackgroundHangMonitor::Allow()--->
gecko/ipc/glue/ProcessUtils_linux.cpp:629, Fuc:ProcLoaderServiceRun  call XRE_DeinitCommandLine--->
gecko/ipc/glue/ProcessUtils_linux.cpp:636, Fuc:ProcLoaderServiceRun  call task->DoWork()--->
gecko/ipc/glue/ProcessUtils_linux.cpp:449, Fuc:DoWork  ----------------------------------
gecko/ipc/glue/ProcessUtils_linux.cpp:465, Fuc:DoWork    call SetCurrentProcessPrivileges(mPrivs)--->
gecko/ipc/glue/ProcessUtils_linux.cpp:469, Fuc:DoWork   Start Nuwa (main function),call int ret = content_process_main(argc, argv)--->
gecko/toolkit/xre/nsAppRunner.cpp:4580, Fuc:XRE_InitCommandLine  ----------------------------------
gecko/toolkit/xre/nsAppRunner.cpp:4592, Fuc:XRE_InitCommandLine  XRE_GetBinaryPath(aArgv:/system/b2g/plugin-container)--->
gecko/toolkit/xre/nsAppRunner.cpp:4649, Fuc:XRE_InitCommandLine  mozilla::Omnijar::Init(greOmni, appOmni)-->
gecko/ipc/glue/ProcessChild.cpp:46, Fuc:ProcessChild  >>>>>>>>>>>>>>>>>>>>>>>>>>>st.func
gecko/ipc/glue/ProcessUtils_linux.cpp:273, Fuc:ProcLoaderClientDeinit  ----------------------------------

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值