Skip to content

Commit

Permalink
[hapjs-platform#628]V8升级到9.3.345.11
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-chai authored and jianghai33 committed Dec 12, 2023
1 parent 839910c commit 22d96ee
Show file tree
Hide file tree
Showing 39 changed files with 478 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;

import com.eclipsesource.v8.V8ArrayBuffer;
import com.eclipsesource.v8.utils.ArrayBuffer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -32,6 +35,7 @@
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.internal.http.HttpMethod;

import org.hapjs.bridge.CallbackHybridFeature;
import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
Expand Down Expand Up @@ -255,10 +259,11 @@ private RequestBody getSimplePostBody(Headers headers, Object objData, String pk
if (TextUtils.isEmpty(contentType)) {
contentType = RequestHelper.CONTENT_TYPE_OCTET_STREAM;
}
ByteBuffer b = ((ArrayBuffer) objData).getByteBuffer();
// copy memory to heap
byte[] buffer = new byte[b.remaining()];
b.get(buffer);

//copy memory to heap
V8ArrayBuffer v8ArrayBuffer = ((ArrayBuffer) objData).getV8ArrayBuffer();
byte[] buffer = new byte[v8ArrayBuffer.remaining()];
v8ArrayBuffer.get(buffer);
return RequestBody.create(MediaType.parse(contentType), buffer);
}

Expand Down Expand Up @@ -434,7 +439,11 @@ private void parseData(Request request, SerializeObject result, okhttp3.Response
throw new IOException("Fail to Parsing Data to Json!");
}
} else if (RESPONSE_TYPE_ARRAYBUFFER.equalsIgnoreCase(responseType)) {
result.put(RESULT_KEY_DATA, new ArrayBuffer(response.body().bytes()));
byte[] bytes = response.body().bytes();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_KEY_DATA, byteBuffer);
} else if (RESPONSE_TYPE_FILE.equalsIgnoreCase(responseType)) {
result.put(RESULT_KEY_DATA, parseFile(request, response));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
package org.hapjs.features;

import android.util.Log;
import com.eclipsesource.v8.utils.typedarrays.TypedArray;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.hapjs.bridge.FeatureExtension;
import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
import org.hapjs.bridge.annotation.ActionAnnotation;
import org.hapjs.bridge.annotation.FeatureExtensionAnnotation;
import org.hapjs.common.json.JSONObject;
import org.hapjs.render.jsruntime.serialize.SerializeObject;
import org.hapjs.render.jsruntime.serialize.TypedArrayProxy;
import org.json.JSONException;

@FeatureExtensionAnnotation(
Expand Down Expand Up @@ -67,13 +68,14 @@ private Response decode(Request request) throws JSONException {
String encoding = params.optString(PARAMS_ENCODING, "UTF-8");
boolean fatal = params.optBoolean(PARAMS_FATAL, false);
boolean ignoreBom = params.optBoolean(PARAMS_IGNORE_BOM, false);
TypedArray typedArray = params.optTypedArray(PARAMS_ARRAY_BUFFER);
if (typedArray == null) {
TypedArrayProxy typedArrayProxy = params.optTypedArrayProxy(PARAMS_ARRAY_BUFFER);
if (typedArrayProxy == null) {
jsonObject.put(KEY_ERROR_CODE, ERROR_CODE_TYPE_ERROR);
jsonObject.put(KEY_ERROR_MSG, "The encoded data was not valid.");
return new Response(ERROR_CODE_TYPE_ERROR, jsonObject);
}
return decode(encoding, typedArray.getByteBuffer(), ignoreBom, fatal);

return decode(encoding, typedArrayProxy.getBuffer(), ignoreBom, fatal);
} catch (Exception e) {
Log.e(TAG, "params are not valid.", e);
jsonObject.put(KEY_ERROR_CODE, Response.CODE_ILLEGAL_ARGUMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import android.os.Looper;
import android.util.Log;

import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;
import com.eclipsesource.v8.utils.typedarrays.UInt8Array;
import com.eclipsesource.v8.V8Value;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.ByteBuffer;
import org.hapjs.bridge.Callback;
import org.hapjs.bridge.CallbackContext;
import org.hapjs.bridge.CallbackHybridFeature;
Expand All @@ -36,6 +36,7 @@
import org.hapjs.render.Display;
import org.hapjs.render.jsruntime.serialize.JavaSerializeObject;
import org.hapjs.render.jsruntime.serialize.SerializeObject;
import org.hapjs.render.jsruntime.serialize.TypedArrayProxy;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -472,8 +473,10 @@ public void run() {
private SerializeObject makeResult(boolean isLastFrame, byte[] bytes) {
SerializeObject result = new JavaSerializeObject();
result.put(RESULT_IS_LAST_FRAME, isLastFrame);
UInt8Array array = new UInt8Array(new ArrayBuffer(bytes));
result.put(RESULT_FRAME_BUFFER, array);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_FRAME_BUFFER, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, byteBuffer));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import android.util.Log;
import android.util.Pair;
import androidx.annotation.NonNull;
import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;

import com.eclipsesource.v8.V8ArrayBuffer;
import com.eclipsesource.v8.utils.ArrayBuffer;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -38,6 +41,7 @@
import java.util.Vector;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.Semaphore;

import org.hapjs.bridge.CallbackContext;
import org.hapjs.bridge.CallbackHybridFeature;
import org.hapjs.bridge.FeatureExtension;
Expand Down Expand Up @@ -632,10 +636,10 @@ private void writeCharacteristic(final Request request) throws SerializeExceptio
String serviceUUID = params.getString(PARAM_SERVICE_UUID);
String charaUUID = params.getString(PARAM_CHARACTERISTIC_UUID);
ArrayBuffer value = (ArrayBuffer) params.get(PARAM_VALUE);
ByteBuffer b = value.getByteBuffer();
// copy memory to heap
byte[] buffer = new byte[b.remaining()];
b.get(buffer);
V8ArrayBuffer v8ArrayBuffer = value.getV8ArrayBuffer();
byte[] buffer = new byte[v8ArrayBuffer.remaining()];
v8ArrayBuffer.get(buffer);
BleManager.getInstance()
.writeCharacteristic(
address, serviceUUID, charaUUID, buffer, getOperationCallback(request));
Expand Down Expand Up @@ -863,7 +867,10 @@ public void onCharacteristicChanged(
serviceUUID.toUpperCase());
result.put(RESULT_CHARACTERISTIC_UUID,
characteristicUUID.toUpperCase());
result.put(RESULT_VALUE, new ArrayBuffer(data));
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length);
byteBuffer.put(data);
byteBuffer.rewind();
result.put(RESULT_VALUE, byteBuffer);
runCallbackContext(
EVENT_ON_CHARACTERISTIC_VALUE_CHANGE,
CODE_ON_CHARACTERISTIC_VALUE_CHANGE,
Expand Down Expand Up @@ -972,10 +979,17 @@ private JavaSerializeObject toJavaSerializeObject() {
new JavaSerializeArray(new JSONArray(mAdvertisServiceUUIDs)));
JavaSerializeObject serviceData = new JavaSerializeObject();
for (Pair<String, byte[]> d : mServiceData) {
serviceData.put(d.first, new ArrayBuffer(d.second));
byte[] bytes = d.second;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
serviceData.put(d.first, byteBuffer);
}
result.put(RESULT_SERVICE_DATA, serviceData);
result.put(RESULT_ADVERTIS_DATA, new ArrayBuffer(mAdvertisData));
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(mAdvertisData.length);
byteBuffer.put(mAdvertisData);
byteBuffer.rewind();
result.put(RESULT_ADVERTIS_DATA, byteBuffer);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@FeatureExtensionAnnotation(
name = RequestTask.FEATURE_NAME,
actions = {
@ActionAnnotation(name = RequestTask.ACTION_REQUEST, mode = FeatureExtension.Mode.SYNC_CALLBACK),
@ActionAnnotation(name = RequestTask.ACTION_REQUEST, mode = FeatureExtension.Mode.SYNC_CALLBACK, normalize = Extension.Normalize.RAW),
@ActionAnnotation(name = RequestTask.ACTION_ON_HEADERS_RECEIVED, mode = FeatureExtension.Mode.CALLBACK, multiple = Extension.Multiple.MULTI),
@ActionAnnotation(name = RequestTask.ACTION_OFF_HEADERS_RECEIVED, mode = FeatureExtension.Mode.SYNC, multiple = Extension.Multiple.MULTI),
@ActionAnnotation(name = RequestTask.ACTION_ABORT, mode = FeatureExtension.Mode.ASYNC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import android.text.TextUtils;
import android.util.Log;

import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;
import com.eclipsesource.v8.V8Value;

import org.hapjs.bridge.ExtensionManager;
import org.hapjs.bridge.InstanceManager;
Expand All @@ -27,6 +27,7 @@
import org.hapjs.render.jsruntime.serialize.SerializeException;
import org.hapjs.render.jsruntime.serialize.SerializeHelper;
import org.hapjs.render.jsruntime.serialize.SerializeObject;
import org.hapjs.render.jsruntime.serialize.TypedArrayProxy;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void execute() {
String pkg = mRequest.getApplicationContext().getPackage();
SerializeObject reader = mRequest.getSerializeParams();
String url = reader.getString(RequestTask.PARAMS_KEY_URL);
String responseType = reader.optString(RequestTask.PARAMS_KEY_RESPOSNE_TYPE, RequestTask.RESPONSE_TYPE_TEXT);
String responseType = reader.optString(RequestTask.PARAMS_KEY_RESPOSNE_TYPE, RequestTask.RESPONSE_TYPE_TEXT).toLowerCase();
String dataType = reader.optString(RequestTask.PARAMS_KEY_DATA_TYPE, RequestTask.DATA_TYPE_JSON);
Object dataObj = reader.opt(RequestTask.PARAMS_KEY_DATA);
SerializeObject jsonHeader = reader.optSerializeObject(RequestTask.PARAMS_KEY_HEADER);
Expand Down Expand Up @@ -252,16 +253,20 @@ private RequestBody getSimplePostBody(Headers headers, Object objData)
return RequestBody.create(
MediaType.parse(CONTENT_TYPE_FORM_URLENCODED),
textParams);
} else if (objData instanceof ArrayBuffer) {
} else if (objData instanceof ByteBuffer) {
Log.d(TAG, "getSimplePost objData is ArrayBuffer, contentType=" + contentType);
if (TextUtils.isEmpty(contentType)) {
contentType = CONTENT_TYPE_JSON;
}
ByteBuffer b = ((ArrayBuffer) objData).getByteBuffer();
//copy memory to heap
byte[] buffer = new byte[b.remaining()];
b.get(buffer);
return RequestBody.create(MediaType.parse(contentType), buffer);
ByteBuffer byteBuffer = (ByteBuffer) objData;
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);
try {
return RequestBody.create(MediaType.parse(contentType), new JSONArray(buffer).toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

contentType = TextUtils.isEmpty(contentType) ? CONTENT_TYPE_TEXT_PLAIN : contentType;
Expand Down Expand Up @@ -319,7 +324,12 @@ public void onResponse(Call call, okhttp3.Response response) throws IOException
result.put(RequestTask.RESULT_KEY_STATUS_CODE, response.code());
result.put(RequestTask.RESULT_KEY_HEADER, parseHeaders(response));
if (response.body() != null) {
result.put(RequestTask.RESULT_KEY_DATA, new ArrayBuffer(response.body().bytes()));
byte[] bytes = response.body().bytes();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();

result.put(RequestTask.RESULT_KEY_DATA, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, byteBuffer));
} else {
Log.w(TAG, "response body is invalid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import android.util.Log;
import android.webkit.URLUtil;

import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;

import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
import org.hapjs.common.utils.FileHelper;
Expand All @@ -23,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Locale;

import okhttp3.Call;
Expand Down Expand Up @@ -97,7 +96,11 @@ private void parseData(SerializeObject result, okhttp3.Response response, String
throw new IOException("Fail to Parsing Data to Json!");
}
} else if (RESPONSE_TYPE_ARRAYBUFFER.equalsIgnoreCase(responseType)) {
result.put(RESULT_KEY_DATA, new ArrayBuffer(response.body().bytes()));
byte[] bytes = response.body().bytes();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_KEY_DATA, byteBuffer);
} else if (RESPONSE_TYPE_FILE.equalsIgnoreCase(responseType)) {
result.put(RESULT_KEY_DATA, parseFile(response));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import android.nfc.NfcAdapter;
import android.nfc.tech.IsoDep;

import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;

import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
import org.hapjs.features.nfc.base.BaseTagTechInstance;
import org.hapjs.render.jsruntime.serialize.JavaSerializeObject;

import java.io.IOException;
import java.nio.ByteBuffer;

public class IsoDepInstance extends BaseTagTechInstance {

Expand Down Expand Up @@ -44,7 +43,10 @@ public byte[] transceive(byte[] buffer) throws IOException {
public void getHistoricalBytes(Request request) {
byte[] historicalBytes = mIsoDep.getHistoricalBytes();
JavaSerializeObject result = new JavaSerializeObject();
result.put(NFC.RESULT_HISTORICAL_BYTES, new ArrayBuffer(historicalBytes));
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(historicalBytes.length);
byteBuffer.put(historicalBytes);
byteBuffer.rewind();
result.put(NFC.RESULT_HISTORICAL_BYTES, byteBuffer);
request.getCallback().callback(new Response(result));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@ActionAnnotation(name = NFC.ACTION_SET_TIMEOUT, mode = FeatureExtension.Mode.ASYNC, permissions = {Manifest.permission.NFC}),
@ActionAnnotation(name = NFC.ACTION_TRANSCEIVE, mode = FeatureExtension.Mode.ASYNC, normalize = FeatureExtension.Normalize.RAW, permissions = {Manifest.permission.NFC}),
// Ndef
@ActionAnnotation(name = NFC.ACTION_WRITE_NDEF_MESSAGE, mode = FeatureExtension.Mode.CALLBACK, permissions = {Manifest.permission.NFC}),
@ActionAnnotation(name = NFC.ACTION_WRITE_NDEF_MESSAGE, mode = FeatureExtension.Mode.CALLBACK, permissions = {Manifest.permission.NFC}, normalize = FeatureExtension.Normalize.RAW),
// IsoDep
@ActionAnnotation(name = NFC.ACTION_GET_HISTORICAL_BYTES, mode = FeatureExtension.Mode.ASYNC, normalize = FeatureExtension.Normalize.RAW),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import android.text.TextUtils;
import android.util.Log;

import com.eclipsesource.v8.utils.typedarrays.ArrayBuffer;

import org.hapjs.bridge.HybridManager;
import org.hapjs.bridge.InstanceManager;
import org.hapjs.bridge.LifecycleListener;
Expand All @@ -37,6 +35,7 @@
import org.hapjs.render.jsruntime.serialize.SerializeArray;
import org.hapjs.render.jsruntime.serialize.SerializeObject;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class NFCAdapterInstance extends BaseInstance {
Expand Down Expand Up @@ -212,7 +211,10 @@ private void onTagDiscovered(Intent intent) {

byte[] id = mDiscoveredTag.getId();
Log.d(TAG, "id: " + Arrays.toString(id));
resultObj.put(RESULT_ID, new ArrayBuffer(id));
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(id.length);
byteBuffer.put(id);
byteBuffer.rewind();
resultObj.put(RESULT_ID, byteBuffer);
} else {
Log.e(TAG, "null of discovered tag");
}
Expand Down Expand Up @@ -264,9 +266,18 @@ private SerializeArray getMessages(Parcelable[] rawMessages) {
SerializeArray arrayRecord = new JavaSerializeArray();
for (NdefRecord record : ndefRecords) {
SerializeObject recordObj = new JavaSerializeObject();
recordObj.put(RESULT_MESSAGES_RECORD_ID, new ArrayBuffer(record.getId()));
recordObj.put(RESULT_MESSAGES_RECORD_PAYLOAD, new ArrayBuffer(record.getPayload()));
recordObj.put(RESULT_MESSAGES_RECORD_TYPE, new ArrayBuffer(record.getType()));
ByteBuffer idByteBuffer = ByteBuffer.allocateDirect(record.getId().length);
idByteBuffer.put(record.getId());
idByteBuffer.rewind();
recordObj.put(RESULT_MESSAGES_RECORD_ID, idByteBuffer);
ByteBuffer payloadByteBuffer = ByteBuffer.allocateDirect(record.getPayload().length);
payloadByteBuffer.put(record.getPayload());
payloadByteBuffer.rewind();
recordObj.put(RESULT_MESSAGES_RECORD_PAYLOAD, payloadByteBuffer);
ByteBuffer typeByteBuffer = ByteBuffer.allocateDirect(record.getType().length);
typeByteBuffer.put(record.getType());
typeByteBuffer.rewind();
recordObj.put(RESULT_MESSAGES_RECORD_TYPE, typeByteBuffer);
recordObj.put(RESULT_MESSAGES_RECORD_TNF, record.getTnf());
arrayRecord.put(recordObj);
}
Expand Down
Loading

0 comments on commit 22d96ee

Please sign in to comment.