版权声明:本文为博主打劫来的文章!!!未经允许可以随便转载。 May you do good and not evil. May you share freely, never taking more than you give. https://blog.csdn.net/hunter___/article/details/82855089
1分别分析b2g process
2和nuwa process,
3 分析b2g 与nuwa的通信交互
4分析messageloop,ipdl
5分析APP如何启动
结构:
|-XRE_main | |-XREMain::XRE_main | |-new ScopedAppData(aAppData); |
|-new ScopedAppData(aAppData); | |-XREMain::XRE_mainInit//简言之,开启路径获取模块,获取路径 | |-XREMain::XRE_mainStartup//设置关闭检查,创建版本 | |-mScopedXPCOM = MakeUnique<ScopedXPCOMStartup>(); | |-mScopedXPCOM->Initialize() ; (toolkit/xre/nsAppRunner.cpp)
|
|-XREMain::XRE_mainRun()
NS_InitXPCOM2
启动xpcom部分:
| |-NS_InitXPCOM2 (xpcom/build/XPCOMInit.cpp) | |-sMessageLoop = new MessageLoopForUI(MessageLoop::TYPE_MOZILLA_UI);//创建UI的messageloop,与界面请求相关,点击等 | |-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
NS_initxpcom 总结性: NS_InitXPCOM2,在b2g进程中调用NS_InitXPCOM2完成了以下工作: 设置挂起时间, 设置线程类型,创建并初始化主线程,启动localfile服务用于获取各文件(app 或gre 文件,在omni.jar中)路径,创建组件服务和管理,自动注册各组件,初始化js 引擎,共享线程和抽象线程初始化,实例化jsloader服务,启动挂起监控,启动后台挂起监控,调用消息队列循环messageloop,再新建一个后台挂起监控。
前启动app 部分:
|-XREMain::XRE_mainRun() | |-mozilla::ipc::ProcLoaderClientGeckoInit();//调用ProcLoaderClientGeckoInit与service端建立连接? | |-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);//通知器通知启动最终ui(窗口) | |-cmdLine->Run(); (toolkit/components/commandlines/nsCommandLine.cpp)//加载进第一个页面iframe进去??? | | | |- nsCommandLine::EnumerateValidators | | | |-nsCommandLine::EnumerateHandlers | | | |-EnumRun | | | |-nsICommandLineHandler->Handle (first page will be loaded in here) //启动第一个页面/窗口(window) | |-mNativeApp->Enable(); | |-appStartup->Run(); (toolkit/components/startup/nsAppStartup.cpp)//启动各种app,先systme app,homescreen 等? | |-mAppShell->Run(); (widget/gonk/nsAppShell.cpp->widget/nsBaseAppShell.cpp) | |-MessageLoop::current()->Run(); // run forever~~~~~
为启动APP做准备:
XRE_MainRun,其所做工作如下:XRE_mainRun()先调用ProcLoaderClientGeckoInit与service端建立连接,启动app观察监听器,初始化命令行,启动命令行(cmdLine),创建隐藏窗口。启动最终ui(窗口),cmdLine->Run()一波复杂的操作最后会启动出第一个(hiden)页面/窗口(window),appStartup->Run()调mAppShell->Run()无限循环messageloop,等待APP启动请求?
nuwa进程做了些什么:
ProcLoaderServiceRun初始化命令行,new ContentProcess()传入了b2g pid作为参数,为创建子进程做各种准备:为子进程创建io子线程,创建uiloop消息循环线程,new一个ProcLoaderChild()作为进程加载器的子端,创建loaderChild,open发一个messageloop去建立连接通道,使建立连接通道,接着loaderChild这侧就在等b2g端的load信号了
疑问:
为何b2g调了NS_InitXPCOM2 启动xpcom相关模块后,nuwa 还要继续来一次?
新进程从哪步开始出来的?新进程的产生过程及相互进程间的消息传递。
每个进程都有一个主线程吗?它的作用是什么,它们下面子线程相互通信可以实现吗?是如何实现的?
Omnijar.h
```
/** * Initializes the Omnijar API with the given directory or file for GRE and * APP. Each of the paths given can be: * - a file path, pointing to the omnijar file, * - a directory path, pointing to a directory containing an "omni.jar" file, * - nullptr for autodetection of an "omni.jar" file. */ static void Init(nsIFile* aGrePath = nullptr, nsIFile* aAppPath = nullptr);
```
messageloop 机制: