提交 594cb879 编写于 作者: L liangshenglin1

implement sendRequest for RemoteObject

Signed-off-by: Nliangshenglin1 <liangshenglin1@huawei.com>
上级 72347a73
......@@ -28,6 +28,12 @@ struct RefCountNode {
class IPCObjectStub : public IRemoteObject {
public:
enum {
OBJECT_TYPE_NATIVE,
OBJECT_TYPE_JAVA,
OBJECT_TYPE_JAVASCRIPT,
};
explicit IPCObjectStub(std::u16string descriptor = nullptr);
~IPCObjectStub();
......@@ -60,6 +66,8 @@ public:
virtual int32_t ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
virtual int GetObjectType() const;
#ifndef CONFIG_IPC_SINGLE
int32_t InvokerThread(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
......
......@@ -289,6 +289,11 @@ pid_t IPCObjectStub::GetCallingUid()
return IPCSkeleton::GetCallingUid();
}
int IPCObjectStub::GetObjectType() const
{
return OBJECT_TYPE_NATIVE;
}
int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
int result = ERR_NONE;
......
......@@ -70,6 +70,8 @@ public:
bool CheckObjectLegality() const override;
int GetObjectType() const override;
int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
int OnRemoteDump(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
......@@ -160,6 +162,11 @@ bool JRemoteObject::CheckObjectLegality() const
return true;
}
int JRemoteObject::GetObjectType() const
{
return OBJECT_TYPE_JAVA;
}
JRemoteObject::~JRemoteObject()
{
JNIEnvHelper env;
......@@ -379,9 +386,13 @@ jobject Java_ohos_rpc_getJavaRemoteObject(JNIEnv *env, const sptr<IRemoteObject>
}
if (target->CheckObjectLegality()) {
ZLOGI(LABEL, "native Get RemoteObject");
auto object = static_cast<JRemoteObject *>(target.GetRefPtr());
return object->GetJObject();
IPCObjectStub *tmp = static_cast<IPCObjectStub *>(target.GetRefPtr());
ZLOGW(LABEL, "object type:%{public}d", tmp->GetObjectType());
if (tmp->GetObjectType() == IPCObjectStub::OBJECT_TYPE_JAVA) {
ZLOGW(LABEL, "native Get Java RemoteObject");
auto object = static_cast<JRemoteObject *>(tmp);
return object->GetJObject();
}
}
std::lock_guard<std::mutex> lockGuard(g_proxyMutex_);
......
......@@ -28,9 +28,8 @@ class NAPI_MessageParcel {
public:
NAPI_MessageParcel(napi_env env, napi_value thisVar, MessageParcel *parcel);
virtual ~NAPI_MessageParcel();
MessageParcel *GetMessageParcel();
std::shared_ptr<MessageParcel> GetMessageParcel();
static napi_value Export(napi_env env, napi_value exports);
static napi_ref GetParcelConsRef();
private:
// Napi methods and properties
static napi_value JS_constructor(napi_env env, napi_callback_info cbinfo);
......@@ -93,14 +92,12 @@ private:
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 void release(MessageParcel *parcel);
napi_env env_ = nullptr;
MessageParcel *nativeParcel_ = nullptr;
size_t maxCapacityToWrite_;
bool owner;
std::shared_ptr<MessageParcel> nativeParcel_ = nullptr;
size_t maxCapacityToWrite_;
};
namespace {
napi_value g_messageParcelConstructor = nullptr;
napi_ref g_messageParcelConsRef = nullptr;
}
} // namespace OHOS
#endif // NAPI_IPC_OHOS_MESSAGE_PARCEL_H
......@@ -59,9 +59,11 @@ EXTERN_C_END
napi_value NAPI_RemoteObject_getCallingUid(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info);
// RemoteProxy napi methods
napi_value SendRequestPromise(napi_env env, sptr<IRemoteObject> target, uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option);
std::shared_ptr<MessageParcel> data, std::shared_ptr<MessageParcel> reply, MessageOption &option);
napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info);
......@@ -82,12 +84,13 @@ EXTERN_C_END
struct SendRequestParam {
sptr<IRemoteObject> target;
uint32_t code;
MessageParcel &data;
MessageParcel &reply;
MessageOption &option;
std::shared_ptr<MessageParcel> data;
std::shared_ptr<MessageParcel> reply;
MessageOption& option;
napi_async_work asyncWork;
napi_deferred deferred;
int errCode;
napi_env env;
};
} // namespace OHOS
#endif // NAPI_IPC_OHOS_REMOTE_OBJECT_H
\ No newline at end of file
......@@ -64,10 +64,10 @@ NAPI_MessageParcel::NAPI_MessageParcel(napi_env env, napi_value thisVar, Message
maxCapacityToWrite_ = MAX_CAPACITY_TO_WRITE;
// do NOT reference js parcel here
if (parcel == nullptr) {
nativeParcel_ = new MessageParcel();
nativeParcel_ = std::shared_ptr<MessageParcel>(new MessageParcel());
owner = true;
} else {
nativeParcel_ = parcel;
nativeParcel_ = std::shared_ptr<MessageParcel>(parcel, release);
owner = false;
}
}
......@@ -75,14 +75,16 @@ NAPI_MessageParcel::NAPI_MessageParcel(napi_env env, napi_value thisVar, Message
NAPI_MessageParcel::~NAPI_MessageParcel()
{
DBINDER_LOGI("NAPI_MessageParcel::Destructor");
if (owner) {
delete nativeParcel_;
}
nativeParcel_ = nullptr;
env_ = nullptr;
}
MessageParcel *NAPI_MessageParcel::GetMessageParcel()
void NAPI_MessageParcel::release(MessageParcel *parcel)
{
DBINDER_LOGI("message parcel is created by others, do nothing");
}
std::shared_ptr<MessageParcel> NAPI_MessageParcel::GetMessageParcel()
{
return nativeParcel_;
}
......@@ -296,8 +298,8 @@ napi_value NAPI_MessageParcel::JS_writeChar(napi_env env, napi_callback_info inf
napi_value NAPI_MessageParcel::JS_writeStringWithLength(napi_env env, napi_callback_info info)
{
size_t expectedArgc = 2;
size_t argc = 2;
size_t expectedArgc = 2;
napi_value argv[2] = {0};
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
......@@ -821,7 +823,8 @@ napi_value NAPI_MessageParcel::JS_writeSequenceable(napi_env env, napi_callback_
napi_value funcArg[1] = { thisVar };
napi_value callResult = nullptr;
bool result = napi_call_function(env, thisVar, prop, 1, funcArg, &callResult);
napi_status status = napi_call_function(env, argv[0], prop, 1, funcArg, &callResult);
bool result = (status == napi_ok);
bool isExceptionPending = false;
napi_is_exception_pending(env, &isExceptionPending);
if (isExceptionPending) {
......@@ -872,7 +875,8 @@ napi_value NAPI_MessageParcel::JS_writeSequenceableArray(napi_env env, napi_call
napi_value funcArg[1] = { thisVar };
napi_value callResult = nullptr;
result = napi_call_function(env, thisVar, prop, 1, funcArg, &callResult);
napi_status status = napi_call_function(env, element, prop, 1, funcArg, &callResult);
result = (status == napi_ok);
bool isExceptionPending = false;
napi_is_exception_pending(env, &isExceptionPending);
......@@ -1236,9 +1240,9 @@ napi_value NAPI_MessageParcel::JS_readByteArray(napi_env env, napi_callback_info
napi_unwrap(env, thisVar, (void **)&napiParcel);
NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
uint32_t maxBufLen = 40960;
uint32_t maxBytesLen = 40960;
uint32_t arrayBufferLength = napiParcel->nativeParcel_->ReadUint32();
NAPI_ASSERT(env, arrayBufferLength < maxBufLen, "byte array length too large");
NAPI_ASSERT(env, arrayBufferLength < maxBytesLen, "byte array length too large");
size_t len = (arrayBufferLength / BYTE_SIZE_32) + (arrayBufferLength % BYTE_SIZE_32 == 0 ? 0 : 1);
DBINDER_LOGI("messageparcel WriteBuffer typedarrayLength = %{public}d", (int)(len));
......@@ -1725,7 +1729,8 @@ napi_value NAPI_MessageParcel::JS_readSequenceable(napi_env env, napi_callback_i
napi_value funcArg[1] = {thisVar};
napi_value callResult = nullptr;
result = napi_call_function(env, thisVar, prop, 1, funcArg, &callResult);
napi_status status = napi_call_function(env, argv[0], prop, 1, funcArg, &callResult);
result = (status == napi_ok);
}
napi_value napiValue = nullptr;
......@@ -1736,9 +1741,12 @@ napi_value NAPI_MessageParcel::JS_readSequenceable(napi_env env, napi_callback_i
napi_value NAPI_MessageParcel::JS_create(napi_env env, napi_callback_info info)
{
// new native parcel object
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;
napi_status status = napi_get_reference_value(env, g_messageParcelConsRef, &constructor);
NAPI_ASSERT(env, constructor != nullptr, "failed to get js MessageParcel constructor");
status = napi_get_named_property(env, global, "IPCParcelConstructor_", &constructor);
NAPI_ASSERT(env, status == napi_ok, "get message parcel constructor failed");
napi_value jsMessageParcel;
status = napi_new_instance(env, constructor, 0, nullptr, &jsMessageParcel);
NAPI_ASSERT(env, status == napi_ok, "failed to construct js MessageParcel");
......@@ -1912,13 +1920,17 @@ napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("readCharArray", NAPI_MessageParcel::JS_readCharArray),
DECLARE_NAPI_FUNCTION("readStringArray", NAPI_MessageParcel::JS_readStringArray),
};
napi_value constructor = nullptr;
napi_define_class(env, className.c_str(), className.length(), JS_constructor, nullptr,
sizeof(properties) / sizeof(properties[0]), properties, &g_messageParcelConstructor);
NAPI_ASSERT(env, g_messageParcelConstructor != nullptr, "define js class MessageParcel failed");
napi_status status = napi_set_named_property(env, exports, "MessageParcel", g_messageParcelConstructor);
sizeof(properties) / sizeof(properties[0]), properties, &constructor);
NAPI_ASSERT(env, constructor != nullptr, "define js class MessageParcel failed");
napi_status status = napi_set_named_property(env, exports, "MessageParcel", constructor);
NAPI_ASSERT(env, status == napi_ok, "set property MessageParcel failed");
status = napi_create_reference(env, g_messageParcelConstructor, 1, &g_messageParcelConsRef);
NAPI_ASSERT(env, status == napi_ok, "create ref to js MessageParcel constructor 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, "IPCParcelConstructor_", constructor);
NAPI_ASSERT(env, status == napi_ok, "set message parcel constructor failed");
return exports;
}
......@@ -1949,9 +1961,4 @@ napi_value NAPI_MessageParcel::JS_constructor(napi_env env, napi_callback_info i
NAPI_ASSERT(env, status == napi_ok, "napi wrap message parcel failed");
return thisVar;
}
napi_ref NAPI_MessageParcel::GetParcelConsRef()
{
return g_messageParcelConsRef;
}
} // namespace OHOS
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册