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_data_ability.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/pa_engine/pa_backend.h"
1923b3eb3cSopenharmony_ci#include "core/common/ace_engine.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cinamespace OHOS {
2223b3eb3cSopenharmony_cinamespace Ace {
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ciusing namespace OHOS::AAFwk;
2523b3eb3cSopenharmony_ciusing namespace OHOS::AppExecFwk;
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_ciusing DataPlatformFinish = std::function<void()>;
2823b3eb3cSopenharmony_ciclass DataPlatformEventCallback final : public Platform::PlatformEventCallback {
2923b3eb3cSopenharmony_cipublic:
3023b3eb3cSopenharmony_ci    explicit DataPlatformEventCallback(DataPlatformFinish onFinish) : onFinish_(onFinish) {}
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ci    ~DataPlatformEventCallback() override = default;
3323b3eb3cSopenharmony_ci
3423b3eb3cSopenharmony_ci    void OnFinish() const override
3523b3eb3cSopenharmony_ci    {
3623b3eb3cSopenharmony_ci        LOGI("DataPlatformEventCallback OnFinish");
3723b3eb3cSopenharmony_ci        CHECK_NULL_VOID(onFinish_);
3823b3eb3cSopenharmony_ci        onFinish_();
3923b3eb3cSopenharmony_ci    }
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_ci    void OnStatusBarBgColorChanged(uint32_t color) override
4223b3eb3cSopenharmony_ci    {
4323b3eb3cSopenharmony_ci        LOGI("DataPlatformEventCallback OnStatusBarBgColorChanged");
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ciprivate:
4723b3eb3cSopenharmony_ci    DataPlatformFinish onFinish_;
4823b3eb3cSopenharmony_ci};
4923b3eb3cSopenharmony_ci
5023b3eb3cSopenharmony_ciconst std::string AceDataAbility::START_PARAMS_KEY = "__startParams";
5123b3eb3cSopenharmony_ciconst std::string AceDataAbility::URI = "url";
5223b3eb3cSopenharmony_ci
5323b3eb3cSopenharmony_ciREGISTER_AA(AceDataAbility)
5423b3eb3cSopenharmony_ci
5523b3eb3cSopenharmony_ciAceDataAbility::AceDataAbility()
5623b3eb3cSopenharmony_ci{
5723b3eb3cSopenharmony_ci    abilityId_ = Container::GenerateId<PA_DATA_CONTAINER>();
5823b3eb3cSopenharmony_ci}
5923b3eb3cSopenharmony_ci
6023b3eb3cSopenharmony_civoid AceDataAbility::OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
6123b3eb3cSopenharmony_ci{
6223b3eb3cSopenharmony_ci    Ability::OnStart(want, sessionInfo);
6323b3eb3cSopenharmony_ci    LOGI("AceDataAbility OnStart called");
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ci    // get url
6623b3eb3cSopenharmony_ci    std::string parsedUrl;
6723b3eb3cSopenharmony_ci    if (want.HasParameter(URI)) {
6823b3eb3cSopenharmony_ci        parsedUrl = want.GetStringParam(URI);
6923b3eb3cSopenharmony_ci    } else {
7023b3eb3cSopenharmony_ci        parsedUrl = "data.js";
7123b3eb3cSopenharmony_ci    }
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    // get asset
7423b3eb3cSopenharmony_ci    auto packagePathStr = GetBundleCodePath();
7523b3eb3cSopenharmony_ci    auto moduleInfo = GetHapModuleInfo();
7623b3eb3cSopenharmony_ci    CHECK_NULL_VOID(moduleInfo);
7723b3eb3cSopenharmony_ci    packagePathStr += "/" + moduleInfo->package + "/";
7823b3eb3cSopenharmony_ci    std::shared_ptr<AbilityInfo> abilityInfo = GetAbilityInfo();
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ci    // init data ability
8123b3eb3cSopenharmony_ci    BackendType backendType = BackendType::DATA;
8223b3eb3cSopenharmony_ci    SrcLanguage srcLanguage = SrcLanguage::ETS;
8323b3eb3cSopenharmony_ci    if (abilityInfo != nullptr && !abilityInfo->srcLanguage.empty()) {
8423b3eb3cSopenharmony_ci        if (abilityInfo->srcLanguage == "js") {
8523b3eb3cSopenharmony_ci            srcLanguage = SrcLanguage::JS;
8623b3eb3cSopenharmony_ci        }
8723b3eb3cSopenharmony_ci    }
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_ci    std::shared_ptr<Platform::WorkerPath> workerPath = std::make_shared<Platform::WorkerPath>();
9023b3eb3cSopenharmony_ci    workerPath->packagePathStr = packagePathStr;
9123b3eb3cSopenharmony_ci    std::vector<std::string> assetBasePathStr;
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci    AceEngine::InitJsDumpHeadSignal();
9423b3eb3cSopenharmony_ci    if (abilityInfo != nullptr && !abilityInfo->srcPath.empty()) {
9523b3eb3cSopenharmony_ci        assetBasePathStr = { "assets/js/" + abilityInfo->srcPath + "/", std::string("assets/js/") };
9623b3eb3cSopenharmony_ci    } else {
9723b3eb3cSopenharmony_ci        assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
9823b3eb3cSopenharmony_ci    }
9923b3eb3cSopenharmony_ci    workerPath->assetBasePathStr = assetBasePathStr;
10023b3eb3cSopenharmony_ci
10123b3eb3cSopenharmony_ci    Platform::PaContainerOptions options;
10223b3eb3cSopenharmony_ci    options.type = backendType;
10323b3eb3cSopenharmony_ci    options.language = srcLanguage;
10423b3eb3cSopenharmony_ci    options.hapPath = moduleInfo->hapPath;
10523b3eb3cSopenharmony_ci    options.workerPath = workerPath;
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_ci    Platform::PaContainer::CreateContainer(abilityId_, this, options,
10823b3eb3cSopenharmony_ci        std::make_unique<DataPlatformEventCallback>([this]() { TerminateAbility(); }));
10923b3eb3cSopenharmony_ci    Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
11023b3eb3cSopenharmony_ci
11123b3eb3cSopenharmony_ci    // run data ability
11223b3eb3cSopenharmony_ci    Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
11323b3eb3cSopenharmony_ci}
11423b3eb3cSopenharmony_ci
11523b3eb3cSopenharmony_civoid AceDataAbility::OnStop()
11623b3eb3cSopenharmony_ci{
11723b3eb3cSopenharmony_ci    LOGI("AceDataAbility OnStop called");
11823b3eb3cSopenharmony_ci    Ability::OnStop();
11923b3eb3cSopenharmony_ci    Platform::PaContainer::DestroyContainer(abilityId_);
12023b3eb3cSopenharmony_ci}
12123b3eb3cSopenharmony_ci
12223b3eb3cSopenharmony_ciint32_t AceDataAbility::Insert(const Uri& uri, const NativeRdb::ValuesBucket& value)
12323b3eb3cSopenharmony_ci{
12423b3eb3cSopenharmony_ci    LOGI("AceDataAbility Insert called");
12523b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::Insert(abilityId_, uri, value);
12623b3eb3cSopenharmony_ci    return ret;
12723b3eb3cSopenharmony_ci}
12823b3eb3cSopenharmony_ci
12923b3eb3cSopenharmony_cistd::shared_ptr<NativeRdb::AbsSharedResultSet> AceDataAbility::Query(
13023b3eb3cSopenharmony_ci    const Uri& uri, const std::vector<std::string>& columns,
13123b3eb3cSopenharmony_ci    const NativeRdb::DataAbilityPredicates& predicates)
13223b3eb3cSopenharmony_ci{
13323b3eb3cSopenharmony_ci    LOGI("AceDataAbility Query called");
13423b3eb3cSopenharmony_ci    auto resultSet = Platform::PaContainer::Query(abilityId_, uri, columns, predicates);
13523b3eb3cSopenharmony_ci    return resultSet;
13623b3eb3cSopenharmony_ci}
13723b3eb3cSopenharmony_ci
13823b3eb3cSopenharmony_ciint32_t AceDataAbility::Update(const Uri& uri, const NativeRdb::ValuesBucket& value,
13923b3eb3cSopenharmony_ci    const NativeRdb::DataAbilityPredicates& predicates)
14023b3eb3cSopenharmony_ci{
14123b3eb3cSopenharmony_ci    LOGI("AceDataAbility Update called");
14223b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::Update(abilityId_, uri, value, predicates);
14323b3eb3cSopenharmony_ci    return ret;
14423b3eb3cSopenharmony_ci}
14523b3eb3cSopenharmony_ci
14623b3eb3cSopenharmony_ciint32_t AceDataAbility::Delete(const Uri& uri, const NativeRdb::DataAbilityPredicates& predicates)
14723b3eb3cSopenharmony_ci{
14823b3eb3cSopenharmony_ci    LOGI("AceDataAbility Delete called");
14923b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::Delete(abilityId_, uri, predicates);
15023b3eb3cSopenharmony_ci    return ret;
15123b3eb3cSopenharmony_ci}
15223b3eb3cSopenharmony_ci
15323b3eb3cSopenharmony_ciint32_t AceDataAbility::BatchInsert(const Uri& uri, const std::vector<NativeRdb::ValuesBucket>& values)
15423b3eb3cSopenharmony_ci{
15523b3eb3cSopenharmony_ci    LOGI("AceDataAbility BatchInsert called");
15623b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::BatchInsert(abilityId_, uri, values);
15723b3eb3cSopenharmony_ci    return ret;
15823b3eb3cSopenharmony_ci}
15923b3eb3cSopenharmony_ci
16023b3eb3cSopenharmony_cistd::string AceDataAbility::GetType(const Uri& uri)
16123b3eb3cSopenharmony_ci{
16223b3eb3cSopenharmony_ci    LOGI("AceDataAbility GetType called");
16323b3eb3cSopenharmony_ci    std::string ret = Platform::PaContainer::GetType(abilityId_, uri);
16423b3eb3cSopenharmony_ci    return ret;
16523b3eb3cSopenharmony_ci}
16623b3eb3cSopenharmony_ci
16723b3eb3cSopenharmony_cistd::vector<std::string> AceDataAbility::GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter)
16823b3eb3cSopenharmony_ci{
16923b3eb3cSopenharmony_ci    LOGI("AceDataAbility GetFileTypes called");
17023b3eb3cSopenharmony_ci    std::vector<std::string> ret = Platform::PaContainer::GetFileTypes(abilityId_, uri, mimeTypeFilter);
17123b3eb3cSopenharmony_ci    return ret;
17223b3eb3cSopenharmony_ci}
17323b3eb3cSopenharmony_ci
17423b3eb3cSopenharmony_ciint32_t AceDataAbility::OpenFile(const Uri& uri, const std::string& mode)
17523b3eb3cSopenharmony_ci{
17623b3eb3cSopenharmony_ci    LOGI("AceDataAbility OpenFile called");
17723b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::OpenFile(abilityId_, uri, mode);
17823b3eb3cSopenharmony_ci    return ret;
17923b3eb3cSopenharmony_ci}
18023b3eb3cSopenharmony_ci
18123b3eb3cSopenharmony_ciint32_t AceDataAbility::OpenRawFile(const Uri& uri, const std::string& mode)
18223b3eb3cSopenharmony_ci{
18323b3eb3cSopenharmony_ci    LOGI("AceDataAbility OpenRawFile called");
18423b3eb3cSopenharmony_ci    int32_t ret = Platform::PaContainer::OpenRawFile(abilityId_, uri, mode);
18523b3eb3cSopenharmony_ci    return ret;
18623b3eb3cSopenharmony_ci}
18723b3eb3cSopenharmony_ci
18823b3eb3cSopenharmony_ciUri AceDataAbility::NormalizeUri(const Uri& uri)
18923b3eb3cSopenharmony_ci{
19023b3eb3cSopenharmony_ci    LOGI("AceDataAbility NormalizeUri called");
19123b3eb3cSopenharmony_ci    Uri ret = Platform::PaContainer::NormalizeUri(abilityId_, uri);
19223b3eb3cSopenharmony_ci    return ret;
19323b3eb3cSopenharmony_ci}
19423b3eb3cSopenharmony_ci
19523b3eb3cSopenharmony_ciUri AceDataAbility::DenormalizeUri(const Uri& uri)
19623b3eb3cSopenharmony_ci{
19723b3eb3cSopenharmony_ci    LOGI("AceDataAbility DenormalizeUri called");
19823b3eb3cSopenharmony_ci    Uri ret = Platform::PaContainer::DenormalizeUri(abilityId_, uri);
19923b3eb3cSopenharmony_ci    return ret;
20023b3eb3cSopenharmony_ci}
20123b3eb3cSopenharmony_ci
20223b3eb3cSopenharmony_cistd::shared_ptr<AppExecFwk::PacMap> AceDataAbility::Call(const Uri& uri,
20323b3eb3cSopenharmony_ci    const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap)
20423b3eb3cSopenharmony_ci{
20523b3eb3cSopenharmony_ci    std::shared_ptr<AppExecFwk::PacMap> ret = Platform::PaContainer::Call(abilityId_, uri, method, arg, pacMap);
20623b3eb3cSopenharmony_ci    return ret;
20723b3eb3cSopenharmony_ci}
20823b3eb3cSopenharmony_ci} // namespace Ace
20923b3eb3cSopenharmony_ci} // namespace OHOS
210