Skip to content

Commit

Permalink
[hapjs-platform#629]支持沙箱运行模式
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghai33 committed Dec 12, 2023
1 parent fb0688b commit 2ad3f50
Show file tree
Hide file tree
Showing 101 changed files with 6,171 additions and 2,189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class Share extends FeatureExtension {

private ShareAction mShareAction;
private static final int MSG_ONLINE_SHARE = 0x01;
private Handler mHandler = new Handler() {
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
import org.hapjs.logging.CardLogManager;
import org.hapjs.model.AppInfo;
import org.hapjs.render.jsruntime.JsThreadFactory;
import org.hapjs.render.jsruntime.SandboxProvider;
import org.hapjs.render.jsruntime.SandboxProviderImpl;
import org.hapjs.render.jsruntime.module.ModuleBridge;
import org.hapjs.runtime.HapEngine;
import org.hapjs.runtime.ProviderManager;
import org.hapjs.runtime.ResourceConfig;
import org.hapjs.runtime.Runtime;

Expand All @@ -54,6 +57,7 @@ public void init(Context context, String platform) {
Log.i(TAG, "CardServiceWorker init, platform=" + platform);
ResourceConfig.getInstance().init(context, platform);
Runtime.setPlatform(platform);
ProviderManager.getDefault().addProvider(SandboxProvider.NAME, new SandboxProviderImpl());
Runtime.getInstance().onCreate(context);
configBlacklist();
JsThreadFactory.getInstance().preload(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,12 @@ private RequestBody getSimplePostBody(Headers headers, Object objData, String pk
String textParams = joinParams((SerializeObject) objData);
return RequestBody.create(
MediaType.parse(RequestHelper.CONTENT_TYPE_FORM_URLENCODED), textParams);
} else if (objData instanceof ArrayBuffer) {
} else if (objData instanceof byte[]) {
if (TextUtils.isEmpty(contentType)) {
contentType = RequestHelper.CONTENT_TYPE_OCTET_STREAM;
}

//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);
return RequestBody.create(MediaType.parse(contentType), (byte[]) objData);
}

contentType =
Expand Down Expand Up @@ -440,10 +436,7 @@ private void parseData(Request request, SerializeObject result, okhttp3.Response
}
} else if (RESPONSE_TYPE_ARRAYBUFFER.equalsIgnoreCase(responseType)) {
byte[] bytes = response.body().bytes();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_KEY_DATA, byteBuffer);
result.put(RESULT_KEY_DATA, bytes);
} 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 @@ -473,10 +473,7 @@ public void run() {
private SerializeObject makeResult(boolean isLastFrame, byte[] bytes) {
SerializeObject result = new JavaSerializeObject();
result.put(RESULT_IS_LAST_FRAME, isLastFrame);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_FRAME_BUFFER, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, byteBuffer));
result.put(RESULT_FRAME_BUFFER, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, bytes));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,7 @@ private void writeCharacteristic(final Request request) throws SerializeExceptio
String address = params.getString(PARAM_DEVICE_ID);
String serviceUUID = params.getString(PARAM_SERVICE_UUID);
String charaUUID = params.getString(PARAM_CHARACTERISTIC_UUID);
ArrayBuffer value = (ArrayBuffer) params.get(PARAM_VALUE);
// copy memory to heap
V8ArrayBuffer v8ArrayBuffer = value.getV8ArrayBuffer();
byte[] buffer = new byte[v8ArrayBuffer.remaining()];
v8ArrayBuffer.get(buffer);
byte[] buffer = (byte[]) params.get(PARAM_VALUE);
BleManager.getInstance()
.writeCharacteristic(
address, serviceUUID, charaUUID, buffer, getOperationCallback(request));
Expand Down Expand Up @@ -867,10 +863,7 @@ public void onCharacteristicChanged(
serviceUUID.toUpperCase());
result.put(RESULT_CHARACTERISTIC_UUID,
characteristicUUID.toUpperCase());
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length);
byteBuffer.put(data);
byteBuffer.rewind();
result.put(RESULT_VALUE, byteBuffer);
result.put(RESULT_VALUE, data);
runCallbackContext(
EVENT_ON_CHARACTERISTIC_VALUE_CHANGE,
CODE_ON_CHARACTERISTIC_VALUE_CHANGE,
Expand Down Expand Up @@ -980,16 +973,10 @@ private JavaSerializeObject toJavaSerializeObject() {
JavaSerializeObject serviceData = new JavaSerializeObject();
for (Pair<String, byte[]> d : mServiceData) {
byte[] bytes = d.second;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
serviceData.put(d.first, byteBuffer);
serviceData.put(d.first, bytes);
}
result.put(RESULT_SERVICE_DATA, serviceData);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(mAdvertisData.length);
byteBuffer.put(mAdvertisData);
byteBuffer.rewind();
result.put(RESULT_ADVERTIS_DATA, byteBuffer);
result.put(RESULT_ADVERTIS_DATA, mAdvertisData);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -253,17 +252,14 @@ private RequestBody getSimplePostBody(Headers headers, Object objData)
return RequestBody.create(
MediaType.parse(CONTENT_TYPE_FORM_URLENCODED),
textParams);
} else if (objData instanceof ByteBuffer) {
} else if (objData instanceof byte[]) {
Log.d(TAG, "getSimplePost objData is ArrayBuffer, contentType=" + contentType);
if (TextUtils.isEmpty(contentType)) {
contentType = CONTENT_TYPE_JSON;
}
//copy memory to heap
ByteBuffer byteBuffer = (ByteBuffer) objData;
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);

try {
return RequestBody.create(MediaType.parse(contentType), new JSONArray(buffer).toString());
return RequestBody.create(MediaType.parse(contentType), new JSONArray(objData).toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -325,11 +321,8 @@ public void onResponse(Call call, okhttp3.Response response) throws IOException
result.put(RequestTask.RESULT_KEY_HEADER, parseHeaders(response));
if (response.body() != null) {
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));
result.put(RequestTask.RESULT_KEY_DATA, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, bytes));
} else {
Log.w(TAG, "response body is invalid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ private void parseData(SerializeObject result, okhttp3.Response response, String
}
} else if (RESPONSE_TYPE_ARRAYBUFFER.equalsIgnoreCase(responseType)) {
byte[] bytes = response.body().bytes();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();
result.put(RESULT_KEY_DATA, byteBuffer);
result.put(RESULT_KEY_DATA, bytes);
} 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 @@ -43,10 +43,7 @@ public byte[] transceive(byte[] buffer) throws IOException {
public void getHistoricalBytes(Request request) {
byte[] historicalBytes = mIsoDep.getHistoricalBytes();
JavaSerializeObject result = new JavaSerializeObject();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(historicalBytes.length);
byteBuffer.put(historicalBytes);
byteBuffer.rewind();
result.put(NFC.RESULT_HISTORICAL_BYTES, byteBuffer);
result.put(NFC.RESULT_HISTORICAL_BYTES, historicalBytes);
request.getCallback().callback(new Response(result));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ private void onTagDiscovered(Intent intent) {

byte[] id = mDiscoveredTag.getId();
Log.d(TAG, "id: " + Arrays.toString(id));
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(id.length);
byteBuffer.put(id);
byteBuffer.rewind();
resultObj.put(RESULT_ID, byteBuffer);
resultObj.put(RESULT_ID, id);
} else {
Log.e(TAG, "null of discovered tag");
}
Expand Down Expand Up @@ -266,18 +263,9 @@ private SerializeArray getMessages(Parcelable[] rawMessages) {
SerializeArray arrayRecord = new JavaSerializeArray();
for (NdefRecord record : ndefRecords) {
SerializeObject recordObj = new JavaSerializeObject();
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_ID, record.getId());
recordObj.put(RESULT_MESSAGES_RECORD_PAYLOAD, record.getPayload());
recordObj.put(RESULT_MESSAGES_RECORD_TYPE, record.getType());
recordObj.put(RESULT_MESSAGES_RECORD_TNF, record.getTnf());
arrayRecord.put(recordObj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public byte[] transceive(byte[] buffer) throws IOException {
public void getAtqa(Request request) {
byte[] atqa = mNfcA.getAtqa();
SerializeObject resultObj = new JavaSerializeObject();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(atqa.length);
byteBuffer.put(atqa);
byteBuffer.rewind();
resultObj.put(NFC.RESULT_ATQA, byteBuffer);
resultObj.put(NFC.RESULT_ATQA, atqa);
request.getCallback().callback(new Response(resultObj));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public void transceive(Request request) {

byte[] resultBytes = transceive(transceiveParams);
SerializeObject resultObj = new JavaSerializeObject();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(resultBytes.length);
byteBuffer.put(resultBytes);
byteBuffer.rewind();
resultObj.put(NFC.RESULT_TRANSCEIVE_DATA, byteBuffer);
resultObj.put(NFC.RESULT_TRANSCEIVE_DATA, resultBytes);
request.getCallback().callback(new Response(resultObj));
} else {
request.getCallback().callback(new Response(NFCConstants.CODE_INVALID_PARAMETER, NFCConstants.DESC_INVALID_PARAMETER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ public Response readArrayBuffer(
"Fail to get resource by " + internalUri);
}
InputStream input = resource.openInputStream();
ByteBuffer byteBuffer = FileUtils.readStreamAsBuffer(input, position, length, true);
byte[] bytes = FileUtils.readStreamAsBytes(input, position, length, true);
SerializeObject result = new JavaSerializeObject();
result.put(FileStorageFeature.RESULT_BUFFER, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, byteBuffer));
result.put(FileStorageFeature.RESULT_BUFFER, new TypedArrayProxy(V8Value.UNSIGNED_INT_8_ARRAY, bytes));
return new Response(result);
} catch (FileNotFoundException e) {
return new Response(Response.CODE_FILE_NOT_FOUND, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,8 @@ private void onSocketMessage(ByteString byteString) {
if (request != null) {
byte[] bytes = byteString != null ? byteString.toByteArray() : new byte[0];
SerializeObject serializeObject = new JavaSerializeObject();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytes.length);
byteBuffer.put(bytes);
byteBuffer.rewind();

serializeObject.put(WebSocket.RESULT_DATA, byteBuffer);
serializeObject.put(WebSocket.RESULT_DATA, bytes);
request.getCallback().callback(new Response(serializeObject));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ private void send(Request request) throws Exception {
ByteBuffer buffer = ((TypedArrayProxy) dataObj).getBuffer();
ByteString data = getByteString(buffer);
sendOk = null != data && socketTask.send(data);
} else if (dataObj instanceof byte[]) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(((byte[]) dataObj).length);
byteBuffer.put((byte[]) dataObj);
byteBuffer.rewind();
ByteString data = getByteString(byteBuffer);
sendOk = null != data && socketTask.send(data);
} else if (null != dataObj) {
String str = dataObj.toString();
sendOk = !TextUtils.isEmpty(str) && socketTask.send(str);
Expand Down
4 changes: 3 additions & 1 deletion core/runtime/android/runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:${rootProject.recyclerviewVersion}"
implementation "androidx.constraintlayout:constraintlayout:${rootProject.androidXconstraintlayoutVersion}"
implementation "com.google.android.material:material:${rootProject.androidXVersion}"

implementation 'com.esotericsoftware:kryo:5.5.0'
}

apply plugin: 'com.moowork.node'
Expand All @@ -117,4 +119,4 @@ task copyInfrasJS(type: Copy, dependsOn: 'buildInfrasJS') {
task cleanJS(type: Delete) {
delete 'src/main/assets/js'
}
clean.dependsOn cleanJS
clean.dependsOn cleanJS
Binary file modified core/runtime/android/runtime/libs/jsenv-no-v8symbols.aar
Binary file not shown.
8 changes: 8 additions & 0 deletions core/runtime/android/runtime/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@
-keep class * extends com.eclipsesource.v8.V8Object {
public *;
}

-keep class * extends com.eclipsesource.v8.V8Object$Undefined {
*;
}

-keep class org.hapjs.render.jsruntime.V8InspectorNative {
public *;
}
21 changes: 21 additions & 0 deletions core/runtime/android/runtime/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>

<service android:name=".sandbox.SandboxService$Sandbox0"
android:isolatedProcess="true"
android:process=":Sandbox0"
android:exported="false" />
<service android:name=".sandbox.SandboxService$Sandbox1"
android:isolatedProcess="true"
android:process=":Sandbox1"
android:exported="false" />
<service android:name=".sandbox.SandboxService$Sandbox2"
android:isolatedProcess="true"
android:process=":Sandbox2"
android:exported="false" />
<service android:name=".sandbox.SandboxService$Sandbox3"
android:isolatedProcess="true"
android:process=":Sandbox3"
android:exported="false" />
<service android:name=".sandbox.SandboxService$Sandbox4"
android:isolatedProcess="true"
android:process=":Sandbox4"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright (C) 2023, hapjs.org. All rights reserved.
*/
package org.hapjs.analyzer.model;

parcelable LogData;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (C) 2023, hapjs.org. All rights reserved.
*/

package org.hapjs.runtime.sandbox;

import org.hapjs.analyzer.model.LogData;

interface ILogListener {
void onLog(in List<LogData> logs);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2023, hapjs.org. All rights reserved.
*/
package org.hapjs.runtime.sandbox;

interface ILogProvider {
void logCountEvent(String appPackage, String category, String key);

void logCountEventWithParams(String appPackage, String category, String key, in Map params);

void logCalculateEvent(String appPackage, String category, String key, long value);

void logCalculateEventWithParams(String appPackage, String category, String key, long value, in Map params);

void logNumericPropertyEvent(String appPackage, String category, String key, long value);

void logNumericPropertyEventWithParams(String appPackage, String category, String key, long value, in Map params);

void logStringPropertyEvent(String appPackage, String category, String key, String value);

void logStringPropertyEventWithParams(String appPackage, String category, String key, String value, in Map params);
}
Loading

0 comments on commit 2ad3f50

Please sign in to comment.