提交 2f2850d7 编写于 作者: Y Yippo

Description:add ashmem api errorProcess method

Feature or Bugfix:add ashmem api errorProcess method
Binary Source: No
Signed-off-by: NYippo <liuyibo12@huawei.com>
上级 832d0b4f
......@@ -35,6 +35,7 @@ ohos_shared_library("rpc") {
"$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_remote_object.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_rpc_error.cpp",
"$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_rpc_native_module.cpp",
]
......
......@@ -46,15 +46,20 @@ private:
static napi_value Ashmem_JS_Constructor(napi_env env, napi_callback_info cbinfo);
static napi_value CloseAshmem(napi_env env, napi_callback_info info);
static napi_value CreateAshmem(napi_env env, napi_callback_info info);
static napi_value Create(napi_env env, napi_callback_info info);
static napi_value CreateAshmemFromExisting(napi_env env, napi_callback_info info);
static napi_value GetAshmemSize(napi_env env, napi_callback_info info);
static napi_value MapAshmem(napi_env env, napi_callback_info info);
static napi_value MapTypedAshmem(napi_env env, napi_callback_info info);
static napi_value MapReadAndWriteAshmem(napi_env env, napi_callback_info info);
static napi_value MapReadOnlyAshmem(napi_env env, napi_callback_info info);
static napi_value ReadFromAshmem(napi_env env, napi_callback_info info);
static napi_value SetProtection(napi_env env, napi_callback_info info);
static napi_value UnmapAshmem(napi_env env, napi_callback_info info);
static napi_value WriteToAshmem(napi_env env, napi_callback_info info);
static napi_value GetAshmemFromExisting(napi_env env, napi_callback_info info);
static napi_value GetAshmemConstructor(napi_env env, napi_value* info);
static napi_value getNewAshmemConstructor(napi_env env, napi_value& info, int32_t fd, uint32_t size);
sptr<Ashmem> ashmem_;
};
} // namespace OHOS
......
/*
* 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 OHOS_NAPI_ERROR_H
#define OHOS_NAPI_ERROR_H
#include<string>
#include<map>
#include "napi/native_node_api.h"
namespace OHOS {
enum errorDesc {
VERIFY_PARAM_FAILED,
OS_MMAP_ERROR,
OS_IOCTL_ERROR,
WRITE_TO_ASHMEM_ERROR,
READ_FROM_ASHMEM_ERROR,
JUST_PROXY_OBJECT_PERMITTED_ERROR,
JUST_REMOTE_OBJECT_PERMITTED_ERROR,
COMMUNICATION_ERROR,
PROXY_OR_REMOTE_OBJECT_INVALID_ERROR,
WRITE_DATA_TO_MESSAGE_PARCEL_ERROR,
READ_DATA_FROM_MESSAGE_PARCEL_ERROR,
PARCEL_MEMORY_ALLOC_ERROR,
CALL_JS_METHOD_ERROR,
OS_DUP_ERROR
};
typedef struct errorInfo {
int errorCode;
std::string errorMsg;
} errorInfo;
class NapiError {
public:
NapiError() {};
NapiError(int32_t errorCode) : errorCode_(errorCode) {};
napi_value GetError(napi_env& env) const;
napi_value ThrowError(napi_env& env, int32_t errorCode = -1);
inline void Error(int32_t errorCode)
{
errorCode_ = (errorCode != -1) ? errorCode : errorCode_;
};
inline bool IsError() const
{
return errorCode_ != -1;
};
private:
int32_t errorCode_{-1};
static std::map<int32_t, errorInfo> napiErrMap_;
}; // NapiError
} // namespace OHOS
#endif // OHOS_NAPI_ERROR_H
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2021-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
......@@ -18,6 +18,7 @@
#include "ipc_debug.h"
#include "log_tags.h"
#include "securec.h"
#include "napi_rpc_error.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_ashmem" };
......@@ -25,6 +26,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC,
static constexpr int MMAP_PROT_MAX = NAPIAshmem::PROT_EXEC | NAPIAshmem::PROT_READ | NAPIAshmem::PROT_WRITE;
constexpr size_t BYTE_SIZE_32 = 4;
OHOS::NapiError napiErr;
NAPIAshmem::NAPIAshmem(sptr<Ashmem> &ashmem) : ashmem_(ashmem)
{
if (ashmem == nullptr) {
......@@ -120,6 +123,138 @@ napi_value NAPIAshmem::CreateAshmemFromExisting(napi_env env, napi_callback_info
return jsAshmem;
}
napi_value NAPIAshmem::Create(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
size_t argc = 2;
size_t argcExistingAshmem = 1;
size_t argcAshmem = 2;
napi_value argv[2] = { 0 };
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
if (!(argc == argcExistingAshmem || argc == argcAshmem)) {
ZLOGE(LOG_LABEL, "requires 1 or 2 parameter");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
if (argc == argcExistingAshmem) {
return GetAshmemFromExisting(env, info);
}
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
if (valueType != napi_string) {
ZLOGE(LOG_LABEL, "type mismatch for parameter 1");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
size_t bufferSize = 0;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize);
if (bufferSize <= 0) {
ZLOGE(LOG_LABEL, "invalid ashmem name");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
napi_typeof(env, argv[1], &valueType);
if (valueType != napi_number) {
ZLOGE(LOG_LABEL, "type mismatch for parameter 2");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
int32_t ashmemSize = 0;
napi_get_value_int32(env, argv[1], &ashmemSize);
if (ashmemSize <= 0) {
ZLOGE(LOG_LABEL, "invalid ashmem size");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
return GetAshmemConstructor(env, argv);
}
napi_value NAPIAshmem::GetAshmemConstructor(napi_env env, napi_value* argv)
{
napi_value global = nullptr;
napi_status status = napi_get_global(env, &global);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "get napi global failed");
return nullptr;
}
napi_value constructor = nullptr;
status = napi_get_named_property(env, global, "AshmemConstructor_", &constructor);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "get Ashmem constructor failed");
return nullptr;
}
napi_value jsAshmem;
status = napi_new_instance(env, constructor, 2, argv, &jsAshmem);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "failed to construct js Ashmem");
return nullptr;
}
return jsAshmem;
}
napi_value NAPIAshmem::GetAshmemFromExisting(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_value global = nullptr;
napi_status status = napi_get_global(env, &global);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "get napi global failed");
return nullptr;
}
napi_value constructor = nullptr;
status = napi_get_named_property(env, global, "AshmemConstructor_", &constructor);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "get Ashmem constructor failed");
return nullptr;
}
bool isAshmem = false;
napi_instanceof(env, argv[0], constructor, &isAshmem);
if (isAshmem == false) {
ZLOGE(LOG_LABEL, "parameter is not instanceof Ashmem");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
NAPIAshmem *napiAshmem = nullptr;
napi_unwrap(env, argv[0], (void **)&napiAshmem);
if (napiAshmem == nullptr) {
ZLOGE(LOG_LABEL, "napiAshmem is null");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
int32_t fd = napiAshmem->GetAshmem()->GetAshmemFd();
uint32_t size = (uint32_t)(napiAshmem->GetAshmem()->GetAshmemSize());
if (!((fd > 0) && (size > 0))) {
ZLOGE(LOG_LABEL, "fd <= 0 or size <= 0");
return nullptr;
}
return getNewAshmemConstructor(env, constructor, fd, size);
}
napi_value NAPIAshmem::getNewAshmemConstructor(napi_env env, napi_value& constructor, int32_t fd, uint32_t size)
{
sptr<Ashmem> newAshmem(new Ashmem(dup(fd), size));
if (newAshmem == nullptr) {
ZLOGE(LOG_LABEL, "newAshmem is null");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
napi_value jsAshmem = nullptr;
napi_status status = napi_new_instance(env, constructor, 0, nullptr, &jsAshmem);
if (status != napi_ok) {
ZLOGE(LOG_LABEL, "failed to construct js Ashmem");
return nullptr;
}
NAPIAshmem *newNapiAshmem = nullptr;
napi_unwrap(env, jsAshmem, (void **)&newNapiAshmem);
if (newNapiAshmem == nullptr) {
ZLOGE(LOG_LABEL, "newNapiAshmem is null");
return nullptr;
}
newNapiAshmem->SetAshmem(newAshmem);
return jsAshmem;
}
napi_value NAPIAshmem::GetAshmemSize(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
......@@ -155,6 +290,45 @@ napi_value NAPIAshmem::MapAshmem(napi_env env, napi_callback_info info)
return napiValue;
}
napi_value NAPIAshmem::MapTypedAshmem(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
size_t argc = 1;
napi_value argv[1] = {0};
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
if (argc != 1) {
ZLOGE(LOG_LABEL, "requires 1 parameter");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
if (valueType != napi_number) {
ZLOGE(LOG_LABEL, "type mismatch for parameter 1");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
uint32_t mapType = 0;
napi_get_value_uint32(env, argv[0], &mapType);
if (mapType > MMAP_PROT_MAX) {
ZLOGE(LOG_LABEL, "napiAshmem mapType error");
return napiErr.ThrowError(env, OHOS::errorDesc::VERIFY_PARAM_FAILED);
}
NAPIAshmem *napiAshmem = nullptr;
napi_unwrap(env, thisVar, (void **)&napiAshmem);
if (napiAshmem == nullptr) {
ZLOGE(LOG_LABEL, "napiAshmem is null");
return napiErr.ThrowError(env, OHOS::errorDesc::OS_MMAP_ERROR);
}
bool result = napiAshmem->GetAshmem()->MapAshmem(mapType);
napi_value napiValue = nullptr;
NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
if (result == false) {
ZLOGE(LOG_LABEL, "get os mmap failed");
return napiErr.ThrowError(env, OHOS::errorDesc::OS_MMAP_ERROR);
}
return napiValue;
}
napi_value NAPIAshmem::MapReadAndWriteAshmem(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
......@@ -318,10 +492,12 @@ napi_value NAPIAshmem::AshmemExport(napi_env env, napi_value exports)
napi_create_int32(env, NAPIAshmem::PROT_WRITE, &write);
napi_property_descriptor properties[] = {
DECLARE_NAPI_STATIC_FUNCTION("createAshmem", NAPIAshmem::CreateAshmem),
DECLARE_NAPI_STATIC_FUNCTION("create", NAPIAshmem::Create),
DECLARE_NAPI_STATIC_FUNCTION("createAshmemFromExisting", NAPIAshmem::CreateAshmemFromExisting),
DECLARE_NAPI_FUNCTION("closeAshmem", NAPIAshmem::CloseAshmem),
DECLARE_NAPI_FUNCTION("getAshmemSize", NAPIAshmem::GetAshmemSize),
DECLARE_NAPI_FUNCTION("mapAshmem", NAPIAshmem::MapAshmem),
DECLARE_NAPI_FUNCTION("mapTypedAshmem", NAPIAshmem::MapTypedAshmem),
DECLARE_NAPI_FUNCTION("mapReadAndWriteAshmem", NAPIAshmem::MapReadAndWriteAshmem),
DECLARE_NAPI_FUNCTION("mapReadOnlyAshmem", NAPIAshmem::MapReadOnlyAshmem),
DECLARE_NAPI_FUNCTION("readFromAshmem", NAPIAshmem::ReadFromAshmem),
......
/*
* 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.
*/
#include "napi_rpc_error.h"
#include "ipc_debug.h"
#include "log_tags.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_rpc_error" };
std::map<int, errorInfo> NapiError::napiErrMap_ {
{VERIFY_PARAM_FAILED, errorInfo{401, "verify param failed"}},
{OS_MMAP_ERROR, errorInfo{1900001, "os mmap function failed"}},
{OS_IOCTL_ERROR, errorInfo{1900002, "os ioctl function failed"}},
{WRITE_TO_ASHMEM_ERROR, errorInfo{1900003, "write to ashmem failed"}},
{READ_FROM_ASHMEM_ERROR, errorInfo{1900004, "read from ashmem failed"}},
{JUST_PROXY_OBJECT_PERMITTED_ERROR, errorInfo{1900005, "just proxy object permitted"}},
{JUST_REMOTE_OBJECT_PERMITTED_ERROR, errorInfo{1900006, "os mmap function failed"}},
{COMMUNICATION_ERROR, errorInfo{1900007, "communication failed"}},
{PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, errorInfo{1900008, "proxy or remote object is invalid"}},
{WRITE_DATA_TO_MESSAGE_PARCEL_ERROR, errorInfo{1900009, "write data to message parcel failed"}},
{READ_DATA_FROM_MESSAGE_PARCEL_ERROR, errorInfo{1900010, "read data from message parcel failed"}},
{PARCEL_MEMORY_ALLOC_ERROR, errorInfo{1900011, "parcel memory alloc failed"}},
{CALL_JS_METHOD_ERROR, errorInfo{1900012, "call js method failed"}},
{OS_DUP_ERROR, errorInfo{1900013, "os dup function failed"}}
};
napi_value NapiError::GetError(napi_env& env) const
{
napi_value napiError = nullptr;
if (!IsError()) {
napi_get_undefined(env, &napiError);
return napiError;
}
napi_value napiCode = nullptr;
std::string msg = napiErrMap_[errorCode_].errorMsg;
int code = napiErrMap_[errorCode_].errorCode;
std::string codeStr = std::to_string(code);
NAPI_CALL(env, napi_create_string_utf8(env, codeStr.c_str(), codeStr.size(), &napiCode));
napi_value napiMsg = nullptr;
NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.size(), &napiMsg));
NAPI_CALL(env, napi_create_error(env, napiCode, napiMsg, &napiError));
ZLOGD(LOG_LABEL, "throw error code: %{public}d, msg: %{public}s,", code, msg.c_str());
return napiError;
}
napi_value NapiError::ThrowError(napi_env& env, int32_t code)
{
Error(code);
napi_value error = GetError(env);
napi_throw(env, error);
return nullptr;
}
} // OHOS
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册