diff --git a/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt b/datamgr_service/services/distributeddataservice/framework/CMakeLists.txt index ff47e2cd74cf227f059e89950973d64d6cd3b005..8819ada0191f654bdadc86f6ab5d07edf30bc116 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/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_server.cpp index 175cda1fb6e2bd1cd79358eb19ca9bc78c5f7ca2..9d9c4a7d6c64b89fc936878d530e748bda8bb3c3 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/include/cloud/cloud_server.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_server.h index 90b78ad8fb40d0658f28969a8516df26e6c3d888..a068eb5a25a1770ceca07a7fa28937da3e97e3d9 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/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 754d0e2f3654fd75a972b364167cf19800869ef8..a5fe5fcf1eec6e14607de1e3ee21bfbde3fe9a0e 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -37,6 +37,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 +592,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 = CloudServer::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 = CloudServer::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 = CloudServer::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 = CloudServer::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 = CloudServer::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 = CloudServer::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 = CloudServer::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 = CloudServer::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 c94deae5c393036a397e4d6f5285462a4407722b..6827ac87337ebfbf7c3c1e396bac0d28f4cd230f 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 34a65dc155c4a6fbd3098652813d7dad75e2fd3d..655e15055176b882a72204ac42e5f7ecc9155853 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 4175015d2731ca380b02874ab4af692502cd22d9..347db56f461da5359983c1dffbaafd0a358f68c0 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 f9c2189016c4e675f4abdc0f04a1296743855f64..1e1ca51bde3d06b8f16c65dd5b013f4e6543e3ee 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 0000000000000000000000000000000000000000..7b36f18519be9df816c0c38bff705999e1cae68c --- /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 0000000000000000000000000000000000000000..e0b6bfa5c597772a627d4696a136b4ae4c276c7d --- /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/include/napi_queue.h b/relational_store/frameworks/js/napi/cloud_data/include/napi_queue.h index b1f8b0459e6c3514da92e0c7a2ed960cc64a5670..46ed90d7373ecab0c32ff15887ca55e38a7db240 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) { \ 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 8dd249b99648f8bf0980ce34d3e83791510530b8..417b0bb53cfdbf8aff31d364bec512b7ce5c7002 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 0000000000000000000000000000000000000000..3ef7737d36b4a1315a641258715050eda87f30ff --- /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 0000000000000000000000000000000000000000..e01d9d4b94660178f418fd7c087f6399f2ecc7ae --- /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 850cb693f6ee01deca7220255f665c9420b9b761..1247d8350c60c884ab2a9d2adb8cf48b8daf2baf 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 611c43786b783ba4387a6cf5b62b5eb87a5554c3..25dca0c418f0ce3756106d41caedf62c4864c24b 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 { @@ -54,6 +55,7 @@ struct JsFeatureSpace { #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 +64,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 +119,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 +232,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 eb470eb7e09c359bd9cf7535f8055a99b9b8c944..419572e07b063887288a771df9506c83f65ed40d 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 58aa6445ad22f8141cc4af17853e67c60e0c5024..d9bb24e96bcdc24f7caa47c365a463cdee67f8a0 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 0000000000000000000000000000000000000000..13b4764cc3d88122802a11e15254aa03e4e4a931 --- /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 1755597cd875eb62cf3ee7333dc114a3323ffd3c..e7941529b984e1cca8da2d4da44f22f1a567516f 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 0000000000000000000000000000000000000000..57a50c64521f134e4d0ced29ba6f19fb49081574 --- /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 84e854af67af94fede1116fa7a8ffdb86c7a1313..cbfad21d47adde47d076fe19d1f346d4fee84725 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 0000000000000000000000000000000000000000..4f9e86d26e69e4333a4a70f7e86092cf381d56e1 --- /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