目录

Mutual Invoking Between the Application and the Frontend Page (C/C++)

This guide applies to the communication between ArkWeb applications and frontend pages. You can use the ArkWeb native APIs to conduct the service communication mechanism (native JSBridge for short) based on the application architecture.

For details about how to optimize the performance of JSBridge, see JSBridge Optimization Solution.

Applicable Application Architecture

If an application is developed using a mix of ArkTS and C++, or if the application has an architecture close to that of an applet and a built-in C++ environment, you are advised to use ArkWeb_ControllerAPI and ArkWeb_ComponentAPI provided by ArkWeb on the native side to implement the JSBridge capabilities.

The preceding figure shows a general architecture of applets with universal applicability. In this architecture, the logical layer depends on a JavaScript runtime built in an application, and the runtime runs in an existing C++ environment. The logic layer can communicate with the view layer (in which ArkWeb serves as the renderer) in the C++ environment through the native API, instead of using the ArkTS JSBridge API in the ArkTS environment.

The figure on the left shows that the application needs to invoke the ArkTS environment and then the C++ environment to build an applet using the ArkTS JSBridge API. Using the native JSBridge API is more efficient because the switching between the ArkTS and C++ environments is not required, as shown in the figure on the right.

The native JSBridge APIs are provided to avoid unnecessary switching to the ArkTS environment and allow callbacks to run in non-UI threads to avoid UI blocking.

[h2]Substitute APIs

Unrecommended API Substitute API Description
ArkWeb_OnJavaScriptProxyCallback ArkWeb_OnJavaScriptProxyCallbackWithResult Defines a callback used when the proxy method is executed.
ArkWeb_ProxyMethod ArkWeb_ProxyMethodWithResult Defines a proxy method.
ArkWeb_ProxyObject ArkWeb_ProxyObjectWithResult Defines a proxy object.
registerJavaScriptProxy registerJavaScriptProxyEx Registers a JavaScript object with the window. Synchronous APIs of this object can then be invoked in the window.
registerAsyncJavaScriptProxy registerAsyncJavaScriptProxyEx Registers a JavaScript object with the window. Asynchronous APIs of this object can then be invoked in the window.

[h2]Binding the Native API to ArkWeb

[h2]Obtaining API Struct Using the Native API

On the ArkWeb Native side, you must first obtain the API struct before you can call the Native APIs in it. The ArkWeb Native-side APIs are obtained through the OH_ArkWeb_GetNativeAPI function. Depending on the input parameter type, you can obtain the ArkWeb_ControllerAPI and ArkWeb_ComponentAPI structs respectively. Among them, ArkWeb_ControllerAPI corresponds to the ArkTS-side WebviewController API, and ArkWeb_ComponentAPI corresponds to the ArkTS-side ArkWeb component API.

static ArkWeb_ControllerAPI *controller = nullptr;
static ArkWeb_ComponentAPI *component = nullptr;
// ...
controller = reinterpret_cast<ArkWeb_ControllerAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_CONTROLLER));
component = reinterpret_cast<ArkWeb_ComponentAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_COMPONENT));

[h2]Registering Component Lifecycle Callback on the Native Side

Register the component lifecycle callback using ArkWeb_ComponentAPI. Before calling the API, you are advised to use ARKWEB_MEMBER_MISSING to check whether the function struct has the corresponding pointer to avoid crash caused by mismatch between the SDK and the device ROM.

