123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/ace_service_ability.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "core/common/ace_engine.h"
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_cinamespace OHOS {
2123b3eb3cSopenharmony_cinamespace Ace {
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ciusing namespace OHOS::AAFwk;
2423b3eb3cSopenharmony_ciusing namespace OHOS::AppExecFwk;
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_ciusing ServicePlatformFinish = std::function<void()>;
2723b3eb3cSopenharmony_ciclass ServicePlatformEventCallback final : public Platform::PlatformEventCallback {
2823b3eb3cSopenharmony_cipublic:
2923b3eb3cSopenharmony_ci    explicit ServicePlatformEventCallback(ServicePlatformFinish onFinish) : onFinish_(onFinish) {}
3023b3eb3cSopenharmony_ci
3123b3eb3cSopenharmony_ci    ~ServicePlatformEventCallback() override = default;
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ci    void OnFinish() const override
3423b3eb3cSopenharmony_ci    {
3523b3eb3cSopenharmony_ci        LOGI("ServicePlatformEventCallback OnFinish");
3623b3eb3cSopenharmony_ci        CHECK_NULL_VOID(onFinish_);
3723b3eb3cSopenharmony_ci        onFinish_();
3823b3eb3cSopenharmony_ci    }
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_ci    void OnStatusBarBgColorChanged(uint32_t color) override
4123b3eb3cSopenharmony_ci    {
4223b3eb3cSopenharmony_ci        LOGI("ServicePlatformEventCallback OnStatusBarBgColorChanged");
4323b3eb3cSopenharmony_ci    }
4423b3eb3cSopenharmony_ci
4523b3eb3cSopenharmony_ciprivate:
4623b3eb3cSopenharmony_ci    ServicePlatformFinish onFinish_;
4723b3eb3cSopenharmony_ci};
4823b3eb3cSopenharmony_ci
4923b3eb3cSopenharmony_ciconst std::string AceServiceAbility::START_PARAMS_KEY = "__startParams";
5023b3eb3cSopenharmony_ciconst std::string AceServiceAbility::URI = "url";
5123b3eb3cSopenharmony_ci
5223b3eb3cSopenharmony_ciREGISTER_AA(AceServiceAbility)
5323b3eb3cSopenharmony_ci
5423b3eb3cSopenharmony_ciAceServiceAbility::AceServiceAbility()
5523b3eb3cSopenharmony_ci{
5623b3eb3cSopenharmony_ci    abilityId_ = Container::GenerateId<PA_SERVICE_CONTAINER>();
5723b3eb3cSopenharmony_ci}
5823b3eb3cSopenharmony_ci
5923b3eb3cSopenharmony_civoid AceServiceAbility::OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
6023b3eb3cSopenharmony_ci{
6123b3eb3cSopenharmony_ci    Ability::OnStart(want, sessionInfo);
6223b3eb3cSopenharmony_ci    LOGI("AceServiceAbility OnStart called");
6323b3eb3cSopenharmony_ci    // get url
6423b3eb3cSopenharmony_ci    std::string parsedUrl;
6523b3eb3cSopenharmony_ci    if (want.HasParameter(URI)) {
6623b3eb3cSopenharmony_ci        parsedUrl = want.GetStringParam(URI);
6723b3eb3cSopenharmony_ci    } else {
6823b3eb3cSopenharmony_ci        parsedUrl = "service.js";
6923b3eb3cSopenharmony_ci    }
7023b3eb3cSopenharmony_ci
7123b3eb3cSopenharmony_ci    // get asset
7223b3eb3cSopenharmony_ci    auto packagePathStr = GetBundleCodePath();
7323b3eb3cSopenharmony_ci    auto moduleInfo = GetHapModuleInfo();
7423b3eb3cSopenharmony_ci    CHECK_NULL_VOID(moduleInfo);
7523b3eb3cSopenharmony_ci    packagePathStr += "/" + moduleInfo->package + "/";
7623b3eb3cSopenharmony_ci    auto abilityInfo = GetAbilityInfo();
7723b3eb3cSopenharmony_ci
7823b3eb3cSopenharmony_ci    // init service
7923b3eb3cSopenharmony_ci    BackendType backendType = BackendType::SERVICE;
8023b3eb3cSopenharmony_ci    SrcLanguage srcLanguage = SrcLanguage::ETS;
8123b3eb3cSopenharmony_ci    if (abilityInfo != nullptr && !abilityInfo->srcLanguage.empty()) {
8223b3eb3cSopenharmony_ci        if (abilityInfo->srcLanguage == "js") {
8323b3eb3cSopenharmony_ci            srcLanguage = SrcLanguage::JS;
8423b3eb3cSopenharmony_ci        }
8523b3eb3cSopenharmony_ci    }
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci    std::shared_ptr<Platform::WorkerPath> workerPath = std::make_shared<Platform::WorkerPath>();
8823b3eb3cSopenharmony_ci    workerPath->packagePathStr = packagePathStr;
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_ci    std::vector<std::string> assetBasePathStr;
9123b3eb3cSopenharmony_ci    AceEngine::InitJsDumpHeadSignal();
9223b3eb3cSopenharmony_ci    if (abilityInfo != nullptr && !abilityInfo->srcPath.empty()) {
9323b3eb3cSopenharmony_ci        assetBasePathStr = { "assets/js/" + abilityInfo->srcPath + "/", std::string("assets/js/") };
9423b3eb3cSopenharmony_ci    } else {
9523b3eb3cSopenharmony_ci        assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_ci    workerPath->assetBasePathStr = assetBasePathStr;
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_ci    Platform::PaContainerOptions options;
10123b3eb3cSopenharmony_ci    options.type = backendType;
10223b3eb3cSopenharmony_ci    options.language = srcLanguage;
10323b3eb3cSopenharmony_ci    options.hapPath = moduleInfo->hapPath;
10423b3eb3cSopenharmony_ci    options.workerPath = workerPath;
10523b3eb3cSopenharmony_ci
10623b3eb3cSopenharmony_ci    Platform::PaContainer::CreateContainer(abilityId_, this, options,
10723b3eb3cSopenharmony_ci        std::make_unique<ServicePlatformEventCallback>([this]() { TerminateAbility(); }));
10823b3eb3cSopenharmony_ci    Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
10923b3eb3cSopenharmony_ci
11023b3eb3cSopenharmony_ci    // run service
11123b3eb3cSopenharmony_ci    Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
11223b3eb3cSopenharmony_ci}
11323b3eb3cSopenharmony_ci
11423b3eb3cSopenharmony_civoid AceServiceAbility::OnStop()
11523b3eb3cSopenharmony_ci{
11623b3eb3cSopenharmony_ci    LOGI("AceServiceAbility OnStop called ");
11723b3eb3cSopenharmony_ci    Ability::OnStop();
11823b3eb3cSopenharmony_ci    Platform::PaContainer::DestroyContainer(abilityId_);
11923b3eb3cSopenharmony_ci}
12023b3eb3cSopenharmony_ci
12123b3eb3cSopenharmony_cisptr<IRemoteObject> AceServiceAbility::OnConnect(const Want& want)
12223b3eb3cSopenharmony_ci{
12323b3eb3cSopenharmony_ci    LOGI("AceServiceAbility OnConnect start");
12423b3eb3cSopenharmony_ci    Ability::OnConnect(want);
12523b3eb3cSopenharmony_ci    auto ret = Platform::PaContainer::OnConnect(abilityId_, want);
12623b3eb3cSopenharmony_ci    if (ret == nullptr) {
12723b3eb3cSopenharmony_ci        LOGE("AceServiceAbility OnConnect, the iremoteObject is null");
12823b3eb3cSopenharmony_ci        return nullptr;
12923b3eb3cSopenharmony_ci    }
13023b3eb3cSopenharmony_ci    return ret;
13123b3eb3cSopenharmony_ci}
13223b3eb3cSopenharmony_ci
13323b3eb3cSopenharmony_civoid AceServiceAbility::OnDisconnect(const Want& want)
13423b3eb3cSopenharmony_ci{
13523b3eb3cSopenharmony_ci    LOGI("AceServiceAbility OnDisconnect start");
13623b3eb3cSopenharmony_ci    Ability::OnDisconnect(want);
13723b3eb3cSopenharmony_ci    Platform::PaContainer::OnDisConnect(abilityId_, want);
13823b3eb3cSopenharmony_ci}
13923b3eb3cSopenharmony_ci
14023b3eb3cSopenharmony_civoid AceServiceAbility::OnCommand(const AAFwk::Want &want, bool restart, int startId)
14123b3eb3cSopenharmony_ci{
14223b3eb3cSopenharmony_ci    LOGI("AceServiceAbility OnCommand start");
14323b3eb3cSopenharmony_ci    Ability::OnCommand(want, restart, startId);
14423b3eb3cSopenharmony_ci    Platform::PaContainer::OnCommand(want, startId, abilityId_);
14523b3eb3cSopenharmony_ci}
14623b3eb3cSopenharmony_ci} // namespace Ace
14723b3eb3cSopenharmony_ci} // namespace OHOS
148