add cloud shared interface

Signed-off-by: zuojiangjiang <zuojiangjiang@huawei.com>
This commit is contained in:
zuojiangjiang 2023-11-27 02:39:55 +08:00
parent 060d07a4e9
commit 81bea35ab9
15 changed files with 611 additions and 92 deletions

View File

@ -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
interfaces/inner_api/rdb/include/distributeddata_relational_store_ipc_interface_code.h @leonchan5

View File

@ -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",

View File

@ -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<ResultSet> &value);
template<>
napi_value Convert2JSValue(napi_env env, const std::pair<int32_t, std::string> &value);
}; // namespace JSUtils
} // namespace OHOS::AppDataMgrJsKit
#endif // CLOUD_DATA_JS_CLOUD_SHARE_H

View File

@ -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

View File

@ -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<Participant> participants;
Participants participants;
std::vector<std::string> columns;
std::shared_ptr<RdbPredicates> predicates = nullptr;
std::shared_ptr<ResultSet> 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<Participant>,
* callback: AsyncCallback<Result<Array<Result<Participant>>>>): void;
*
* [Promise]
* share(sharingRes: string, participants: Array<Participant>): Promise<Result<Array<Result<Participant>>>>;
*/
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<ShareContext>();
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<Participant>.");
});
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<Participant>,
* callback: AsyncCallback<Result<Array<Result<Participant>>>>): void;
*
* [Promise]
* unshare(sharingRes: string, participants: Array<Participant>): Promise<Result<Array<Result<Participant>>>>;
*/
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<UnshareContext>();
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<Participant>.");
});
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<Result<void>>): void;
*
* [Promise]
* exit(sharingRes: string): Promise<Result<void>>;
*/
napi_value Exit(napi_env env, napi_callback_info info)
{
struct ExitContext : public ContextBase {
std::string sharingRes;
std::pair<int32_t, std::string> result;
};
auto ctxt = std::make_shared<ExitContext>();
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<Participant>,
* callback: AsyncCallback<Result<Array<Result<Participant>>>>): void;
*
* [Promise]
* changePrivilege(sharingRes: string, participants: Array<Participant>): Promise<Result<Array<Result<Participant>>>>;
*/
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<ChangePrivilegeContext>();
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<Participant>.");
});
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<Result<Array<Participant>>>): void;
*
* [Promise]
* queryParticipants(sharingRes: string): Promise<Result<Array<Participant>>>;
*/
napi_value Query(napi_env env, napi_callback_info info)
{
struct QueryContext : public ContextBase {
std::string sharingRes;
QueryResults results;
};
auto ctxt = std::make_shared<QueryContext>();
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<Result<Array<Participant>>>): void;
*
* [Promise]
* queryParticipantsByInvitation(invitationCode: string): Promise<Result<Array<Participant>>>;
*/
napi_value QueryByInvitation(napi_env env, napi_callback_info info)
{
struct QueryByInvitationContext : public ContextBase {
std::string invitationCode;
QueryResults results;
};
auto ctxt = std::make_shared<QueryByInvitationContext>();
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<Result<string>>): void;
*
* [Promise]
* confirmInvitation(invitationCode: string, status: Status): Promise<Result<string>>;
*/
napi_value ConfirmInvitation(napi_env env, napi_callback_info info)
{
struct ConfirmInvitationContext : public ContextBase {
std::string invitationCode;
int32_t confirmation;
std::tuple<int32_t, std::string, std::string> result;
};
auto ctxt = std::make_shared<ConfirmInvitationContext>();
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<Result<void>>): void;
*
* [Promise]
* changeConfirmation(sharingRes: string, status: Status): Promise<Result<void>>;
*/
napi_value ChangeConfirmation(napi_env env, napi_callback_info info)
{
struct ChangeConfirmationContext : public ContextBase {
std::string sharingRes;
int32_t confirmation;
std::pair<int32_t, std::string> results;
};
auto ctxt = std::make_shared<ChangeConfirmationContext>();
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));

View File

@ -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<ResultSet> &value
proxy->SetInstance(value);
return instance;
}
template<>
napi_value Convert2JSValue(napi_env env, const std::pair<int32_t, std::string> &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

View File

@ -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"

View File

@ -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

View File

@ -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]);

View File

@ -120,6 +120,9 @@ napi_value Convert2JSValue(napi_env env, const std::vector<T> &value);
template<typename K, typename V>
napi_value Convert2JSValue(napi_env env, const std::map<K, V> &value);
template<typename T>
napi_value Convert2JSValue(napi_env env, const std::tuple<int32_t, std::string, T> &value);
template<typename... Types>
napi_value Convert2JSValue(napi_env env, const std::variant<Types...> &value);
@ -275,6 +278,26 @@ napi_value JSUtils::Convert2JSValue(napi_env env, const std::map<K, V> &value)
return jsValue;
}
template<typename T>
napi_value JSUtils::Convert2JSValue(napi_env env, const std::tuple<int32_t, std::string, T> &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<typename... Types>
int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant<Types...> &value)
{

View File

@ -34,7 +34,19 @@ public:
std::pair<int32_t, std::vector<NativeRdb::ValuesBucket>> AllocResourceAndShare(const std::string& storeId,
const DistributedRdb::PredicatesMemo& predicates, const std::vector<std::string>& columns,
const std::vector<Participant>& 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<int32_t, std::string> &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<int32_t, std::string, std::string> &result) override;
int32_t ChangeConfirmation(const std::string &sharingRes,
int32_t confirmation, std::pair<int32_t, std::string> &result) override;
private:
sptr<IRemoteObject> remote_;
};

View File

@ -126,4 +126,101 @@ int32_t CloudServiceProxy::NotifyDataChange(const std::string &eventId, const st
}
return static_cast<Status>(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>(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>(status);
}
int32_t CloudServiceProxy::Exit(const std::string &sharingRes, std::pair<int32_t, std::string> &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>(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>(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>(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>(status);
}
int32_t CloudServiceProxy::ConfirmInvitation(const std::string &invitation,
int32_t confirmation, std::tuple<int32_t, std::string, std::string> &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>(status);
}
int32_t CloudServiceProxy::ChangeConfirmation(const std::string &sharingRes,
int32_t confirmation, std::pair<int32_t, std::string> &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>(status);
}
} // namespace OHOS::CloudData

View File

@ -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<Role>(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<SharingCode>(result);
return true;
}

View File

@ -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<int32_t, std::vector<NativeRdb::ValuesBucket>> AllocResourceAndShare(const std::string &storeId,
const DistributedRdb::PredicatesMemo &predicates, const std::vector<std::string> &columns,
const std::vector<Participant> &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<int32_t, std::string> &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<int32_t, std::string, std::string> &result) = 0;
virtual int32_t ChangeConfirmation(const std::string &sharingRes,
int32_t confirmation, std::pair<int32_t, std::string> &result) = 0;
inline static constexpr const char *SERVICE_NAME = "cloud";
};
} // namespace CloudData

View File

@ -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<Participant>;
using Results = std::tuple<int32_t, std::string, std::vector<std::pair<int32_t, std::string>>>;
using QueryResults = std::tuple<int32_t, std::string, Participants>;
/**
* Enumerates the error code of sharing invitation.
*/