OpenHarmony源码解析之Ability运行时调用流程介绍 原创
作者:苟晶晶
1 简介
Ability是应用所具备能力的抽象,也是程序的重要组成部分。OpenHarmony的Ability子系统提供了Ability启动及其生命周期管理的功能。Ability框架模型结构分为FA模型和Stage模型,FA模型Ability 分为 FA (Feature Ability) 和 PA (Particle Ability)两种类,其中 FA 支持 Page Ability,PA 支持 Service Ability 和 Data Ability。本文主要介绍基于FA模型的Page Ability运行时介绍,启动流程贯穿了应用程序用户进程、系统服务进程,其中客户端与服务端发送消息主要采用IPC通信。
1.1 OpenHarmony架构图
1.2 代码目录
foundation/ability #元能力子系统
├── ability_runtime #ability_runtime元能力运行时部件
│ ├── frameworks
│ │ ├── js
│ │ │ └── napi # ability_runtime的napi代码实现
│ │ └── native # ability_runtime的核心代码实现
│ ├── interfaces
│ │ ├── inner_api # ability_runtime的系统内部件间接口
│ │ └── kits
│ │ └── native # ability_runtime的对外接口
│ ├── services
│ │ ├── abilitymgr # Ability管理服务框架代码
│ │ ├── appmgr # App管理服务框架代码
│ │ ├── common # 服务公共组件目录
│ │ ├── dataobsmgr # DataAbilityObserver管理服务框架代码
│ │ └── uripermmgr # UriPermission管理服务框架代码
│ ├── test # 测试目录
│ └── tools # aa命令工具代码目录
2 Ability运行时及生命周期介绍
2.1 启动应用的简图
AbilityManagerService是应用线程管理服务核心类,是操作各类Ability方法的入口。
AppMgrService是应用管理服务主线程类,实现了IPC调用IAppMgr的接口,并通过AMSEventHandler将进程内各类事件及任务投递到主线程。(注:应用进程与服务管理进程之间,通过IDL实现进程间调用。IAppMgr是进程间调用的接口类。)
AppSpawn是app孵化器,负责接受应用程序框架的命令孵化应用进程,设置其对应权限,并调用应用程序框架的入口。
MainThread是应用进程的核心类。应用进程内的各类事件及任务通过MainThread中mainHandler_投递到主线程并调用MainThread中的方法执行。
AbilityThread是应用线程的核心类,操作各种Ability生命周期及方法的入口都在此类,实现IPC调用的各接口。
AbilityImpl派生出不同的子类,用以差异处理各类型Ability的逻辑,如DataAbilityImpl(Data Ability)、PageAbilityImpl(Page Ability)、ServiceAbilityImpl(Service Ability)、NewAbilityImpl(Stage模型)。
2.2 时序图
2.3 源码分析
限于篇幅,只摘抄重要流程源码
1.应用程序调用JS API startAbility函数,通过NAPI机制,调用到Native层的对应函数 NAPI_StartAbility,之后调用到NAPI中的函数StartAbilityExecuteCB,进入Ability主类的start流程中;在Ability主类中,调用了父类Context的StartAbility函数。
foundation/ability/ability_runtime/frameworks/native/ability/native/ability.cpp
ErrCode Ability::StartAbility(const Want &want)
{
HILOG_DEBUG("%{public}s begin Ability::StartAbility", __func__);
return AbilityContext::StartAbility(want, -1);
}
foundation/ability/ability_lite/frameworks/ability_lite/src/ability_context.cpp
int AbilityContext::StartAbility(const Want &want)
{
return AbilityMsClient::GetInstance().ScheduleAms(&want, 0, nullptr, START_ABILITY);
}
2.调用到Ability子系统的Client端,进入IPC调用流程。
foundation/ability/ability_runtime/services/abilitymgr/src/ability_manager_client.cpp
ErrCode AbilityManagerClient::StartAbility(
const Want &want, const sptr<IRemoteObject> &callerToken, int requestCode, int32_t userId) //userId:为每个应用分配的id
{
return abms->StartAbility(want, callerToken, userId, requestCode);
}
foundation/ability/ability_runtime/services/abilitymgr/src/ability_manager_service.cpp
int AbilityManagerService::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken,
int32_t userId, int requestCode)
{
int32_t ret = StartAbilityInner(want, callerToken, requestCode, -1, userId);
}
//通过IPC调用,系统进程的AbilityManagerService做出响应,进入服务层的Ability启动逻辑
int AbilityManagerService::StartAbilityInner(const Want &want, const sptr<IRemoteObject> &callerToken,
int requestCode, int callerUid, int32_t userId)
{
return missionListManager->StartAbility(abilityRequest);
}
3.在 MissiionListManager 的流程中,初始化目标Ability映射在系统层的AbilityRecord、MissionRecord、MissionList等结构中,随后进入AbilityRecord的创建流程
foundation/ability/ability_runtime/services/abilitymgr/src/mission_list_manager.cpp
int MissionListManager::StartAbility(const AbilityRequest &abilityRequest)
{
return StartAbility(currentTopAbility, callerAbility, abilityRequest);
}
int MissionListManager::StartAbility(const std::shared_ptr<AbilityRecord> ¤tTopAbility,...)
{
return StartAbilityLocked(currentTopAbility, callerAbility, abilityRequest);
}
int MissionListManager::StartAbilityLocked(const std::shared_ptr<AbilityRecord> ¤tTopAbility,...)
{
targetAbilityRecord->ProcessForegroundAbility();
}
4.调用LoadAbility函数,随后进入AppManagerService(用户程序框架)流程
foundation/ability/ability_runtime/services/abilitymgr/src/ability_record.cpp
void AbilityRecord::ProcessForegroundAbility(uint32_t sceneFlag)
{
LoadAbility();
}
int AbilityRecord::LoadAbility()
{
auto result = DelayedSingleton<AppScheduler>::GetInstance()->LoadAbility(
token_, callerToken_, abilityInfo_, applicationInfo_, want_);
}
foundation/ability/ability_runtime/services/abilitymgr/src/app_scheduler.cpp
int AppScheduler::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
const AppExecFwk::AbilityInfo &abilityInfo, const AppExecFwk::ApplicationInfo &applicationInfo, const Want &want)
{
int ret = static_cast<int>(appMgrClient_->LoadAbility(token, preToken, abilityInfo, applicationInfo, want));
}
进入AppManagerService(用户程序框架)流程
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp
AppMgrResultCode AppMgrClient::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,const AbilityInfo &abilityInfo, const ApplicationInfo &appInfo, const AAFwk::Want &want)
{
amsService->LoadAbility(token, preToken, abilityInfoPtr, appInfoPtr, wantPtr);
}
foundation/ability/ability_runtime/services/appmgr/src/ams_mgr_scheduler.cpp
void AmsMgrScheduler::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
const std::shared_ptr<AAFwk::Want> &want)
{
std::function<void()> loadAbilityFunc =
std::bind(&AppMgrServiceInner::LoadAbility, amsMgrServiceInner_, token, preToken, abilityInfo, appInfo, want);
}
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service_inner.cpp
void AppMgrServiceInner::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
const std::shared_ptr<AAFwk::Want> &want)
{
if (!CheckLoadAbilityConditions(token, abilityInfo, appInfo)) {
HILOG_ERROR("CheckLoadAbilityConditions failed");
return;
}
//启动相应应用进程
StartProcess(abilityInfo->applicationName, processName, startFlags, appRecord, appInfo->uid, appInfo->bundleName, bundleIndex);
}
5.利用app孵化器fork一个子进程,创建子进程的同时创建该进程的主线程MainThread,启动调用MainThread::Start()方法
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service_inner.cpp
void AppMgrServiceInner::StartProcess(const std::string &appName, const std::string &processName, uint32_t startFlags,const std::shared_ptr<AppRunningRecord> &appRecord, const int uid,const std::string &bundleName, const int32_t bundleIndex)
{
remoteClientManager_->GetSpawnClient()->StartProcess(startMsg, pid); // 令AppSpawn创建应用子进程
}
foundation/ability/ability_runtime/services/appmgr/src/app_spawn_client.cpp
ErrCode AppSpawnClient::StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid)
{
StartProcessImpl(startMsg, pid);
}
ErrCode AppSpawnClient::StartProcessImpl(const AppSpawnStartMsg &startMsg, pid_t &pid)
{
msgWrapper.AssembleMsg(startMsg)
}
foundation/ability/ability_runtime/services/appmgr/src/app_spawn_msg_wrapper.cpp
bool AppSpawnMsgWrapper::AssembleMsg(const AppSpawnStartMsg &startMsg)
{
DumpMsg();
}
base/startup/appspawn/standard/appspawn_service.c
APPSPAWN_STATIC void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen)
{
int result = AppSpawnProcessMsg(sandboxArg, &appProperty->pid);
}
base/startup/appspawn/common/appspawn_server.c
int AppSpawnProcessMsg(AppSandboxArg *sandbox, pid_t *childPid)
{
pid = fork();//创建出应用进程
if (pid == 0) { //执行子进程流程
AppSpawnChild((void *)sandbox);
}
}
int AppSpawnChild(void *arg)
{
content->runChildProcessor(content, client);
}
base/startup/appspawn/adapter/appspawn_ace.cpp
void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client)
{
OHOS::AppExecFwk::MainThread::Start(); //调用MAinThread
}
6.当目标应用程序进程启动完成后,应用端会通过IPC调用AttachApplication函数,再次调回到AppManagerService端
foundation/ability/ability_runtime/frameworks/native/appkit/app/main_thread.cpp
void MainThread::Start()
{
sptr<MainThread> thread = sptr<MainThread>(new (std::nothrow) MainThread()); //调用MainThread()构造函数
...
thread->Init(runner);
thread->Attach(); //将主线程连接到AppMgr
runner->Run();
}
void MainThread::Attach()
{
if (!ConnectToAppMgr()) {
HILOG_ERROR("attachApplication failed");
return;
}
mainThreadState_ = MainThreadState::ATTACH;
}
bool MainThread::ConnectToAppMgr()
{
// 获取 APP_MGR_SERVICE 服务的remote proxy
auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
appMgr_ = iface_cast<IAppMgr>(object);
appMgr_->AttachApplication(this); //向 APP_MGR_SERVICE 服务发送 attach application 请求
}
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp
//客户端发送 attach application 请求
AppMgrProxy::AttachApplication(const sptr<IRemoteObject> &obj)
{
remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::APP_ATTACH_APPLICATION), ...);
}
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp
//服务端收到 attach application 请求
int AppMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
// 日志打印 code = 0 (IAppMgr::Message::APP_ATTACH_APPLICATION),所以调用memberFuncMap_[0],即 AppMgrStub::HandleAttachApplication() 方法
memberFuncMap_[0] = &AppMgrStub::HandleAttachApplication;
}
int32_t AppMgrStub::HandleAttachApplication(MessageParcel &data, MessageParcel &reply)
{
AttachApplication(client); //调用子类 AppMgrService 实现的 AttachApplication() 方法
}
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service.cpp
void AppMgrService::AttachApplication(const sptr<IRemoteObject> &app)
{
std::function<void()> attachApplicationFunc = std::bind(&AppMgrServiceInner::AttachApplication, ...);
handler_->PostTask(attachApplicationFunc, TASK_ATTACH_APPLICATION);
}
7.函数处理逻辑回到服务层之后,此时目标应用程序端状态准备就绪,AppManagerService会真正进入start待启动的Ability的流程LaunchPendingAbilities函数
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service_inner.cpp
void AppMgrServiceInner::AttachApplication(const pid_t pid, const sptr<IAppScheduler> &appScheduler)
{
LaunchApplication(appRecord); //通知应用程序,以启动应用程序
}
void AppMgrServiceInner::LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord)
{
appRecord->LaunchApplication(*configuration_); //(1)通过appRecord启动Application
appRecord->LaunchPendingAbilities(); //(2)启动Ability线程,将Ability线程加载上
}
(1)通过appRecord启动Application
foundation/ability/ability_runtime/services/appmgr/src/app_running_record.cpp
void AppRunningRecord::LaunchApplication(const Configuration &config)
{
appLifeCycleDeal_->GetApplicationClient()
appLifeCycleDeal_->LaunchApplication(launchData, config); //通过appLifeCycleDeal启动application
}
foundation/ability/ability_runtime/services/appmgr/src/app_lifecycle_deal.cpp
void AppLifeCycleDeal::LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability)
{
appThread_->ScheduleLaunchAbility(*(ability->GetAbilityInfo()), ability->GetToken(), ability->GetWant()); //通知app启动应用程序
}
foundation/ability/ability_runtime/frameworks/native/appkit/app/main_thread.cpp
void MainThread::ScheduleLaunchApplication(const AppLaunchData &data, const Configuration &config)
{
appThread->HandleLaunchApplication(data, config);
}
(2)启动Ability线程,将Ability线程加载上
foundation/ability/ability_runtime/services/appmgr/src/app_running_record.cpp
void AppRunningRecord::LaunchPendingAbilities()
{
moduleRecord->LaunchPendingAbilities(); //通过moduleRecord启动Ability线程
}
foundation/ability/ability_runtime/services/appmgr/src/module_running_record.cpp
void ModuleRunningRecord::LaunchPendingAbilities()
{
for (auto item : abilities_) {
if (item.second->GetState() == AbilityState::ABILITY_STATE_CREATE) {
LaunchAbility(item.second); //启动Ability线程
}
}
}
void ModuleRunningRecord::LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability)
{
appLifeCycleDeal_->LaunchAbility(ability);
ability->SetState(AbilityState::ABILITY_STATE_READY);
}
8.通过IPC调用ScheduleLaunchAbility函数,将操作传递到目标应用程序端,目标应用程序的相应Ability开始其生命周期切换
foundation/ability/ability_runtime/services/appmgr/src/app_lifecycle_deal.cpp
AppLifeCycleDeal::LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability)
{
if (appThread_ && ability) {
appThread_->ScheduleLaunchAbility(*(ability->GetAbilityInfo()), ability->GetToken(),
ability->GetWant()); //通知App启动Ability线程
}
}
foundation/ability/ability_runtime/frameworks/native/appkit/app/main_thread.cpp
MainThread::ScheduleLaunchAbility(const AbilityInfo &info, const sptr<IRemoteObject> &token, const std::shared_ptr<AAFwk::Want> &want)
{
auto task = [weak, abilityRecord]() { //创建task到mainHandler_中运行EventHandler->MainHandler
...
appThread->HandleLaunchAbility(abilityRecord); //通过appthread启动Ability
};
mainHandler_->PostTask(task); ----- task中会执行 MainThread::HandleLaunchAbility
}
MainThread::HandleLaunchAbility(const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
{
AbilityThread::AbilityThreadMain(application_, abilityRecord, stageContext); //通过AbilityThread启动Ability线程
}
foundation/ability/ability_runtime/frameworks/native/ability/native/ability_thread.cpp
void AbilityThread::AbilityThreadMain(
std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
const std::shared_ptr<AbilityRuntime::Context> &stageContext)
{
thread->Attach(application, abilityRecord, stageContext);
}
void AbilityThread::Attach(
std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
const std::shared_ptr<AbilityRuntime::Context> &stageContext)
{
ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
}
通过ipc调用AbilityManagerService的AttachAbilityThread
foundation/ability/ability_runtime/services/abilitymgr/src/ability_manager_service.cpp
int AbilityManagerService::AttachAbilityThread(
const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token)
{
returnCode = missionListManager->AttachAbilityThread(scheduler, token);
}
foundation/ability/ability_runtime/services/abilitymgr/src/mission_list_manager.cpp
int MissionListManager::AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token)
{
abilityRecord->SetScheduler(scheduler); //设置scheduler,对应应用程序中相应的ability_thread
DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token); //应用进入前台
}
/foundation/ability/ability_runtime/services/abilitymgr/src/app_scheduler.cpp
void AppScheduler::MoveToForeground(const sptr<IRemoteObject> &token)
{
appMgrClient_->UpdateAbilityState(token, AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND);
}
通过IPC调用,进入AppManagerService(用户程序框架)流程,更新ability状态
foundation\aafwk\standard\services\appmgr\src\app_mgr_service_inner.cpp
void AppMgrServiceInner::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
{
appRecord->UpdateAbilityState(token, state);
}
foundation\aafwk\standard\services\appmgr\src\app_running_record.cpp
void AppRunningRecord::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
{
AbilityForeground(abilityRecord);
}
void AppRunningRecord::AbilityForeground(const std::shared_ptr<AbilityRunningRecord> &ability)
{
ScheduleForegroundRunning(); //切换至前台
}
void AppRunningRecord::ScheduleForegroundRunning()
{
appLifeCycleDeal_->ScheduleForegroundRunning();
}
9.通过IPC调用应用程序的ScheduleForegroundApplication, 从AppLifeCycle到MainThread
foundation/ability/ability_runtime/services/appmgr/src/app_lifecycle_deal.cpp
void AppLifeCycleDeal::ScheduleForegroundRunning()
{
appThread_->ScheduleForegroundApplication();
}
客户端发送SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION请求
/foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_proxy.cpp
void AppSchedulerProxy::ScheduleForegroundApplication()
{
remote->SendRequest(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION),...);
}
服务端收到SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION请求
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_host.cpp
int AppSchedulerHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
// 日志打印 code = 0 (IAppScheduler::Message::SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION),所以调用memberFuncMap_[0],即 AppSchedulerHost::HandleScheduleForegroundApplication() 方法
memberFuncMap_[0] = &AppSchedulerHost::HandleScheduleForegroundApplication;
}
int32_t AppSchedulerHost::HandleScheduleForegroundApplication(MessageParcel &data, MessageParcel &reply)
{
ScheduleForegroundApplication(); //调用子类MainThread
}
foundation/ability/ability_runtime/frameworks/native/appkit/app/main_thread.cpp
void MainThread::ScheduleForegroundApplication()
{
appThread->HandleForegroundApplication();
}
void MainThread::ScheduleForegroundApplication()
{
auto task = [weak]() {
...
appThread->HandleForegroundApplication(); //创建 task 到 mainHandler_ 中运行
}
mainHandler_->PostTask(task); // task中会执行 MainThread::HandleForegroundApplication
}
void MainThread::HandleForegroundApplication()
{
appMgr_->ApplicationForegrounded(applicationImpl_->GetRecordId());
}
10.通过IPC调用AppMgrService的HandleApplicationForegrounded, 从MainThred到AppMgrService
客户端发送 APP_APPLICATION_FOREGROUNDED 请求
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp
void AppMgrProxy::ApplicationForegrounded(const int32_t recordId)
{
remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::APP_APPLICATION_FOREGROUNDED), ...);
}
服务端收到 APP_APPLICATION_FOREGROUNDED 请求
foundation/ability/ability_runtime/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp
int AppMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
// 日志打印 code = 1(IAppMgr::Message::APP_APPLICATION_FOREGROUNDED), 所以调用memberFuncMap_[1],即AppMgrStub::HandleApplicationForegrounded()方法
memberFuncMap_[1] = &AppMgrStub::HandleApplicationForegrounded();
}
int32_t AppMgrStub::HandleApplicationForegrounded(MessageParcel &data, MessageParcel &reply)
{
ApplicationForegrounded(data.ReadInt32()); //调用子类AppMgrService
}
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service.cpp
void AppMgrService::ApplicationForegrounded(const int32_t recordId)
{
std::bind(&AppMgrServiceInner::ApplicationForegrounded, appMgrServiceInner_, recordId);
}
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service_inner.cpp
void AppMgrServiceInner::ApplicationForegrounded(const int32_t recordId)
{
appRecord->PopForegroundingAbilityTokens();
}
- 从AppMgrService到AbilityThread
foundation/ability/ability_runtime/services/appmgr/src/app_running_record.cpp
void AppRunningRecord::PopForegroundingAbilityTokens()
{
moduleRecord->OnAbilityStateChanged(ability, AbilityState::ABILITY_STATE_FOREGROUND);
}
foundation/ability/ability_runtime/services/appmgr/src/module_running_record.cpp
void ModuleRunningRecord::OnAbilityStateChanged(
const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state)
{
serviceInner->OnAbilityStateChanged(ability, state);
}
foundation/ability/ability_runtime/services/appmgr/src/app_mgr_service_inner.cpp
void AppMgrServiceInner::OnAbilityStateChanged(
const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state)
{
callback->OnAbilityRequestDone(ability->GetToken(), state);
}
foundation/ability/ability_runtime/services/abilitymgr/src/app_scheduler.cpp
void AppScheduler::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state)
{
callback->OnAbilityRequestDone(token, static_cast<int32_t>(state));
}
foundation/ability/ability_runtime/services/abilitymgr/src/ability_manager_service.cpp
void AbilityManagerService::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state)
{
missionListManager->OnAbilityRequestDone(token, state);
}
foundation/ability/ability_runtime/services/abilitymgr/src/mission_list_manager.cpp
void MissionListManager::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state)
{
abilityRecord->ForegroundAbility(abilityRecord->lifeCycleStateInfo_.sceneFlagBak);
}
foundation/ability/ability_runtime/services/abilitymgr/src/ability_record.cpp
void AbilityRecord::ForegroundAbility(uint32_t sceneFlag)
{
lifecycleDeal_->ForegroundNew(want_, lifeCycleStateInfo_);
}
通过IPC调用应用程序的Ability线程的ForegroundNew
foundation\aafwk\standard\services\abilitymgr\src\lifecycle_deal.cpp
void LifecycleDeal::ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo)
{
abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
}
客户端发送 SCHEDULE_ABILITY_TRANSACTION
foundation/ability/ability_runtime/services/abilitymgr/src/ability_scheduler_proxy.cpp
void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo)
{
int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, data, reply, option);
}
服务端收到 SCHEDULE_ABILITY_TRANSACTION
foundation/ability/ability_runtime/services/abilitymgr/src/ability_scheduler_stub.cpp
int AbilitySchedulerStub::OnRemoteRequest
{
ScheduleAbilityTransaction(*want, *stateInfo); //调用子类AbilityThread
}
foundation/ability/ability_runtime/frameworks/native/ability/native/ability_thread.cpp
void AbilityThread::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
{
abilityThread->HandleAbilityTransaction(want, lifeCycleStateInfo);
}
void AbilityThread::HandleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
{
abilityImpl_->SetCallingContext(lifeCycleStateInfo.caller.deviceId,
lifeCycleStateInfo.caller.bundleName,
lifeCycleStateInfo.caller.abilityName);
abilityImpl_->HandleAbilityTransaction(want, lifeCycleStateInfo);
}
foundation/ability/ability_runtime/frameworks/native/ability/native/page_ability_impl.cpp
void PageAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
{
if (ability_ && lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
ability_->SetStartAbilitySetting(targetState.setting);
Start(want); //调用实体的Start
CheckAndRestore();
}
if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE &&
targetState.state != AAFwk::ABILITY_STATE_FOREGROUND_NEW) {
Inactive();
}
if (targetState.state == AAFwk::ABILITY_STATE_BACKGROUND_NEW ||
targetState.state == AAFwk::ABILITY_STATE_BACKGROUND) {
CheckAndSave();
}
if (AbilityTransaction(want, targetState)) {
HILOG_INFO("Handle ability transaction done, notify ability manager service.");
AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state, GetRestoreData());
}
}
foundation\aafwk\standard\frameworks\kits\ability\native\src\ability_impl.cpp
void AbilityImpl::Start(const Want &want)
{
ability_->OnStart(want); //start ability, 触发ace_ability OnStart回调
#ifdef SUPPORT_GRAPHICS
if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
(ability_->GetAbilityInfo()->isStageBasedModel)) {
lifecycleState_ = AAFwk::ABILITY_STATE_STARTED_NEW;
} else {
#endif
if (ability_->GetAbilityInfo()->type == AbilityType::DATA) {
lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
} else {
lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
}
#ifdef SUPPORT_GRAPHICS
}
#endif
abilityLifecycleCallbacks_->OnAbilityStart(ability_);
}
3 总结
Ability 子系统与用户程序框架,ArkUI以及图形子系统联系紧密,对于系统开发者/ 应用开发者来说,都很有必要去深入了解。以上流程简要阐述了启动PageAbility的流程;通过本例,可以大致了解Ability运行时调用的重要方法与类,从而了解启动PageAbility的大致框架流程;本篇文档并未深究细节,若需了解更多Ability子系统相关知识,后续内容会持续发布。
更多原创内容请关注:深开鸿技术团队
入门到精通、技巧到案例,系统化分享OpenHarmony开发技术,欢迎投稿和订阅,让我们一起携手前行共建生态。
老师时序图能传一份到附件中吗,文中的实在看不清
老师时序图能传一份到附件中吗,文中的实在看不清
老师时序图能传一份到附件中吗,文中的实在看不清
老师时序图能传一份到附件中吗,文中的实在看不清