fix ipc js interfaces bug

Signed-off-by: liangshenglin1 <liangshenglin1@huawei.com>
This commit is contained in:
liangshenglin1 2021-10-20 10:38:03 +08:00
parent 1cdfa196a3
commit 001df61dd8
5 changed files with 513 additions and 247 deletions

View File

@ -15,26 +15,57 @@
const rpcSo = requireInternal("rpc");
const EXC_INSECURITY = -1;
const EXC_ILLEGAL_ARGUMENT = -3;
const EXC_NULL_POINTER = -4;
const EXC_ILLEGAL_STATE = -5;
const EXC_UNSUPPORTED_OPERATION = -7;
const EXC_INDEX_OUTOF_BOUNDS = -10;
const EXC_NEGATIVE_ARRAY_SIZE = -11;
const EXC_ARRAY_STORE = -12;
const EXC_CLASS_CAST = -13;
const EXC_PARCEL_CAPACITY_ERROR = -14;
const EXC_REMOTE_TRANSACTION_FAILED = -200;
let MessageParcel = rpcSo.MessageParcel;
let IPCSkeleton = rpcSo.IPCSkeleton;
let RemoteObject = rpcSo.RemoteObject;
let NAPIMessageParcel = rpcSo.MessageParcel;
let NAPIIPCSkeleton = rpcSo.IPCSkeleton;
let NAPIRemoteObject = rpcSo.RemoteObject;
let RemoteProxy = rpcSo.RemoteProxy;
let MessageOption = rpcSo.MessageOption;
class RemoteObject extends NAPIRemoteObject {
constructor(descriptor) {
if (typeof descriptor === 'string' && descriptor.length > 0) {
super(descriptor, descriptor.length);
this.descriptor = descriptor;
} else {
throw new NullPointerException("invalid descriptor");
}
}
addDeathRecipient(recipient, flags) {
return false;
}
removeDeathRecipient(recipient, flags) {
return false;
}
isObjectDead() {
return false;
}
attachLocalInterface(localInterface, descriptor) {
this.descriptor = descriptor;
this.interface = localInterface;
}
queryLocalInterface(descriptor) {
if (this.descriptor === descriptor) {
return this.interface;
}
return null;
}
}
class IPCSkeleton extends NAPIIPCSkeleton {
static setCallingIdentity(identity) {
if (typeof identity === 'string') {
return NAPIIPCSkeleton.setCallingIdentity(identity, identity.length);
}
return false;
}
}
class Exception {
message;
constructor(msg) {
this.message = msg;
}
@ -57,101 +88,110 @@ class RemoteException extends Exception {}
class ParcelException extends Exception {}
class RuntimeException extends Exception {}
class MessageParcel extends NAPIMessageParcel {
static EXC_INSECURITY = -1;
static EXC_ILLEGAL_ARGUMENT = -3;
static EXC_NULL_POINTER = -4;
static EXC_ILLEGAL_STATE = -5;
static EXC_UNSUPPORTED_OPERATION = -7;
static EXC_INDEX_OUTOF_BOUNDS = -10;
static EXC_NEGATIVE_ARRAY_SIZE = -11;
static EXC_ARRAY_STORE = -12;
static EXC_CLASS_CAST = -13;
static EXC_PARCEL_CAPACITY_ERROR = -14;
static EXC_REMOTE_TRANSACTION_FAILED = -200;
MessageParcel.prototype.createException = function(code, msg) {
switch (code) {
case EXC_INSECURITY: {
return new SecurityException(msg);
}
case EXC_ILLEGAL_ARGUMENT: {
return new IllegalArgumentException(msg);
}
case EXC_NULL_POINTER: {
return new NullPointerException(msg);
}
case EXC_ILLEGAL_STATE: {
return new IllegalStateException(msg);
}
case EXC_UNSUPPORTED_OPERATION: {
return new UnsupportedOperationException(msg);
}
case EXC_INDEX_OUTOF_BOUNDS: {
return new IndexOutOfBoundsException(msg);
}
case EXC_NEGATIVE_ARRAY_SIZE: {
return new NegativeArraySizeException(msg);
}
case EXC_ARRAY_STORE: {
return new ArrayStoreException(msg);
}
case EXC_CLASS_CAST: {
return new ClassCastException(msg);
}
case EXC_REMOTE_TRANSACTION_FAILED: {
return new RemoteException(msg);
}
case EXC_PARCEL_CAPACITY_ERROR: {
return new ParcelException(msg);
}
default: {
return new RuntimeException("Unknown exception code: " + code + " msg " + msg);
createException(code, msg) {
switch (code) {
case MessageParcel.EXC_INSECURITY: {
return new SecurityException(msg);
}
case MessageParcel.EXC_ILLEGAL_ARGUMENT: {
return new IllegalArgumentException(msg);
}
case MessageParcel.EXC_NULL_POINTER: {
return new NullPointerException(msg);
}
case MessageParcel.EXC_ILLEGAL_STATE: {
return new IllegalStateException(msg);
}
case MessageParcel.EXC_UNSUPPORTED_OPERATION: {
return new UnsupportedOperationException(msg);
}
case MessageParcel.EXC_INDEX_OUTOF_BOUNDS: {
return new IndexOutOfBoundsException(msg);
}
case MessageParcel.EXC_NEGATIVE_ARRAY_SIZE: {
return new NegativeArraySizeException(msg);
}
case MessageParcel.EXC_ARRAY_STORE: {
return new ArrayStoreException(msg);
}
case MessageParcel.EXC_CLASS_CAST: {
return new ClassCastException(msg);
}
case MessageParcel.EXC_REMOTE_TRANSACTION_FAILED: {
return new RemoteException(msg);
}
case MessageParcel.EXC_PARCEL_CAPACITY_ERROR: {
return new ParcelException(msg);
}
default: {
return new RuntimeException("Unknown exception code: " + code + " msg " + msg);
}
}
}
}
MessageParcel.prototype.writeException = function(exception) {
let code = 0;
if (exception instanceof SecurityException) {
code = EXC_INSECURITY;
} else if (exception instanceof IllegalArgumentException) {
code = EXC_ILLEGAL_ARGUMENT;
} else if (exception instanceof NullPointerException) {
code = EXC_NULL_POINTER;
} else if (exception instanceof IllegalStateException) {
code = EXC_ILLEGAL_STATE;
} else if (exception instanceof UnsupportedOperationException) {
code = EXC_UNSUPPORTED_OPERATION;
} else if (exception instanceof IndexOutOfBoundsException) {
code = EXC_INDEX_OUTOF_BOUNDS;
} else if (exception instanceof NegativeArraySizeException) {
code = EXC_NEGATIVE_ARRAY_SIZE;
} else if (exception instanceof ArrayStoreException) {
code = EXC_ARRAY_STORE;
} else if (exception instanceof ClassCastException) {
code = EXC_CLASS_CAST;
} else if (exception instanceof RemoteException) {
code = EXC_REMOTE_TRANSACTION_FAILED;
} else if (exception instanceof ParcelException) {
code = EXC_PARCEL_CAPACITY_ERROR;
} else {
code = 0;
}
this.writeInt(code);
if (code === 0) {
if (exception instanceof RuntimeException) {
writeException(exception) {
let code = 0;
if (exception instanceof SecurityException) {
code = MessageParcel.EXC_INSECURITY;
} else if (exception instanceof IllegalArgumentException) {
code = MessageParcel.EXC_ILLEGAL_ARGUMENT;
} else if (exception instanceof NullPointerException) {
code = MessageParcel.EXC_NULL_POINTER;
} else if (exception instanceof IllegalStateException) {
code = MessageParcel.EXC_ILLEGAL_STATE;
} else if (exception instanceof UnsupportedOperationException) {
code = MessageParcel.EXC_UNSUPPORTED_OPERATION;
} else if (exception instanceof IndexOutOfBoundsException) {
code = MessageParcel.EXC_INDEX_OUTOF_BOUNDS;
} else if (exception instanceof NegativeArraySizeException) {
code = MessageParcel.EXC_NEGATIVE_ARRAY_SIZE;
} else if (exception instanceof ArrayStoreException) {
code = MessageParcel.EXC_ARRAY_STORE;
} else if (exception instanceof ClassCastException) {
code = MessageParcel.EXC_CLASS_CAST;
} else if (exception instanceof RemoteException) {
code = MessageParcel.EXC_REMOTE_TRANSACTION_FAILED;
} else if (exception instanceof ParcelException) {
code = MessageParcel.EXC_PARCEL_CAPACITY_ERROR;
} else {
code = 0;
}
this.writeInt(code);
if (code === 0) {
throw new RuntimeException(exception.getMessage());
}
throw new RuntimeException(exception.getMessage());
}
this.writeString(exception.getMessage());
}
this.writeString(exception.getMessage());
}
MessageParcel.prototype.writeNoException = function() {
this.writeInt(0);
}
writeNoException() {
this.writeInt(0);
}
MessageParcel.prototype.readException = function() {
let code = this.readInt();
readException() {
let code = this.readInt();
if (code === 0) {
return;
}
let msg = this.readString();
let exception = this.createException(code, msg);
return exception;
}
throw exception;
}
MessageParcel.prototype.createRemoteObjectArray = function() {
let num = this.readInt();
createRemoteObjectArray() {
let num = this.readInt();
if (num <= 0) {
return null;
}
@ -160,46 +200,35 @@ MessageParcel.prototype.createRemoteObjectArray = function() {
list[i] = this.readRemoteObject();
}
return list;
}
MessageParcel.prototype.writeRemoteObjectArray = function(objects) {
if (objects === null || objects.length <= 0 ) {
this.writeInt(-1);
return false;
}
let num = objects.length;
this.writeInt(num);
for (let i = 0; i < num; i ++) {
this.writeRemoteObject(objects[i]);
}
return true;
}
writeRemoteObjectArray(objects) {
if (objects === null || objects.length <= 0) {
this.writeInt(-1);
return false;
}
MessageParcel.prototype.readRemoteObjectArray = function(objects) {
if (objects === null) {
return;
let num = objects.length;
this.writeInt(num);
for (let i = 0; i < num; i++) {
this.writeRemoteObject(objects[i]);
}
return true;
}
let num = this.readInt();
if (num !== objects.length) {
return;
readRemoteObjectArray(objects) {
if (objects === null) {
return;
}
let num = this.readInt();
if (num !== objects.length) {
return;
}
for (let i = 0; i < num; i++) {
objects[i] = this.readRemoteObject();
}
}
for (let i = 0; i < num; i ++) {
objects[i] = this.readRemoteObject();
}
}
RemoteObject.prototype.addDeathRecipient = function(recipient, flags) {
return false;
}
RemoteObject.prototype.removeDeathRecipient = function(recipient, flags) {
return false;
}
RemoteObject.prototype.isObjectDead = function() {
return false;
}
export default {

View File

@ -55,6 +55,8 @@ EXTERN_C_END
napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info info);
// RemoteObject napi methods
napi_value NAPI_RemoteObject_getInterfaceDescriptor(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteObject_getCallingPid(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteObject_getCallingUid(napi_env env, napi_callback_info info);
@ -67,6 +69,8 @@ EXTERN_C_END
napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteProxy_queryLocalInterface(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteProxy_addDeathRecipient(napi_env env, napi_callback_info info);
napi_value NAPI_RemoteProxy_removeDeathRecipient(napi_env env, napi_callback_info info);

View File

@ -42,11 +42,11 @@ napi_value NapiOhosRpcMessageOptionGetFlags(napi_env env, napi_callback_info inf
napi_value NapiOhosRpcMessageOptionSetFlags(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
size_t argc;
size_t argc = 1;
napi_value argv[1] = { 0 };
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
napi_valuetype valueType;
NAPI_ASSERT(env, thisVar != nullptr && argv[0] != 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");
int32_t flags = 0;
@ -85,11 +85,11 @@ napi_value NapiOhosRpcMessageOptionGetWaittime(napi_env env, napi_callback_info
napi_value NapiOhosRpcMessageOptionSetWaittime(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
size_t argc;
size_t argc = 1;
napi_value argv[1] = { 0 };
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, thisVar != nullptr, "failed to get js message option object");
napi_valuetype valueType;
NAPI_ASSERT(env, thisVar != nullptr && argv[0] != 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");
int32_t waittime = 0;
@ -98,7 +98,7 @@ napi_value NapiOhosRpcMessageOptionSetWaittime(napi_env env, napi_callback_info
MessageOption *option = nullptr;
status = napi_unwrap(env, thisVar, (void **)&option);
NAPI_ASSERT(env, option != nullptr, "failed to get native message option");
option->SetFlags(waittime);
option->SetWaitTime(waittime);
napi_value result = nullptr;
napi_get_undefined(env, &result);
return result;

View File

@ -97,7 +97,7 @@ napi_value NAPI_MessageParcel::JS_writeByte(napi_env env, napi_callback_info inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -122,7 +122,7 @@ napi_value NAPI_MessageParcel::JS_writeShort(napi_env env, napi_callback_info in
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -147,7 +147,7 @@ napi_value NAPI_MessageParcel::JS_writeInt(napi_env env, napi_callback_info info
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -172,7 +172,7 @@ napi_value NAPI_MessageParcel::JS_writeLong(napi_env env, napi_callback_info inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -197,7 +197,7 @@ napi_value NAPI_MessageParcel::JS_writeFloat(napi_env env, napi_callback_info in
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -222,7 +222,7 @@ napi_value NAPI_MessageParcel::JS_writeDouble(napi_env env, napi_callback_info i
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -247,7 +247,7 @@ napi_value NAPI_MessageParcel::JS_writeBoolean(napi_env env, napi_callback_info
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_boolean, "type mismatch for parameter 1");
@ -272,7 +272,7 @@ napi_value NAPI_MessageParcel::JS_writeChar(napi_env env, napi_callback_info inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
@ -309,7 +309,7 @@ napi_value NAPI_MessageParcel::JS_writeStringWithLength(napi_env env, napi_callb
napi_unwrap(env, thisVar, (void **)&napiParcel);
NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
@ -343,11 +343,11 @@ napi_value NAPI_MessageParcel::JS_writeByteArray(napi_env env, napi_callback_inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isTypedArray;
bool isTypedArray = false;
napi_is_typedarray(env, argv[0], &isTypedArray);
NAPI_ASSERT(env, isTypedArray == true, "type mismatch for parameter 1");
napi_typedarray_type typedarrayType;
napi_typedarray_type typedarrayType = napi_uint8_array;
size_t typedarrayLength = 0;
void *typedarrayBufferPtr = nullptr;
napi_value tmpArrayBuffer = nullptr;
@ -361,7 +361,7 @@ napi_value NAPI_MessageParcel::JS_writeByteArray(napi_env env, napi_callback_inf
NAPI_MessageParcel *napiParcel = nullptr;
napi_unwrap(env, thisVar, (void **)&napiParcel);
NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
size_t len = (typedarrayLength / BYTE_SIZE_32) + (typedarrayLength % BYTE_SIZE_32 == 0 ? 0 : 1);
size_t len = ((typedarrayLength / BYTE_SIZE_32) + (typedarrayLength % BYTE_SIZE_32 == 0 ? 0 : 1));
DBINDER_LOGI("messageparcel WriteBuffer len = %{public}d", (int)(len));
CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (len + 1), napiParcel);
napiParcel->nativeParcel_->WriteUint32(typedarrayLength);
@ -380,7 +380,7 @@ napi_value NAPI_MessageParcel::JS_writeShortArray(napi_env env, napi_callback_in
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -426,7 +426,7 @@ napi_value NAPI_MessageParcel::JS_writeIntArray(napi_env env, napi_callback_info
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -476,7 +476,7 @@ napi_value NAPI_MessageParcel::JS_writeLongArray(napi_env env, napi_callback_inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -524,7 +524,7 @@ napi_value NAPI_MessageParcel::JS_writeFloatArray(napi_env env, napi_callback_in
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -571,7 +571,7 @@ napi_value NAPI_MessageParcel::JS_writeDoubleArray(napi_env env, napi_callback_i
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -618,7 +618,7 @@ napi_value NAPI_MessageParcel::JS_writeBooleanArray(napi_env env, napi_callback_
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -665,7 +665,7 @@ napi_value NAPI_MessageParcel::JS_writeCharArray(napi_env env, napi_callback_inf
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -722,7 +722,7 @@ napi_value NAPI_MessageParcel::JS_writeString(napi_env env, napi_callback_info i
napi_unwrap(env, thisVar, (void **)&napiParcel);
NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
@ -754,7 +754,7 @@ napi_value NAPI_MessageParcel::JS_writeStringArray(napi_env env, napi_callback_i
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -845,7 +845,7 @@ napi_value NAPI_MessageParcel::JS_writeSequenceableArray(napi_env env, napi_call
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1068,7 +1068,7 @@ napi_value NAPI_MessageParcel::JS_setSize(napi_env env, napi_callback_info info)
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -1094,7 +1094,7 @@ napi_value NAPI_MessageParcel::JS_setCapacity(napi_env env, napi_callback_info i
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -1171,7 +1171,7 @@ napi_value NAPI_MessageParcel::JS_rewindRead(napi_env env, napi_callback_info in
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -1213,7 +1213,7 @@ napi_value NAPI_MessageParcel::JS_rewindWrite(napi_env env, napi_callback_info i
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
@ -1243,7 +1243,7 @@ napi_value NAPI_MessageParcel::JS_readByteArray(napi_env env, napi_callback_info
uint32_t maxBytesLen = 40960;
uint32_t arrayBufferLength = napiParcel->nativeParcel_->ReadUint32();
NAPI_ASSERT(env, arrayBufferLength < maxBytesLen, "byte array length too large");
size_t len = (arrayBufferLength / BYTE_SIZE_32) + (arrayBufferLength % BYTE_SIZE_32 == 0 ? 0 : 1);
size_t len = ((arrayBufferLength / BYTE_SIZE_32) + (arrayBufferLength % BYTE_SIZE_32 == 0 ? 0 : 1));
DBINDER_LOGI("messageparcel WriteBuffer typedarrayLength = %{public}d", (int)(len));
if (argc > 0) {
@ -1253,7 +1253,7 @@ napi_value NAPI_MessageParcel::JS_readByteArray(napi_env env, napi_callback_info
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isTypedArray;
bool isTypedArray = false;
napi_is_typedarray(env, argv[0], &isTypedArray);
NAPI_ASSERT(env, isTypedArray == true, "type mismatch for parameter 1");
@ -1312,7 +1312,7 @@ napi_value NAPI_MessageParcel::JS_readShortArray(napi_env env, napi_callback_inf
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1363,7 +1363,7 @@ napi_value NAPI_MessageParcel::JS_readIntArray(napi_env env, napi_callback_info
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1414,7 +1414,7 @@ napi_value NAPI_MessageParcel::JS_readLongArray(napi_env env, napi_callback_info
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1465,7 +1465,7 @@ napi_value NAPI_MessageParcel::JS_readFloatArray(napi_env env, napi_callback_inf
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1516,7 +1516,7 @@ napi_value NAPI_MessageParcel::JS_readDoubleArray(napi_env env, napi_callback_in
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1567,7 +1567,7 @@ napi_value NAPI_MessageParcel::JS_readBooleanArray(napi_env env, napi_callback_i
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1619,7 +1619,7 @@ napi_value NAPI_MessageParcel::JS_readCharArray(napi_env env, napi_callback_info
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1670,7 +1670,7 @@ napi_value NAPI_MessageParcel::JS_readStringArray(napi_env env, napi_callback_in
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
bool isArray;
bool isArray = false;
napi_is_array(env, argv[0], &isArray);
NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
@ -1816,7 +1816,7 @@ napi_value NAPI_MessageParcel::JS_writeInterfaceToken(napi_env env, napi_callbac
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter");
@ -1865,26 +1865,6 @@ napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports)
napi_property_descriptor properties[] = {
DECLARE_NAPI_STATIC_FUNCTION("create", NAPI_MessageParcel::JS_create),
DECLARE_NAPI_FUNCTION("reclaim", NAPI_MessageParcel::JS_reclaim),
DECLARE_NAPI_FUNCTION("writeByte", NAPI_MessageParcel::JS_writeByte),
DECLARE_NAPI_FUNCTION("writeShort", NAPI_MessageParcel::JS_writeShort),
DECLARE_NAPI_FUNCTION("writeInt", NAPI_MessageParcel::JS_writeInt),
DECLARE_NAPI_FUNCTION("writeLong", NAPI_MessageParcel::JS_writeLong),
DECLARE_NAPI_FUNCTION("writeFloat", NAPI_MessageParcel::JS_writeFloat),
DECLARE_NAPI_FUNCTION("writeDouble", NAPI_MessageParcel::JS_writeDouble),
DECLARE_NAPI_FUNCTION("writeBoolean", NAPI_MessageParcel::JS_writeBoolean),
DECLARE_NAPI_FUNCTION("writeChar", NAPI_MessageParcel::JS_writeChar),
DECLARE_NAPI_FUNCTION("writeStringWithLength", NAPI_MessageParcel::JS_writeStringWithLength),
DECLARE_NAPI_FUNCTION("writeString", NAPI_MessageParcel::JS_writeString),
DECLARE_NAPI_FUNCTION("writeByteArray", NAPI_MessageParcel::JS_writeByteArray),
DECLARE_NAPI_FUNCTION("readByte", NAPI_MessageParcel::JS_readByte),
DECLARE_NAPI_FUNCTION("readShort", NAPI_MessageParcel::JS_readShort),
DECLARE_NAPI_FUNCTION("readInt", NAPI_MessageParcel::JS_readInt),
DECLARE_NAPI_FUNCTION("readLong", NAPI_MessageParcel::JS_readLong),
DECLARE_NAPI_FUNCTION("readFloat", NAPI_MessageParcel::JS_readFloat),
DECLARE_NAPI_FUNCTION("readDouble", NAPI_MessageParcel::JS_readDouble),
DECLARE_NAPI_FUNCTION("readBoolean", NAPI_MessageParcel::JS_readBoolean),
DECLARE_NAPI_FUNCTION("readChar", NAPI_MessageParcel::JS_readChar),
DECLARE_NAPI_FUNCTION("readString", NAPI_MessageParcel::JS_readString),
DECLARE_NAPI_FUNCTION("writeRemoteObject", NAPI_MessageParcel::JS_writeRemoteObject),
DECLARE_NAPI_FUNCTION("readRemoteObject", NAPI_MessageParcel::JS_readRemoteObject),
DECLARE_NAPI_FUNCTION("writeInterfaceToken", NAPI_MessageParcel::JS_writeInterfaceToken),
@ -1897,9 +1877,19 @@ napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("getReadableBytes", NAPI_MessageParcel::JS_getReadableBytes),
DECLARE_NAPI_FUNCTION("getReadPosition", NAPI_MessageParcel::JS_getReadPosition),
DECLARE_NAPI_FUNCTION("getWritePosition", NAPI_MessageParcel::JS_getWritePosition),
DECLARE_NAPI_FUNCTION("rewindWrite", NAPI_MessageParcel::JS_rewindWrite),
DECLARE_NAPI_FUNCTION("rewindRead", NAPI_MessageParcel::JS_rewindRead),
DECLARE_NAPI_FUNCTION("rewindWrite", NAPI_MessageParcel::JS_rewindWrite),
DECLARE_NAPI_FUNCTION("writeByte", NAPI_MessageParcel::JS_writeByte),
DECLARE_NAPI_FUNCTION("writeShort", NAPI_MessageParcel::JS_writeShort),
DECLARE_NAPI_FUNCTION("writeInt", NAPI_MessageParcel::JS_writeInt),
DECLARE_NAPI_FUNCTION("writeLong", NAPI_MessageParcel::JS_writeLong),
DECLARE_NAPI_FUNCTION("writeFloat", NAPI_MessageParcel::JS_writeFloat),
DECLARE_NAPI_FUNCTION("writeDouble", NAPI_MessageParcel::JS_writeDouble),
DECLARE_NAPI_FUNCTION("writeBoolean", NAPI_MessageParcel::JS_writeBoolean),
DECLARE_NAPI_FUNCTION("writeChar", NAPI_MessageParcel::JS_writeChar),
DECLARE_NAPI_FUNCTION("writeString", NAPI_MessageParcel::JS_writeString),
DECLARE_NAPI_FUNCTION("writeSequenceable", NAPI_MessageParcel::JS_writeSequenceable),
DECLARE_NAPI_FUNCTION("writeByteArray", NAPI_MessageParcel::JS_writeByteArray),
DECLARE_NAPI_FUNCTION("writeShortArray", NAPI_MessageParcel::JS_writeShortArray),
DECLARE_NAPI_FUNCTION("writeIntArray", NAPI_MessageParcel::JS_writeIntArray),
DECLARE_NAPI_FUNCTION("writeLongArray", NAPI_MessageParcel::JS_writeLongArray),
@ -1909,6 +1899,16 @@ napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("writeCharArray", NAPI_MessageParcel::JS_writeCharArray),
DECLARE_NAPI_FUNCTION("writeStringArray", NAPI_MessageParcel::JS_writeStringArray),
DECLARE_NAPI_FUNCTION("writeSequenceableArray", NAPI_MessageParcel::JS_writeSequenceableArray),
DECLARE_NAPI_FUNCTION("writeStringWithLength", NAPI_MessageParcel::JS_writeStringWithLength),
DECLARE_NAPI_FUNCTION("readByte", NAPI_MessageParcel::JS_readByte),
DECLARE_NAPI_FUNCTION("readShort", NAPI_MessageParcel::JS_readShort),
DECLARE_NAPI_FUNCTION("readInt", NAPI_MessageParcel::JS_readInt),
DECLARE_NAPI_FUNCTION("readLong", NAPI_MessageParcel::JS_readLong),
DECLARE_NAPI_FUNCTION("readFloat", NAPI_MessageParcel::JS_readFloat),
DECLARE_NAPI_FUNCTION("readDouble", NAPI_MessageParcel::JS_readDouble),
DECLARE_NAPI_FUNCTION("readBoolean", NAPI_MessageParcel::JS_readBoolean),
DECLARE_NAPI_FUNCTION("readChar", NAPI_MessageParcel::JS_readChar),
DECLARE_NAPI_FUNCTION("readString", NAPI_MessageParcel::JS_readString),
DECLARE_NAPI_FUNCTION("readSequenceable", NAPI_MessageParcel::JS_readSequenceable),
DECLARE_NAPI_FUNCTION("readByteArray", NAPI_MessageParcel::JS_readByteArray),
DECLARE_NAPI_FUNCTION("readShortArray", NAPI_MessageParcel::JS_readShortArray),
@ -1953,8 +1953,7 @@ napi_value NAPI_MessageParcel::JS_constructor(napi_env env, napi_callback_info i
// connect native object to js thisVar
status = napi_wrap(
env, thisVar, messageParcel,
[](napi_env env, void *data, void *hint) {
},
[](napi_env env, void *data, void *hint) {},
nullptr, nullptr);
NAPI_ASSERT(env, status == napi_ok, "napi wrap message parcel failed");
return thisVar;

View File

@ -212,13 +212,28 @@ EXTERN_C_START
napi_value NAPIRemoteProxyExport(napi_env env, napi_value exports)
{
const std::string className = "RemoteProxy";
napi_value pingTransaction = nullptr;
napi_create_int32(env, PING_TRANSACTION, &pingTransaction);
napi_value dumpTransaction = nullptr;
napi_create_int32(env, DUMP_TRANSACTION, &dumpTransaction);
napi_value interfaceTransaction = nullptr;
napi_create_int32(env, INTERFACE_TRANSACTION, &interfaceTransaction);
napi_value minTransactionId = nullptr;
napi_create_int32(env, MIN_TRANSACTION_ID, &minTransactionId);
napi_value maxTransactionId = nullptr;
napi_create_int32(env, MAX_TRANSACTION_ID, &maxTransactionId);
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("sendRequest", NAPI_RemoteProxy_sendRequest),
DECLARE_NAPI_FUNCTION("queryLocalInterface", NAPI_RemoteProxy_queryLocalInterface),
DECLARE_NAPI_FUNCTION("addDeathRecipient", NAPI_RemoteProxy_addDeathRecipient),
DECLARE_NAPI_FUNCTION("removeDeathRecipient", NAPI_RemoteProxy_removeDeathRecipient),
DECLARE_NAPI_FUNCTION("getInterfaceDescriptor", NAPI_RemoteProxy_getInterfaceDescriptor),
DECLARE_NAPI_FUNCTION("sendRequest", NAPI_RemoteProxy_sendRequest),
DECLARE_NAPI_FUNCTION("isObjectDead", NAPI_RemoteProxy_isObjectDead),
DECLARE_NAPI_FUNCTION("getHandle", NAPI_RemoteProxy_getHandle),
DECLARE_NAPI_STATIC_PROPERTY("PING_TRANSACTION", pingTransaction),
DECLARE_NAPI_STATIC_PROPERTY("DUMP_TRANSACTION", dumpTransaction),
DECLARE_NAPI_STATIC_PROPERTY("INTERFACE_TRANSACTION", interfaceTransaction),
DECLARE_NAPI_STATIC_PROPERTY("MIN_TRANSACTION_ID", minTransactionId),
DECLARE_NAPI_STATIC_PROPERTY("MAX_TRANSACTION_ID", maxTransactionId),
};
napi_value constructor = nullptr;
napi_define_class(env, className.c_str(), className.length(), RemoteProxy_JS_Constructor, nullptr,
@ -272,6 +287,8 @@ private:
pid_t callingUid;
std::string callingDeviceID;
std::string localDeviceID;
bool isLocalCalling;
int activeStatus;
ThreadLockInfo *lockInfo;
int result;
};
@ -339,7 +356,7 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
@ -351,9 +368,9 @@ napi_value RemoteObject_JS_Constructor(napi_env env, napi_callback_info info)
napi_get_value_uint32(env, argv[1], &stringLength);
NAPI_ASSERT(env, stringLength < maxStrLen, "string length too large");
char stringValue[stringLength];
char stringValue[stringLength + 1];
size_t jsStringLength = 0;
napi_get_value_string_utf8(env, argv[0], stringValue, stringLength, &jsStringLength);
napi_get_value_string_utf8(env, argv[0], stringValue, stringLength + 1, &jsStringLength);
NAPI_ASSERT(env, jsStringLength == stringLength, "string length wrong");
std::string descriptor = stringValue;
@ -378,9 +395,10 @@ napi_value NAPIRemoteObjectExport(napi_env env, napi_value exports)
{
const std::string className = "RemoteObject";
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("getInterfaceDescriptor", NAPI_RemoteObject_getInterfaceDescriptor),
DECLARE_NAPI_FUNCTION("sendRequest", NAPI_RemoteObject_sendRequest),
DECLARE_NAPI_FUNCTION("getCallingPid", NAPI_RemoteObject_getCallingPid),
DECLARE_NAPI_FUNCTION("getCallingUid", NAPI_RemoteObject_getCallingUid),
DECLARE_NAPI_FUNCTION("sendRequest", NAPI_RemoteObject_sendRequest),
};
napi_value constructor = nullptr;
napi_define_class(env, className.c_str(), className.length(), RemoteObject_JS_Constructor, nullptr,
@ -441,8 +459,10 @@ int NAPIRemoteObject::OnRemoteRequest(uint32_t code, MessageParcel &data, Messag
pid_t callingUid = IPCSkeleton::GetCallingUid();
std::string callingDeviceID = IPCSkeleton::GetCallingDeviceID();
std::string localDeviceID = IPCSkeleton::GetLocalDeviceID();
DBINDER_LOGI("callingPid:%{public}u, callingUid:%{public}u, callingDeviceID:%{public}s, localDeviceId:%{public}s",
callingPid, callingUid, callingDeviceID.c_str(), localDeviceID.c_str());
bool isLocalCalling = IPCSkeleton::IsLocalCalling();
DBINDER_LOGI("callingPid:%{public}u, callingUid:%{public}u, callingDeviceID:%{public}s,\
localDeviceId:%{public}s, localCalling:%{public}d",
callingPid, callingUid, callingDeviceID.c_str(), localDeviceID.c_str(), isLocalCalling);
std::shared_ptr<struct ThreadLockInfo> lockInfo = std::make_shared<struct ThreadLockInfo>();
CallbackParam *param = new CallbackParam {
.env = env_,
@ -455,6 +475,8 @@ int NAPIRemoteObject::OnRemoteRequest(uint32_t code, MessageParcel &data, Messag
.callingUid = callingUid,
.callingDeviceID = callingDeviceID,
.localDeviceID = localDeviceID,
.isLocalCalling = isLocalCalling,
.activeStatus = IRemoteInvoker::ACTIVE_INVOKER,
.lockInfo = lockInfo.get(),
.result = 0
};
@ -604,6 +626,10 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam)
napi_get_named_property(param->env, global, "callingDeviceID_", &oldCallingDeviceID);
napi_value oldLocalDeviceID;
napi_get_named_property(param->env, global, "localDeviceID_", &oldLocalDeviceID);
napi_value oldIsLocalCalling;
napi_get_named_property(param->env, global, "isLocalCalling_", &oldIsLocalCalling);
napi_value oldActiveStatus;
napi_get_named_property(param->env, global, "activeStatus_", &oldActiveStatus);
// set new calling pid, uid, device id
napi_value newPid;
@ -618,6 +644,12 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam)
napi_value newLocalDeviceID;
napi_create_string_utf8(param->env, param->localDeviceID.c_str(), NAPI_AUTO_LENGTH, &newLocalDeviceID);
napi_set_named_property(param->env, global, "localDeviceID_", newLocalDeviceID);
napi_value newIsLocalCalling;
napi_get_boolean(param->env, param->isLocalCalling, &newIsLocalCalling);
napi_set_named_property(param->env, global, "isLocalCalling_", newIsLocalCalling);
napi_value newActiveStatus;
napi_create_int32(param->env, param->activeStatus, &newActiveStatus);
napi_set_named_property(param->env, global, "activeStatus_", newActiveStatus);
// start to call onRemoteRequest
size_t argc2 = 4;
@ -629,7 +661,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam)
DBINDER_LOGE("OnRemoteRequest got exception");
param->result = ERR_UNKNOWN_TRANSACTION;
} else {
bool result;
bool result = false;
napi_get_value_bool(param->env, return_val, &result);
if (!result) {
DBINDER_LOGE("OnRemoteRequest res:%{public}s", result ? "true" : "false");
@ -643,6 +675,8 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam)
napi_set_named_property(param->env, global, "callingUid_", oldUid);
napi_set_named_property(param->env, global, "callingDeviceID_", oldCallingDeviceID);
napi_set_named_property(param->env, global, "localDeviceID_", oldLocalDeviceID);
napi_set_named_property(param->env, global, "isLocalCalling_", oldIsLocalCalling);
napi_set_named_property(param->env, global, "activeStatus_", oldActiveStatus);
std::unique_lock<std::mutex> lock(param->lockInfo->mutex);
param->lockInfo->ready = true;
param->lockInfo->condition.notify_all();
@ -694,7 +728,7 @@ napi_value NAPI_ohos_rpc_CreateJsRemoteObject(napi_env env, const sptr<IRemoteOb
// create a new js remote object
size_t argc = 2;
napi_value argv[2] = { jsDesc, len };
napi_value jsRemoteObject;
napi_value jsRemoteObject = nullptr;
status = napi_new_instance(env, constructor, argc, argv, &jsRemoteObject);
NAPI_ASSERT(env, status == napi_ok, "failed to construct js RemoteObject");
// retrieve holder and set object
@ -767,46 +801,104 @@ napi_value NAPI_IPCSkeleton_getContextObject(napi_env env, napi_callback_info in
napi_value NAPI_IPCSkeleton_getCallingPid(napi_env env, napi_callback_info info)
{
napi_value global;
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value callingPid;
napi_get_named_property(env, global, "callingPid_", &callingPid);
return callingPid;
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
if (napiActiveStatus != nullptr) {
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
napi_value callingPid = nullptr;
napi_get_named_property(env, global, "callingPid_", &callingPid);
return callingPid;
}
}
pid_t pid = getpid();
napi_value result = nullptr;
napi_create_int32(env, static_cast<int32_t>(pid), &result);
return result;
}
napi_value NAPI_IPCSkeleton_getCallingUid(napi_env env, napi_callback_info info)
{
napi_value global;
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value callingUid;
napi_get_named_property(env, global, "callingUid_", &callingUid);
return callingUid;
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
if (napiActiveStatus != nullptr) {
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
napi_value callingUid = nullptr;
napi_get_named_property(env, global, "callingUid_", &callingUid);
return callingUid;
}
}
pid_t uid = getuid();
napi_value result = nullptr;
napi_create_int32(env, static_cast<int32_t>(uid), &result);
return result;
}
napi_value NAPI_IPCSkeleton_getCallingDeviceID(napi_env env, napi_callback_info info)
{
napi_value global;
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value callingDeviceID;
napi_get_named_property(env, global, "callingDeviceID_", &callingDeviceID);
return callingDeviceID;
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
if (napiActiveStatus != nullptr) {
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
napi_value callingDeviceID = nullptr;
napi_get_named_property(env, global, "callingDeviceID_", &callingDeviceID);
return callingDeviceID;
}
}
napi_value result = nullptr;
napi_create_string_utf8(env, "", 0, &result);
return result;
}
napi_value NAPI_IPCSkeleton_getLocalDeviceID(napi_env env, napi_callback_info info)
{
napi_value global;
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value localDeviceID;
napi_get_named_property(env, global, "localDeviceID_", &localDeviceID);
return localDeviceID;
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
if (napiActiveStatus != nullptr) {
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
napi_value localDeviceID = nullptr;
napi_get_named_property(env, global, "localDeviceID_", &localDeviceID);
return localDeviceID;
}
}
napi_value result = nullptr;
napi_create_string_utf8(env, "", 0, &result);
return result;
}
napi_value NAPI_IPCSkeleton_isLocalCalling(napi_env env, napi_callback_info info)
{
bool isLocal = IPCSkeleton::IsLocalCalling();
napi_value napiValue = nullptr;
NAPI_CALL(env, napi_get_boolean(env, isLocal, &napiValue));
return napiValue;
napi_value global = nullptr;
napi_get_global(env, &global);
napi_value napiActiveStatus = nullptr;
napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
if (napiActiveStatus != nullptr) {
int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
napi_get_value_int32(env, napiActiveStatus, &activeStatus);
if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
napi_value isLocalCalling = nullptr;
napi_get_named_property(env, global, "isLocalCalling_", &isLocalCalling);
return isLocalCalling;
}
}
napi_value result = nullptr;
napi_get_boolean(env, true, &result);
return result;
}
napi_value NAPI_IPCSkeleton_flushCommands(napi_env env, napi_callback_info info)
@ -818,7 +910,7 @@ napi_value NAPI_IPCSkeleton_flushCommands(napi_env env, napi_callback_info info)
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 1");
@ -831,14 +923,87 @@ napi_value NAPI_IPCSkeleton_flushCommands(napi_env env, napi_callback_info info)
napi_value NAPI_IPCSkeleton_resetCallingIdentity(napi_env env, napi_callback_info info)
{
std::string identity = IPCSkeleton::ResetCallingIdentity();
napi_value napiValue = nullptr;
NAPI_CALL(env, napi_create_string_utf8(env, identity.c_str(), identity.length(), &napiValue));
return napiValue;
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) {
napi_value result = nullptr;
napi_create_string_utf8(env, "", 0, &result);
return result;
}
napi_value napiCallingPid = nullptr;
napi_get_named_property(env, global, "callingPid_", &napiCallingPid);
int32_t callerPid;
napi_get_value_int32(env, napiCallingPid, &callerPid);
napi_value napiCallingUid = nullptr;
napi_get_named_property(env, global, "callingUid_", &napiCallingUid);
int32_t callerUid;
napi_get_value_int32(env, napiCallingUid, &callerUid);
napi_value napiIsLocalCalling = nullptr;
napi_get_named_property(env, global, "isLocalCalling_", &napiIsLocalCalling);
bool isLocalCalling = true;
napi_get_value_bool(env, napiIsLocalCalling, &isLocalCalling);
if (isLocalCalling) {
int64_t identity = (static_cast<int64_t>(callerUid) << PID_LEN) | callerPid;
callerPid = getpid();
callerUid = getuid();
napi_value newCallingPid;
napi_create_int32(env, callerPid, &newCallingPid);
napi_set_named_property(env, global, "callingPid_", newCallingPid);
napi_value newCallingUid;
napi_create_int32(env, callerUid, &newCallingUid);
napi_set_named_property(env, global, "callingUid_", newCallingUid);
napi_value result;
napi_create_string_utf8(env, std::to_string(identity).c_str(), NAPI_AUTO_LENGTH, &result);
return result;
} else {
napi_value napiCallingDeviceID = nullptr;
napi_get_named_property(env, global, "callingDeviceID_", &napiCallingDeviceID);
size_t bufferSize = 0;
size_t maxLen = 4096;
napi_get_value_string_utf8(env, napiCallingDeviceID, 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, napiCallingDeviceID, stringValue, bufferSize + 1, &jsStringLength);
NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong");
std::string callerDeviceID = stringValue;
std::string token = std::to_string(((static_cast<int64_t>(callerUid) << PID_LEN) | callerPid));
std::string identity = callerDeviceID + token;
callerUid = getuid();
napi_value newCallingUid;
napi_create_int32(env, callerUid, &newCallingUid);
napi_set_named_property(env, global, "callingUid_", newCallingUid);
callerPid = getpid();
napi_value newCallingPid;
napi_create_int32(env, callerPid, &newCallingPid);
napi_set_named_property(env, global, "callingPid_", newCallingPid);
napi_value newCallingDeviceID = nullptr;
napi_get_named_property(env, global, "localDeviceID_", &newCallingDeviceID);
napi_set_named_property(env, global, "callingDeviceID_", newCallingDeviceID);
napi_value result = nullptr;
napi_create_string_utf8(env, identity.c_str(), NAPI_AUTO_LENGTH, &result);
return result;
}
}
napi_value NAPI_IPCSkeleton_setCallingIdentity(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) {
napi_value result = nullptr;
napi_get_boolean(env, true, &result);
return result;
}
size_t argc = 2;
size_t expectedArgc = 2;
napi_value argv[2] = {0};
@ -847,7 +1012,7 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
@ -859,20 +1024,72 @@ napi_value NAPI_IPCSkeleton_setCallingIdentity(napi_env env, napi_callback_info
napi_get_value_uint32(env, argv[1], &stringLength);
NAPI_ASSERT(env, stringLength < maxStrLen, "string length too large");
char stringValue[stringLength];
char stringValue[stringLength + 1];
size_t jsStringLength = 0;
napi_get_value_string_utf8(env, argv[0], stringValue, stringLength, &jsStringLength);
napi_get_value_string_utf8(env, argv[0], stringValue, stringLength + 1, &jsStringLength);
NAPI_ASSERT(env, jsStringLength == stringLength, "string length wrong");
std::string identity = stringValue;
bool result = IPCSkeleton::SetCallingIdentity(identity);
napi_value napiValue = nullptr;
NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
return napiValue;
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;
if (isLocalCalling) {
if (identity.empty()) {
napi_get_boolean(env, false, &result);
return result;
}
int64_t token = std::atoll(identity.c_str());
int callerUid = static_cast<int>(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_get_boolean(env, true, &result);
return result;
} else {
if (identity.empty() || identity.length() <= DEVICEID_LENGTH) {
napi_get_boolean(env, false, &result);
return result;
}
std::string deviceId = identity.substr(0, DEVICEID_LENGTH);
int64_t token = std::atoll(identity.substr(DEVICEID_LENGTH, identity.length() - DEVICEID_LENGTH).c_str());
int callerUid = static_cast<int>(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);
napi_get_boolean(env, true, &result);
return result;
}
}
napi_value NAPI_RemoteObject_getInterfaceDescriptor(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, 0, nullptr, &thisVar, nullptr);
sptr<IRemoteObject> nativeObject = NAPI_ohos_rpc_getNativeRemoteObject(env, thisVar);
std::u16string descriptor = nativeObject->GetObjectDescriptor();
napi_create_string_utf8(env, Str16ToStr8(descriptor).c_str(), NAPI_AUTO_LENGTH, &result);
return result;
}
napi_value NAPI_RemoteObject_getCallingPid(napi_env env, napi_callback_info info)
{
napi_value result;
napi_value result = nullptr;
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, 0, nullptr, &thisVar, nullptr);
sptr<IRemoteObject> nativeObject = NAPI_ohos_rpc_getNativeRemoteObject(env, thisVar);
@ -889,7 +1106,7 @@ napi_value NAPI_RemoteObject_getCallingPid(napi_env env, napi_callback_info info
napi_value NAPI_RemoteObject_getCallingUid(napi_env env, napi_callback_info info)
{
napi_value result;
napi_value result = nullptr;
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, 0, nullptr, &thisVar, nullptr);
sptr<IRemoteObject> nativeObject = NAPI_ohos_rpc_getNativeRemoteObject(env, thisVar);
@ -961,7 +1178,7 @@ napi_value NAPI_RemoteObject_sendRequest(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == expectedArgc, "requires 4 parameter");
napi_valuetype valueType;
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);
@ -1048,7 +1265,7 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == expectedArgc, "requires 4 parameter");
napi_valuetype valueType;
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);
@ -1091,6 +1308,11 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info)
return SendRequestPromise(env, target, code, data->GetMessageParcel(), reply->GetMessageParcel(), *option);
}
napi_value NAPI_RemoteProxy_queryLocalInterface(napi_env env, napi_callback_info info)
{
return nullptr;
}
napi_value NAPI_RemoteProxy_addDeathRecipient(napi_env env, napi_callback_info info)
{
DBINDER_LOGI("add death recipient");
@ -1101,7 +1323,7 @@ napi_value NAPI_RemoteProxy_addDeathRecipient(napi_env env, napi_callback_info i
void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 1");
napi_typeof(env, argv[1], &valueType);
@ -1151,7 +1373,7 @@ napi_value NAPI_RemoteProxy_removeDeathRecipient(napi_env env, napi_callback_inf
size_t expectedArgc = 2;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
NAPI_ASSERT(env, argc == expectedArgc, "requires 2 parameter");
napi_valuetype valueType;
napi_valuetype valueType = napi_null;
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 1");
napi_typeof(env, argv[1], &valueType);
@ -1332,7 +1554,7 @@ napi_value NAPIMessageOption_JS_Constructor(napi_env env, napi_callback_info inf
flags = jsFlags;
waittime = MessageOption::TF_WAIT_TIME;
} else {
napi_valuetype valueType;
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);
@ -1365,11 +1587,23 @@ EXTERN_C_START
napi_value NAPIMessageOptionExport(napi_env env, napi_value exports)
{
const std::string className = "MessageOption";
napi_value tfSync = nullptr;
napi_create_int32(env, MessageOption::TF_SYNC, &tfSync);
napi_value tfAsync = nullptr;
napi_create_int32(env, MessageOption::TF_ASYNC, &tfAsync);
napi_value tfFds = nullptr;
napi_create_int32(env, MessageOption::TF_ACCEPT_FDS, &tfFds);
napi_value tfWaitTime = nullptr;
napi_create_int32(env, MessageOption::TF_WAIT_TIME, &tfWaitTime);
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("getFlags", NapiOhosRpcMessageOptionGetFlags),
DECLARE_NAPI_FUNCTION("setFlags", NapiOhosRpcMessageOptionSetFlags),
DECLARE_NAPI_FUNCTION("getWaitTime", NapiOhosRpcMessageOptionGetWaittime),
DECLARE_NAPI_FUNCTION("setWaitTime", NapiOhosRpcMessageOptionSetWaittime),
DECLARE_NAPI_STATIC_PROPERTY("TF_SYNC", tfSync),
DECLARE_NAPI_STATIC_PROPERTY("TF_ASYNC", tfAsync),
DECLARE_NAPI_STATIC_PROPERTY("TF_ACCEPT_FDS", tfFds),
DECLARE_NAPI_STATIC_PROPERTY("TF_WAIT_TIME", tfWaitTime),
};
napi_value constructor = nullptr;
napi_define_class(env, className.c_str(), className.length(), NAPIMessageOption_JS_Constructor, nullptr,