10826e83eSopenharmony_ci/* 20826e83eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 30826e83eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40826e83eSopenharmony_ci * you may not use this file except in compliance with the License. 50826e83eSopenharmony_ci * You may obtain a copy of the License at 60826e83eSopenharmony_ci * 70826e83eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80826e83eSopenharmony_ci * 90826e83eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100826e83eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110826e83eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120826e83eSopenharmony_ci * See the License for the specific language governing permissions and 130826e83eSopenharmony_ci * limitations under the License. 140826e83eSopenharmony_ci */ 150826e83eSopenharmony_ci 160826e83eSopenharmony_ci#include "native_interface_arkweb.h" 170826e83eSopenharmony_ci 180826e83eSopenharmony_ci#include <memory> 190826e83eSopenharmony_ci#include <mutex> 200826e83eSopenharmony_ci#include <unordered_map> 210826e83eSopenharmony_ci#include <vector> 220826e83eSopenharmony_ci 230826e83eSopenharmony_ci#include "native_arkweb_utils.h" 240826e83eSopenharmony_ci#include "native_javascript_execute_callback.h" 250826e83eSopenharmony_ci#include "nweb.h" 260826e83eSopenharmony_ci#include "nweb_log.h" 270826e83eSopenharmony_ci 280826e83eSopenharmony_cinamespace { 290826e83eSopenharmony_cistd::mutex g_mtxMap; // the mutex to protect the shared resource 300826e83eSopenharmony_cistd::unordered_map<std::string, NativeArkWeb_OnValidCallback> g_validMap; 310826e83eSopenharmony_cistd::unordered_map<std::string, NativeArkWeb_OnDestroyCallback> g_destroyMap; 320826e83eSopenharmony_ci} // namespace 330826e83eSopenharmony_ci 340826e83eSopenharmony_cinamespace OHOS::NWeb { 350826e83eSopenharmony_ci 360826e83eSopenharmony_ciclass NWebJsProxyCallbackImpl : public NWebJsProxyCallback { 370826e83eSopenharmony_cipublic: 380826e83eSopenharmony_ci NWebJsProxyCallbackImpl(const char *methodName, NativeArkWeb_OnJavaScriptProxyCallback methodCallback) 390826e83eSopenharmony_ci : methodName_(methodName), methodCallback_(methodCallback) { 400826e83eSopenharmony_ci } 410826e83eSopenharmony_ci ~NWebJsProxyCallbackImpl() = default; 420826e83eSopenharmony_ci 430826e83eSopenharmony_ci std::string GetMethodName() override 440826e83eSopenharmony_ci { 450826e83eSopenharmony_ci return methodName_; 460826e83eSopenharmony_ci } 470826e83eSopenharmony_ci 480826e83eSopenharmony_ci NativeArkWeb_OnJavaScriptProxyCallback GetMethodCallback() override 490826e83eSopenharmony_ci { 500826e83eSopenharmony_ci return methodCallback_; 510826e83eSopenharmony_ci } 520826e83eSopenharmony_ci 530826e83eSopenharmony_ciprivate: 540826e83eSopenharmony_ci std::string methodName_; 550826e83eSopenharmony_ci NativeArkWeb_OnJavaScriptProxyCallback methodCallback_ = nullptr; 560826e83eSopenharmony_ci}; 570826e83eSopenharmony_ci 580826e83eSopenharmony_ci}; // namespace OHOS::NWeb 590826e83eSopenharmony_ci 600826e83eSopenharmony_ciusing namespace OHOS; 610826e83eSopenharmony_civoid OH_NativeArkWeb_RunJavaScript(const char* webTag, const char* jsCode, NativeArkWeb_OnJavaScriptCallback callback) 620826e83eSopenharmony_ci{ 630826e83eSopenharmony_ci std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag); 640826e83eSopenharmony_ci if (auto nweb = nwebWeak.lock()) { 650826e83eSopenharmony_ci auto callbackImpl = std::make_shared<OHOS::NWeb::NativeJavaScriptExecuteCallback>(callback); 660826e83eSopenharmony_ci WVLOG_I("native RunJavaScript webTag: %{public}s", webTag); 670826e83eSopenharmony_ci nweb->ExecuteJavaScript(jsCode, callbackImpl, false); 680826e83eSopenharmony_ci } else { 690826e83eSopenharmony_ci WVLOG_E("native RunJavaScript get nweb null: %{public}s", webTag); 700826e83eSopenharmony_ci } 710826e83eSopenharmony_ci} 720826e83eSopenharmony_ci 730826e83eSopenharmony_civoid OH_NativeArkWeb_RegisterJavaScriptProxy(const char* webTag, const char* objName, const char** methodList, 740826e83eSopenharmony_ci NativeArkWeb_OnJavaScriptProxyCallback* callback, int32_t size, bool isNeedRefresh) 750826e83eSopenharmony_ci{ 760826e83eSopenharmony_ci WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy webTag:%{public}s", webTag); 770826e83eSopenharmony_ci std::vector<std::shared_ptr<OHOS::NWeb::NWebJsProxyCallback>> proxyCallbacks; 780826e83eSopenharmony_ci for (int i = 0; i < size; i++) { 790826e83eSopenharmony_ci std::shared_ptr<OHOS::NWeb::NWebJsProxyCallback> proxyCallback = 800826e83eSopenharmony_ci std::make_shared<OHOS::NWeb::NWebJsProxyCallbackImpl>(methodList[i], callback[i]); 810826e83eSopenharmony_ci proxyCallbacks.push_back(proxyCallback); 820826e83eSopenharmony_ci } 830826e83eSopenharmony_ci 840826e83eSopenharmony_ci std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag); 850826e83eSopenharmony_ci if (auto nweb = nwebWeak.lock()) { 860826e83eSopenharmony_ci nweb->RegisterNativeArkJSFunction(objName, proxyCallbacks); 870826e83eSopenharmony_ci if (isNeedRefresh) { 880826e83eSopenharmony_ci nweb->Reload(); 890826e83eSopenharmony_ci } 900826e83eSopenharmony_ci } else { 910826e83eSopenharmony_ci WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag); 920826e83eSopenharmony_ci } 930826e83eSopenharmony_ci} 940826e83eSopenharmony_ci 950826e83eSopenharmony_civoid OH_NativeArkWeb_UnregisterJavaScriptProxy(const char* webTag, const char* objName) 960826e83eSopenharmony_ci{ 970826e83eSopenharmony_ci WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy: %{public}s", webTag); 980826e83eSopenharmony_ci std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag); 990826e83eSopenharmony_ci if (auto nweb = nwebWeak.lock()) { 1000826e83eSopenharmony_ci nweb->UnRegisterNativeArkJSFunction(objName); 1010826e83eSopenharmony_ci } else { 1020826e83eSopenharmony_ci WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag); 1030826e83eSopenharmony_ci } 1040826e83eSopenharmony_ci} 1050826e83eSopenharmony_ci 1060826e83eSopenharmony_civoid OH_NativeArkWeb_SetDestroyCallback(const char* webTag, NativeArkWeb_OnDestroyCallback callback) 1070826e83eSopenharmony_ci{ 1080826e83eSopenharmony_ci WVLOG_I("native RegisterDestroyCallback, webTag: %{public}s", webTag); 1090826e83eSopenharmony_ci std::lock_guard<std::mutex> guard(g_mtxMap); 1100826e83eSopenharmony_ci g_destroyMap[webTag] = callback; 1110826e83eSopenharmony_ci std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag); 1120826e83eSopenharmony_ci if (auto nweb = nwebWeak.lock()) { 1130826e83eSopenharmony_ci WVLOG_I("native RegisterNativeDestroyCallback call nweb"); 1140826e83eSopenharmony_ci nweb->RegisterNativeDestroyCallback(webTag, callback); 1150826e83eSopenharmony_ci } else { 1160826e83eSopenharmony_ci WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag); 1170826e83eSopenharmony_ci } 1180826e83eSopenharmony_ci} 1190826e83eSopenharmony_ci 1200826e83eSopenharmony_ciNativeArkWeb_OnDestroyCallback OH_NativeArkWeb_GetDestroyCallback(const char* webTag) 1210826e83eSopenharmony_ci{ 1220826e83eSopenharmony_ci WVLOG_I("native OH_Web_GetDestroyCallback, webTag: %{public}s", webTag); 1230826e83eSopenharmony_ci std::lock_guard<std::mutex> guard(g_mtxMap); 1240826e83eSopenharmony_ci std::unordered_map<std::string, NativeArkWeb_OnDestroyCallback>::iterator iter; 1250826e83eSopenharmony_ci if ((iter = g_destroyMap.find(webTag)) != g_destroyMap.end()) { 1260826e83eSopenharmony_ci return iter->second; 1270826e83eSopenharmony_ci } 1280826e83eSopenharmony_ci return nullptr; 1290826e83eSopenharmony_ci} 1300826e83eSopenharmony_ci 1310826e83eSopenharmony_civoid OH_NativeArkWeb_SetJavaScriptProxyValidCallback(const char* webTag, NativeArkWeb_OnValidCallback callback) 1320826e83eSopenharmony_ci{ 1330826e83eSopenharmony_ci WVLOG_I("native RegisterValidCallback, webTag: %{public}s", webTag); 1340826e83eSopenharmony_ci std::lock_guard<std::mutex> guard(g_mtxMap); 1350826e83eSopenharmony_ci g_validMap[webTag] = callback; 1360826e83eSopenharmony_ci std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag); 1370826e83eSopenharmony_ci if (auto nweb = nwebWeak.lock()) { 1380826e83eSopenharmony_ci WVLOG_I("native OH_NativeArkWeb_SetJavaScriptProxyValidCallback call nweb"); 1390826e83eSopenharmony_ci nweb->RegisterNativeValideCallback(webTag, callback); 1400826e83eSopenharmony_ci } else { 1410826e83eSopenharmony_ci WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag); 1420826e83eSopenharmony_ci } 1430826e83eSopenharmony_ci} 1440826e83eSopenharmony_ci 1450826e83eSopenharmony_ciNativeArkWeb_OnValidCallback OH_NativeArkWeb_GetJavaScriptProxyValidCallback(const char* webTag) 1460826e83eSopenharmony_ci{ 1470826e83eSopenharmony_ci WVLOG_I("native OH_Web_GetValidCallback, webTag: %{public}s", webTag); 1480826e83eSopenharmony_ci std::lock_guard<std::mutex> guard(g_mtxMap); 1490826e83eSopenharmony_ci std::unordered_map<std::string, NativeArkWeb_OnValidCallback>::iterator iter; 1500826e83eSopenharmony_ci if ((iter = g_validMap.find(webTag)) != g_validMap.end()) { 1510826e83eSopenharmony_ci return iter->second; 1520826e83eSopenharmony_ci } 1530826e83eSopenharmony_ci return nullptr; 1540826e83eSopenharmony_ci} 155