114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2022 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 "UdmfServiceClient"
1614cf0368Sopenharmony_ci#include "udmf_service_client.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "iservice_registry.h"
1914cf0368Sopenharmony_ci#include "datamgr_service_proxy.h"
2014cf0368Sopenharmony_ci#include "system_ability_definition.h"
2114cf0368Sopenharmony_ci#include "unified_data_helper.h"
2214cf0368Sopenharmony_ci
2314cf0368Sopenharmony_ci#include "logger.h"
2414cf0368Sopenharmony_ci
2514cf0368Sopenharmony_cinamespace OHOS {
2614cf0368Sopenharmony_cinamespace UDMF {
2714cf0368Sopenharmony_cistd::shared_ptr<UdmfServiceClient> UdmfServiceClient::instance_;
2814cf0368Sopenharmony_cistd::mutex UdmfServiceClient::mutex_;
2914cf0368Sopenharmony_cisptr<DistributedKv::IKvStoreDataService> UdmfServiceClient::kvDataServiceProxy_;
3014cf0368Sopenharmony_ci
3114cf0368Sopenharmony_ciUdmfServiceClient::UdmfServiceClient(const sptr<UdmfServiceProxy> &proxy) : udmfProxy_(proxy)
3214cf0368Sopenharmony_ci{
3314cf0368Sopenharmony_ci    LOG_INFO(UDMF_SERVICE, "construct");
3414cf0368Sopenharmony_ci}
3514cf0368Sopenharmony_ci
3614cf0368Sopenharmony_cistd::shared_ptr<UdmfServiceClient> UdmfServiceClient::GetInstance()
3714cf0368Sopenharmony_ci{
3814cf0368Sopenharmony_ci    std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
3914cf0368Sopenharmony_ci    if (instance_ != nullptr) {
4014cf0368Sopenharmony_ci        return instance_;
4114cf0368Sopenharmony_ci    }
4214cf0368Sopenharmony_ci    sptr<DistributedKv::IKvStoreDataService> ability = GetDistributedKvDataService();
4314cf0368Sopenharmony_ci    if (ability == nullptr) {
4414cf0368Sopenharmony_ci        return nullptr;
4514cf0368Sopenharmony_ci    }
4614cf0368Sopenharmony_ci    sptr<IRemoteObject> service = ability->GetFeatureInterface("udmf");
4714cf0368Sopenharmony_ci    if (service == nullptr) {
4814cf0368Sopenharmony_ci        return nullptr;
4914cf0368Sopenharmony_ci    }
5014cf0368Sopenharmony_ci    sptr<UdmfServiceProxy> proxy = iface_cast<UdmfServiceProxy>(service);
5114cf0368Sopenharmony_ci    if (proxy == nullptr) {
5214cf0368Sopenharmony_ci        return nullptr;
5314cf0368Sopenharmony_ci    }
5414cf0368Sopenharmony_ci    instance_ = std::make_shared<UdmfServiceClient>(proxy);
5514cf0368Sopenharmony_ci    return instance_;
5614cf0368Sopenharmony_ci}
5714cf0368Sopenharmony_ci
5814cf0368Sopenharmony_cisptr<DistributedKv::IKvStoreDataService> UdmfServiceClient::GetDistributedKvDataService()
5914cf0368Sopenharmony_ci{
6014cf0368Sopenharmony_ci    if (kvDataServiceProxy_ != nullptr) {
6114cf0368Sopenharmony_ci        return kvDataServiceProxy_;
6214cf0368Sopenharmony_ci    }
6314cf0368Sopenharmony_ci    LOG_INFO(UDMF_SERVICE, "create remote proxy.");
6414cf0368Sopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
6514cf0368Sopenharmony_ci    if (samgr == nullptr) {
6614cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "get samgr fail.");
6714cf0368Sopenharmony_ci        return nullptr;
6814cf0368Sopenharmony_ci    }
6914cf0368Sopenharmony_ci
7014cf0368Sopenharmony_ci    auto remote = samgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
7114cf0368Sopenharmony_ci    kvDataServiceProxy_ = iface_cast<DistributedKv::DataMgrServiceProxy>(remote);
7214cf0368Sopenharmony_ci    if (kvDataServiceProxy_ == nullptr) {
7314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "initialize proxy failed.");
7414cf0368Sopenharmony_ci        return nullptr;
7514cf0368Sopenharmony_ci    }
7614cf0368Sopenharmony_ci    sptr<UdmfServiceClient::ServiceDeathRecipient> deathRecipientPtr = new (std::nothrow)ServiceDeathRecipient();
7714cf0368Sopenharmony_ci    if (deathRecipientPtr == nullptr) {
7814cf0368Sopenharmony_ci        return nullptr;
7914cf0368Sopenharmony_ci    }
8014cf0368Sopenharmony_ci    if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipientPtr))) {
8114cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "Add death recipient fail!");
8214cf0368Sopenharmony_ci    }
8314cf0368Sopenharmony_ci    return kvDataServiceProxy_;
8414cf0368Sopenharmony_ci}
8514cf0368Sopenharmony_ci
8614cf0368Sopenharmony_ciUdmfServiceClient::ServiceDeathRecipient::ServiceDeathRecipient()
8714cf0368Sopenharmony_ci{
8814cf0368Sopenharmony_ci    LOG_INFO(UDMF_SERVICE, "Construct!");
8914cf0368Sopenharmony_ci}
9014cf0368Sopenharmony_ci
9114cf0368Sopenharmony_ciUdmfServiceClient::ServiceDeathRecipient::~ServiceDeathRecipient()
9214cf0368Sopenharmony_ci{
9314cf0368Sopenharmony_ci    LOG_INFO(UDMF_SERVICE, "Destruct!");
9414cf0368Sopenharmony_ci}
9514cf0368Sopenharmony_ci
9614cf0368Sopenharmony_civoid UdmfServiceClient::ServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
9714cf0368Sopenharmony_ci{
9814cf0368Sopenharmony_ci    LOG_WARN(UDMF_SERVICE, "DistributedDataService die!");
9914cf0368Sopenharmony_ci    std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
10014cf0368Sopenharmony_ci    kvDataServiceProxy_ = nullptr;
10114cf0368Sopenharmony_ci    instance_ = nullptr;
10214cf0368Sopenharmony_ci}
10314cf0368Sopenharmony_ci
10414cf0368Sopenharmony_ciint32_t UdmfServiceClient::SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key)
10514cf0368Sopenharmony_ci{
10614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: %{public}d", option.intention);
10714cf0368Sopenharmony_ci    if (!UnifiedDataUtils::IsValidIntention(option.intention)) {
10814cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "Invalid intention");
10914cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
11014cf0368Sopenharmony_ci    }
11114cf0368Sopenharmony_ci
11214cf0368Sopenharmony_ci    if (!unifiedData.IsValid()) {
11314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "UnifiedData is invalid.");
11414cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
11514cf0368Sopenharmony_ci    }
11614cf0368Sopenharmony_ci    if (UnifiedDataHelper::ExceedKVSizeLimit(unifiedData)) {
11714cf0368Sopenharmony_ci        if (!UnifiedDataHelper::Pack(unifiedData)) {
11814cf0368Sopenharmony_ci            LOG_ERROR(UDMF_SERVICE, "Failed to pack unified data.");
11914cf0368Sopenharmony_ci            return E_FS_ERROR;
12014cf0368Sopenharmony_ci        }
12114cf0368Sopenharmony_ci    }
12214cf0368Sopenharmony_ci    return udmfProxy_->SetData(option, unifiedData, key);
12314cf0368Sopenharmony_ci}
12414cf0368Sopenharmony_ci
12514cf0368Sopenharmony_ciint32_t UdmfServiceClient::GetData(const QueryOption &query, UnifiedData &unifiedData)
12614cf0368Sopenharmony_ci{
12714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: %{public}s", query.key.c_str());
12814cf0368Sopenharmony_ci    UnifiedKey key(query.key);
12914cf0368Sopenharmony_ci    if (!key.IsValid()) {
13014cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key");
13114cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
13214cf0368Sopenharmony_ci    }
13314cf0368Sopenharmony_ci
13414cf0368Sopenharmony_ci    auto err = udmfProxy_->GetData(query, unifiedData);
13514cf0368Sopenharmony_ci    if (err == E_OK) {
13614cf0368Sopenharmony_ci        if (UnifiedDataHelper::IsTempUData(unifiedData)) {
13714cf0368Sopenharmony_ci            if (!UnifiedDataHelper::Unpack(unifiedData)) {
13814cf0368Sopenharmony_ci                LOG_ERROR(UDMF_SERVICE, "failed to unpack unified data");
13914cf0368Sopenharmony_ci                return E_FS_ERROR;
14014cf0368Sopenharmony_ci            }
14114cf0368Sopenharmony_ci        }
14214cf0368Sopenharmony_ci    }
14314cf0368Sopenharmony_ci    return err;
14414cf0368Sopenharmony_ci}
14514cf0368Sopenharmony_ci
14614cf0368Sopenharmony_ciint32_t UdmfServiceClient::GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
14714cf0368Sopenharmony_ci{
14814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: intention = %{public}d, key = %{public}s", query.intention, query.key.c_str());
14914cf0368Sopenharmony_ci    auto find = UD_INTENTION_MAP.find(query.intention);
15014cf0368Sopenharmony_ci    std::string intention = find == UD_INTENTION_MAP.end() ? intention : find->second;
15114cf0368Sopenharmony_ci    if (!UnifiedDataUtils::IsValidOptions(query.key, intention)) {
15214cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid option, query.key: %{public}s, intention: %{public}s", query.key.c_str(),
15314cf0368Sopenharmony_ci            intention.c_str());
15414cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
15514cf0368Sopenharmony_ci    }
15614cf0368Sopenharmony_ci    std::vector<UnifiedData> tempDataSet {};
15714cf0368Sopenharmony_ci    auto err = udmfProxy_->GetBatchData(query, tempDataSet);
15814cf0368Sopenharmony_ci    if (err != E_OK) {
15914cf0368Sopenharmony_ci        return err;
16014cf0368Sopenharmony_ci    }
16114cf0368Sopenharmony_ci    for (auto &data : tempDataSet) {
16214cf0368Sopenharmony_ci        if (UnifiedDataHelper::IsTempUData(data)) {
16314cf0368Sopenharmony_ci            if (!UnifiedDataHelper::Unpack(data)) {
16414cf0368Sopenharmony_ci                LOG_ERROR(UDMF_SERVICE, "failed to unpack unified data");
16514cf0368Sopenharmony_ci                return E_FS_ERROR;
16614cf0368Sopenharmony_ci            }
16714cf0368Sopenharmony_ci        }
16814cf0368Sopenharmony_ci        unifiedDataSet.emplace_back(data);
16914cf0368Sopenharmony_ci    }
17014cf0368Sopenharmony_ci    return E_OK;
17114cf0368Sopenharmony_ci}
17214cf0368Sopenharmony_ci
17314cf0368Sopenharmony_ciint32_t UdmfServiceClient::UpdateData(const QueryOption &query, UnifiedData &unifiedData)
17414cf0368Sopenharmony_ci{
17514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: %{public}s", query.key.c_str());
17614cf0368Sopenharmony_ci    UnifiedKey key(query.key);
17714cf0368Sopenharmony_ci    if (!key.IsValid() || !UnifiedDataUtils::IsPersist(key.intention)) {
17814cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key, key.intention: %{public}s", key.intention.c_str());
17914cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
18014cf0368Sopenharmony_ci    }
18114cf0368Sopenharmony_ci
18214cf0368Sopenharmony_ci    if (!unifiedData.IsValid()) {
18314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "UnifiedData is invalid.");
18414cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
18514cf0368Sopenharmony_ci    }
18614cf0368Sopenharmony_ci    if (UnifiedDataHelper::ExceedKVSizeLimit(unifiedData)) {
18714cf0368Sopenharmony_ci        if (!UnifiedDataHelper::Pack(unifiedData)) {
18814cf0368Sopenharmony_ci            LOG_ERROR(UDMF_SERVICE, "Failed to pack unified data.");
18914cf0368Sopenharmony_ci            return E_FS_ERROR;
19014cf0368Sopenharmony_ci        }
19114cf0368Sopenharmony_ci    }
19214cf0368Sopenharmony_ci    return udmfProxy_->UpdateData(query, unifiedData);
19314cf0368Sopenharmony_ci}
19414cf0368Sopenharmony_ci
19514cf0368Sopenharmony_ciint32_t UdmfServiceClient::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet)
19614cf0368Sopenharmony_ci{
19714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: intention = %{public}d, key = %{public}s", query.intention, query.key.c_str());
19814cf0368Sopenharmony_ci    auto find = UD_INTENTION_MAP.find(query.intention);
19914cf0368Sopenharmony_ci    std::string intention = find == UD_INTENTION_MAP.end() ? intention : find->second;
20014cf0368Sopenharmony_ci    if (!UnifiedDataUtils::IsValidOptions(query.key, intention)) {
20114cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid option, query.key: %{public}s, intention: %{public}s", query.key.c_str(),
20214cf0368Sopenharmony_ci            intention.c_str());
20314cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
20414cf0368Sopenharmony_ci    }
20514cf0368Sopenharmony_ci    return udmfProxy_->DeleteData(query, unifiedDataSet);
20614cf0368Sopenharmony_ci}
20714cf0368Sopenharmony_ci
20814cf0368Sopenharmony_ciint32_t UdmfServiceClient::GetSummary(const QueryOption &query, Summary &summary)
20914cf0368Sopenharmony_ci{
21014cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, tag: %{public}s", query.key.c_str());
21114cf0368Sopenharmony_ci    UnifiedKey key(query.key);
21214cf0368Sopenharmony_ci    if (!key.IsValid()) {
21314cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key");
21414cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
21514cf0368Sopenharmony_ci    }
21614cf0368Sopenharmony_ci    return udmfProxy_->GetSummary(query, summary);
21714cf0368Sopenharmony_ci}
21814cf0368Sopenharmony_ci
21914cf0368Sopenharmony_ciint32_t UdmfServiceClient::AddPrivilege(const QueryOption &query, Privilege &privilege)
22014cf0368Sopenharmony_ci{
22114cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, key: %{public}s", query.key.c_str());
22214cf0368Sopenharmony_ci    UnifiedKey key(query.key);
22314cf0368Sopenharmony_ci    if (!key.IsValid()) {
22414cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key");
22514cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
22614cf0368Sopenharmony_ci    }
22714cf0368Sopenharmony_ci    return udmfProxy_->AddPrivilege(query, privilege);
22814cf0368Sopenharmony_ci}
22914cf0368Sopenharmony_ci
23014cf0368Sopenharmony_ciint32_t UdmfServiceClient::Sync(const QueryOption &query, const std::vector<std::string> &devices)
23114cf0368Sopenharmony_ci{
23214cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, key: %{public}s", query.key.c_str());
23314cf0368Sopenharmony_ci    UnifiedKey key(query.key);
23414cf0368Sopenharmony_ci    if (!key.IsValid()) {
23514cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key");
23614cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
23714cf0368Sopenharmony_ci    }
23814cf0368Sopenharmony_ci    return udmfProxy_->Sync(query, devices);
23914cf0368Sopenharmony_ci}
24014cf0368Sopenharmony_ci
24114cf0368Sopenharmony_ciint32_t UdmfServiceClient::IsRemoteData(const QueryOption &query, bool &result)
24214cf0368Sopenharmony_ci{
24314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, key: %{public}s", query.key.c_str());
24414cf0368Sopenharmony_ci    UnifiedKey key(query.key);
24514cf0368Sopenharmony_ci    if (!key.IsValid()) {
24614cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid key");
24714cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
24814cf0368Sopenharmony_ci    }
24914cf0368Sopenharmony_ci    return udmfProxy_->IsRemoteData(query, result);
25014cf0368Sopenharmony_ci}
25114cf0368Sopenharmony_ci
25214cf0368Sopenharmony_ciint32_t UdmfServiceClient::SetAppShareOption(const std::string &intention, int32_t shareOption)
25314cf0368Sopenharmony_ci{
25414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, intention: %{public}s, shareOption: %{public}d",
25514cf0368Sopenharmony_ci              intention.c_str(), shareOption);
25614cf0368Sopenharmony_ci    if (intention.empty() || shareOption < IN_APP || shareOption > CROSS_APP) {
25714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid parameters");
25814cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
25914cf0368Sopenharmony_ci    }
26014cf0368Sopenharmony_ci    return udmfProxy_->SetAppShareOption(intention, shareOption);
26114cf0368Sopenharmony_ci}
26214cf0368Sopenharmony_ci
26314cf0368Sopenharmony_ciint32_t UdmfServiceClient::GetAppShareOption(const std::string &intention, int32_t &shareOption)
26414cf0368Sopenharmony_ci{
26514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, intention: %{public}s", intention.c_str());
26614cf0368Sopenharmony_ci    if (intention.empty()) {
26714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid parameters");
26814cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
26914cf0368Sopenharmony_ci    }
27014cf0368Sopenharmony_ci    return udmfProxy_->GetAppShareOption(intention, shareOption);
27114cf0368Sopenharmony_ci}
27214cf0368Sopenharmony_ci
27314cf0368Sopenharmony_ciint32_t UdmfServiceClient::RemoveAppShareOption(const std::string &intention)
27414cf0368Sopenharmony_ci{
27514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_SERVICE, "start, intention: %{public}s", intention.c_str());
27614cf0368Sopenharmony_ci    if (intention.empty()) {
27714cf0368Sopenharmony_ci        LOG_ERROR(UDMF_SERVICE, "invalid parameters");
27814cf0368Sopenharmony_ci        return E_INVALID_PARAMETERS;
27914cf0368Sopenharmony_ci    }
28014cf0368Sopenharmony_ci    return udmfProxy_->RemoveAppShareOption(intention);
28114cf0368Sopenharmony_ci}
28214cf0368Sopenharmony_ci
28314cf0368Sopenharmony_ciint32_t UdmfServiceClient::ObtainAsynProcess(AsyncProcessInfo& processInfo)
28414cf0368Sopenharmony_ci{
28514cf0368Sopenharmony_ci    return udmfProxy_->ObtainAsynProcess(processInfo);
28614cf0368Sopenharmony_ci}
28714cf0368Sopenharmony_ci
28814cf0368Sopenharmony_ciint32_t UdmfServiceClient::ClearAsynProcess()
28914cf0368Sopenharmony_ci{
29014cf0368Sopenharmony_ci    return udmfProxy_->ClearAsynProcess();
29114cf0368Sopenharmony_ci}
29214cf0368Sopenharmony_ci} // namespace UDMF
29314cf0368Sopenharmony_ci} // namespace OHOS