1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "native_interface_arkweb.h"
17
18 #include <memory>
19 #include <mutex>
20 #include <unordered_map>
21 #include <vector>
22
23 #include "native_arkweb_utils.h"
24 #include "native_javascript_execute_callback.h"
25 #include "nweb.h"
26 #include "nweb_log.h"
27
28 namespace {
29 std::mutex g_mtxMap; // the mutex to protect the shared resource
30 std::unordered_map<std::string, NativeArkWeb_OnValidCallback> g_validMap;
31 std::unordered_map<std::string, NativeArkWeb_OnDestroyCallback> g_destroyMap;
32 } // namespace
33
34 using namespace OHOS;
OH_NativeArkWeb_RunJavaScript(const char * webTag,const char * jsCode,NativeArkWeb_OnJavaScriptCallback callback)35 void OH_NativeArkWeb_RunJavaScript(const char* webTag, const char* jsCode, NativeArkWeb_OnJavaScriptCallback callback)
36 {
37 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
38 if (auto nweb = nwebWeak.lock()) {
39 auto callbackImpl = std::make_shared<OHOS::NWeb::NativeJavaScriptExecuteCallback>(callback);
40 WVLOG_I("native RunJavaScript webTag: %{public}s", webTag);
41 nweb->ExecuteJavaScript(jsCode, callbackImpl, false);
42 } else {
43 WVLOG_E("native RunJavaScript get nweb null: %{public}s", webTag);
44 }
45 }
46
OH_NativeArkWeb_RegisterJavaScriptProxy(const char * webTag,const char * objName,const char ** methodList,NativeArkWeb_OnJavaScriptProxyCallback * callback,int32_t size,bool isNeedRefresh)47 void OH_NativeArkWeb_RegisterJavaScriptProxy(const char* webTag, const char* objName, const char** methodList,
48 NativeArkWeb_OnJavaScriptProxyCallback* callback, int32_t size, bool isNeedRefresh)
49 {
50 WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy webTag:%{public}s", webTag);
51 std::vector<std::function<char*(const char**, int32_t size)>> callbackList;
52 for (int i = 0; i < size; i++) {
53 callbackList.emplace_back(callback[i]);
54 }
55
56 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
57 if (auto nweb = nwebWeak.lock()) {
58 nweb->RegisterNativeArkJSFunction(objName, methodList, callbackList, size);
59 if (isNeedRefresh) {
60 nweb->Reload();
61 }
62 } else {
63 WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag);
64 }
65 }
66
OH_NativeArkWeb_UnregisterJavaScriptProxy(const char * webTag,const char * objName)67 void OH_NativeArkWeb_UnregisterJavaScriptProxy(const char* webTag, const char* objName)
68 {
69 WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy: %{public}s", webTag);
70 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
71 if (auto nweb = nwebWeak.lock()) {
72 nweb->UnRegisterNativeArkJSFunction(objName);
73 } else {
74 WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag);
75 }
76 }
77
OH_NativeArkWeb_SetDestroyCallback(const char * webTag,NativeArkWeb_OnDestroyCallback callback)78 void OH_NativeArkWeb_SetDestroyCallback(const char* webTag, NativeArkWeb_OnDestroyCallback callback)
79 {
80 WVLOG_I("native RegisterDestroyCallback, webTag: %{public}s", webTag);
81 std::lock_guard<std::mutex> guard(g_mtxMap);
82 g_destroyMap[webTag] = callback;
83 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
84 if (auto nweb = nwebWeak.lock()) {
85 WVLOG_I("native RegisterNativeDestroyCallback call nweb");
86 nweb->RegisterNativeDestroyCallback(webTag, callback);
87 } else {
88 WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag);
89 }
90 }
91
OH_NativeArkWeb_GetDestroyCallback(const char * webTag)92 NativeArkWeb_OnDestroyCallback OH_NativeArkWeb_GetDestroyCallback(const char* webTag)
93 {
94 WVLOG_I("native OH_Web_GetDestroyCallback, webTag: %{public}s", webTag);
95 std::lock_guard<std::mutex> guard(g_mtxMap);
96 std::unordered_map<std::string, NativeArkWeb_OnDestroyCallback>::iterator iter;
97 if ((iter = g_destroyMap.find(webTag)) != g_destroyMap.end()) {
98 return iter->second;
99 }
100 return nullptr;
101 }
102
OH_NativeArkWeb_SetJavaScriptProxyValidCallback(const char * webTag,NativeArkWeb_OnValidCallback callback)103 void OH_NativeArkWeb_SetJavaScriptProxyValidCallback(const char* webTag, NativeArkWeb_OnValidCallback callback)
104 {
105 WVLOG_I("native RegisterValidCallback, webTag: %{public}s", webTag);
106 std::lock_guard<std::mutex> guard(g_mtxMap);
107 g_validMap[webTag] = callback;
108 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
109 if (auto nweb = nwebWeak.lock()) {
110 WVLOG_I("native OH_NativeArkWeb_SetJavaScriptProxyValidCallback call nweb");
111 nweb->RegisterNativeValideCallback(webTag, callback);
112 } else {
113 WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag);
114 }
115 }
116
OH_NativeArkWeb_GetJavaScriptProxyValidCallback(const char * webTag)117 NativeArkWeb_OnValidCallback OH_NativeArkWeb_GetJavaScriptProxyValidCallback(const char* webTag)
118 {
119 WVLOG_I("native OH_Web_GetValidCallback, webTag: %{public}s", webTag);
120 std::lock_guard<std::mutex> guard(g_mtxMap);
121 std::unordered_map<std::string, NativeArkWeb_OnValidCallback>::iterator iter;
122 if ((iter = g_validMap.find(webTag)) != g_validMap.end()) {
123 return iter->second;
124 }
125 return nullptr;
126 }
127