114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci */
1514cf0368Sopenharmony_ci#define LOG_TAG "UdmfClient"
1614cf0368Sopenharmony_ci#include "udmf_client.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "dds_trace.h"
1914cf0368Sopenharmony_ci#include "udmf_radar_reporter.h"
2014cf0368Sopenharmony_ci
2114cf0368Sopenharmony_ci#include "logger.h"
2214cf0368Sopenharmony_ci#include "udmf_service_client.h"
2314cf0368Sopenharmony_ci#include "udmf_utils.h"
2414cf0368Sopenharmony_ci#include "accesstoken_kit.h"
2514cf0368Sopenharmony_ci#include "ipc_skeleton.h"
2614cf0368Sopenharmony_ci#include "unified_data_helper.h"
2714cf0368Sopenharmony_ci
2814cf0368Sopenharmony_cinamespace OHOS {
2914cf0368Sopenharmony_cinamespace UDMF {
3014cf0368Sopenharmony_ciconstexpr const char *TAG = "UdmfClient::";
3114cf0368Sopenharmony_ciusing namespace OHOS::DistributedDataDfx;
3214cf0368Sopenharmony_ciusing namespace RadarReporter;
3314cf0368Sopenharmony_ciUdmfClient &UdmfClient::GetInstance()
3414cf0368Sopenharmony_ci{
3514cf0368Sopenharmony_ci    static UdmfClient instance;
3614cf0368Sopenharmony_ci    return instance;
3714cf0368Sopenharmony_ci}
3814cf0368Sopenharmony_ci
3914cf0368Sopenharmony_ciStatus UdmfClient::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
4014cf0368Sopenharmony_ci{
4114cf0368Sopenharmony_ci    DdsTrace trace(
4214cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
4314cf0368Sopenharmony_ci    RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
4414cf0368Sopenharmony_ci        BizScene::SET_DATA, SetDataStage::SET_DATA_BEGIN, StageRes::IDLE, BizState::DFX_BEGIN);
4514cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
4614cf0368Sopenharmony_ci    if (service == nullptr) {
4714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
4814cf0368Sopenharmony_ci        RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
4914cf0368Sopenharmony_ci            BizScene::SET_DATA, SetDataStage::SET_DATA_BEGIN, StageRes::FAILED, E_IPC, BizState::DFX_ABNORMAL_END);
5014cf0368Sopenharmony_ci        return E_IPC;
5114cf0368Sopenharmony_ci    }
5214cf0368Sopenharmony_ci
5314cf0368Sopenharmony_ci    if (option.intention == UD_INTENTION_DRAG) {
5414cf0368Sopenharmony_ci        ShareOptions shareOption = SHARE_OPTIONS_BUTT;
5514cf0368Sopenharmony_ci        auto status = GetAppShareOption(UD_INTENTION_MAP.at(option.intention), shareOption);
5614cf0368Sopenharmony_ci        if (status != E_NOT_FOUND && status != E_OK) {
5714cf0368Sopenharmony_ci            LOG_ERROR(UDMF_CLIENT, "get appShareOption fail, intention:%{public}s",
5814cf0368Sopenharmony_ci                      UD_INTENTION_MAP.at(option.intention).c_str());
5914cf0368Sopenharmony_ci            return static_cast<Status>(status);
6014cf0368Sopenharmony_ci        }
6114cf0368Sopenharmony_ci        if (shareOption == ShareOptions::IN_APP) {
6214cf0368Sopenharmony_ci            std::string bundleName = "udmf.inapp.data";
6314cf0368Sopenharmony_ci            UnifiedKey udKey = UnifiedKey(UD_INTENTION_MAP.at(option.intention), bundleName, UTILS::GenerateId());
6414cf0368Sopenharmony_ci            key = udKey.GetUnifiedKey();
6514cf0368Sopenharmony_ci            dataCache_.Clear();
6614cf0368Sopenharmony_ci            dataCache_.Insert(key, unifiedData);
6714cf0368Sopenharmony_ci            LOG_INFO(UDMF_CLIENT, "SetData in app success, bundleName:%{public}s.", bundleName.c_str());
6814cf0368Sopenharmony_ci            RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
6914cf0368Sopenharmony_ci                BizScene::SET_DATA, SetDataStage::SET_DATA_END, StageRes::SUCCESS, BizState::DFX_NORMAL_END);
7014cf0368Sopenharmony_ci            return E_OK;
7114cf0368Sopenharmony_ci        }
7214cf0368Sopenharmony_ci    }
7314cf0368Sopenharmony_ci    int32_t ret = service->SetData(option, unifiedData, key);
7414cf0368Sopenharmony_ci    if (ret != E_OK) {
7514cf0368Sopenharmony_ci        RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
7614cf0368Sopenharmony_ci            BizScene::SET_DATA, SetDataStage::SET_DATA_END, StageRes::FAILED, ret, BizState::DFX_ABNORMAL_END);
7714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
7814cf0368Sopenharmony_ci    }
7914cf0368Sopenharmony_ci    RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
8014cf0368Sopenharmony_ci        BizScene::SET_DATA, SetDataStage::SET_DATA_END, StageRes::SUCCESS, BizState::DFX_NORMAL_END);
8114cf0368Sopenharmony_ci    return static_cast<Status>(ret);
8214cf0368Sopenharmony_ci}
8314cf0368Sopenharmony_ci
8414cf0368Sopenharmony_ciStatus UdmfClient::GetData(const QueryOption &query, UnifiedData &unifiedData)
8514cf0368Sopenharmony_ci{
8614cf0368Sopenharmony_ci    DdsTrace trace(
8714cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
8814cf0368Sopenharmony_ci    RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
8914cf0368Sopenharmony_ci        BizScene::GET_DATA, GetDataStage::GET_DATA_BEGIN, StageRes::IDLE, BizState::DFX_BEGIN);
9014cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
9114cf0368Sopenharmony_ci    if (service == nullptr) {
9214cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
9314cf0368Sopenharmony_ci        RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
9414cf0368Sopenharmony_ci            BizScene::GET_DATA, GetDataStage::GET_DATA_BEGIN, StageRes::FAILED, E_IPC, BizState::DFX_ABNORMAL_END);
9514cf0368Sopenharmony_ci        return E_IPC;
9614cf0368Sopenharmony_ci    }
9714cf0368Sopenharmony_ci    auto it = dataCache_.Find(query.key);
9814cf0368Sopenharmony_ci    if (it.first) {
9914cf0368Sopenharmony_ci        unifiedData = it.second;
10014cf0368Sopenharmony_ci        dataCache_.Erase(query.key);
10114cf0368Sopenharmony_ci        RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
10214cf0368Sopenharmony_ci            BizScene::GET_DATA, GetDataStage::GET_DATA_END, StageRes::SUCCESS, BizState::DFX_NORMAL_END);
10314cf0368Sopenharmony_ci        return E_OK;
10414cf0368Sopenharmony_ci    }
10514cf0368Sopenharmony_ci    LOG_WARN(UDMF_CLIENT, "query data from cache failed! key = %{public}s", query.key.c_str());
10614cf0368Sopenharmony_ci    int32_t ret = service->GetData(query, unifiedData);
10714cf0368Sopenharmony_ci    if (ret != E_OK) {
10814cf0368Sopenharmony_ci        RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
10914cf0368Sopenharmony_ci            BizScene::GET_DATA, GetDataStage::GET_DATA_END, StageRes::FAILED, ret, BizState::DFX_ABNORMAL_END);
11014cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
11114cf0368Sopenharmony_ci    }
11214cf0368Sopenharmony_ci    RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
11314cf0368Sopenharmony_ci        BizScene::GET_DATA, GetDataStage::GET_DATA_END, StageRes::SUCCESS, BizState::DFX_NORMAL_END);
11414cf0368Sopenharmony_ci    return static_cast<Status>(ret);
11514cf0368Sopenharmony_ci}
11614cf0368Sopenharmony_ci
11714cf0368Sopenharmony_ciStatus UdmfClient::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
11814cf0368Sopenharmony_ci{
11914cf0368Sopenharmony_ci    DdsTrace trace(
12014cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
12114cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
12214cf0368Sopenharmony_ci    if (service == nullptr) {
12314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
12414cf0368Sopenharmony_ci        return E_IPC;
12514cf0368Sopenharmony_ci    }
12614cf0368Sopenharmony_ci    int32_t ret = service->GetBatchData(query, unifiedDataSet);
12714cf0368Sopenharmony_ci    if (ret != E_OK) {
12814cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
12914cf0368Sopenharmony_ci    }
13014cf0368Sopenharmony_ci    return static_cast<Status>(ret);
13114cf0368Sopenharmony_ci}
13214cf0368Sopenharmony_ci
13314cf0368Sopenharmony_ciStatus UdmfClient::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
13414cf0368Sopenharmony_ci{
13514cf0368Sopenharmony_ci    DdsTrace trace(
13614cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
13714cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
13814cf0368Sopenharmony_ci    if (service == nullptr) {
13914cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
14014cf0368Sopenharmony_ci        return E_IPC;
14114cf0368Sopenharmony_ci    }
14214cf0368Sopenharmony_ci    int32_t ret = service->UpdateData(query, unifiedData);
14314cf0368Sopenharmony_ci    if (ret != E_OK) {
14414cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
14514cf0368Sopenharmony_ci    }
14614cf0368Sopenharmony_ci    return static_cast<Status>(ret);
14714cf0368Sopenharmony_ci}
14814cf0368Sopenharmony_ci
14914cf0368Sopenharmony_ciStatus UdmfClient::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
15014cf0368Sopenharmony_ci{
15114cf0368Sopenharmony_ci    DdsTrace trace(
15214cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
15314cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
15414cf0368Sopenharmony_ci    if (service == nullptr) {
15514cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
15614cf0368Sopenharmony_ci        return E_IPC;
15714cf0368Sopenharmony_ci    }
15814cf0368Sopenharmony_ci    int32_t ret = service->DeleteData(query, unifiedDataSet);
15914cf0368Sopenharmony_ci    if (ret != E_OK) {
16014cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
16114cf0368Sopenharmony_ci    }
16214cf0368Sopenharmony_ci    return static_cast<Status>(ret);
16314cf0368Sopenharmony_ci}
16414cf0368Sopenharmony_ci
16514cf0368Sopenharmony_ciStatus UdmfClient::GetSummary(const QueryOption &query, Summary &summary)
16614cf0368Sopenharmony_ci{
16714cf0368Sopenharmony_ci    DdsTrace trace(
16814cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
16914cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
17014cf0368Sopenharmony_ci    if (service == nullptr) {
17114cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
17214cf0368Sopenharmony_ci        return E_IPC;
17314cf0368Sopenharmony_ci    }
17414cf0368Sopenharmony_ci    auto it = dataCache_.Find(query.key);
17514cf0368Sopenharmony_ci    if (it.first) {
17614cf0368Sopenharmony_ci        UnifiedDataHelper::GetSummary(it.second, summary);
17714cf0368Sopenharmony_ci        LOG_INFO(UDMF_CLIENT, "GetSummary in cache! key = %{public}s", query.key.c_str());
17814cf0368Sopenharmony_ci        return E_OK;
17914cf0368Sopenharmony_ci    }
18014cf0368Sopenharmony_ci
18114cf0368Sopenharmony_ci    int32_t ret = service->GetSummary(query, summary);
18214cf0368Sopenharmony_ci    if (ret != E_OK) {
18314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
18414cf0368Sopenharmony_ci    }
18514cf0368Sopenharmony_ci    return static_cast<Status>(ret);
18614cf0368Sopenharmony_ci}
18714cf0368Sopenharmony_ci
18814cf0368Sopenharmony_ciStatus UdmfClient::AddPrivilege(const QueryOption &query, Privilege &privilege)
18914cf0368Sopenharmony_ci{
19014cf0368Sopenharmony_ci    DdsTrace trace(
19114cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
19214cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
19314cf0368Sopenharmony_ci    if (service == nullptr) {
19414cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
19514cf0368Sopenharmony_ci        return E_IPC;
19614cf0368Sopenharmony_ci    }
19714cf0368Sopenharmony_ci    int32_t ret = service->AddPrivilege(query, privilege);
19814cf0368Sopenharmony_ci    if (ret != E_OK) {
19914cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
20014cf0368Sopenharmony_ci    }
20114cf0368Sopenharmony_ci    return static_cast<Status>(ret);
20214cf0368Sopenharmony_ci}
20314cf0368Sopenharmony_ci
20414cf0368Sopenharmony_ciStatus UdmfClient::Sync(const QueryOption &query, const std::vector<std::string> &devices)
20514cf0368Sopenharmony_ci{
20614cf0368Sopenharmony_ci    DdsTrace trace(
20714cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
20814cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
20914cf0368Sopenharmony_ci    if (service == nullptr) {
21014cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
21114cf0368Sopenharmony_ci        return E_IPC;
21214cf0368Sopenharmony_ci    }
21314cf0368Sopenharmony_ci    int32_t ret = service->Sync(query, devices);
21414cf0368Sopenharmony_ci    if (ret != E_OK) {
21514cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
21614cf0368Sopenharmony_ci    }
21714cf0368Sopenharmony_ci    return static_cast<Status>(ret);
21814cf0368Sopenharmony_ci}
21914cf0368Sopenharmony_ci
22014cf0368Sopenharmony_ciStatus UdmfClient::IsRemoteData(const QueryOption &query, bool &result)
22114cf0368Sopenharmony_ci{
22214cf0368Sopenharmony_ci    DdsTrace trace(
22314cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
22414cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
22514cf0368Sopenharmony_ci    if (service == nullptr) {
22614cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
22714cf0368Sopenharmony_ci        return E_IPC;
22814cf0368Sopenharmony_ci    }
22914cf0368Sopenharmony_ci    int32_t ret = service->IsRemoteData(query, result);
23014cf0368Sopenharmony_ci    if (ret != E_OK) {
23114cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
23214cf0368Sopenharmony_ci    }
23314cf0368Sopenharmony_ci    return static_cast<Status>(ret);
23414cf0368Sopenharmony_ci}
23514cf0368Sopenharmony_ci
23614cf0368Sopenharmony_ciStatus UdmfClient::SetAppShareOption(const std::string &intention, enum ShareOptions shareOption)
23714cf0368Sopenharmony_ci{
23814cf0368Sopenharmony_ci    DdsTrace trace(
23914cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
24014cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
24114cf0368Sopenharmony_ci    if (service == nullptr) {
24214cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
24314cf0368Sopenharmony_ci        return E_IPC;
24414cf0368Sopenharmony_ci    }
24514cf0368Sopenharmony_ci    int32_t ret = service->SetAppShareOption(intention, shareOption);
24614cf0368Sopenharmony_ci    if (ret != E_OK) {
24714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
24814cf0368Sopenharmony_ci    }
24914cf0368Sopenharmony_ci    return static_cast<Status>(ret);
25014cf0368Sopenharmony_ci}
25114cf0368Sopenharmony_ci
25214cf0368Sopenharmony_ciStatus UdmfClient::GetAppShareOption(const std::string &intention, enum ShareOptions &shareOption)
25314cf0368Sopenharmony_ci{
25414cf0368Sopenharmony_ci    DdsTrace trace(
25514cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
25614cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
25714cf0368Sopenharmony_ci    if (service == nullptr) {
25814cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
25914cf0368Sopenharmony_ci        return E_IPC;
26014cf0368Sopenharmony_ci    }
26114cf0368Sopenharmony_ci    int32_t shareOptionRet = SHARE_OPTIONS_BUTT;
26214cf0368Sopenharmony_ci    int32_t ret = service->GetAppShareOption(intention, shareOptionRet);
26314cf0368Sopenharmony_ci    if (ShareOptionsUtil::IsValid(shareOptionRet)) {
26414cf0368Sopenharmony_ci        shareOption = static_cast<ShareOptions>(shareOptionRet);
26514cf0368Sopenharmony_ci    }
26614cf0368Sopenharmony_ci    if (ret != E_OK) {
26714cf0368Sopenharmony_ci        LOG_INFO(UDMF_CLIENT, "No share option was obtained, ret = %{public}d", ret);
26814cf0368Sopenharmony_ci    }
26914cf0368Sopenharmony_ci    return static_cast<Status>(ret);
27014cf0368Sopenharmony_ci}
27114cf0368Sopenharmony_ci
27214cf0368Sopenharmony_ci
27314cf0368Sopenharmony_ciStatus UdmfClient::RemoveAppShareOption(const std::string &intention)
27414cf0368Sopenharmony_ci{
27514cf0368Sopenharmony_ci    DdsTrace trace(
27614cf0368Sopenharmony_ci        std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
27714cf0368Sopenharmony_ci    auto service = UdmfServiceClient::GetInstance();
27814cf0368Sopenharmony_ci    if (service == nullptr) {
27914cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "Service unavailable");
28014cf0368Sopenharmony_ci        return E_IPC;
28114cf0368Sopenharmony_ci    }
28214cf0368Sopenharmony_ci    int32_t ret = service->RemoveAppShareOption(intention);
28314cf0368Sopenharmony_ci    if (ret != E_OK) {
28414cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "failed! ret = %{public}d", ret);
28514cf0368Sopenharmony_ci    }
28614cf0368Sopenharmony_ci    return static_cast<Status>(ret);
28714cf0368Sopenharmony_ci}
28814cf0368Sopenharmony_ci
28914cf0368Sopenharmony_cistd::string UdmfClient::GetSelfBundleName()
29014cf0368Sopenharmony_ci{
29114cf0368Sopenharmony_ci    uint32_t tokenId = IPCSkeleton::GetSelfTokenID();
29214cf0368Sopenharmony_ci    Security::AccessToken::HapTokenInfo hapInfo;
29314cf0368Sopenharmony_ci    if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo)
29414cf0368Sopenharmony_ci        != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
29514cf0368Sopenharmony_ci        return "";
29614cf0368Sopenharmony_ci    }
29714cf0368Sopenharmony_ci    return hapInfo.bundleName;
29814cf0368Sopenharmony_ci}
29914cf0368Sopenharmony_ci
30014cf0368Sopenharmony_ciStatus UdmfClient::GetDataAsync(const QueryOption &query, ObtainDataCallback callback)
30114cf0368Sopenharmony_ci{
30214cf0368Sopenharmony_ci    asyncObtainData_.ClearTask();
30314cf0368Sopenharmony_ci
30414cf0368Sopenharmony_ci    auto it = this->dataCache_.Find(query.key);
30514cf0368Sopenharmony_ci    if (it.first) {
30614cf0368Sopenharmony_ci        dataCache_.Erase(query.key);
30714cf0368Sopenharmony_ci        ProgressInfo info{ "Local", ASYNC_SUCCESS, 100 };
30814cf0368Sopenharmony_ci        callback(info, it.second);
30914cf0368Sopenharmony_ci        return E_OK;
31014cf0368Sopenharmony_ci    }
31114cf0368Sopenharmony_ci
31214cf0368Sopenharmony_ci    auto ret = asyncObtainData_.InitTask(query, callback);
31314cf0368Sopenharmony_ci    if (ret == E_OK) {
31414cf0368Sopenharmony_ci        ret = asyncObtainData_.RunTask();
31514cf0368Sopenharmony_ci    }
31614cf0368Sopenharmony_ci    if (ret != E_OK) {
31714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_CLIENT, "InitTask or RunTask faile ret=%{public}d", ret);
31814cf0368Sopenharmony_ci        asyncObtainData_.ClearTask();
31914cf0368Sopenharmony_ci        return ret;
32014cf0368Sopenharmony_ci    }
32114cf0368Sopenharmony_ci    return E_OK;
32214cf0368Sopenharmony_ci}
32314cf0368Sopenharmony_ci} // namespace UDMF
32414cf0368Sopenharmony_ci} // namespace OHOS