未验证 提交 0e651b53 编写于 作者: O openharmony_ci 提交者: Gitee

!393 add error process methods for MessageSequence and IPCSkeleton

Merge pull request !393 from 刘淞/master
......@@ -34,6 +34,7 @@ ohos_shared_library("rpc") {
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_ashmem.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_option.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_parcel.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_sequence.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_remote_object.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_remote_proxy.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_rpc_error.cpp",
......
/*
* Copyright (c) 2022 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 NAPI_IPC_OHOS_MESSAGE_SEQUENCE_H
#define NAPI_IPC_OHOS_MESSAGE_SEQUENCE_H
#include "ipc_skeleton.h"
#include "message_parcel.h"
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "napi_rpc_error.h"
#include "securec.h"
namespace OHOS {
class NAPI_MessageSequence {
public:
NAPI_MessageSequence(napi_env env, napi_value thisVar, MessageParcel *parcel);
virtual ~NAPI_MessageSequence();
std::shared_ptr<MessageParcel> GetMessageParcel();
static napi_value Export(napi_env env, napi_value exports);
private:
// Napi methods and properties
static napi_value JS_create(napi_env env, napi_callback_info cbinfo);
static napi_value JS_reclaim(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeRemoteObject(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readRemoteObject(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeInterfaceToken(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readInterfaceToken(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getSize(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getCapacity(napi_env env, napi_callback_info cbinfo);
static napi_value JS_setSize(napi_env env, napi_callback_info cbinfo);
static napi_value JS_setCapacity(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getWritableBytes(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getReadableBytes(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getReadPosition(napi_env env, napi_callback_info cbinfo);
static napi_value JS_getWritePosition(napi_env env, napi_callback_info cbinfo);
static napi_value JS_rewindWrite(napi_env env, napi_callback_info cbinfo);
static napi_value JS_rewindRead(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeNoException(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readException(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeByte(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeShort(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeInt(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeLong(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeFloat(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeDouble(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeBoolean(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeChar(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeString(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeSequenceable(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeByteArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeShortArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeIntArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeLongArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeFloatArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeDoubleArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeBooleanArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeCharArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeStringArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeSequenceableArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_writeRemoteObjectArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readByte(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readShort(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readInt(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readLong(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readFloat(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readDouble(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readBoolean(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readChar(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readString(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readSequenceable(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readByteArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readShortArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readIntArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readLongArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readFloatArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readDoubleArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readBooleanArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readCharArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readStringArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readSequenceableArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_readRemoteObjectArray(napi_env env, napi_callback_info cbinfo);
static napi_value JS_CloseFileDescriptor(napi_env env, napi_callback_info info);
static napi_value JS_DupFileDescriptor(napi_env env, napi_callback_info info);
static napi_value JS_WriteFileDescriptor(napi_env env, napi_callback_info info);
static napi_value JS_ReadFileDescriptor(napi_env env, napi_callback_info info);
static napi_value JS_ContainFileDescriptors(napi_env env, napi_callback_info info);
static napi_value JS_WriteAshmem(napi_env env, napi_callback_info info);
static napi_value JS_ReadAshmem(napi_env env, napi_callback_info info);
static napi_value JS_GetRawDataCapacity(napi_env env, napi_callback_info info);
static napi_value JS_WriteRawData(napi_env env, napi_callback_info info);
static napi_value JS_ReadRawData(napi_env env, napi_callback_info info);
static napi_value JS_constructor(napi_env env, napi_callback_info cbinfo);
static void release(MessageParcel *parcel);
static napi_value JS_checkWriteByteArrayArgs(napi_env env, size_t argc, napi_value* argv, uint32_t &arrayLength);
static napi_value JS_checkWriteArrayArgs(napi_env env, size_t argc, napi_value* argv, uint32_t &arrayLength);
static napi_value JS_checkWriteStringArrayElement(napi_env env, napi_value* argv, size_t &index,
size_t &bufferSize, napi_value &element);
static napi_value JS_writeSequenceableArrayCallJsFunc(napi_env env, napi_value &element, napi_value &thisVar);
static napi_value JS_checkReadArrayArgs(napi_env env, napi_callback_info info, size_t &argc,
napi_value &thisVar, napi_value* argv);
static napi_value JS_readSequenceableArrayCallJsFunc(napi_env env, napi_value &element, napi_value &thisVar);
static napi_value JS_checkWriteRawDataArgs(napi_env env, size_t argc, napi_value* argv);
napi_env env_ = nullptr;
bool owner;
std::shared_ptr<MessageParcel> nativeParcel_ = nullptr;
size_t maxCapacityToWrite_;
static NapiError napiErr;
};
} // namespace OHOS
#endif // NAPI_IPC_OHOS_MESSAGE_SEQUENCE_H
......@@ -118,10 +118,14 @@ public:
napi_value NAPI_IPCSkeleton_flushCommands(napi_env env, napi_callback_info info);
napi_value NAPI_IPCSkeleton_flushCmdBuffer(napi_env env, napi_callback_info info);
napi_value NAPI_IPCSkeleton_resetCallingIdentity(napi_env env, napi_callback_info info);
napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info info);
napi_value NAPI_IPCSkeleton_restoreCallingIdentity(napi_env env, napi_callback_info info);
napi_value NAPI_IPCSkeleton_getCallingTokenId(napi_env env, napi_callback_info info);
// RemoteObject napi methods
......
此差异已折叠。
......@@ -31,6 +31,7 @@
#include "log_tags.h"
#include "napi_message_option.h"
#include "napi_message_parcel.h"
#include "napi_message_sequence.h"
#include "napi_rpc_error.h"
#include "rpc_bytrace.h"
#include "string_ex.h"
......@@ -43,10 +44,10 @@ static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag.
static NapiError napiErr;
static const int ARGV_INDEX_0 = 0;
static const int ARGV_INDEX_1 = 1;
static const int ARGV_INDEX_2 = 2;
static const int ARGV_INDEX_3 = 3;
static const size_t ARGV_INDEX_0 = 0;
static const size_t ARGV_INDEX_1 = 1;
static const size_t ARGV_INDEX_2 = 2;
static const size_t ARGV_INDEX_3 = 3;
/*
* The native NAPIRemoteObject act as bridger between js and native.
......@@ -514,7 +515,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam)
}
napi_value jsParcelConstructor = nullptr;
if (isOnRemoteMessageRequest) {
napi_get_named_property(param->env, global, "IPCParcelConstructor_", &jsParcelConstructor);
napi_get_named_property(param->env, global, "IPCSequenceConstructor_", &jsParcelConstructor);
} else {
napi_get_named_property(param->env, global, "IPCParcelConstructor_", &jsParcelConstructor);
}
......@@ -919,6 +920,32 @@ napi_value NAPI_IPCSkeleton_flushCommands(napi_env env, napi_callback_info info)
return napiValue;
}
napi_value NAPI_IPCSkeleton_flushCmdBuffer(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = {0};
napi_value thisVar = nullptr;
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
if (argc != 1) {
ZLOGE(LOG_LABEL, "requires 1 parameter");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
if (valueType != napi_object) {
ZLOGE(LOG_LABEL, "type mismatch for parameter 1");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
sptr<IRemoteObject> target = NAPI_ohos_rpc_getNativeRemoteObject(env, argv[0]);
IPCSkeleton::FlushCommands(target);
napi_value napiValue = nullptr;
napi_get_undefined(env, &napiValue);
return napiValue;
}
napi_value NAPI_IPCSkeleton_resetCallingIdentity(napi_env env, napi_callback_info info)
{
napi_value global = nullptr;
......@@ -1072,6 +1099,104 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info
}
}
static napi_value NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(napi_env env,
napi_value &global,
char* stringValue)
{
std::string identity = stringValue;
napi_value napiIsLocalCalling = nullptr;
napi_get_named_property(env, global, "isLocalCalling_", &napiIsLocalCalling);
bool isLocalCalling = true;
napi_get_value_bool(env, napiIsLocalCalling, &isLocalCalling);
napi_value result;
napi_get_undefined(env, &result);
if (isLocalCalling) {
if (identity.empty()) {
ZLOGE(LOG_LABEL, "identity is empty");
return result;
}
int64_t token = std::stoll(identity);
int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN);
int callerPid = static_cast<int>(token);
napi_value napiCallingPid;
napi_create_int32(env, callerPid, &napiCallingPid);
napi_set_named_property(env, global, "callingPid_", napiCallingPid);
napi_value napiCallingUid;
napi_create_int32(env, callerUid, &napiCallingUid);
napi_set_named_property(env, global, "callingUid_", napiCallingUid);
return result;
} else {
if (identity.empty() || identity.length() <= DEVICEID_LENGTH) {
ZLOGE(LOG_LABEL, "identity is empty or length is too short");
return result;
}
std::string deviceId = identity.substr(0, DEVICEID_LENGTH);
int64_t token = std::stoll(identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH));
int callerUid = static_cast<int>((static_cast<uint64_t>(token)) >> PID_LEN);
int callerPid = static_cast<int>(token);
napi_value napiCallingPid;
napi_create_int32(env, callerPid, &napiCallingPid);
napi_set_named_property(env, global, "callingPid_", napiCallingPid);
napi_value napiCallingUid;
napi_create_int32(env, callerUid, &napiCallingUid);
napi_set_named_property(env, global, "callingUid_", napiCallingUid);
napi_value napiCallingDeviceID = nullptr;
napi_create_string_utf8(env, deviceId.c_str(), NAPI_AUTO_LENGTH, &napiCallingDeviceID);
napi_set_named_property(env, global, "callingDeviceID_", napiCallingDeviceID);
return result;
}
}
napi_value NAPI_IPCSkeleton_restoreCallingIdentity(napi_env env, napi_callback_info info)
{
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus != IRemoteInvoker::ACTIVE_INVOKER) {
ZLOGD(LOG_LABEL, "status is not active");
napi_value result = nullptr;
napi_get_undefined(env, &result);
return result;
}
size_t argc = 1;
size_t expectedArgc = 1;
napi_value argv[ARGV_INDEX_1] = { 0 };
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
if (argc != expectedArgc) {
ZLOGE(LOG_LABEL, "requires 1 parameter");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[ARGV_INDEX_0], &valueType);
if (valueType != napi_string) {
ZLOGE(LOG_LABEL, "type mismatch for parameter 1");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
size_t bufferSize = 0;
size_t maxLen = 40960;
napi_get_value_string_utf8(env, argv[ARGV_INDEX_0], nullptr, 0, &bufferSize);
if (bufferSize >= maxLen) {
ZLOGE(LOG_LABEL, "string length too large");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
char stringValue[bufferSize + 1];
size_t jsStringLength = 0;
napi_get_value_string_utf8(env, argv[ARGV_INDEX_0], stringValue, bufferSize + 1, &jsStringLength);
if (jsStringLength != bufferSize) {
ZLOGE(LOG_LABEL, "string length wrong");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
return NAPI_IPCSkeleton_restoreCallingIdentitySetProperty(env, global, stringValue);
}
napi_value NAPI_RemoteObject_queryLocalInterface(napi_env env, napi_callback_info info)
{
size_t argc = 1;
......@@ -1417,13 +1542,13 @@ napi_value NAPI_RemoteObject_sendMessageRequest(napi_env env, napi_callback_info
if (checkArgsResult == nullptr) {
return checkArgsResult;
}
NAPI_MessageParcel *data = nullptr;
NAPI_MessageSequence *data = nullptr;
napi_status status = napi_unwrap(env, argv[1], (void **)&data);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "failed to get data message sequence");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
NAPI_MessageParcel *reply = nullptr;
NAPI_MessageSequence *reply = nullptr;
status = napi_unwrap(env, argv[2], (void **)&reply);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "failed to get data message sequence");
......@@ -1593,8 +1718,10 @@ napi_value NAPIIPCSkeletonExport(napi_env env, napi_value exports)
DECLARE_NAPI_STATIC_FUNCTION("getCallingDeviceID", NAPI_IPCSkeleton_getCallingDeviceID),
DECLARE_NAPI_STATIC_FUNCTION("getLocalDeviceID", NAPI_IPCSkeleton_getLocalDeviceID),
DECLARE_NAPI_STATIC_FUNCTION("isLocalCalling", NAPI_IPCSkeleton_isLocalCalling),
DECLARE_NAPI_STATIC_FUNCTION("flushCmdBuffer", NAPI_IPCSkeleton_flushCmdBuffer),
DECLARE_NAPI_STATIC_FUNCTION("flushCommands", NAPI_IPCSkeleton_flushCommands),
DECLARE_NAPI_STATIC_FUNCTION("resetCallingIdentity", NAPI_IPCSkeleton_resetCallingIdentity),
DECLARE_NAPI_STATIC_FUNCTION("restoreCallingIdentity", NAPI_IPCSkeleton_restoreCallingIdentity),
DECLARE_NAPI_STATIC_FUNCTION("setCallingIdentity", NAPI_IPCSkeleton_setCallingIdentity),
DECLARE_NAPI_STATIC_FUNCTION("getCallingTokenId", NAPI_IPCSkeleton_getCallingTokenId),
};
......
......@@ -31,6 +31,7 @@
#include "log_tags.h"
#include "napi_message_option.h"
#include "napi_message_parcel.h"
#include "napi_message_sequence.h"
#include "napi_rpc_error.h"
#include "rpc_bytrace.h"
#include "string_ex.h"
......@@ -43,10 +44,10 @@ static const uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag.
static NapiError napiErr;
static const int ARGV_INDEX_0 = 0;
static const int ARGV_INDEX_1 = 1;
static const int ARGV_INDEX_2 = 2;
static const int ARGV_INDEX_3 = 3;
static const size_t ARGV_INDEX_0 = 0;
static const size_t ARGV_INDEX_1 = 1;
static const size_t ARGV_INDEX_2 = 2;
static const size_t ARGV_INDEX_3 = 3;
NAPIDeathRecipient::NAPIDeathRecipient(napi_env env, napi_value jsDeathRecipient)
{
......@@ -415,8 +416,8 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info)
napi_value NAPI_RemoteProxy_checkSendMessageRequestArgs(napi_env env,
napi_value* argv,
NAPI_MessageParcel* &data,
NAPI_MessageParcel* &reply,
NAPI_MessageSequence* &data,
NAPI_MessageSequence* &reply,
MessageOption* &option)
{
napi_valuetype valueType = napi_null;
......@@ -473,8 +474,8 @@ napi_value NAPI_RemoteProxy_sendMessageRequest(napi_env env, napi_callback_info
ZLOGE(LOG_LABEL, "requires 4 or 5 parameters");
return napiErr.ThrowError(env, errorDesc::VERIFY_PARAM_FAILED);
}
NAPI_MessageParcel *data = nullptr;
NAPI_MessageParcel *reply = nullptr;
NAPI_MessageSequence *data = nullptr;
NAPI_MessageSequence *reply = nullptr;
MessageOption *option = nullptr;
napi_value checkArgsResult = NAPI_RemoteProxy_checkSendMessageRequestArgs(env, argv, data, reply, option);
if (checkArgsResult == nullptr) {
......
......@@ -18,6 +18,7 @@
#include "log_tags.h"
#include "napi_ashmem.h"
#include "napi_message_parcel.h"
#include "napi_message_sequence.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "napi_remote_object.h"
......@@ -27,6 +28,7 @@ EXTERN_C_START
static napi_value rpcExport(napi_env env, napi_value exports)
{
NAPI_MessageParcel::Export(env, exports);
NAPI_MessageSequence::Export(env, exports);
NAPIAshmem::AshmemExport(env, exports);
NAPIIPCSkeletonExport(env, exports);
NAPIRemoteObjectExport(env, exports);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册