From 81bea35ab921d61e7ba7aaaf34e67a3cfc040dab Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 27 Nov 2023 02:39:55 +0800 Subject: [PATCH] add cloud shared interface Signed-off-by: zuojiangjiang --- CODEOWNERS | 2 +- frameworks/js/napi/cloud_data/BUILD.gn | 1 - .../napi/cloud_data/include/js_cloud_utils.h | 9 + .../napi/cloud_data/include/js_config_util.h | 29 -- .../js/napi/cloud_data/src/js_cloud_share.cpp | 388 +++++++++++++++++- .../js/napi/cloud_data/src/js_cloud_utils.cpp | 46 ++- .../js/napi/cloud_data/src/js_config.cpp | 2 +- .../js/napi/cloud_data/src/js_config_util.cpp | 35 -- .../cloud_data/src/js_const_properties.cpp | 20 +- frameworks/js/napi/common/include/js_utils.h | 23 ++ .../cloud_data/include/cloud_service_proxy.h | 14 +- .../cloud_data/src/cloud_service_proxy.cpp | 97 +++++ .../cloud_data/src/cloud_types_util.cpp | 9 +- .../cloud_data/include/cloud_service.h | 22 +- .../cloud_data/include/cloud_types.h | 6 +- 15 files changed, 611 insertions(+), 92 deletions(-) delete mode 100644 frameworks/js/napi/cloud_data/include/js_config_util.h delete mode 100644 frameworks/js/napi/cloud_data/src/js_config_util.cpp diff --git a/CODEOWNERS b/CODEOWNERS index 58615a78f..7cfaea60b 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -14,4 +14,4 @@ # any change to # interfaces/inner_api/rdb/include/distributeddata_relational_store_ipc_interface_code.h # needs to be reviewed by @leonchan5 -# interfaces/inner_api/rdb/include/distributeddata_relational_store_ipc_interface_code.h @leonchan5 \ No newline at end of file +interfaces/inner_api/rdb/include/distributeddata_relational_store_ipc_interface_code.h @leonchan5 \ No newline at end of file diff --git a/frameworks/js/napi/cloud_data/BUILD.gn b/frameworks/js/napi/cloud_data/BUILD.gn index 98159a566..a5053131f 100644 --- a/frameworks/js/napi/cloud_data/BUILD.gn +++ b/frameworks/js/napi/cloud_data/BUILD.gn @@ -34,7 +34,6 @@ ohos_shared_library("clouddata") { "${cloud_data_napi_path}/src/js_cloud_share.cpp", "${cloud_data_napi_path}/src/js_cloud_utils.cpp", "${cloud_data_napi_path}/src/js_config.cpp", - "${cloud_data_napi_path}/src/js_config_util.cpp", "${cloud_data_napi_path}/src/js_const_properties.cpp", "${cloud_data_napi_path}/src/js_error_utils.cpp", "${cloud_data_napi_path}/src/napi_queue.cpp", diff --git a/frameworks/js/napi/cloud_data/include/js_cloud_utils.h b/frameworks/js/napi/cloud_data/include/js_cloud_utils.h index dcc033238..b9c202a59 100644 --- a/frameworks/js/napi/cloud_data/include/js_cloud_utils.h +++ b/frameworks/js/napi/cloud_data/include/js_cloud_utils.h @@ -19,6 +19,7 @@ #include "cloud_types.h" #include "rdb_predicates.h" #include "result_set.h" +#include "js_config.h" namespace OHOS::AppDataMgrJsKit { namespace JSUtils { @@ -26,6 +27,10 @@ using Participant = OHOS::CloudData::Participant; using Privilege = OHOS::CloudData::Privilege; using RdbPredicates = OHOS::NativeRdb::RdbPredicates; using ResultSet = OHOS::NativeRdb::ResultSet; +using ExtraData = OHOS::CloudData::JsConfig::ExtraData; + +template<> +int32_t Convert2Value(napi_env env, napi_value input, ExtraData &output); template<> int32_t Convert2Value(napi_env env, napi_value input, Participant &output); @@ -44,6 +49,10 @@ napi_value Convert2JSValue(napi_env env, const Privilege &value); template<> napi_value Convert2JSValue(napi_env env, const std::shared_ptr &value); + +template<> +napi_value Convert2JSValue(napi_env env, const std::pair &value); + }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit #endif // CLOUD_DATA_JS_CLOUD_SHARE_H diff --git a/frameworks/js/napi/cloud_data/include/js_config_util.h b/frameworks/js/napi/cloud_data/include/js_config_util.h deleted file mode 100644 index 2051e44b3..000000000 --- a/frameworks/js/napi/cloud_data/include/js_config_util.h +++ /dev/null @@ -1,29 +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 LDBPROJ_JS_CONFIG_UTIL_H -#define LDBPROJ_JS_CONFIG_UTIL_H - -#include "js_config.h" -#include "js_utils.h" - -namespace OHOS::AppDataMgrJsKit { -using ExtraData = OHOS::CloudData::JsConfig::ExtraData; -namespace JSUtils { -template<> -int32_t Convert2Value(napi_env env, napi_value input, ExtraData &output); -} // namespace JSUtils -} // namespace OHOS::AppDataMgrJsKit -#endif // LDBPROJ_JS_CONFIG_UTIL_H \ No newline at end of file diff --git a/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp b/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp index 94fe99dfb..3af9b8fc5 100644 --- a/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp +++ b/frameworks/js/napi/cloud_data/src/js_cloud_share.cpp @@ -19,9 +19,9 @@ #include "cloud_manager.h" #include "cloud_service.h" #include "cloud_types.h" -#include "js_utils.h" #include "js_cloud_utils.h" #include "js_error_utils.h" +#include "js_utils.h" #include "logger.h" #include "napi_queue.h" #include "rdb_predicates.h" @@ -47,7 +47,7 @@ napi_value AllocResourceAndShare(napi_env env, napi_callback_info info) { struct AllocResAndShareContext : public ContextBase { std::string storeId; - std::vector participants; + Participants participants; std::vector columns; std::shared_ptr predicates = nullptr; std::shared_ptr resultSet; @@ -97,6 +97,382 @@ napi_value AllocResourceAndShare(napi_env env, napi_callback_info info) return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); } +/* + * [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; + Participants participants; + Results results; + }; + 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 && !ctxt->sharingRes.empty(), + 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 && !ctxt->participants.empty(), + 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->results); + 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->results); + 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; + Participants participants; + Results results; + }; + 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 && !ctxt->sharingRes.empty(), + 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 && !ctxt->participants.empty(), + 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->results); + 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->results); + 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; + std::pair result; + }; + 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 && !ctxt->sharingRes.empty(), + 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->Exit(ctxt->sharingRes, ctxt->result); + 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->result); + 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; + Participants participants; + Results results; + }; + 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 && !ctxt->sharingRes.empty(), + 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 && !ctxt->participants.empty(), + 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->results); + 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->results); + 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 Query(napi_env env, napi_callback_info info) +{ + struct QueryContext : public ContextBase { + std::string sharingRes; + QueryResults results; + }; + 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 && !ctxt->sharingRes.empty(), + 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->Query(ctxt->sharingRes, ctxt->results); + 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->results); + 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 QueryByInvitation(napi_env env, napi_callback_info info) +{ + struct QueryByInvitationContext : public ContextBase { + std::string invitationCode; + QueryResults results; + }; + 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 && !ctxt->invitationCode.empty(), + 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->QueryByInvitation(ctxt->invitationCode, ctxt->results); + 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->results); + 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; + int32_t confirmation; + std::tuple result; + }; + 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 && !ctxt->invitationCode.empty(), + Status::INVALID_ARGUMENT, "The type of invitationCode must be string."); + int32_t confirmation; + status = JSUtils::Convert2ValueExt(env, argv[1], confirmation); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK && + confirmation > Confirmation::CFM_NIL && confirmation <= Confirmation::CFM_BUTT, + Status::INVALID_ARGUMENT, "The type of status must be Status."); + ctxt->confirmation = confirmation; + }); + 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->confirmation, ctxt->result); + 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->result); + 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; + int32_t confirmation; + std::pair results; + }; + 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 && !ctxt->sharingRes.empty(), + Status::INVALID_ARGUMENT, "The type of sharingRes must be string."); + int32_t confirmation; + status = JSUtils::Convert2ValueExt(env, argv[1], confirmation); + ASSERT_BUSINESS_ERR(ctxt, status == JSUtils::OK && + confirmation > Confirmation::CFM_NIL && confirmation < Confirmation::CFM_BUTT, + Status::INVALID_ARGUMENT, "The type of status must be Status."); + ctxt->confirmation = confirmation; + }); + 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->confirmation, ctxt->results); + 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->results); + ASSERT_VALUE(ctxt, result != nullptr, napi_generic_failure, "output failed"); + }; + return NapiQueue::AsyncWork(env, ctxt, std::string(__FUNCTION__), execute, output); +} + napi_value InitCloudSharing(napi_env env, napi_value exports) { napi_value sharing = nullptr; @@ -106,6 +482,14 @@ napi_value InitCloudSharing(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", Query), + DECLARE_NAPI_FUNCTION("queryParticipantsByInvitation", QueryByInvitation), + DECLARE_NAPI_FUNCTION("confirmInvitation", ConfirmInvitation), + DECLARE_NAPI_FUNCTION("changeConfirmation", ChangeConfirmation), }; NAPI_CALL(env, napi_define_properties(env, sharing, sizeof(properties) / sizeof(*properties), properties)); NAPI_CALL(env, napi_set_named_property(env, exports, "sharing", sharing)); diff --git a/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp b/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp index 0768486d0..467ba63d4 100644 --- a/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp +++ b/frameworks/js/napi/cloud_data/src/js_cloud_utils.cpp @@ -20,7 +20,7 @@ #include "logger.h" #define NAPI_CALL_RETURN_ERR(call, ret) \ - ASSERT_RETURN((call) != napi_ok, ret) + ASSERT_RETURN((call) == napi_ok, ret) #define ASSERT_RETURN(call, ret) \ do { \ @@ -32,6 +32,22 @@ namespace OHOS::AppDataMgrJsKit { namespace JSUtils { using namespace OHOS::Rdb; + +template<> +int32_t Convert2Value(napi_env env, napi_value input, ExtraData &output) +{ + napi_valuetype type = napi_undefined; + napi_status status = napi_typeof(env, input, &type); + if (status != napi_ok || type != napi_object) { + return napi_invalid_arg; + } + int32_t result = GET_PROPERTY(env, input, output, eventId); + if (result != napi_ok) { + return napi_invalid_arg; + } + return GET_PROPERTY(env, input, output, extraData); +} + template<> int32_t Convert2Value(napi_env env, napi_value input, Participant &output) { @@ -46,9 +62,9 @@ int32_t Convert2Value(napi_env env, napi_value input, Participant &output) 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::Confirmation::CFM_UNKNOWN || - output.status > CloudData::Confirmation::CFM_SUSPENDED) { + NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "state", output.state), napi_invalid_arg); + if (output.state < CloudData::Confirmation::CFM_UNKNOWN || + output.state > CloudData::Confirmation::CFM_SUSPENDED) { return napi_invalid_arg; } NAPI_CALL_RETURN_ERR(GetOptionalValue(env, input, "privilege", output.privilege), napi_invalid_arg); @@ -99,7 +115,7 @@ napi_value Convert2JSValue(napi_env env, const Participant &value) } napi_value identity = Convert2JSValue(env, value.identity); napi_value role = Convert2JSValue(env, value.role); - napi_value sharingStatus = Convert2JSValue(env, value.status); + napi_value sharingState = Convert2JSValue(env, value.state); napi_value privilege = Convert2JSValue(env, value.privilege); if (privilege == nullptr) { return nullptr; @@ -107,7 +123,7 @@ napi_value Convert2JSValue(napi_env env, const Participant &value) napi_value attachInfo = Convert2JSValue(env, value.attachInfo); 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, "state", sharingState); napi_set_named_property(env, jsValue, "privilege", privilege); napi_set_named_property(env, jsValue, "attachInfo", attachInfo); return jsValue; @@ -153,5 +169,23 @@ napi_value Convert2JSValue(napi_env env, const std::shared_ptr &value proxy->SetInstance(value); return instance; } + +template<> +napi_value Convert2JSValue(napi_env env, const std::pair &value) +{ + napi_value jsValue; + napi_status status = napi_create_object(env, &jsValue); + if (status != napi_ok) { + return nullptr; + } + napi_value code = Convert2JSValue(env, value.first); + napi_value description = Convert2JSValue(env, value.second); + napi_value val; + napi_get_undefined(env, &val); + napi_set_named_property(env, jsValue, "code", code); + napi_set_named_property(env, jsValue, "description", description); + napi_set_named_property(env, jsValue, "value", val); + return jsValue; +} }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit \ No newline at end of file diff --git a/frameworks/js/napi/cloud_data/src/js_config.cpp b/frameworks/js/napi/cloud_data/src/js_config.cpp index 02bd49231..533d87f62 100644 --- a/frameworks/js/napi/cloud_data/src/js_config.cpp +++ b/frameworks/js/napi/cloud_data/src/js_config.cpp @@ -22,7 +22,7 @@ #include "cloud_service.h" #include "js_error_utils.h" #include "js_utils.h" -#include "js_config_util.h" +#include "js_cloud_utils.h" #include "logger.h" #include "napi_queue.h" diff --git a/frameworks/js/napi/cloud_data/src/js_config_util.cpp b/frameworks/js/napi/cloud_data/src/js_config_util.cpp deleted file mode 100644 index dca20dfbf..000000000 --- a/frameworks/js/napi/cloud_data/src/js_config_util.cpp +++ /dev/null @@ -1,35 +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 "js_config_util.h" - -namespace OHOS::AppDataMgrJsKit { -namespace JSUtils { -template<> -int32_t Convert2Value(napi_env env, napi_value input, ExtraData &output) -{ - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, input, &type); - if (status != napi_ok || type != napi_object) { - return napi_invalid_arg; - } - int32_t result = GET_PROPERTY(env, input, output, eventId); - if (result != napi_ok) { - return napi_invalid_arg; - } - return GET_PROPERTY(env, input, output, extraData); -} -} // namespace JSUtils -} // namespace OHOS::AppDataMgrJsKit \ No newline at end of file diff --git a/frameworks/js/napi/cloud_data/src/js_const_properties.cpp b/frameworks/js/napi/cloud_data/src/js_const_properties.cpp index 7bf75a35e..e12c5d888 100644 --- a/frameworks/js/napi/cloud_data/src/js_const_properties.cpp +++ b/frameworks/js/napi/cloud_data/src/js_const_properties.cpp @@ -53,16 +53,16 @@ static napi_value ExportRole(napi_env env) return role; } -static napi_value ExportShareStatus(napi_env env) +static napi_value ExportShareState(napi_env env) { - napi_value status = nullptr; - napi_create_object(env, &status); - SetNamedProperty(env, status, "STATUS_UNKNOWN", Confirmation::CFM_UNKNOWN); - SetNamedProperty(env, status, "STATUS_ACCEPTED", Confirmation::CFM_ACCEPTED); - SetNamedProperty(env, status, "STATUS_REJECTED", Confirmation::CFM_REJECTED); - SetNamedProperty(env, status, "STATUS_SUSPENDED", Confirmation::CFM_SUSPENDED); - napi_object_freeze(env, status); - return status; + napi_value state = nullptr; + napi_create_object(env, &state); + SetNamedProperty(env, state, "STATE_UNKNOWN", Confirmation::CFM_UNKNOWN); + SetNamedProperty(env, state, "STATE_ACCEPTED", Confirmation::CFM_ACCEPTED); + SetNamedProperty(env, state, "STATE_REJECTED", Confirmation::CFM_REJECTED); + SetNamedProperty(env, state, "STATE_SUSPENDED", Confirmation::CFM_SUSPENDED); + napi_object_freeze(env, state); + return state; } static napi_value ExportShareCode(napi_env env) @@ -105,7 +105,7 @@ napi_status InitSharingConstProperties(napi_env env, napi_value exports) } const napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("Role", ExportRole(env)), - DECLARE_NAPI_PROPERTY("Status", ExportShareStatus(env)), + DECLARE_NAPI_PROPERTY("State", ExportShareState(env)), DECLARE_NAPI_PROPERTY("SharingCode", ExportShareCode(env)), }; size_t count = sizeof(properties) / sizeof(properties[0]); diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index a7f776920..23068026e 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -120,6 +120,9 @@ napi_value Convert2JSValue(napi_env env, const std::vector &value); template napi_value Convert2JSValue(napi_env env, const std::map &value); +template +napi_value Convert2JSValue(napi_env env, const std::tuple &value); + template napi_value Convert2JSValue(napi_env env, const std::variant &value); @@ -275,6 +278,26 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) return jsValue; } +template +napi_value JSUtils::Convert2JSValue(napi_env env, const std::tuple &value) +{ + napi_value jsValue; + napi_status status = napi_create_object(env, &jsValue); + if (status != napi_ok) { + return nullptr; + } + napi_value code = Convert2JSValue(env, std::get<0>(value)); + napi_value description = Convert2JSValue(env, std::get<1>(value)); + napi_value val = Convert2JSValue(env, std::get<2>(value)); + if (description == nullptr || val == nullptr) { + return nullptr; + } + napi_set_named_property(env, jsValue, "code", code); + napi_set_named_property(env, jsValue, "description", description); + napi_set_named_property(env, jsValue, "value", val); + return jsValue; +} + template int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant &value) { diff --git a/frameworks/native/cloud_data/include/cloud_service_proxy.h b/frameworks/native/cloud_data/include/cloud_service_proxy.h index 8197d573a..e03c74caa 100644 --- a/frameworks/native/cloud_data/include/cloud_service_proxy.h +++ b/frameworks/native/cloud_data/include/cloud_service_proxy.h @@ -34,7 +34,19 @@ public: std::pair> AllocResourceAndShare(const std::string& storeId, const DistributedRdb::PredicatesMemo& predicates, const std::vector& columns, - const std::vector& participants) override; + const Participants& participants) override; + int32_t Share(const std::string &sharingRes, const Participants &participants, Results &results) override; + int32_t Unshare(const std::string &sharingRes, const Participants &participants, Results &results) override; + int32_t Exit(const std::string &sharingRes, std::pair &result) override; + int32_t ChangePrivilege( + const std::string &sharingRes, const Participants &participants, Results &results) override; + int32_t Query(const std::string &sharingRes, QueryResults &results) override; + int32_t QueryByInvitation(const std::string &invitation, QueryResults &results) override; + int32_t ConfirmInvitation(const std::string &invitation, + int32_t confirmation, std::tuple &result) override; + int32_t ChangeConfirmation(const std::string &sharingRes, + int32_t confirmation, std::pair &result) override; + private: sptr remote_; }; diff --git a/frameworks/native/cloud_data/src/cloud_service_proxy.cpp b/frameworks/native/cloud_data/src/cloud_service_proxy.cpp index c1d96ce4b..869fa1642 100644 --- a/frameworks/native/cloud_data/src/cloud_service_proxy.cpp +++ b/frameworks/native/cloud_data/src/cloud_service_proxy.cpp @@ -126,4 +126,101 @@ int32_t CloudServiceProxy::NotifyDataChange(const std::string &eventId, const st } return static_cast(status); } + +int32_t CloudServiceProxy::Share( + const std::string &sharingRes, const Participants &participants, Results &results) +{ + 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, results); + return static_cast(status); +} + +int32_t CloudServiceProxy::Unshare( + const std::string &sharingRes, const Participants &participants, Results &results) +{ + 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, results); + return static_cast(status); +} + +int32_t CloudServiceProxy::Exit(const std::string &sharingRes, std::pair &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_EXIT, 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 Participants &participants, Results &results) +{ + 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, results); + return static_cast(status); +} + +int32_t CloudServiceProxy::Query(const std::string &sharingRes, QueryResults &results) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_QUERY, reply, sharingRes); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str()); + } + ITypesUtil::Unmarshal(reply, results); + return static_cast(status); +} + +int32_t CloudServiceProxy::QueryByInvitation( + const std::string &invitation, QueryResults &results) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_QUERY_BY_INVITATION, reply, invitation); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x invitation:%{public}.6s", status, invitation.c_str()); + } + ITypesUtil::Unmarshal(reply, results); + return static_cast(status); +} + +int32_t CloudServiceProxy::ConfirmInvitation(const std::string &invitation, + int32_t confirmation, std::tuple &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CONFIRM_INVITATION, reply, invitation, confirmation); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x invitation:%{public}.6s", status, invitation.c_str()); + } + ITypesUtil::Unmarshal(reply, result); + return static_cast(status); +} + +int32_t CloudServiceProxy::ChangeConfirmation(const std::string &sharingRes, + int32_t confirmation, std::pair &result) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CHANGE_CONFIRMATION, reply, sharingRes, confirmation); + if (status != SUCCESS) { + LOG_ERROR("status:0x%{public}x sharingRes:%{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/frameworks/native/cloud_data/src/cloud_types_util.cpp b/frameworks/native/cloud_data/src/cloud_types_util.cpp index 025966246..b811d9d47 100644 --- a/frameworks/native/cloud_data/src/cloud_types_util.cpp +++ b/frameworks/native/cloud_data/src/cloud_types_util.cpp @@ -20,13 +20,14 @@ template<> bool Marshalling(const Participant &input, MessageParcel &data) { return ITypesUtil::Marshal( - data, input.identity, input.role, input.status, input.privilege, input.attachInfo); + data, input.identity, input.role, input.state, input.privilege, input.attachInfo); } + template<> bool Unmarshalling(Participant &output, MessageParcel &data) { return ITypesUtil::Unmarshal( - data, output.identity, output.role, output.status, output.privilege, output.attachInfo); + data, output.identity, output.role, output.state, output.privilege, output.attachInfo); } template<> @@ -53,7 +54,7 @@ template<> bool Unmarshalling(Role &output, MessageParcel &data) { int32_t result; - if (!data.ReadInt32(result) || result <= Role::ROLE_NIL || result >= Role::ROLE_BUTT) { + if (!data.ReadInt32(result) || result <= Role::ROLE_NIL || result >= Role::ROLE_BUTT) { return false; } output = static_cast(result); @@ -91,7 +92,7 @@ bool Unmarshalling(SharingCode &output, MessageParcel &data) if (!data.ReadInt32(result) || result < SharingCode::SUCCESS) { return false; } - output = static_cast< SharingCode>(result); + output = static_cast(result); return true; } diff --git a/interfaces/inner_api/cloud_data/include/cloud_service.h b/interfaces/inner_api/cloud_data/include/cloud_service.h index f93a14777..d17654042 100644 --- a/interfaces/inner_api/cloud_data/include/cloud_service.h +++ b/interfaces/inner_api/cloud_data/include/cloud_service.h @@ -35,6 +35,14 @@ public: TRANS_NOTIFY_DATA_CHANGE, TRANS_NOTIFY_DATA_CHANGE_EXT, TRANS_ALLOC_RESOURCE_AND_SHARE, + TRANS_SHARE, + TRANS_UNSHARE, + TRANS_EXIT, + TRANS_CHANGE_PRIVILEGE, + TRANS_QUERY, + TRANS_QUERY_BY_INVITATION, + TRANS_CONFIRM_INVITATION, + TRANS_CHANGE_CONFIRMATION, TRANS_BUTT, }; enum Action : int32_t { @@ -75,7 +83,19 @@ public: virtual std::pair> AllocResourceAndShare(const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates, const std::vector &columns, - const std::vector &participants) = 0; + const Participants &participants) = 0; + virtual int32_t Share(const std::string &sharingRes, const Participants &participants, Results &results) = 0; + virtual int32_t Unshare(const std::string &sharingRes, const Participants &participants, Results &results) = 0; + virtual int32_t Exit(const std::string &sharingRes, std::pair &result) = 0; + virtual int32_t ChangePrivilege( + const std::string &sharingRes, const Participants &participants, Results &results) = 0; + virtual int32_t Query(const std::string &sharingRes, QueryResults &results) = 0; + virtual int32_t QueryByInvitation(const std::string &invitation, QueryResults &results) = 0; + virtual int32_t ConfirmInvitation(const std::string &invitation, int32_t confirmation, + std::tuple &result) = 0; + virtual int32_t ChangeConfirmation(const std::string &sharingRes, + int32_t confirmation, std::pair &result) = 0; + inline static constexpr const char *SERVICE_NAME = "cloud"; }; } // namespace CloudData diff --git a/interfaces/inner_api/cloud_data/include/cloud_types.h b/interfaces/inner_api/cloud_data/include/cloud_types.h index 88399759f..b0136552d 100644 --- a/interfaces/inner_api/cloud_data/include/cloud_types.h +++ b/interfaces/inner_api/cloud_data/include/cloud_types.h @@ -46,11 +46,15 @@ struct Privilege { struct Participant { std::string identity; int32_t role = Role::ROLE_NIL; - int32_t status = Confirmation::CFM_NIL; + int32_t state = Confirmation::CFM_NIL; Privilege privilege; std::string attachInfo; }; +using Participants = std::vector; +using Results = std::tuple>>; +using QueryResults = std::tuple; + /** * Enumerates the error code of sharing invitation. */