From 26f9ef1f534fab557a39a4577f9a33b2f44c5537 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 6 Nov 2023 10:08:38 +0800 Subject: [PATCH 1/4] update Signed-off-by: zuojiangjiang --- .../framework/cloud/sharing_center.cpp | 80 ++++ .../framework/include/cloud/sharing_center.h | 53 +++ .../service/cloud/cloud_service_impl.cpp | 136 ++++++ .../service/cloud/cloud_service_impl.h | 15 + .../service/cloud/cloud_service_stub.cpp | 112 ++++- .../service/cloud/cloud_service_stub.h | 10 + kv_store/frameworks/common/itypes_util.h | 29 ++ .../napi/cloud_data/include/js_cloud_share.h | 26 + .../napi/cloud_data/include/js_cloud_utils.h | 39 ++ .../js/napi/cloud_data/src/entry_point.cpp | 2 + .../js/napi/cloud_data/src/js_cloud_share.cpp | 446 ++++++++++++++++++ .../js/napi/cloud_data/src/js_cloud_utils.cpp | 112 +++++ .../cloud_data/src/js_const_properties.cpp | 25 + .../js/napi/common/include/js_utils.h | 47 ++ .../js/napi/common/src/js_utils.cpp | 39 ++ .../cloud_data/include/cloud_service_proxy.h | 15 + .../cloud_data/include/cloud_types_util.h | 57 +++ .../cloud_data/src/cloud_service_proxy.cpp | 97 ++++ .../cloud_data/src/cloud_types_util.cpp | 96 ++++ .../cloud_data/include/cloud_service.h | 28 ++ .../cloud_data/include/cloud_types.h | 57 +++ 21 files changed, 1513 insertions(+), 8 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp create mode 100644 datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h create mode 100644 relational_store/frameworks/js/napi/cloud_data/include/js_cloud_share.h create mode 100644 relational_store/frameworks/js/napi/cloud_data/include/js_cloud_utils.h create mode 100644 relational_store/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp create mode 100644 relational_store/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp create mode 100644 relational_store/frameworks/native/cloud_data/include/cloud_types_util.h create mode 100644 relational_store/frameworks/native/cloud_data/src/cloud_types_util.cpp create mode 100644 relational_store/interfaces/inner_api/cloud_data/include/cloud_types.h diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp new file mode 100644 index 00000000..be2eb7cb --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud/sharing_center.h" +namespace OHOS::DistributedData { +SharingCenter *SharingCenter::instance_ = nullptr; +SharingCenter *SharingCenter::GetInstance() +{ + return instance_; +} + +bool SharingCenter::RegisterSharingInstance(SharingCenter *instance) +{ + if (instance_ != nullptr) { + return false; + } + instance_ = instance; + return true; +} + +int32_t SharingCenter::Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t SharingCenter::Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t SharingCenter::ExitSharing(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result &result) +{ + return 0; +} + +int32_t SharingCenter::ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t SharingCenter::QueryParticipants(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result> &result) +{ + return 0; +} + +int32_t SharingCenter::QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, Result> &result) +{ + return 0; +} + +int32_t SharingCenter::ConfirmInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, SharingStatus sharingStatus, Result &result) +{ + return 0; +} + +int32_t SharingCenter::ChangeConfirmation(int32_t userId, const std::string &bundleName, + const std::string &sharingRes,SharingStatus sharingStatus, Result &result) +{ + return 0; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h new file mode 100644 index 00000000..9d6264c2 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H + +//#include "store/general_value.h" +#include "visibility.h" +#include "itypes_util.h" +#include "cloud_types.h" + +namespace OHOS::DistributedData { +class API_EXPORT SharingCenter { +public: + using Participant = OHOS::CloudData::Participant; + using SharingStatus = OHOS::CloudData::SharingStatus; + API_EXPORT static SharingCenter *GetInstance(); + API_EXPORT static bool RegisterSharingInstance(SharingCenter *instance); + + virtual int32_t Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t ExitSharing(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result &result); + virtual int32_t ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t QueryParticipants(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result> &result); + virtual int32_t QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, Result> &result); + virtual int32_t ConfirmInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, SharingStatus sharingStatus, Result &result); + virtual int32_t ChangeConfirmation(int32_t userId, const std::string &bundleName, + const std::string &sharingRes,SharingStatus sharingStatus, Result &result); + +private: + static SharingCenter *instance_; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 754d0e2f..e76c59fc 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -20,6 +20,7 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" #include "cloud/cloud_server.h" +#include "cloud/sharing_center.h" #include "communicator/device_manager_adapter.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" @@ -37,6 +38,7 @@ using namespace std::chrono; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Account = OHOS::DistributedKv::AccountDelegate; using AccessTokenKit = Security::AccessToken::AccessTokenKit; +using namespace Security::AccessToken; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; CloudServiceImpl::Factory::Factory() noexcept @@ -591,4 +593,138 @@ std::map CloudServiceImpl::ConvertAction(const std::map &participants, + Result>> &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->Share(tokenInfo.userID, tokenInfo.bundleName, sharingRes, participants, result); +} + +int32_t CloudServiceImpl::Unshare(const std::string &sharingRes, const std::vector &participants, + Result>> &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->Unshare(tokenInfo.userID, tokenInfo.bundleName, sharingRes, participants, result); +} + +int32_t CloudServiceImpl::ExitSharing(const std::string &sharingRes, Result &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->ExitSharing(tokenInfo.userID, tokenInfo.bundleName, sharingRes, result); +} + +int32_t CloudServiceImpl::ChangePrivilege(const std::string &sharingRes, const std::vector &participants, + Result>> &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->ChangePrivilege(tokenInfo.userID, tokenInfo.bundleName, sharingRes, participants, result); +} + +int32_t CloudServiceImpl::QueryParticipants(const std::string &sharingRes, Result> &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->QueryParticipants(tokenInfo.userID, tokenInfo.bundleName, sharingRes, result); +} + +int32_t CloudServiceImpl::QueryParticipantsByInvitation(const std::string &invitationCode, + Result> &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->QueryParticipantsByInvitation(tokenInfo.userID, tokenInfo.bundleName, invitationCode, result); +} + +int32_t CloudServiceImpl::ConfirmInvitation(const std::string &invitationCode, SharingStatus sharingStatus, + Result &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->ConfirmInvitation(tokenInfo.userID, tokenInfo.bundleName, invitationCode, sharingStatus, result); +} + +int32_t CloudServiceImpl::ChangeConfirmation( + const std::string &sharingRes, SharingStatus sharingStatus, Result &result) +{ + auto tokenId = IPCSkeleton::GetCallingTokenID(); + HapTokenInfo tokenInfo; + auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (status != RET_SUCCESS) { + ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); + return E_ERROR; + } + auto instance = SharingCenter::GetInstance(); + if (instance == nullptr) { + return E_NOT_SUPPORT; + } + return instance->ChangeConfirmation(tokenInfo.userID, tokenInfo.bundleName, sharingRes, sharingStatus, result); +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index c94deae5..6827ac87 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -42,6 +42,21 @@ public: int32_t Online(const std::string &device) override; int32_t Offline(const std::string &device) override; + int32_t Share(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t Unshare(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t ExitSharing(const std::string &sharingRes, Result &result) override; + int32_t ChangePrivilege(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t QueryParticipants(const std::string &sharingRes, Result> &result) override; + int32_t QueryParticipantsByInvitation(const std::string &invitationCode, + Result> &result) override; + int32_t ConfirmInvitation(const std::string &invitationCode, SharingStatus sharingStatus, + Result &result) override; + int32_t ChangeConfirmation( + const std::string &sharingRes, SharingStatus sharingStatus, Result &result) override; + private: using StaticActs = DistributedData::StaticActs; class CloudStatic : public StaticActs { diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index 34a65dc1..655e1505 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -30,6 +30,14 @@ const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnChangeAppSwitch, &CloudServiceStub::OnClean, &CloudServiceStub::OnNotifyDataChange, + &CloudServiceStub::OnShare, + &CloudServiceStub::OnUnshare, + &CloudServiceStub::OnExitSharing, + &CloudServiceStub::OnChangePrivilege, + &CloudServiceStub::OnQueryParticipants, + &CloudServiceStub::OnQueryParticipantsByInvitation, + &CloudServiceStub::OnConfirmInvitation, + &CloudServiceStub::OnChangeConfirmation, }; int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply) @@ -53,18 +61,22 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } - if (!DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID())) { - ZLOGE("cloud config permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); - auto result = static_cast(CLOUD_CONFIG_PERMISSION_DENIED); - return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; + if(code >= TRANS_HEAD && code < TRANS_SHARE) { + auto permit = + DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID()); + if (!permit) { + ZLOGE("cloud config permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); + auto result = static_cast(CLOUD_CONFIG_PERMISSION_DENIED); + return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; + } } - std::string id; - if (!ITypesUtil::Unmarshal(data, id)) { - ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); + std::string first; + if (!ITypesUtil::Unmarshal(data, first)) { + ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(first).c_str()); return IPC_STUB_INVALID_DATA_ERR; } - return (this->*HANDLERS[code])(id, data, reply); + return (this->*HANDLERS[code])(first, data, reply); } int32_t CloudServiceStub::OnEnableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply) @@ -117,4 +129,88 @@ int32_t CloudServiceStub::OnNotifyDataChange(const std::string &id, MessageParce auto result = NotifyDataChange(id, bundleName); return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; } + +int32_t CloudServiceStub::OnShare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + std::vector participants; + if (!ITypesUtil::Unmarshal(data, participants)) { + ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + Result>> result; + auto status = Share(sharingRes, participants, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnUnshare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + std::vector participants; + if (!ITypesUtil::Unmarshal(data, participants)) { + ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + Result>> result; + auto status = Unshare(sharingRes, participants, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnExitSharing(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + Result result; + auto status = ExitSharing(sharingRes, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnChangePrivilege(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + std::vector participants; + if (!ITypesUtil::Unmarshal(data, participants)) { + ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + Result>> result; + auto status = ChangePrivilege(sharingRes, participants, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnQueryParticipants(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + Result> result; + auto status = QueryParticipants(sharingRes, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnQueryParticipantsByInvitation( + const std::string &invitationCode, MessageParcel &data, MessageParcel &reply) +{ + Result> result; + auto status = QueryParticipantsByInvitation(invitationCode, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnConfirmInvitation( + const std::string &invitationCode, MessageParcel &data, MessageParcel &reply) +{ + SharingStatus sharingStatus; + if (!ITypesUtil::Unmarshal(data, sharingStatus)) { + ZLOGE("Unmarshal invitationCode:%{public}s", Anonymous::Change(invitationCode).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + Result result; + auto status = ConfirmInvitation(invitationCode, sharingStatus, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} + +int32_t CloudServiceStub::OnChangeConfirmation( + const std::string &sharingRes, MessageParcel &data, MessageParcel &reply) +{ + SharingStatus sharingStatus; + if (!ITypesUtil::Unmarshal(data, sharingStatus)) { + ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + Result result; + auto status = ChangeConfirmation(sharingRes, sharingStatus, result); + return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR; +} } // namespace OHOS::CloudData diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.h index 4175015d..347db56f 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.h @@ -31,6 +31,16 @@ private: int32_t OnChangeAppSwitch(const std::string &id, MessageParcel &data, MessageParcel &reply); int32_t OnClean(const std::string &id, MessageParcel &data, MessageParcel &reply); int32_t OnNotifyDataChange(const std::string &id, MessageParcel &data, MessageParcel &reply); + + int32_t OnShare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); + int32_t OnUnshare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); + int32_t OnExitSharing(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); + int32_t OnChangePrivilege(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); + int32_t OnQueryParticipants(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); + int32_t OnQueryParticipantsByInvitation( + const std::string &invitationCode, MessageParcel &data, MessageParcel &reply); + int32_t OnConfirmInvitation(const std::string &invitationCode, MessageParcel &data, MessageParcel &reply) ; + int32_t OnChangeConfirmation(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply); static const Handler HANDLERS[TRANS_BUTT]; }; } // namespace OHOS::CloudData diff --git a/kv_store/frameworks/common/itypes_util.h b/kv_store/frameworks/common/itypes_util.h index f9c21890..1e1ca51b 100644 --- a/kv_store/frameworks/common/itypes_util.h +++ b/kv_store/frameworks/common/itypes_util.h @@ -35,6 +35,12 @@ struct is_container> : std::true_type { template struct is_container> : std::true_type { }; +template +struct Result { + int32_t errCode; + using value_type = typename std::conditional::value, std::nullptr_t, T>::type; + value_type value; +}; namespace ITypesUtil { inline constexpr size_t MAX_COUNT = 100000; inline constexpr size_t MAX_SIZE = 1 * 1024 * 1024 * 1024; //1G @@ -204,6 +210,11 @@ bool Marshalling(const T &input, MessageParcel &data); template bool Unmarshalling(T &output, MessageParcel &data); +template +bool Marshalling(const Result &val, MessageParcel &data); +template +bool Unmarshalling(Result &val, MessageParcel &data); + template{}, int>::type = 0> bool MarshalToContainer(const T &val, MessageParcel &parcel); template{}, int>::type = 0> @@ -226,6 +237,24 @@ template bool Unmarshal(MessageParcel &parcel, T &first, Types &...others); } // namespace ITypesUtil +template +bool ITypesUtil::Marshalling(const Result &val, MessageParcel &data) +{ + if (!data.WriteInt32(val.errCode)) { + return false; + } + return std::is_null_pointer::value ? true : Marshalling(val.value, data); +} + +template +bool ITypesUtil::Unmarshalling(Result &val, MessageParcel &data) +{ + if (!data.ReadInt32(val.errCode)) { + return false; + } + return std::is_null_pointer::value ? true : Unmarshalling(val.value, data); +} + template bool ITypesUtil::ReadVariant(uint32_t step, uint32_t index, const _OutTp &output, MessageParcel &data) { diff --git a/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_share.h b/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_share.h new file mode 100644 index 00000000..7b36f185 --- /dev/null +++ b/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_share.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CLOUD_DATA_JS_CLOUD_SHARE_H +#define CLOUD_DATA_JS_CLOUD_SHARE_H + +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" + +namespace OHOS::CloudData { +napi_value InitCloudDataShare(napi_env env, napi_value exports); +} // namespace OHOS::CloudData +#endif // CLOUD_DATA_JS_CLOUD_SHARE_H diff --git a/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_utils.h b/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_utils.h new file mode 100644 index 00000000..e0b6bfa5 --- /dev/null +++ b/relational_store/frameworks/js/napi/cloud_data/include/js_cloud_utils.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CLOUD_DATA_JS_CLOUD_SHARE_H +#define CLOUD_DATA_JS_CLOUD_SHARE_H + +#include "js_utils.h" +#include "cloud_types.h" + +namespace OHOS::AppDataMgrJsKit { +namespace JSUtils { +using Participant = OHOS::CloudData::Participant; +using Privilege = OHOS::CloudData::Privilege; + +template<> +int32_t Convert2Value(napi_env env, napi_value input, Participant &output); + +template<> +int32_t Convert2Value(napi_env env, napi_value input, Privilege &output); + +template<> +napi_value Convert2JSValue(napi_env env, const Participant &value); + +template<> +napi_value Convert2JSValue(napi_env env, const Privilege &value); +}; // namespace JSUtils +} // namespace OHOS::AppDataMgrJsKit +#endif // CLOUD_DATA_JS_CLOUD_SHARE_H diff --git a/relational_store/frameworks/js/napi/cloud_data/src/entry_point.cpp b/relational_store/frameworks/js/napi/cloud_data/src/entry_point.cpp index 8dd249b9..417b0bb5 100644 --- a/relational_store/frameworks/js/napi/cloud_data/src/entry_point.cpp +++ b/relational_store/frameworks/js/napi/cloud_data/src/entry_point.cpp @@ -16,12 +16,14 @@ #include "js_config.h" #include "js_const_properties.h" #include "logger.h" +#include "js_cloud_share.h" using namespace OHOS::CloudData; using namespace OHOS::Rdb; static napi_value Init(napi_env env, napi_value exports) { + InitCloudDataShare(env, exports); exports = JsConfig::InitConfig(env, exports); napi_status status = InitConstProperties(env, exports); LOG_INFO("init Enumerate Constants %{public}d", status); diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp new file mode 100644 index 00000000..3ef7737d --- /dev/null +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp @@ -0,0 +1,446 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_cloud_share.h" + +#include "cloud_manager.h" +#include "cloud_service.h" +#include "cloud_types.h" +#include "js_error_utils.h" +#include "js_utils.h" +#include "logger.h" +#include "napi_queue.h" +#include "rdb_predicates.h" + +namespace OHOS::CloudData { +using namespace OHOS::NativeRdb; +using namespace OHOS::AppDataMgrJsKit; +/* + * [JS API Prototype] + * [AsyncCallback] + * allocResourceAndShare(storeId: string, predicates: relationalStore.RdbPredicates, + * participants: Array, callback: AsyncCallback): void; + * allocResourceAndShare(storeId: string, predicates: relationalStore.RdbPredicates, + * participants: Array, columns: Array, + * callback: AsyncCallback ): void; + * + * [Promise] + * allocResourceAndShare(storeId: string, predicates: relationalStore.RdbPredicates, + * participants: Array, columns?: Array): Promise; + */ +napi_value AllocResourceAndShare(napi_env env, napi_callback_info info) +{ + struct AllocResAndShareContext : public ContextBase { + std::string sharingRes; + std::vector participants; + std::vector columns; + std::shared_ptr predicates = nullptr; + }; + return nullptr; +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * share(sharingRes: string, participants: Array, + * callback: AsyncCallback>>>): void; + * + * [Promise] + * share(sharingRes: string, participants: Array): Promise>>>; + */ +napi_value Share(napi_env env, napi_callback_info info) +{ + struct ShareContext : public ContextBase { + std::string sharingRes; + std::vector participants; + Result>> value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + status = JSUtils::Convert2Value(env, argv[1], ctxt->participants); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of participants must be Array."); + }); + ASSERT_NULL(!ctxt->isThrowError, "share exit"); + + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->Share(ctxt->sharingRes, ctxt->participants, ctxt->value); + LOG_DEBUG("share result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * unshare(sharingRes: string, participants: Array, + * callback: AsyncCallback>>>): void; + * + * [Promise] + * unshare(sharingRes: string, participants: Array): Promise>>>; + */ +napi_value Unshare(napi_env env, napi_callback_info info) +{ + struct UnshareContext : public ContextBase { + std::string sharingRes; + std::vector participants; + Result>> value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + status = JSUtils::Convert2Value(env, argv[1], ctxt->participants); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of participants must be Array."); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->Unshare(ctxt->sharingRes, ctxt->participants, ctxt->value); + LOG_DEBUG("unshare result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * exit(sharingRes: string, callback: AsyncCallback>): void; + * + * [Promise] + * exit(sharingRes: string): Promise>; + */ +napi_value Exit(napi_env env, napi_callback_info info) +{ + struct ExitContext : public ContextBase { + std::string sharingRes; + Result value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->ExitSharing(ctxt->sharingRes, ctxt->value); + LOG_DEBUG("exit sharing result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * changePrivilege(sharingRes: string, participants: Array, + * callback: AsyncCallback>>>): void; + * + * [Promise] + * changePrivilege(sharingRes: string, participants: Array): Promise>>>; + */ +napi_value ChangePrivilege(napi_env env, napi_callback_info info) +{ + struct ChangePrivilegeContext : public ContextBase { + std::string sharingRes; + std::vector participants; + Result>> value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + status = JSUtils::Convert2Value(env, argv[1], ctxt->participants); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of participants must be Array."); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->ChangePrivilege(ctxt->sharingRes, ctxt->participants, ctxt->value); + LOG_DEBUG("change privilege result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * queryParticipants(sharingRes: string, callback: AsyncCallback>>): void; + * + * [Promise] + * queryParticipants(sharingRes: string): Promise>>; + */ +napi_value QueryParticipants(napi_env env, napi_callback_info info) +{ + struct QueryParticipantsContext : public ContextBase { + std::string sharingRes; + Result> value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->QueryParticipants(ctxt->sharingRes, ctxt->value); + LOG_DEBUG("query participants result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * queryParticipantsByInvitation(invitationCode: string, + * callback: AsyncCallback>>): void; + * + * [Promise] + * queryParticipantsByInvitation(invitationCode: string): Promise>>; + */ +napi_value QueryParticipantsByInvitation(napi_env env, napi_callback_info info) +{ + struct QueryByInvitationContext : public ContextBase { + std::string invitationCode; + Result> value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->invitationCode); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of invitationCode must be string."); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->QueryParticipantsByInvitation(ctxt->invitationCode, ctxt->value); + LOG_DEBUG("query participants by invitation result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * confirmInvitation(invitationCode: string, status: Status, callback: AsyncCallback>): void; + * + * [Promise] + * confirmInvitation(invitationCode: string, status: Status): Promise>; + */ +napi_value ConfirmInvitation(napi_env env, napi_callback_info info) +{ + struct ConfirmInvitationContext : public ContextBase { + std::string invitationCode; + SharingStatus sharingStatus; + Result value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->invitationCode); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of invitationCode must be string."); + int32_t sharingStatus; + status = JSUtils::Convert2Value(env, argv[1], sharingStatus); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK && + sharingStatus >= SharingStatus::STATUS_UNKNOWN && sharingStatus <= SharingStatus::STATUS_SUSPENDED, + Status::INVALID_ARGUMENT, "The type of status must be Status."); + ctxt->sharingStatus = static_cast(sharingStatus); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->ConfirmInvitation(ctxt->invitationCode, ctxt->sharingStatus, ctxt->value); + LOG_DEBUG("confirm invitation result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +/* + * [JS API Prototype] + * [AsyncCallback] + * changeConfirmation(sharingRes: string, status: Status, callback: AsyncCallback>): void; + * + * [Promise] + * changeConfirmation(sharingRes: string, status: Status): Promise>; + */ +napi_value ChangeConfirmation(napi_env env, napi_callback_info info) +{ + struct ChangeConfirmationContext : public ContextBase { + std::string sharingRes; + SharingStatus sharingStatus; + Result value; + }; + auto ctxt = std::make_shared(); + ctxt->GetCbInfo(env, info, [env, ctxt](size_t argc, napi_value *argv) { + ASSERT_BUSINESS_ERR(ctxt, argc >= 2, Status::INVALID_ARGUMENT, "The number of parameters is incorrect."); + int status = JSUtils::Convert2Value(env, argv[0], ctxt->sharingRes); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK, + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + int32_t sharingStatus; + status = JSUtils::Convert2Value(env, argv[1], sharingStatus); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK && + sharingStatus >= SharingStatus::STATUS_UNKNOWN && sharingStatus <= SharingStatus::STATUS_SUSPENDED, + Status::INVALID_ARGUMENT, "The type of status must be Status."); + ctxt->sharingStatus = static_cast(sharingStatus); + }); + auto execute = [env, ctxt]() { + auto [status, proxy] = CloudManager::GetInstance().GetCloudService(); + if (proxy == nullptr) { + if (status != CloudService::SERVER_UNAVAILABLE) { + status = CloudService::NOT_SUPPORT; + } + ctxt->status = (GenerateNapiError(status, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + return; + } + int32_t result = proxy->ChangeConfirmation(ctxt->sharingRes, ctxt->sharingStatus, ctxt->value); + LOG_DEBUG("change confirmation result %{public}d", result); + ctxt->status = (GenerateNapiError(result, ctxt->jsCode, ctxt->error) == Status::SUCCESS) ? + napi_ok : napi_generic_failure; + }; + auto output = [env, ctxt](napi_value& result) { + result = JSUtils::Convert2JSValue(env, ctxt->value); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + +napi_value InitCloudDataShare(napi_env env, napi_value exports) +{ + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("allocResourceAndShare", AllocResourceAndShare), + DECLARE_NAPI_FUNCTION("share", Share), + DECLARE_NAPI_FUNCTION("unshare", Unshare), + DECLARE_NAPI_FUNCTION("exit", Exit), + DECLARE_NAPI_FUNCTION("changePrivilege", ChangePrivilege), + DECLARE_NAPI_FUNCTION("queryParticipants", QueryParticipants), + DECLARE_NAPI_FUNCTION("queryParticipantsByInvitation", QueryParticipantsByInvitation), + DECLARE_NAPI_FUNCTION("confirmInvitation", ConfirmInvitation), + DECLARE_NAPI_FUNCTION("changeConfirmation", ChangeConfirmation), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties)); + return exports; +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp new file mode 100644 index 00000000..e01d9d4b --- /dev/null +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_cloud_utils.h" +#include "logger.h" + +#define NAPI_CALL_RETURN_ERR(call, ret) \ + do { \ + if ((call) != napi_ok) { \ + return ret; \ + } \ + } while (0) + +namespace OHOS::AppDataMgrJsKit { +namespace JSUtils { +using namespace OHOS::Rdb; +template<> +int32_t Convert2Value(napi_env env, napi_value input, Participant &output) +{ + napi_valuetype type = napi_undefined; + napi_status status = napi_typeof(env, input, &type); + if (status != napi_ok || type != napi_object) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + NAPI_CALL_RETURN_ERR(GET_PROPERTY(env, input, output, identity), napi_invalid_arg); + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "role", output.role), napi_invalid_arg); + if (output.role < CloudData::Role::ROLE_INVITER || output.role > CloudData::Role::ROLE_INVITEE) { + return napi_invalid_arg; + } + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "status", output.status), napi_invalid_arg); + if (output.status < CloudData::SharingStatus::STATUS_UNKNOWN || + output.status > CloudData::SharingStatus::STATUS_SUSPENDED) { + return napi_invalid_arg; + } + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "privilege", output.privilege), napi_invalid_arg); + return napi_ok; +} + +template<> +int32_t Convert2Value(napi_env env, napi_value input, Privilege &output) +{ + napi_valuetype type = napi_undefined; + napi_status status = napi_typeof(env, input, &type); + if (status != napi_ok || type != napi_object) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "writeable", output.writeable), napi_invalid_arg); + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "readable", output.readable), napi_invalid_arg); + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "creatable", output.creatable), napi_invalid_arg); + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "deletable", output.deletable), napi_invalid_arg); + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "shareable", output.shareable), napi_invalid_arg); + return napi_ok; +} + +template<> +napi_value Convert2JSValue(napi_env env, const Participant &value) +{ + napi_value jsValue = nullptr; + napi_status status = napi_create_object(env, &jsValue); + if (status != napi_ok) { + return nullptr; + } + napi_value identity = Convert2JSValue(env, value.identity); + napi_value role = Convert2JSValue(env, value.role); + napi_value sharingStatus = Convert2JSValue(env, value.status); + napi_value privilege = Convert2JSValue(env, value.privilege); + if (privilege == nullptr) { + return nullptr; + } + napi_set_named_property(env, jsValue, "identity", identity); + napi_set_named_property(env, jsValue, "role", role); + napi_set_named_property(env, jsValue, "status", sharingStatus); + napi_set_named_property(env, jsValue, "privilege", privilege); + return jsValue; +} + +template<> +napi_value Convert2JSValue(napi_env env, const Privilege &value) +{ + napi_value jsValue = nullptr; + napi_status status = napi_create_object(env, &jsValue); + if (status != napi_ok) { + return nullptr; + } + napi_value writeable = Convert2JSValue(env, value.writeable); + napi_value readable = Convert2JSValue(env, value.readable); + napi_value creatable = Convert2JSValue(env, value.creatable); + napi_value deletable = Convert2JSValue(env, value.deletable); + napi_value shareable = Convert2JSValue(env, value.shareable); + napi_set_named_property(env, jsValue, "writeable", writeable); + napi_set_named_property(env, jsValue, "readable", readable); + napi_set_named_property(env, jsValue, "creatable", creatable); + napi_set_named_property(env, jsValue, "deletable", deletable); + napi_set_named_property(env, jsValue, "shareable", shareable); + return jsValue; +} +}; // namespace JSUtils +} // namespace OHOS::AppDataMgrJsKit \ No newline at end of file diff --git a/relational_store/frameworks/js/napi/cloud_data/src/js_const_properties.cpp b/relational_store/frameworks/js/napi/cloud_data/src/js_const_properties.cpp index 850cb693..1247d835 100644 --- a/relational_store/frameworks/js/napi/cloud_data/src/js_const_properties.cpp +++ b/relational_store/frameworks/js/napi/cloud_data/src/js_const_properties.cpp @@ -16,6 +16,7 @@ #include "js_const_properties.h" #include "cloud_service.h" +#include "cloud_types.h" #include "napi_queue.h" using namespace OHOS::Rdb; @@ -42,11 +43,35 @@ static napi_value ExportAction(napi_env env) return action; } +static napi_value ExportRole(napi_env env) +{ + napi_value role = nullptr; + napi_create_object(env, &role); + SetNamedProperty(env, role, "ROLE_INVITER", Role::ROLE_INVITER); + SetNamedProperty(env, role, "ROLE_INVITEE", Role::ROLE_INVITEE); + napi_object_freeze(env, role); + return role; +} + +static napi_value ExportShareStatus(napi_env env) +{ + napi_value status = nullptr; + napi_create_object(env, &status); + SetNamedProperty(env, status, "STATUS_UNKNOWN", SharingStatus::STATUS_UNKNOWN); + SetNamedProperty(env, status, "STATUS_ACCEPTED", SharingStatus::STATUS_ACCEPTED); + SetNamedProperty(env, status, "STATUS_REJECTED", SharingStatus::STATUS_REJECTED); + SetNamedProperty(env, status, "STATUS_SUSPENDED", SharingStatus::STATUS_SUSPENDED); + napi_object_freeze(env, status); + return status; +} + napi_status InitConstProperties(napi_env env, napi_value exports) { const napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("Action", ExportAction(env)), DECLARE_NAPI_PROPERTY("ClearAction", ExportAction(env)), + DECLARE_NAPI_PROPERTY("Role", ExportRole(env)), + DECLARE_NAPI_PROPERTY("Status", ExportShareStatus(env)), }; size_t count = sizeof(properties) / sizeof(properties[0]); diff --git a/relational_store/frameworks/js/napi/common/include/js_utils.h b/relational_store/frameworks/js/napi/common/include/js_utils.h index 611c4378..1b6eae0f 100644 --- a/relational_store/frameworks/js/napi/common/include/js_utils.h +++ b/relational_store/frameworks/js/napi/common/include/js_utils.h @@ -27,6 +27,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "itypes_util.h" namespace OHOS { namespace AppDataMgrJsKit { @@ -48,12 +49,18 @@ struct JsFeatureSpace { napi_set_named_property((env), (object), #member, Convert2JSValue((env), (value).member)) #endif +#ifndef ADD_JS_CONST_PROPERTY +#define ADD_JS_CONST_PROPERTY(env, object, name, value) \ + napi_set_named_property((env), (object), (name), Convert2JSValue((env), (value))) +#endif + #ifndef GET_PROPERTY #define GET_PROPERTY(env, object, value, member) \ Convert2Value((env), GetNamedProperty((env), (object), #member), (value).member) #endif napi_value GetNamedProperty(napi_env env, napi_value object, const char *name); +std::pair GetOptionalNamedProperty(napi_env env, napi_value input, const char *name); int32_t Convert2ValueExt(napi_env env, napi_value jsValue, uint32_t &output); int32_t Convert2ValueExt(napi_env env, napi_value jsValue, int32_t &output); @@ -62,6 +69,7 @@ int32_t Convert2ValueExt(napi_env env, napi_value jsValue, int64_t &output); int32_t Convert2Value(napi_env env, napi_value jsValue, bool &output); int32_t Convert2Value(napi_env env, napi_value jsValue, double &output); int32_t Convert2Value(napi_env env, napi_value jsValue, int64_t &output); +int32_t Convert2Value(napi_env env, napi_value jsValue, int32_t &output); // TODO int32_t Convert2Value(napi_env env, napi_value jsValue, std::string &output); int32_t Convert2Value(napi_env env, napi_value jsValue, std::vector &output); int32_t Convert2Value(napi_env env, napi_value jsValue, std::monostate &value); @@ -116,9 +124,25 @@ napi_value Convert2JSValue(napi_env env, const std::map &value); template napi_value Convert2JSValue(napi_env env, const std::variant &value); +template +napi_value Convert2JSValue(napi_env env, const Result &value); + template std::string ToString(const T &key); +template +int32_t GetOptionalValue(napi_env env, napi_value in, const char *name, T& out) +{ + auto [status, value] = GetOptionalNamedProperty(env, in, name); + if (status != napi_ok) { + return status; + } + if (value == nullptr) { + return napi_ok; + } + return JSUtils::Convert2Value(env, value, out); +} + template std::enable_if_t, std::string> ConvertMapKey(const K &key) { @@ -213,6 +237,29 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) return jsValue; } +template +napi_value JSUtils::Convert2JSValue(napi_env env, const Result &value) +{ + napi_value jsValue; + napi_status status = napi_create_object(env, &jsValue); + if (status != napi_ok) { + return nullptr; + } + napi_value errCode = Convert2JSValue(env, value.errCode); + napi_value result = nullptr; + if (std::is_null_pointer::value) { + napi_get_undefined(env, &result); + } else { + result = Convert2JSValue(env, value.value); + } + if (result == nullptr) { + return nullptr; + } + napi_set_named_property(env, jsValue, "errCode", errCode); + napi_set_named_property(env, jsValue, "value", result); + return jsValue; +} + template int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant &value) { diff --git a/relational_store/frameworks/js/napi/common/src/js_utils.cpp b/relational_store/frameworks/js/napi/common/src/js_utils.cpp index eb470eb7..419572e0 100644 --- a/relational_store/frameworks/js/napi/common/src/js_utils.cpp +++ b/relational_store/frameworks/js/napi/common/src/js_utils.cpp @@ -60,6 +60,28 @@ napi_value JSUtils::GetNamedProperty(napi_env env, napi_value object, const char return jsItem; } + +std::pair JSUtils::GetOptionalNamedProperty(napi_env env, napi_value input, const char *name) +{ + bool hasProp = false; + napi_status status = napi_has_named_property(env, input, name, &hasProp); + if (status != napi_ok) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (!hasProp) { + return std::make_pair(napi_ok, nullptr);; + } + napi_value inner = nullptr; + status = napi_get_named_property(env, input, name, &inner); + if (status != napi_ok || inner == nullptr) { + return std::make_pair(napi_generic_failure, nullptr); + } + if (JSUtils::IsNull(env, inner)) { + return std::make_pair(napi_ok, nullptr); + } + return std::make_pair(napi_ok, inner); +} + std::string JSUtils::Convert2String(napi_env env, napi_value jsStr) { std::string value = ""; // TD: need to check everywhere in use whether empty is work well. @@ -101,6 +123,23 @@ int32_t JSUtils::Convert2ValueExt(napi_env env, napi_value jsValue, int32_t &out return status; } +int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int32_t &output) +{ + napi_valuetype type = napi_undefined; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + status = napi_get_value_int32(env, jsValue, &output); + if (status != napi_ok) { + LOG_DEBUG("napi_get_value_int32 failed, status = %{public}d", status); + return status; + } + return status; +} + int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, bool &output) { napi_valuetype type = napi_undefined; diff --git a/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h b/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h index 58aa6445..d9bb24e9 100644 --- a/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h +++ b/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h @@ -31,6 +31,21 @@ public: int32_t Clean(const std::string &id, const std::map &actions) override; int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; + int32_t Share(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t Unshare(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t ExitSharing(const std::string &sharingRes, Result &result) override; + int32_t ChangePrivilege(const std::string &sharingRes, const std::vector &participants, + Result>> &result) override; + int32_t QueryParticipants(const std::string &sharingRes, Result> &result) override; + int32_t QueryParticipantsByInvitation(const std::string &invitationCode, + Result> &result) override; + int32_t ConfirmInvitation(const std::string &invitationCode, SharingStatus sharingStatus, + Result &result) override; + int32_t ChangeConfirmation(const std::string &sharingRes, + SharingStatus sharingStatus, Result &result) override; + private: sptr remote_; }; diff --git a/relational_store/frameworks/native/cloud_data/include/cloud_types_util.h b/relational_store/frameworks/native/cloud_data/include/cloud_types_util.h new file mode 100644 index 00000000..13b4764c --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/include/cloud_types_util.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_UTIL_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_UTIL_H +#include "itypes_util.h" +#include "cloud_types.h" + +namespace OHOS::ITypesUtil { +using OHOS::CloudData::Participant; +using OHOS::CloudData::Privilege; +using OHOS::CloudData::Role; +using OHOS::CloudData::SharingStatus; +using OHOS::CloudData::SharingCode; +template<> +bool Marshalling(const Participant &input, MessageParcel &data); + +template<> +bool Unmarshalling(Participant &output, MessageParcel &data); + +template<> +bool Marshalling(const Privilege &input, MessageParcel &data); + +template<> +bool Unmarshalling(Privilege &output, MessageParcel &data); + +template<> +bool Marshalling(const Role &input, MessageParcel &data); + +template<> +bool Unmarshalling(Role &output, MessageParcel &data); + +template<> +bool Marshalling(const SharingStatus &input, MessageParcel &data); + +template<> +bool Unmarshalling(SharingStatus &output, MessageParcel &data); + +template<> +bool Marshalling(const SharingCode &input, MessageParcel &data); + +template<> +bool Unmarshalling(SharingCode &output, MessageParcel &data); +} // namespace OHOS::ITypesUtil +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_UTIL_H diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp index 1755597c..e7941529 100644 --- a/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp +++ b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp @@ -101,4 +101,101 @@ int32_t CloudServiceProxy::NotifyDataChange(const std::string &id, const std::st } return static_cast(status); } + +int32_t CloudServiceProxy::Share(const std::string &sharingRes, const std::vector &participants, + Result>> &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_SHARE, reply, sharingRes, participants); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu", + status, sharingRes.c_str(), participants.size()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::Unshare(const std::string &sharingRes, const std::vector &participants, + Result>> &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_UNSHARE, reply, sharingRes, participants); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu", + status, sharingRes.c_str(), participants.size()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::ExitSharing(const std::string &sharingRes, Result &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_EXIT_SHARING, reply, sharingRes); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::ChangePrivilege(const std::string &sharingRes, const std::vector &participants, + Result>> &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CHANGE_PRIVILEGE, reply, sharingRes, participants); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu", + status, sharingRes.c_str(), participants.size()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::QueryParticipants(const std::string &sharingRes, Result> &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_QUERY_PARTICIPANTS, reply, 02); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::QueryParticipantsByInvitation( + const std::string &invitationCode, Result> &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_QUERY_PARTICIPANTS_BY_INVITATION, reply, invitationCode); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x invitationCode:%{public}.6s", status, invitationCode.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::ConfirmInvitation(const std::string &invitationCode, + SharingStatus sharingStatus, Result &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CONFIRM_INVITATION, reply, invitationCode, sharingStatus); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x invitationCode:%{public}.6s", status, invitationCode.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::ChangeConfirmation(const std::string &sharingRes, + SharingStatus sharingStatus, Result &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CHANGE_CONFIRMATION, reply, sharingRes, sharingStatus); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x invitationCode:%{public}.6s", status, sharingRes.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_types_util.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_types_util.cpp new file mode 100644 index 00000000..57a50c64 --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/src/cloud_types_util.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud_types_util.h" + +namespace OHOS::ITypesUtil { +template<> +bool Marshalling(const Participant &input, MessageParcel &data) +{ + return ITypesUtil::Marshal(data, input.identity, input.role, input.status, input.privilege); +} + +template<> +bool Unmarshalling(Participant &output, MessageParcel &data) +{ + return ITypesUtil::Unmarshal(data, output.identity, output.role, output.status, output.privilege); +} + +template<> +bool Marshalling(const Privilege &input, MessageParcel &data) +{ + return ITypesUtil::Marshal(data, input.writeable, input.readable, + input.creatable, input.deletable, input.shareable); +} + +template<> +bool Unmarshalling(Privilege &output, MessageParcel &data) +{ + return ITypesUtil::Unmarshal(data, output.writeable, output.readable, + output.creatable, output.deletable, output.shareable); +} + +template<> +bool Marshalling(const Role &input, MessageParcel &data) +{ + return data.WriteInt32(static_cast(input)); +} + +template<> +bool Unmarshalling(Role &output, MessageParcel &data) +{ + int32_t result; + if (!data.ReadInt32(result) || result < Role::ROLE_INVITER || result > Role::ROLE_INVITEE) { + return false; + } + output = static_cast(result); + return true; +} + +template<> +bool Marshalling(const SharingStatus &input, MessageParcel &data) +{ + return data.WriteInt32(static_cast(input)); +} + +template<> +bool Unmarshalling(SharingStatus &output, MessageParcel &data) +{ + int32_t result; + if (!data.ReadInt32(result) || result < SharingStatus::STATUS_UNKNOWN || + result > SharingStatus::STATUS_SUSPENDED) { + return false; + } + output = static_cast(result); + return true; +} + +template<> +bool Marshalling(const SharingCode &input, MessageParcel &data) +{ + return data.WriteInt32(static_cast(input)); +} + +template<> +bool Unmarshalling(SharingCode &output, MessageParcel &data) +{ + int32_t result; + if (!data.ReadInt32(result) || result < SharingCode::SHARING_OK || result >= SharingCode::SHARING_BUTT) { + return false; + } + output = static_cast(result); + return true; +} +} // namespace OHOS::ITypesUtil \ No newline at end of file diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h index 84e854af..cbfad21d 100644 --- a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h @@ -18,6 +18,9 @@ #include #include #include +#include "rdb_types.h" +#include "cloud_types.h" +#include "itypes_util.h" namespace OHOS::CloudData { class CloudService { public: @@ -28,6 +31,14 @@ public: TRANS_CHANGE_APP_SWITCH, TRANS_CLEAN, TRANS_NOTIFY_DATA_CHANGE, + TRANS_SHARE, + TRANS_UNSHARE, + TRANS_EXIT_SHARING, + TRANS_CHANGE_PRIVILEGE, + TRANS_QUERY_PARTICIPANTS, + TRANS_QUERY_PARTICIPANTS_BY_INVITATION, + TRANS_CONFIRM_INVITATION, + TRANS_CHANGE_CONFIRMATION, TRANS_BUTT, }; enum Action : int32_t { @@ -63,6 +74,23 @@ public: virtual int32_t Clean(const std::string &id, const std::map &actions) = 0; virtual int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) = 0; +// virtual int32_t AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, +// const Participant &participant, const std::vector &columns) = 0; // TODO + virtual int32_t Share(const std::string &sharingRes, const std::vector &participants, + Result>> &result) = 0; + virtual int32_t Unshare(const std::string &sharingRes, const std::vector &participants, + Result>> &result) = 0; + virtual int32_t ExitSharing(const std::string &sharingRes, Result &result) = 0; + virtual int32_t ChangePrivilege(const std::string &sharingRes, const std::vector &participants, + Result>> &result) = 0; + virtual int32_t QueryParticipants(const std::string &sharingRes, Result> &result) = 0; + virtual int32_t QueryParticipantsByInvitation(const std::string &invitationCode, + Result> &result) = 0; + virtual int32_t ConfirmInvitation(const std::string &invitationCode, SharingStatus sharingStatus, + Result &result) = 0; + virtual int32_t ChangeConfirmation(const std::string &sharingRes, + SharingStatus sharingStatus, Result &result) = 0; + inline static constexpr const char *SERVICE_NAME = "cloud"; }; } // namespace OHOS::CloudData diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_types.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_types.h new file mode 100644 index 00000000..4f9e86d2 --- /dev/null +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_types.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_H + +#include + +namespace OHOS::CloudData { +enum Role : int32_t { + ROLE_INVITER = 0, + ROLE_INVITEE +}; + +enum SharingStatus : int32_t { + STATUS_UNKNOWN = 0, + STATUS_ACCEPTED, + STATUS_REJECTED, + STATUS_SUSPENDED +}; + +struct Privilege { + bool writeable = false; + bool readable = true; + bool creatable = false; + bool deletable = false; + bool shareable = false; +}; + +struct Participant { + std::string identity; + int32_t role = Role::ROLE_INVITER; + int32_t status = SharingStatus::STATUS_ACCEPTED; + Privilege privilege; +}; + +enum SharingCode : int32_t { + SHARING_OK = 0, + SHARING_ERROR, + SHARING_NETWORK_ERROR, + SHARING_UNKNOWN_ERROR, + SHARING_BUTT, +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_TYPES_H -- Gitee From b82d61edbc61926e319da6659f9ebef15f37b951 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 6 Nov 2023 10:13:19 +0800 Subject: [PATCH 2/4] update Signed-off-by: zuojiangjiang --- .../frameworks/js/napi/common/include/js_utils.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/relational_store/frameworks/js/napi/common/include/js_utils.h b/relational_store/frameworks/js/napi/common/include/js_utils.h index 1b6eae0f..25dca0c4 100644 --- a/relational_store/frameworks/js/napi/common/include/js_utils.h +++ b/relational_store/frameworks/js/napi/common/include/js_utils.h @@ -49,11 +49,6 @@ struct JsFeatureSpace { napi_set_named_property((env), (object), #member, Convert2JSValue((env), (value).member)) #endif -#ifndef ADD_JS_CONST_PROPERTY -#define ADD_JS_CONST_PROPERTY(env, object, name, value) \ - napi_set_named_property((env), (object), (name), Convert2JSValue((env), (value))) -#endif - #ifndef GET_PROPERTY #define GET_PROPERTY(env, object, value, member) \ Convert2Value((env), GetNamedProperty((env), (object), #member), (value).member) -- Gitee From 2ade55b0667af7c75d703266ad1545fc573fe0a6 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 6 Nov 2023 11:30:49 +0800 Subject: [PATCH 3/4] update Signed-off-by: zuojiangjiang --- .../distributeddataservice/framework/CMakeLists.txt | 1 + .../frameworks/js/napi/cloud_data/include/napi_queue.h | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt b/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt index ff47e2cd..8819ada0 100644 --- a/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt +++ b/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt @@ -24,6 +24,7 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/utils svcFwkSrc) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../adapter/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../utils_native/base/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../utils_native/safwk/native/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../relational_store/interfaces/inner_api/cloud_data/include) include(${MOCK_DIR}/include/CMakeLists.txt OPTIONAL) include(${KV_STORE_DIR}/interfaces/CMakeLists.txt OPTIONAL) diff --git a/relational_store/frameworks/js/napi/cloud_data/include/napi_queue.h b/relational_store/frameworks/js/napi/cloud_data/include/napi_queue.h index b1f8b045..46ed90d7 100644 --- a/relational_store/frameworks/js/napi/cloud_data/include/napi_queue.h +++ b/relational_store/frameworks/js/napi/cloud_data/include/napi_queue.h @@ -61,16 +61,19 @@ private: }; /* check condition related to argc/argv, return and logging. */ -#define ASSERT_ARGS(ctxt, condition, message) \ +#define ASSERT_VALUE(ctxt, condition, errCode, message) \ do { \ if (!(condition)) { \ - (ctxt)->status = napi_invalid_arg; \ + (ctxt)->status = errCode; \ (ctxt)->error = std::string(message); \ LOG_ERROR("test (" #condition ") failed: " message); \ return; \ } \ } while (0) +#define ASSERT_ARGS(ctxt, condition, message) \ + ASSERT_VALUE(ctxt, condition, napi_invalid_arg, message) + #define ASSERT_STATUS(ctxt, message) \ do { \ if ((ctxt)->status != napi_ok) { \ -- Gitee From 8e704e47aa33e5b9be2577770ad1a0191e20ae4b Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 6 Nov 2023 14:16:28 +0800 Subject: [PATCH 4/4] update Signed-off-by: zuojiangjiang --- .../framework/cloud/cloud_server.cpp | 48 +++++++++++ .../framework/cloud/sharing_center.cpp | 80 ------------------- .../framework/include/cloud/cloud_server.h | 21 +++++ .../framework/include/cloud/sharing_center.h | 53 ------------ .../service/cloud/cloud_service_impl.cpp | 17 ++-- 5 files changed, 77 insertions(+), 142 deletions(-) delete mode 100644 datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp delete mode 100644 datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp index 175cda1f..9d9c4a7d 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp @@ -67,4 +67,52 @@ void CloudServer::Clean(int32_t userId) void CloudServer::ReleaseUserInfo(int32_t userId) { } + +int32_t CloudServer::Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t CloudServer::Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t CloudServer::ExitSharing(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result &result) +{ + return 0; +} + +int32_t CloudServer::ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result) +{ + return 0; +} + +int32_t CloudServer::QueryParticipants(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result> &result) +{ + return 0; +} + +int32_t CloudServer::QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, Result> &result) +{ + return 0; +} + +int32_t CloudServer::ConfirmInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, SharingStatus sharingStatus, Result &result) +{ + return 0; +} + +int32_t CloudServer::ChangeConfirmation(int32_t userId, const std::string &bundleName, + const std::string &sharingRes,SharingStatus sharingStatus, Result &result) +{ + return 0; +} } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp deleted file mode 100644 index be2eb7cb..00000000 --- a/datamgr_service/services/distributeddataservice/framework/cloud/sharing_center.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "cloud/sharing_center.h" -namespace OHOS::DistributedData { -SharingCenter *SharingCenter::instance_ = nullptr; -SharingCenter *SharingCenter::GetInstance() -{ - return instance_; -} - -bool SharingCenter::RegisterSharingInstance(SharingCenter *instance) -{ - if (instance_ != nullptr) { - return false; - } - instance_ = instance; - return true; -} - -int32_t SharingCenter::Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result) -{ - return 0; -} - -int32_t SharingCenter::Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result) -{ - return 0; -} - -int32_t SharingCenter::ExitSharing(int32_t userId, const std::string &bundleName, - const std::string &sharingRes, Result &result) -{ - return 0; -} - -int32_t SharingCenter::ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result) -{ - return 0; -} - -int32_t SharingCenter::QueryParticipants(int32_t userId, const std::string &bundleName, - const std::string &sharingRes, Result> &result) -{ - return 0; -} - -int32_t SharingCenter::QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, - const std::string &invitationCode, Result> &result) -{ - return 0; -} - -int32_t SharingCenter::ConfirmInvitation(int32_t userId, const std::string &bundleName, - const std::string &invitationCode, SharingStatus sharingStatus, Result &result) -{ - return 0; -} - -int32_t SharingCenter::ChangeConfirmation(int32_t userId, const std::string &bundleName, - const std::string &sharingRes,SharingStatus sharingStatus, Result &result) -{ - return 0; -} -} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_server.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_server.h index 90b78ad8..a068eb5a 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_server.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_server.h @@ -19,11 +19,15 @@ #include "cloud/cloud_db.h" #include "cloud/cloud_info.h" #include "cloud/schema_meta.h" +#include "itypes_util.h" +#include "cloud_types.h" #include "visibility.h" namespace OHOS::DistributedData { class API_EXPORT CloudServer { public: using Database = SchemaMeta::Database; + using Participant = OHOS::CloudData::Participant; + using SharingStatus = OHOS::CloudData::SharingStatus; API_EXPORT static CloudServer *GetInstance(); API_EXPORT static bool RegisterCloudInstance(CloudServer *instance); @@ -36,6 +40,23 @@ public: virtual void Clean(int32_t userId); virtual void ReleaseUserInfo(int32_t userId); + virtual int32_t Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t ExitSharing(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result &result); + virtual int32_t ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, + const std::vector &participants, Result>> &result); + virtual int32_t QueryParticipants(int32_t userId, const std::string &bundleName, + const std::string &sharingRes, Result> &result); + virtual int32_t QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, Result> &result); + virtual int32_t ConfirmInvitation(int32_t userId, const std::string &bundleName, + const std::string &invitationCode, SharingStatus sharingStatus, Result &result); + virtual int32_t ChangeConfirmation(int32_t userId, const std::string &bundleName, + const std::string &sharingRes,SharingStatus sharingStatus, Result &result); + private: static CloudServer *instance_; }; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h deleted file mode 100644 index 9d6264c2..00000000 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/sharing_center.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H - -//#include "store/general_value.h" -#include "visibility.h" -#include "itypes_util.h" -#include "cloud_types.h" - -namespace OHOS::DistributedData { -class API_EXPORT SharingCenter { -public: - using Participant = OHOS::CloudData::Participant; - using SharingStatus = OHOS::CloudData::SharingStatus; - API_EXPORT static SharingCenter *GetInstance(); - API_EXPORT static bool RegisterSharingInstance(SharingCenter *instance); - - virtual int32_t Share(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result); - virtual int32_t Unshare(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result); - virtual int32_t ExitSharing(int32_t userId, const std::string &bundleName, - const std::string &sharingRes, Result &result); - virtual int32_t ChangePrivilege(int32_t userId, const std::string &bundleName, const std::string &sharingRes, - const std::vector &participants, Result>> &result); - virtual int32_t QueryParticipants(int32_t userId, const std::string &bundleName, - const std::string &sharingRes, Result> &result); - virtual int32_t QueryParticipantsByInvitation(int32_t userId, const std::string &bundleName, - const std::string &invitationCode, Result> &result); - virtual int32_t ConfirmInvitation(int32_t userId, const std::string &bundleName, - const std::string &invitationCode, SharingStatus sharingStatus, Result &result); - virtual int32_t ChangeConfirmation(int32_t userId, const std::string &bundleName, - const std::string &sharingRes,SharingStatus sharingStatus, Result &result); - -private: - static SharingCenter *instance_; -}; -} // namespace OHOS::DistributedData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SHARING_CENTER_H diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index e76c59fc..a5fe5fcf 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -20,7 +20,6 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" #include "cloud/cloud_server.h" -#include "cloud/sharing_center.h" #include "communicator/device_manager_adapter.h" #include "eventcenter/event_center.h" #include "ipc_skeleton.h" @@ -604,7 +603,7 @@ int32_t CloudServiceImpl::Share(const std::string &sharingRes, const std::vector ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); return E_ERROR; } - auto instance = SharingCenter::GetInstance(); + auto instance = CloudServer::GetInstance(); if (instance == nullptr) { return E_NOT_SUPPORT; } @@ -621,7 +620,7 @@ int32_t CloudServiceImpl::Unshare(const std::string &sharingRes, const std::vect ZLOGE("token:0x%{public}x, result:%{public}d", tokenId, status); return E_ERROR; } - auto instance = SharingCenter::GetInstance(); + auto instance = CloudServer::GetInstance(); if (instance == nullptr) { return E_NOT_SUPPORT; } @@ -637,7 +636,7 @@ int32_t CloudServiceImpl::ExitSharing(const std::string &sharingRes, Result