Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
communication_ipc
提交
afde1c06
C
communication_ipc
项目概览
OpenHarmony
/
communication_ipc
大约 1 年 前同步成功
通知
20
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
communication_ipc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
afde1c06
编写于
12月 11, 2021
作者:
L
liangshenglin1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix ipc js API 7 interface bugs and implement API 8 interfaces
Signed-off-by:
N
liangshenglin1
<
liangshenglin1@huawei.com
>
上级
af07bf9c
变更
9
展开全部
隐藏空白更改
内联
并排
Showing
9 changed file
with
1152 addition
and
136 deletion
+1152
-136
interfaces/kits/js/napi/BUILD.gn
interfaces/kits/js/napi/BUILD.gn
+1
-2
ipc/native/src/napi/include/napi_ashmem.h
ipc/native/src/napi/include/napi_ashmem.h
+61
-0
ipc/native/src/napi/include/napi_message_parcel.h
ipc/native/src/napi/include/napi_message_parcel.h
+16
-0
ipc/native/src/napi/include/napi_remote_object.h
ipc/native/src/napi/include/napi_remote_object.h
+4
-2
ipc/native/src/napi/src/napi_ashmem.cpp
ipc/native/src/napi/src/napi_ashmem.cpp
+362
-0
ipc/native/src/napi/src/napi_message_option.cpp
ipc/native/src/napi/src/napi_message_option.cpp
+2
-2
ipc/native/src/napi/src/napi_message_parcel.cpp
ipc/native/src/napi/src/napi_message_parcel.cpp
+467
-18
ipc/native/src/napi/src/napi_remote_object.cpp
ipc/native/src/napi/src/napi_remote_object.cpp
+236
-98
ipc/native/src/napi/src/napi_rpc_native_module.cpp
ipc/native/src/napi/src/napi_rpc_native_module.cpp
+3
-14
未找到文件。
interfaces/kits/js/napi/BUILD.gn
浏览文件 @
afde1c06
...
...
@@ -29,6 +29,7 @@ ohos_shared_library("rpc") {
public_configs = [ ":rpc_public_config" ]
sources = [
"$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_remote_object.cpp",
...
...
@@ -44,8 +45,6 @@ ohos_shared_library("rpc") {
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"safwk:system_ability_fwk",
"samgr_L2:samgr_proxy",
]
relative_install_dir = "module"
...
...
ipc/native/src/napi/include/napi_ashmem.h
0 → 100644
浏览文件 @
afde1c06
/*
* Copyright (C) 2021 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_ASHMEM_H
#define NAPI_IPC_OHOS_ASHMEM_H
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "ashmem.h"
namespace
OHOS
{
class
NAPIAshmem
{
public:
enum
{
PROT_EXEC
=
4
,
PROT_NONE
=
0
,
PROT_READ
=
1
,
PROT_WRITE
=
2
,
};
explicit
NAPIAshmem
(
sptr
<
Ashmem
>
&
ashmem
);
NAPIAshmem
()
:
ashmem_
(
nullptr
){}
~
NAPIAshmem
()
=
default
;
sptr
<
Ashmem
>
&
GetAshmem
()
{
return
ashmem_
;
}
void
SetAshmem
(
sptr
<
Ashmem
>
&
ashmem
)
{
ashmem_
=
ashmem
;
}
static
napi_value
AshmemExport
(
napi_env
env
,
napi_value
exports
);
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
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
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
);
sptr
<
Ashmem
>
ashmem_
;
};
}
// namespace OHOS
#endif
\ No newline at end of file
ipc/native/src/napi/include/napi_message_parcel.h
浏览文件 @
afde1c06
...
...
@@ -48,6 +48,8 @@ private:
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
);
...
...
@@ -69,6 +71,7 @@ private:
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
);
...
...
@@ -89,6 +92,19 @@ private:
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
);
...
...
ipc/native/src/napi/include/napi_remote_object.h
浏览文件 @
afde1c06
...
...
@@ -17,11 +17,9 @@
#define NAPI_IPC_OHOS_REMOTE_OBJECT_H
#include "iremote_object.h"
#include "iservice_registry.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "refbase.h"
#include "system_ability_manager_proxy.h"
namespace
OHOS
{
EXTERN_C_START
...
...
@@ -95,6 +93,10 @@ EXTERN_C_END
napi_async_work
asyncWork
;
napi_deferred
deferred
;
int
errCode
;
napi_ref
jsCodeRef
;
napi_ref
jsDataRef
;
napi_ref
jsReplyRef
;
napi_ref
callback
;
napi_env
env
;
};
}
// namespace OHOS
...
...
ipc/native/src/napi/src/napi_ashmem.cpp
0 → 100644
浏览文件 @
afde1c06
/*
* Copyright (C) 2021 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_ashmem.h"
#include <unistd.h>
#include "ipc_debug.h"
#include "log_tags.h"
#include "securec.h"
namespace
OHOS
{
static
constexpr
OHOS
::
HiviewDFX
::
HiLogLabel
LOG_LABEL
=
{
LOG_CORE
,
LOG_ID_IPC
,
"napi_ashmem"
};
#ifndef TITLE
#define TITLE __PRETTY_FUNCTION__
#endif
#define DBINDER_LOGE(fmt, args...) \
(
void
)
OHOS
::
HiviewDFX
::
HiLog
::
Error
(
LOG_LABEL
,
"%{public}s %{public}d: "
fmt
,
TITLE
,
__LINE__
,
##
args
)
#define DBINDER_LOGI(fmt, args...) \
(
void
)
OHOS
::
HiviewDFX
::
HiLog
::
Info
(
LOG_LABEL
,
"%{public}s %{public}d: "
fmt
,
TITLE
,
__LINE__
,
##
args
)
NAPIAshmem
::
NAPIAshmem
(
sptr
<
Ashmem
>
&
ashmem
)
:
ashmem_
(
ashmem
)
{
if
(
ashmem
==
nullptr
)
{
ZLOGE
(
LOG_LABEL
,
"%s: ashmem is null"
,
__func__
);
}
}
napi_value
NAPIAshmem
::
CloseAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
nullptr
,
nullptr
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
napiAshmem
->
GetAshmem
()
->
CloseAshmem
();
napi_value
result
=
nullptr
;
napi_get_undefined
(
env
,
&
result
);
return
result
;
}
napi_value
NAPIAshmem
::
CreateAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
size_t
argc
=
2
;
napi_value
argv
[
2
]
=
{
0
};
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPI_ASSERT
(
env
,
argc
==
2
,
"requires 2 parameter"
);
napi_value
global
=
nullptr
;
napi_status
status
=
napi_get_global
(
env
,
&
global
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get napi global failed"
);
napi_value
constructor
=
nullptr
;
status
=
napi_get_named_property
(
env
,
global
,
"AshmemConstructor_"
,
&
constructor
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get Ashmem constructor failed"
);
napi_value
jsAshmem
;
status
=
napi_new_instance
(
env
,
constructor
,
2
,
argv
,
&
jsAshmem
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"failed to construct js Ashmem"
);
return
jsAshmem
;
}
napi_value
NAPIAshmem
::
CreateAshmemFromExisting
(
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_ASSERT
(
env
,
argc
==
1
,
"requires 1 parameter"
);
napi_value
global
=
nullptr
;
napi_status
status
=
napi_get_global
(
env
,
&
global
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get napi global failed"
);
napi_value
constructor
=
nullptr
;
status
=
napi_get_named_property
(
env
,
global
,
"AshmemConstructor_"
,
&
constructor
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get Ashmem constructor failed"
);
bool
isAshmem
=
false
;
napi_instanceof
(
env
,
argv
[
0
],
constructor
,
&
isAshmem
);
NAPI_ASSERT
(
env
,
isAshmem
==
true
,
"parameter is not instanceof Ashmem"
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
argv
[
0
],
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
int32_t
fd
=
napiAshmem
->
GetAshmem
()
->
GetAshmemFd
();
uint32_t
size
=
napiAshmem
->
GetAshmem
()
->
GetAshmemSize
();
NAPI_ASSERT
(
env
,
(
fd
>
0
)
&&
(
size
>
0
),
"fd <= 0 or size <= 0"
);
sptr
<
Ashmem
>
newAshmem
(
new
Ashmem
(
dup
(
fd
),
size
));
NAPI_ASSERT
(
env
,
newAshmem
!=
nullptr
,
"napiAshmem is null"
);
napi_value
jsAshmem
=
nullptr
;
status
=
napi_new_instance
(
env
,
constructor
,
0
,
nullptr
,
&
jsAshmem
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"failed to construct js Ashmem"
);
NAPIAshmem
*
newNapiAshmem
=
nullptr
;
napi_unwrap
(
env
,
jsAshmem
,
(
void
**
)
&
newNapiAshmem
);
NAPI_ASSERT
(
env
,
newNapiAshmem
!=
nullptr
,
"newNapiAshmem is null"
);
newNapiAshmem
->
SetAshmem
(
newAshmem
);
return
jsAshmem
;
}
napi_value
NAPIAshmem
::
GetAshmemSize
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
nullptr
,
nullptr
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
int32_t
ashmemSize
=
napiAshmem
->
GetAshmem
()
->
GetAshmemSize
();
napi_value
napiValue
;
napi_create_uint32
(
env
,
ashmemSize
,
&
napiValue
);
return
napiValue
;
}
napi_value
NAPIAshmem
::
MapAshmem
(
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
);
NAPI_ASSERT
(
env
,
argc
==
1
,
"requires 1 parameter"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 1"
);
uint32_t
mapType
=
0
;
napi_get_value_uint32
(
env
,
argv
[
0
],
&
mapType
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
bool
result
=
napiAshmem
->
GetAshmem
()
->
MapAshmem
(
mapType
);
napi_value
napiValue
=
nullptr
;
NAPI_CALL
(
env
,
napi_get_boolean
(
env
,
result
,
&
napiValue
));
return
napiValue
;
}
napi_value
NAPIAshmem
::
MapReadAndWriteAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
nullptr
,
nullptr
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
bool
result
=
napiAshmem
->
GetAshmem
()
->
MapReadAndWriteAshmem
();
napi_value
napiValue
=
nullptr
;
NAPI_CALL
(
env
,
napi_get_boolean
(
env
,
result
,
&
napiValue
));
return
napiValue
;
}
napi_value
NAPIAshmem
::
MapReadOnlyAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
nullptr
,
nullptr
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
bool
result
=
napiAshmem
->
GetAshmem
()
->
MapReadOnlyAshmem
();
napi_value
napiValue
=
nullptr
;
NAPI_CALL
(
env
,
napi_get_boolean
(
env
,
result
,
&
napiValue
));
return
napiValue
;
}
napi_value
NAPIAshmem
::
ReadFromAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
size_t
argc
=
2
;
napi_value
argv
[
2
]
=
{
0
};
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPI_ASSERT
(
env
,
argc
==
2
,
"requires 2 parameter"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 1"
);
napi_typeof
(
env
,
argv
[
1
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 2"
);
uint32_t
size
=
0
;
uint32_t
offset
=
0
;
napi_get_value_uint32
(
env
,
argv
[
0
],
&
size
);
napi_get_value_uint32
(
env
,
argv
[
1
],
&
offset
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
const
void
*
result
=
napiAshmem
->
GetAshmem
()
->
ReadFromAshmem
(
size
,
offset
);
if
(
result
==
nullptr
)
{
DBINDER_LOGE
(
"ashmem->ReadFromAshmem returns null"
);
return
nullptr
;
}
// c++ byte[] to js []
napi_value
arrayBuffer
=
nullptr
;
void
*
arrayBufferPtr
=
nullptr
;
napi_create_arraybuffer
(
env
,
size
,
&
arrayBufferPtr
,
&
arrayBuffer
);
napi_value
typedarray
=
nullptr
;
napi_create_typedarray
(
env
,
napi_int8_array
,
size
,
arrayBuffer
,
0
,
&
typedarray
);
bool
isTypedArray
=
false
;
napi_is_typedarray
(
env
,
typedarray
,
&
isTypedArray
);
NAPI_ASSERT
(
env
,
isTypedArray
==
true
,
"create TypedArray failed"
);
if
(
size
==
0
)
{
return
typedarray
;
}
errno_t
status
=
memcpy_s
(
arrayBufferPtr
,
size
,
result
,
size
);
NAPI_ASSERT
(
env
,
status
==
EOK
,
"memcpy_s is failed"
);
return
typedarray
;
}
napi_value
NAPIAshmem
::
SetProtection
(
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
);
NAPI_ASSERT
(
env
,
argc
==
1
,
"requires 1 parameter"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 1"
);
uint32_t
protectionType
=
0
;
napi_get_value_uint32
(
env
,
argv
[
0
],
&
protectionType
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
bool
result
=
napiAshmem
->
GetAshmem
()
->
SetProtection
(
protectionType
);
napi_value
napiValue
=
nullptr
;
NAPI_CALL
(
env
,
napi_get_boolean
(
env
,
result
,
&
napiValue
));
return
napiValue
;
}
napi_value
NAPIAshmem
::
UnmapAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
nullptr
,
nullptr
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
napiAshmem
->
GetAshmem
()
->
UnmapAshmem
();
napi_value
result
=
nullptr
;
napi_get_undefined
(
env
,
&
result
);
return
result
;
}
napi_value
NAPIAshmem
::
WriteToAshmem
(
napi_env
env
,
napi_callback_info
info
)
{
size_t
argc
=
3
;
napi_value
argv
[
3
]
=
{
0
};
napi_value
thisVar
=
nullptr
;
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPI_ASSERT
(
env
,
argc
==
3
,
"requires 1 parameter"
);
bool
isTypedArray
=
false
;
napi_is_typedarray
(
env
,
argv
[
0
],
&
isTypedArray
);
NAPI_ASSERT
(
env
,
isTypedArray
==
true
,
"type mismatch for parameter 1"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
1
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 2"
);
napi_typeof
(
env
,
argv
[
2
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 3"
);
napi_typedarray_type
typedarrayType
=
napi_uint8_array
;
size_t
typedarrayLength
=
0
;
void
*
typedarrayBufferPtr
=
nullptr
;
napi_value
tmpArrayBuffer
=
nullptr
;
size_t
byteOffset
=
0
;
napi_get_typedarray_info
(
env
,
argv
[
0
],
&
typedarrayType
,
&
typedarrayLength
,
&
typedarrayBufferPtr
,
&
tmpArrayBuffer
,
&
byteOffset
);
NAPI_ASSERT
(
env
,
typedarrayType
==
napi_int8_array
,
"array type mismatch for parameter 1"
);
DBINDER_LOGI
(
"ashmem WriteBuffer typedarrayLength = %{public}d"
,
(
int
)(
typedarrayLength
));
uint32_t
size
=
0
;
napi_get_value_uint32
(
env
,
argv
[
1
],
&
size
);
uint32_t
offset
=
0
;
napi_get_value_uint32
(
env
,
argv
[
2
],
&
offset
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
napi_unwrap
(
env
,
thisVar
,
(
void
**
)
&
napiAshmem
);
NAPI_ASSERT
(
env
,
napiAshmem
!=
nullptr
,
"napiAshmem is null"
);
// need check size offset and capacity
bool
result
=
napiAshmem
->
GetAshmem
()
->
WriteToAshmem
(
typedarrayBufferPtr
,
size
,
offset
);
napi_value
napiValue
=
nullptr
;
NAPI_CALL
(
env
,
napi_get_boolean
(
env
,
result
,
&
napiValue
));
return
napiValue
;
}
napi_value
NAPIAshmem
::
AshmemExport
(
napi_env
env
,
napi_value
exports
)
{
const
std
::
string
className
=
"Ashmem"
;
napi_value
exec
=
nullptr
;
napi_create_int32
(
env
,
NAPIAshmem
::
PROT_EXEC
,
&
exec
);
napi_value
none
=
nullptr
;
napi_create_int32
(
env
,
NAPIAshmem
::
PROT_NONE
,
&
none
);
napi_value
read
=
nullptr
;
napi_create_int32
(
env
,
NAPIAshmem
::
PROT_READ
,
&
read
);
napi_value
write
=
nullptr
;
napi_create_int32
(
env
,
NAPIAshmem
::
PROT_WRITE
,
&
write
);
napi_property_descriptor
properties
[]
=
{
DECLARE_NAPI_STATIC_FUNCTION
(
"createAshmem"
,
NAPIAshmem
::
CreateAshmem
),
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
(
"mapReadAndWriteAshmem"
,
NAPIAshmem
::
MapReadAndWriteAshmem
),
DECLARE_NAPI_FUNCTION
(
"mapReadOnlyAshmem"
,
NAPIAshmem
::
MapReadOnlyAshmem
),
DECLARE_NAPI_FUNCTION
(
"readFromAshmem"
,
NAPIAshmem
::
ReadFromAshmem
),
DECLARE_NAPI_FUNCTION
(
"setProtection"
,
NAPIAshmem
::
SetProtection
),
DECLARE_NAPI_FUNCTION
(
"unmapAshmem"
,
NAPIAshmem
::
UnmapAshmem
),
DECLARE_NAPI_FUNCTION
(
"writeToAshmem"
,
NAPIAshmem
::
WriteToAshmem
),
DECLARE_NAPI_STATIC_PROPERTY
(
"PROT_EXEC"
,
exec
),
DECLARE_NAPI_STATIC_PROPERTY
(
"PROT_NONE"
,
none
),
DECLARE_NAPI_STATIC_PROPERTY
(
"PROT_READ"
,
read
),
DECLARE_NAPI_STATIC_PROPERTY
(
"PROT_WRITE"
,
write
),
};
napi_value
constructor
=
nullptr
;
napi_define_class
(
env
,
className
.
c_str
(),
className
.
length
(),
Ashmem_JS_Constructor
,
nullptr
,
sizeof
(
properties
)
/
sizeof
(
properties
[
0
]),
properties
,
&
constructor
);
NAPI_ASSERT
(
env
,
constructor
!=
nullptr
,
"define js class Ashmem failed"
);
napi_status
status
=
napi_set_named_property
(
env
,
exports
,
"Ashmem"
,
constructor
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"set property Ashmem failed"
);
napi_value
global
=
nullptr
;
status
=
napi_get_global
(
env
,
&
global
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get napi global failed"
);
status
=
napi_set_named_property
(
env
,
global
,
"AshmemConstructor_"
,
constructor
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"set Ashmem constructor failed"
);
return
exports
;
}
napi_value
NAPIAshmem
::
Ashmem_JS_Constructor
(
napi_env
env
,
napi_callback_info
info
)
{
napi_value
thisVar
=
nullptr
;
size_t
argc
=
2
;
napi_value
argv
[
2
]
=
{
0
};
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPIAshmem
*
napiAshmem
=
nullptr
;
if
(
argc
==
0
)
{
napiAshmem
=
new
NAPIAshmem
();
}
else
{
NAPI_ASSERT
(
env
,
argc
==
2
,
"requires 2 parameter"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_string
,
"type mismatch for parameter 1"
);
napi_typeof
(
env
,
argv
[
1
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 2"
);
size_t
bufferSize
=
0
;
size_t
maxLen
=
40960
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
bufferSize
);
NAPI_ASSERT
(
env
,
bufferSize
<
maxLen
,
"string length too large"
);
char
stringValue
[
bufferSize
+
1
];
size_t
jsStringLength
=
0
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
stringValue
,
bufferSize
+
1
,
&
jsStringLength
);
NAPI_ASSERT
(
env
,
jsStringLength
==
bufferSize
,
"string length wrong"
);
std
::
string
ashmemName
=
stringValue
;
uint32_t
ashmemSize
=
0
;
napi_get_value_uint32
(
env
,
argv
[
1
],
&
ashmemSize
);
// new napi Ashmem
sptr
<
Ashmem
>
nativeAshmem
=
Ashmem
::
CreateAshmem
(
ashmemName
.
c_str
(),
ashmemSize
);
NAPI_ASSERT
(
env
,
nativeAshmem
!=
nullptr
,
"invalid parameters"
);
napiAshmem
=
new
NAPIAshmem
(
nativeAshmem
);
}
// connect native object to js thisVar
napi_status
status
=
napi_wrap
(
env
,
thisVar
,
napiAshmem
,
[](
napi_env
env
,
void
*
data
,
void
*
hint
)
{
DBINDER_LOGI
(
"Ashmem destructed by js callback"
);
delete
(
reinterpret_cast
<
NAPIAshmem
*>
(
data
));
},
nullptr
,
nullptr
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"wrap js Ashmem and native holder failed"
);
return
thisVar
;
}
}
ipc/native/src/napi/src/napi_message_option.cpp
浏览文件 @
afde1c06
...
...
@@ -45,7 +45,7 @@ napi_value NapiOhosRpcMessageOptionSetFlags(napi_env env, napi_callback_info inf
size_t
argc
=
1
;
napi_value
argv
[
1
]
=
{
0
};
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPI_ASSERT
(
env
,
thisVar
!=
nullptr
&&
argv
[
0
]
!=
nullptr
,
"failed to get js message option object"
);
NAPI_ASSERT
(
env
,
thisVar
!=
nullptr
,
"failed to get js message option object"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 1"
);
...
...
@@ -88,7 +88,7 @@ napi_value NapiOhosRpcMessageOptionSetWaittime(napi_env env, napi_callback_info
size_t
argc
=
1
;
napi_value
argv
[
1
]
=
{
0
};
napi_get_cb_info
(
env
,
info
,
&
argc
,
argv
,
&
thisVar
,
nullptr
);
NAPI_ASSERT
(
env
,
thisVar
!=
nullptr
&&
argv
[
0
]
!=
nullptr
,
"failed to get js message option object"
);
NAPI_ASSERT
(
env
,
thisVar
!=
nullptr
,
"failed to get js message option object"
);
napi_valuetype
valueType
=
napi_null
;
napi_typeof
(
env
,
argv
[
0
],
&
valueType
);
NAPI_ASSERT
(
env
,
valueType
==
napi_number
,
"type mismatch for parameter 1"
);
...
...
ipc/native/src/napi/src/napi_message_parcel.cpp
浏览文件 @
afde1c06
此差异已折叠。
点击以展开。
ipc/native/src/napi/src/napi_remote_object.cpp
浏览文件 @
afde1c06
此差异已折叠。
点击以展开。
ipc/native/src/napi/src/napi_rpc_native_module.cpp
浏览文件 @
afde1c06
...
...
@@ -16,6 +16,7 @@
#include <unistd.h>
#include "hilog/log.h"
#include "log_tags.h"
#include "napi_ashmem.h"
#include "napi_message_parcel.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
...
...
@@ -26,6 +27,7 @@ EXTERN_C_START
static
napi_value
rpcExport
(
napi_env
env
,
napi_value
exports
)
{
NAPI_MessageParcel
::
Export
(
env
,
exports
);
NAPIAshmem
::
AshmemExport
(
env
,
exports
);
NAPIIPCSkeletonExport
(
env
,
exports
);
NAPIRemoteObjectExport
(
env
,
exports
);
NAPIRemoteProxyExport
(
env
,
exports
);
...
...
@@ -34,21 +36,8 @@ static napi_value rpcExport(napi_env env, napi_value exports)
}
EXTERN_C_END
static
napi_module
RPCModule_
=
{
.
nm_version
=
1
,
.
nm_flags
=
0
,
.
nm_filename
=
nullptr
,
.
nm_register_func
=
rpcExport
,
.
nm_modname
=
"rpc"
,
.
nm_priv
=
((
void
*
)
0
),
.
reserved
=
{
0
}
};
/*
* Module register function
*/
extern
"C"
__attribute__
((
constructor
))
void
RegisterModule
(
void
)
{
napi_module_register
(
&
RPCModule_
);
}
NAPI_MODULE
(
rpc
,
rpcExport
)
}
// namesapce OHOS
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录