if (!ARKWEB_MEMBER_MISSING(component, onControllerAttached)) {
    component->onControllerAttached(
        webTagValue, ValidCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onControllerAttached func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onPageBegin)) {
    component->onPageBegin(webTagValue, LoadStartCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onPageBegin func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onPageEnd)) {
    component->onPageEnd(webTagValue, LoadEndCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onPageEnd func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onDestroy)) {
    component->onDestroy(webTagValue, DestroyCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onDestroy func not exist");
}

[h2]Invoking Application Functions on the Frontend Page

Register the application functions with the frontend page through registerJavaScriptProxyEx. The registration takes effect after the next loading or reloading.

// Register an object.
OH_LOG_Print(
    LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit registerJavaScriptProxyEx begin");
ArkWeb_ProxyMethodWithResult method1 = {
    "method1", ProxyMethod1, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
ArkWeb_ProxyMethodWithResult method2 = {
    "method2", ProxyMethod2, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
ArkWeb_ProxyMethodWithResult methodList[2] = {method1, method2};
// Call the Native Development Kit API to register an object.
// With this registration, the H5 page can call ProxyMethod1 and ProxyMethod2 in this file through proxy.method1 and proxy.method2.
ArkWeb_ProxyObjectWithResult proxyObject = {"ndkProxy", methodList, 2};
// An empty permission parameter indicates that no permission control is performed.
controller->registerJavaScriptProxyEx(webTag, &proxyObject, "");
{
  "javascriptProxyPermission": {
    "urlPermissionList": [       // Object-level permission. If it is granted, all methods are available.
      {
        "scheme": "resource",    // Exact match. The value cannot be empty.
        "host": "rawfile",       // Exact match. The value cannot be empty.
        "port": "",              // Exact match. If the value is empty, it is not checked.
        "path": ""               // Prefix match. If the value is empty, it is not checked.
      },
      {
        "scheme": "https",       // Exact match. The value cannot be empty.
        "host": "xxx.com",       // Exact match. The value cannot be empty.
        "port": "8080",          // Exact match. If the value is empty, it is not checked.
        "path": "a/b/c"          // Prefix match. If the value is empty, it is not checked.
      }
    ],
    "methodList": [
      {
        "methodName": "test",
        "urlPermissionList": [   // Method-level permission.
          {
            "scheme": "https",   // Exact match. The value cannot be empty.
            "host": "xxx.com",   // Exact match. The value cannot be empty.
            "port": "",          // Exact match. If the value is empty, it is not checked.
            "path": ""           // Prefix match. If the value is empty, it is not checked.
          },
          {
            "scheme": "resource",// Exact match. The value cannot be empty.
            "host": "rawfile",   // Exact match. The value cannot be empty.
            "port": "",          // Exact match. If the value is empty, it is not checked.
            "path": ""           // Prefix match. If the value is empty, it is not checked.
          }
        ]
      },
      {
        "methodName": "test11",
        "urlPermissionList": [   // Method-level permission.
          {
            "scheme": "q",       // Exact match. The value cannot be empty.
            "host": "r",         // Exact match. The value cannot be empty.
            "port": "",          // Exact match. If the value is empty, it is not checked.
            "path": "t"          // Prefix match. If the value is empty, it is not checked.
          },
          {
            "scheme": "u",       // Exact match. The value cannot be empty.
            "host": "v",         // Exact match. The value cannot be empty.
            "port": "",          // Exact match. If the value is empty, it is not checked.
            "path": ""           // Prefix match. If the value is empty, it is not checked.
          }
        ]
      }
    ]
  }
}

[h2]Invoking Frontend Page Functions on the Application

Use runJavaScript to call the frontend page function.

// Construct a struct executed in runJS.
char* jsCode = "runJSRetStr()";
ArkWeb_JavaScriptObject object = {(uint8_t *)jsCode, bufferSize, &JSBridgeObject::StaticRunJavaScriptCallback,
                                     static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
// Call runJSRetStr() of the frontend page.
controller->runJavaScript(webTagValue, &object);

[h2]Sample Code

Using the Native API to Implement JSBridge

[h2]Binding the Native API to ArkWeb

[h2]Obtaining API Struct Using the Native API

On the ArkWeb Native side, you must first obtain the API struct before you can call the Native APIs in it. The ArkWeb Native-side APIs are obtained through the OH_ArkWeb_GetNativeAPI function. Depending on the input parameter type, you can obtain the ArkWeb_ControllerAPI and ArkWeb_ComponentAPI function pointer structs respectively. Among them, ArkWeb_ControllerAPI corresponds to the ArkTS-side WebviewController API, and ArkWeb_ComponentAPI corresponds to the ArkTS-side ArkWeb component API.

static ArkWeb_ControllerAPI *controller = nullptr;
static ArkWeb_ComponentAPI *component = nullptr;
// ...
controller = reinterpret_cast<ArkWeb_ControllerAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_CONTROLLER));
component = reinterpret_cast<ArkWeb_ComponentAPI *>(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_COMPONENT));

[h2]Registering Component Lifecycle Callback on the Native Side

Register the component lifecycle callback using ArkWeb_ComponentAPI. Before calling the API, you are advised to use ARKWEB_MEMBER_MISSING to check whether the function struct has the corresponding pointer to avoid crash caused by mismatch between the SDK and the device ROM.

if (!ARKWEB_MEMBER_MISSING(component, onControllerAttached)) {
    component->onControllerAttached(
        webTagValue, ValidCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onControllerAttached func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onPageBegin)) {
    component->onPageBegin(webTagValue, LoadStartCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onPageBegin func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onPageEnd)) {
    component->onPageEnd(webTagValue, LoadEndCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onPageEnd func not exist");
}

if (!ARKWEB_MEMBER_MISSING(component, onDestroy)) {
    component->onDestroy(webTagValue, DestroyCallback, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr()));
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ArkWeb", "component onDestroy func not exist");
}

[h2]Invoking Application Functions on the Frontend Page

Register the application functions with the frontend page through registerJavaScriptProxy. The registration takes effect after the next loading or reloading.

// Register an object.
OH_LOG_Print(
    LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ArkWeb", "Native Development Kit registerJavaScriptProxy begin");
ArkWeb_ProxyMethod method1 = {"method1", ProxyMethod1, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
ArkWeb_ProxyMethod method2 = {"method2", ProxyMethod2, static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
ArkWeb_ProxyMethod methodList[2] = {method1, method2};
// Call the Native Development Kit API to register the object.
// In this case, you can use proxy.method1 and proxy.method2 to call ProxyMethod1 and ProxyMethod2 in this file on HTML5 pages.
ArkWeb_ProxyObject proxyObject = {"ndkProxy", methodList, 2};
controller->registerJavaScriptProxy(webTag, &proxyObject);

[h2]Invoking Frontend Page Functions on the Application

Use runJavaScript to call the frontend page function.

// Construct a struct executed in runJS.
const char* jsCode = "runJSRetStr()";
ArkWeb_JavaScriptObject object = {(uint8_t *)jsCode, bufferSize, &JSBridgeObject::StaticRunJavaScriptCallback,
                                     static_cast<void *>(jsbridge_object_ptr->GetWeakPtr())};
// Call runJSRetStr() of the frontend page.
controller->runJavaScript(webTagValue, &object);

[h2]Sample Code