Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
communication_ipc
提交
a2c4a436
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,发现更多精彩内容 >>
提交
a2c4a436
编写于
10月 08, 2022
作者:
W
wanghaoxu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support RemoteObject transfer between js worker thread.
Signed-off-by:
N
wanghaoxu
<
wanghaoxu1@huawei.com
>
上级
a50910db
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
112 addition
and
8 deletion
+112
-8
interfaces/kits/js/napi/BUILD.gn
interfaces/kits/js/napi/BUILD.gn
+1
-0
ipc/native/src/napi/src/napi_remote_object.cpp
ipc/native/src/napi/src/napi_remote_object.cpp
+111
-8
未找到文件。
interfaces/kits/js/napi/BUILD.gn
浏览文件 @
a2c4a436
...
...
@@ -24,6 +24,7 @@ ohos_shared_library("rpc") {
include_dirs = [
"$SUBSYSTEM_DIR/utils/include",
"//foundation/arkui/napi/interfaces/kits",
"//foundation/arkui/napi/native_engine",
"//utils/system/safwk/native/include",
"$SUBSYSTEM_DIR/ipc/native/c/adapter/include",
"$SUBSYSTEM_DIR/ipc/native/c/adapter/access_token/include",
...
...
ipc/native/src/napi/src/napi_remote_object.cpp
浏览文件 @
a2c4a436
...
...
@@ -15,6 +15,7 @@
#include "napi_remote_object.h"
#include <mutex>
#include <native_value.h>
#include <cstring>
#include <thread>
#include <unistd.h>
...
...
@@ -42,6 +43,12 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC,
static
const
uint64_t
HITRACE_TAG_RPC
=
(
1ULL
<<
46
);
// RPC and IPC tag.
template
<
class
T
>
inline
T
*
ConvertNativeValueTo
(
NativeValue
*
value
)
{
return
(
value
!=
nullptr
)
?
static_cast
<
T
*>
(
value
->
GetInterface
(
T
::
INTERFACE_ID
))
:
nullptr
;
}
static
NapiError
napiErr
;
static
const
size_t
ARGV_INDEX_0
=
0
;
...
...
@@ -104,16 +111,45 @@ public:
void
Set
(
sptr
<
NAPIRemoteObject
>
object
);
void
attachLocalInterface
(
napi_value
localInterface
,
std
::
string
&
descriptor
);
napi_value
queryLocalInterface
(
std
::
string
&
descriptor
);
void
Lock
()
{
mutex_
.
lock
();
};
void
Unlock
()
{
mutex_
.
unlock
();
};
void
IncAttachCount
()
{
++
attachCount_
;
};
int32_t
DecAttachCount
()
{
if
(
attachCount_
>
0
)
{
--
attachCount_
;
}
return
attachCount_
;
};
std
::
u16string
GetDescriptor
()
{
return
descriptor_
;
};
private:
std
::
mutex
mutex_
;
napi_env
env_
=
nullptr
;
std
::
u16string
descriptor_
;
sptr
<
NAPIRemoteObject
>
cachedObject_
;
napi_ref
localInterfaceRef_
;
int32_t
attachCount_
;
};
NAPIRemoteObjectHolder
::
NAPIRemoteObjectHolder
(
napi_env
env
,
const
std
::
u16string
&
descriptor
)
:
env_
(
env
),
descriptor_
(
descriptor
),
cachedObject_
(
nullptr
),
localInterfaceRef_
(
nullptr
)
:
env_
(
env
),
descriptor_
(
descriptor
),
cachedObject_
(
nullptr
),
localInterfaceRef_
(
nullptr
)
,
attachCount_
(
1
)
{}
NAPIRemoteObjectHolder
::~
NAPIRemoteObjectHolder
()
...
...
@@ -169,6 +205,68 @@ napi_value NAPIRemoteObjectHolder::queryLocalInterface(std::string &descriptor)
return
result
;
}
static
void
RemoteObjectHolderFinalizeCb
(
napi_env
env
,
void
*
data
,
void
*
hint
)
{
NAPIRemoteObjectHolder
*
holder
=
reinterpret_cast
<
NAPIRemoteObjectHolder
*>
(
data
);
if
(
holder
==
nullptr
)
{
ZLOGW
(
LOG_LABEL
,
"RemoteObjectHolderFinalizeCb null holder"
);
return
;
}
holder
->
Lock
();
int32_t
curAttachCount
=
holder
->
DecAttachCount
();
if
(
curAttachCount
==
0
)
{
delete
holder
;
}
}
static
void
*
RemoteObjectDetachCb
(
NativeEngine
*
engine
,
void
*
value
,
void
*
hint
)
{
(
void
)
engine
;
(
void
)
hint
;
return
value
;
}
static
NativeValue
*
RemoteObjectAttachCb
(
NativeEngine
*
engine
,
void
*
value
,
void
*
hint
)
{
(
void
)
hint
;
NAPIRemoteObjectHolder
*
holder
=
reinterpret_cast
<
NAPIRemoteObjectHolder
*>
(
value
);
if
(
holder
==
nullptr
)
{
ZLOGE
(
LOG_LABEL
,
"holder is nullptr when attach"
);
return
nullptr
;
}
holder
->
Lock
();
ZLOGI
(
LOG_LABEL
,
"create js remote object when attach"
);
napi_env
env
=
reinterpret_cast
<
napi_env
>
(
engine
);
// retrieve js remote object constructor
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
,
"IPCStubConstructor_"
,
&
constructor
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"get stub constructor failed"
);
NAPI_ASSERT
(
env
,
constructor
!=
nullptr
,
"failed to get js RemoteObject constructor"
);
// retrieve descriptor and it's length
std
::
u16string
descriptor
=
holder
->
GetDescriptor
();
std
::
string
desc
=
Str16ToStr8
(
descriptor
);
napi_value
jsDesc
=
nullptr
;
napi_create_string_utf8
(
env
,
desc
.
c_str
(),
desc
.
length
(),
&
jsDesc
);
// create a new js remote object
size_t
argc
=
1
;
napi_value
argv
[
1
]
=
{
jsDesc
};
napi_value
jsRemoteObject
=
nullptr
;
status
=
napi_new_instance
(
env
,
constructor
,
argc
,
argv
,
&
jsRemoteObject
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"failed to construct js RemoteObject when attach"
);
// retrieve and remove create holder
NAPIRemoteObjectHolder
*
createHolder
=
nullptr
;
status
=
napi_remove_wrap
(
env
,
jsRemoteObject
,
(
void
**
)
&
createHolder
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
&&
createHolder
!=
nullptr
,
"failed to remove create holder when attach"
);
status
=
napi_wrap
(
env
,
jsRemoteObject
,
holder
,
RemoteObjectHolderFinalizeCb
,
nullptr
,
nullptr
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"wrap js RemoteObject and native holder failed when attach"
);
holder
->
IncAttachCount
();
holder
->
Unlock
();
return
reinterpret_cast
<
NativeValue
*>
(
jsRemoteObject
);
}
napi_value
RemoteObject_JS_Constructor
(
napi_env
env
,
napi_callback_info
info
)
{
// new napi remote object
...
...
@@ -191,15 +289,20 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
NAPI_ASSERT
(
env
,
jsStringLength
==
bufferSize
,
"string length wrong"
);
std
::
string
descriptor
=
stringValue
;
auto
holder
=
new
NAPIRemoteObjectHolder
(
env
,
Str8ToStr16
(
descriptor
));
auto
nativeObj
=
ConvertNativeValueTo
<
NativeObject
>
(
reinterpret_cast
<
NativeValue
*>
(
thisVar
));
if
(
nativeObj
==
nullptr
)
{
ZLOGE
(
LOG_LABEL
,
"Failed to get RemoteObject native object"
);
delete
holder
;
return
nullptr
;
}
nativeObj
->
ConvertToNativeBindingObject
(
env
,
RemoteObjectDetachCb
,
RemoteObjectAttachCb
,
holder
,
nullptr
);
// connect native object to js thisVar
napi_status
status
=
napi_wrap
(
env
,
thisVar
,
holder
,
[](
napi_env
env
,
void
*
data
,
void
*
hint
)
{
ZLOGI
(
LOG_LABEL
,
"NAPIRemoteObjectHolder destructed by js callback"
);
delete
(
reinterpret_cast
<
NAPIRemoteObjectHolder
*>
(
data
));
},
nullptr
,
nullptr
);
napi_status
status
=
napi_wrap
(
env
,
thisVar
,
holder
,
RemoteObjectHolderFinalizeCb
,
nullptr
,
nullptr
);
NAPI_ASSERT
(
env
,
status
==
napi_ok
,
"wrap js RemoteObject and native holder failed"
);
if
(
NAPI_ohos_rpc_getNativeRemoteObject
(
env
,
thisVar
)
==
nullptr
)
{
ZLOGE
(
LOG_LABEL
,
"RemoteObject_JS_Constructor create native object failed"
);
return
nullptr
;
}
return
thisVar
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录