之前看的是init到系统启动到system app再到system app 将其他核心应用启动起来的过程。那么支持上层应用的底层(gecko 层的这些组件又做了什么呢?)
启动过程总览:
物理按键---->固件(firmware)--->(加载/启动)内核(kernel)---->init进程--->解析init.rc/init.b2g.rc(有b2g.sh)---->b2g.sh--->system/b2g/b2g(可执行文件由gecko/b2g/...生成)--->(something i need to study now!!!)--->libxul(.so)--->b2g.js--->shell.html--->shell.js(SystemAppFrame(id=systemapp),load blank.html into this iframe)--->system app--->core app(homescreen ect...)
而另一条线即是,b2g(可执行文件被执行之后(b2g进程)做了哪些支撑工作)
b2g进程底层做的工作。
理清:
XPCOM的初始化,
以及如何支持上层的应用(有点大了)。
init进程启动许多进程,其中之一就是gecko的入口b2g,实际就是通过运行b2g.sh脚本执行了b2g可执行文件,b2g是gecko/b2g/app/B2GLoader.cpp编译生成的。
主要分析b2g进程做了什么,从而支撑上层应用正常运行。
gecko/xpcom/build/nsXULAppAPI.h
/**
* Begin an XUL application. Does not return until the user exits the
* application.
*
* @param argc/argv Command-line parameters to pass to the application. On
* Windows, these should be in UTF8. On unix-like platforms
* these are in the "native" character set.
*
* @param aAppData Information about the application to be run.
*
* @param aFlags Platform specific flags.
*
* @return A native result code suitable for returning from main().
*
* @note If the binary is linked against the standalone XPCOM glue,
* XPCOMGlueStartup() should be called before this method.
*/
XRE_API(int,
XRE_main, (int argc, char* argv[], const nsXREAppData* aAppData,
uint32_t aFlags))
XRE_API()定义在xrecore.h中
gecko/xpcom/build/xrecore.h
#include "nscore.h"
/**
* Import/export macros for libXUL APIs.
*/
#ifdef XPCOM_GLUE
#define XRE_API(type, name, params) \
typedef type (NS_FROZENCALL * name##Type) params; \
extern name##Type name NS_HIDDEN;
#elif defined(IMPL_LIBXUL)
#define XRE_API(type, name, params) EXPORT_XPCOM_API(type) name params;
#else
#define XRE_API(type, name, params) IMPORT_XPCOM_API(type) name params;
#endif
gecko/xpcom/base/nscore.h
/* Core XPCOM declarations. */
/*----------------------------------------------------------------------*/
/* Import/export defines */
...
#define EXPORT_XPCOM_API(type) NS_EXTERN_C NS_EXPORT type NS_FROZENCALL
#define IMPORT_XPCOM_API(type) NS_EXTERN_C NS_IMPORT type NS_FROZENCALL
#define GLUE_XPCOM_API(type) NS_EXTERN_C NS_HIDDEN_(type) NS_FROZENCALL
#ifdef IMPL_LIBXUL
#define XPCOM_API(type) EXPORT_XPCOM_API(type)
#elif defined(XPCOM_GLUE)
#define XPCOM_API(type) GLUE_XPCOM_API(type)
#else
#define XPCOM_API(type) IMPORT_XPCOM_API(type)
#endif
gecko/xpcom/build/nsXPCOMCID.h 定义XPCOM核心CID。
main 函数主要执行3个大模块的函数:
ReserveFileDescriptors(reservedFds);
bool ok = LoadStaticData(argc, argv);
return RunProcesses(argc, argv, reservedFds); b2g进程与Nuwa之间具体如何通过ipc进行 通信并使nuwa创建出新进程
(0)main()如下:
/**
* B2G Loader is responsible for loading the b2g process and the
* Nuwa process. It forks into the parent process, for the b2g
* process, and the child process, for the Nuwa process.
*
* The loader loads libxul and performs initialization of static data
* before forking, so relocation of libxul and static data can be
* shared between the b2g process, the Nuwa process, and the content
* processes.
*/
int
main(int argc, const char* argv[])
{
// argc :1
// argv :system/b2g/b2g
/**
* Reserve file descriptors before loading static data.
*/
FdArray reservedFds;
ReserveFileDescriptors(reservedFds);
/*
* Before fork(), libxul and static data of Gecko are loaded for
* sharing.
*/
bool ok = LoadStaticData(argc, argv);
if (!ok) {
return 255;
}
return RunProcesses(argc, argv, reservedFds);
}
(1) ReserveFileDescriptors(reservedFds);
保留文件描述符(5个吗)。为什么?以供后用?3-7?
(2) bool ok = LoadStaticData(argc, argv);
1.1 主要分析loadstaticdata部分,它是在runprocesses前要做的工作,主要是loadlibxul,和xre_procloaderpreload。前者load了libxul具体是去调xpcomglue相关的enablepreload,startup,loadxulfunction等,这几个的具体工作还需继续,总体来说就是执行了xpcom相关的几个重要函数。xre_procloaderpreload的主要任务则是在xpcom初始化之前加载好xpt接口信息。
加载固定的数据
static bool
LoadStaticData(int argc, const char *argv[])
{
P_LOGI( );
char xpcomPath[MAXPATHLEN];
bool ok = GetXPCOMPath(argv[0], xpcomPath, MAXPATHLEN);
NS_ENSURE_TRUE(ok, false);
ok = LoadLibxul(xpcomPath);
NS_ENSURE_TRUE(ok, false);
char progDir[MAXPATHLEN];
ok = GetDirnameSlash(xpcomPath, progDir, MAXPATHLEN);
NS_ENSURE_TRUE(ok, false);
nsCOMPtr<nsIFile> appini = GetAppIni(argc, argv);
const nsXREAppData *appData;
if (appini) {
nsresult rv =
XRE_CreateAppData(appini, const_cast<nsXREAppData**>(&appData));
NS_ENSURE_SUCCESS(rv, false);
} else {
appData = &sAppData;
}
XRE_ProcLoaderPreload(progDir, appData);
if (appini) {
XRE_FreeAppData(const_cast<nsXREAppData*>(appData));
}
return true;
}
LoadStaticData
主要看
{
ok = LoadLibxul(xpcomPath);
XRE_ProcLoaderPreload(progDir, appData);// toolkit/xre/ns