鸿蒙JS UI子系统启动结合源码分析

源码目录结构简单介绍:

OpenHarmony/
  ├── applications          # 应用程序样例
  ├── base                  # 基础软件服务子系统集
  ├── docs
  ├── domains               # 增强软件服务子系统集
  ├── drivers               # 驱动子系统
  ├── foundation            # 系统基础能力子系统集
  │   ├── aafwk                # Ability 框架
  │   ├── ace                  # JS UI 框架
  │   │   ├── ace_engine           # ACE 1.0, 仓库名: ace_ace_engine
  │   │   └── ace_engine_lite      # ACE 2.0, 仓库名: ace_engine_lite
  │   ├── appexecfwk           # 用户程序框架和包管理模块
  │   └── graphic              # 图形库相关的子系统
  ├── interface
  │   └── sdk-js               # JS API 的 TS 类型文件
  ├── kernel                # LiteOS 内核
  │   ├── liteos_a
  │   └── liteos_m
  ├── test
  └── third_party

首先调用StartJSApp进入启动js ui子系统的入口函数,后面直接根据该函数分析。

 


    StartJSApp()
        StartAbility(&want)
            OHOS::AbilityMsClient::GetInstance().StartAbility(want);
            //对应路径foundation/aafwk/aafwk_lite/frameworks/abilitymgr_lite/src/slite/ability_manager.cpp
                AbilityMsClient类的单例,成员函数StartAbility,该方法的实现
                    int AbilityMsClient::StartAbility(const Want *want) const
                    //对应路径foundation/aafwk/aafwk_lite/frameworks/abilitymgr_lite/src/slite/abilityms_slite_client.cpp
                            Request request = {
                                .msgId = START_ABILITY,
                                .len = 0,
                                .data = nullptr,
                                .msgValue = 0,
                            };
                            SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
                                BOOL AbilityMgrService::ServiceMessageHandle(Service *service, Request *request)
                                //对应路径 foundation/aafwk/aafwk_lite/services/abilitymgr_lite/src/ability_mgr_service.cpp
                                    if (request->msgId == START_ABILITY)
                                        ret = AbilityService::GetInstance().StartAbility(AbilityService::GetInstance().want_);
                                        //对应的处理如下所示
                                        class AbilityService : public NoCopyable
                                            //对应方法的实现路径 foundation/aafwk/aafwk_lite/services/abilitymgr_lite/src/ability_service.cpp
                                            int32_t AbilityService::StartAbility(AbilitySvcInfo *info)
                                                HILOG_INFO(HILOG_MODULE_AAFWK, "StartAbility");//对应log abilityms: StartAbility
                                                PreCheckStartAbility(info->bundleName, info->path, info->data, info->dataLength);
                                                    if (pendingToken_ == 0 && CreateAppTask(record) != ERR_OK)
                                                        int32_t AbilityService::CreateAppTask(AbilityRecord *record)
                                                            HILOG_INFO(HILOG_MODULE_AAFWK, "CreateAppTask.");
                                                            //对应的打印abilityms: CreateAppTask.
                                                            stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)(JsAppHost::JsAppTaskHandler);
                                                                stTskInitParam.uwStackSize = TASK_STACK_SIZE;
                                                                stTskInitParam.usTaskPrio = OS_TASK_PRIORITY_LOWEST - APP_TASK_PRI;
                                                                stTskInitParam.pcName = const_cast<char *>("AppTask");
                                                                stTskInitParam.uwResved = 0;
                                                                auto jsAppHost = new JsAppHost();
                                                                stTskInitParam.uwArg = reinterpret_cast<UINT32>((uintptr_t)jsAppHost);
                                                            UINT32 res = LOS_TaskCreate(&appTaskId, &stTskInitParam);
                                                                void JsAppHost::JsAppTaskHandler(uint32_t uwArg)
                                                                    switch ((uint32_t)innerMsg.msgId //该消息是由 SchedulerLifecycleInner 发送过来的
                                                                        case ACTIVE:
                                                                            jsappHost->OnActive(innerMsg.token, innerMsg.bundleName, innerMsg.path);
                                                                            void JsAppHost::OnActive(uint16_t token, const char *bundleName, const char *path)
                                                                            //路径 foundation/aafwk/aafwk_lite/services/abilitymgr_lite/src/js_app_host.cpp
                                                                                jsAbility_->Launch(const_cast<char *>(path), bundleName, token);
                                                                                    void JSAbility::Launch(const char *const abilityPath,
                                                                                       const char *const bundleName,
                                                                                       uint16_t token,
                                                                                       const char *pageInfo)
                                                                                    //# JS UI 框架
                                                                                    //路径 foundation/ace/ace_engine_lite/frameworks/src/core/context/js_ability.cpp
                                                                                             HILOG_INFO(HILOG_MODULE_ACE, "LIFECYCLE: JS Ability is launching");
                                                                                             //对应打印Graphic: LIFECYCLE: JS Ability is launching
                                                                                            DumpNativeMemoryUsage();
                                                                                                 HILOG_DEBUG(HILOG_MODULE_ACE, "available free size: %{public}d", memInfo.freeSize);
                                                                                                 //对应log:D 03900/Graphic: available free size: 872478416
                                                                                             jsAbilityImpl_ = new JSAbilityImpl();
                                                                                             //类型转换成对象指针
                                                                                             JSAbilityImpl *jsAbilityImpl = CastAbilityImpl(jsAbilityImpl_);
                                                                                             jsAbilityImpl->InitEnvironment(abilityPath, bundleName, token);
                                                                                             void JSAbilityImpl::InitEnvironment(const char * const abilityPath, const char * const bundleName, uint16_t token)
                                                                                             //路径 foundation/ace/ace_engine_lite/frameworks/src/core/context/js_ability_impl.cpp
                                                                                                    // init engine && js fwk
                                                                                                    JsAppEnvironment *appJsEnv = JsAppEnvironment::GetInstance();
                                                                                                    appJsEnv->InitRuntimeMode();
                                                                                                        void JsAppEnvironment::InitRuntimeMode()
                                                                                                        //路径 foundation/ace/ace_engine_lite/frameworks/src/core/context/js_app_environment.cpp
                                                                                                        HILOG_DEBUG(HILOG_MODULE_ACE, "ACELite is running in snapshot mode");
                                                                                                        //对应logD 03900/ACE: ACELite is running in snapshot mode
                                                                                                    appJsEnv->InitJsFramework();
                                                                                                        LoadFramework();
                                                                                                        void JsAppEnvironment::LoadFramework() const
                                                                                                            HILOG_INFO(HILOG_MODULE_ACE, "Success to load JavaScript framework.")
                                                                                                            //对应打印 ACE: Success to load JavaScript framework.
                                                                                                    abilityModel_ = appContext_->Eval(fileFullPath, strlen(fileFullPath), true); // generate global.$app js object
                                                                                                    jerry_value_t JsAppContext::Eval(char *fullPath, size_t fullPathLength, bool isAppEval) const
                                                                                                    //路径     foundation/ace/ace_engine_lite/frameworks/src/core/context/js_app_context.cpp
                                                                                                         HILOG_ERROR(HILOG_MODULE_ACE, "JS mode changed unexpected [%{public}d]", isSnapshotMode);
                                                                                                    jsCode = ReadFile(fullPath, outLength, isSnapshotMode);
                                                                                                    char *ReadFile(const char * const fullPath, uint32_t &fileSize, const bool binary)
                                                                                                    //路径 foundation/ace/ace_engine_lite/frameworks/src/core/base/js_fwk_common.cpp
                                                                                                jsAbilityImpl->DeliverCreate(pageInfo);
                                                                                                    InvokeOnCreate();
                                                                                                        void JSAbilityImpl::InvokeOnCreate() const
                                                                                                        //路径 foundation/ace/ace_engine_lite/frameworks/src/core/context/js_ability_impl.cpp
                                                                                                            jerry_value_t onCreateFunction = jerryx_get_property_str(abilityModel_, ABILITY_LIFECYCLE_CALLBACK_ON_CREATE); //??
                                                                                                            //其中 constexpr char ABILITY_LIFECYCLE_CALLBACK_ON_CREATE[] = "onCreate";
                                                                                jsAbility_->Show();
                                                                                void JSAbility::Show()
                                                                                    JSAbilityImpl *jsAbilityImpl = CastAbilityImpl(jsAbilityImpl_);
                                                                                    jsAbilityImpl->Show();
                                                                                        void JSAbilityImpl::Show() const
                                                                                        //路径 foundation/ace/ace_engine_lite/frameworks/src/core/context/js_ability_impl.cpp
                                                                                            router_->Show();
                                                                                            void Router::Show()
                                                                                            //路径 foundation/ace/ace_engine_lite/frameworks/src/core/router/js_router.cpp
                                                                                                currentSm_->SetHiddenFlag(hidden_);//类里面访问自己的私有成员
                                                                                                currentSm_->ChangeState(SHOW_STATE);
                                                                                                void StateMachine::ChangeState(int newState)
                                                                                                //路径 foundation/ace/ace_engine_lite/frameworks/src/core/router/js_page_state_machine.cpp
                                                                                                    State *state = stateMap_[newState];
                                                                                                    state->Handle(*this);
                                                                                                    void PageShowState::Handle(StateMachine &sm)
                                                                                                    //路径 foundation/ace/ace_engine_lite/frameworks/src/core/router/js_page_state.cpp
                                                                                                        HILOG_INFO(HILOG_MODULE_ACE, "current state(%{public}d) -> show state", currState);
                                                                                                         sm.ShowPage();

                                                            SchedulerLifecycle(record->GetToken(), STATE_ACTIVE);
                                                            //路径 foundation/aafwk/aafwk_lite/services/abilitymgr_lite/src/ability_service.cpp
                                                                SchedulerLifecycleInner(record, state);
                                                                int32_t AbilityService::SchedulerLifecycleInner(const AbilityRecord *record, int32_t state)
                                                                    (void) SendMsgToJsAbility(state, record);
                                                                        innerMsg.msgId = ACTIVE;
                                                                            innerMsg.bundleName = record->GetAppName();
                                                                            innerMsg.token = record->GetToken();
                                                                            innerMsg.path = record->GetAppPath();
                                                                            innerMsg.data = const_cast<void *>(record->GetAppData());
                                                                            innerMsg.dataLength = record->GetDataLength();
                                                                            osMessageQueueId_t appQueueId = record->GetMessageQueueId();
                                                                            osStatus_t ret = osMessageQueuePut(appQueueId, static_cast<void *>(&innerMsg), 0, 0);


结合hos 2.0源码分析。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值