1 /*
2 * Copyright (c) 2024 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 #include "webview_javascript_result_callback.h"
16
17 #include <sys/mman.h>
18 #include <unistd.h>
19
20 #include "webview_log.h"
21 #include "ohos_adapter_helper.h"
22
23 #define MAX_FLOWBUF_DATA_SIZE 52428800 /* 50MB */
24 #define MAX_ENTRIES 10
25 #define HEADER_SIZE (MAX_ENTRIES * 8) /* 10 * (int position + int length) */
26 #define INDEX_SIZE 2
27
28 using namespace OHOS::NWeb;
29
30 namespace OHOS::Webview {
31
32 std::unordered_map<int32_t, WebviewJavaScriptResultCallBackImpl*> g_webviewJsResultCallbackMap;
33 std::mutex g_objectMtx;
34
vectorEqual(const std::vector<std::string> & v1,const std::vector<std::string> & v2)35 bool vectorEqual(const std::vector<std::string>& v1, const std::vector<std::string>& v2)
36 {
37 if (v1.size() != v2.size()) {
38 return false;
39 }
40 int size = static_cast<int>(v1.size());
41 for (int i = 0; i < size; i++) {
42 if (v1[i] != v2[i]) {
43 return false;
44 }
45 }
46 return true;
47 }
48
WebviewJavaScriptResultCallBackImpl(int32_t nwebId)49 WebviewJavaScriptResultCallBackImpl::WebviewJavaScriptResultCallBackImpl(int32_t nwebId) : nwebId_(nwebId)
50 {
51 std::unique_lock<std::mutex> lk(g_objectMtx);
52 g_webviewJsResultCallbackMap.emplace(nwebId, this);
53 }
54
~WebviewJavaScriptResultCallBackImpl()55 WebviewJavaScriptResultCallBackImpl::~WebviewJavaScriptResultCallBackImpl()
56 {
57 std::unique_lock<std::mutex> lk(g_objectMtx);
58 g_webviewJsResultCallbackMap.erase(nwebId_);
59 }
60
FindObjectIdInJsTd(const std::vector<std::function<char * (const char *)>> & cjFuncs,const std::vector<std::string> & methodList,JavaScriptOb::ObjectID & objectId)61 bool WebviewJavaScriptResultCallBackImpl::FindObjectIdInJsTd(
62 const std::vector<std::function<char*(const char*)>>& cjFuncs,
63 const std::vector<std::string>& methodList, JavaScriptOb::ObjectID& objectId)
64 {
65 objectId = static_cast<JavaScriptOb::ObjectID>(JavaScriptOb::JavaScriptObjIdErrorCode::WEBVIEWCONTROLLERERROR);
66 for (const auto& pair : objects_) {
67 bool result;
68 if (pair.second == nullptr) {
69 result = false;
70 } else {
71 result = (pair.second->GetFuncs().size() == cjFuncs.size())
72 && vectorEqual(pair.second->GetMethodNames(), methodList);
73 }
74 if (result) {
75 objectId = pair.first;
76 return true;
77 }
78 }
79 return false;
80 }
81
AddObject(const std::vector<std::function<char * (const char *)>> & cjFuncs)82 JavaScriptOb::ObjectID WebviewJavaScriptResultCallBackImpl::AddObject(
83 const std::vector<std::function<char*(const char*)>>& cjFuncs)
84 {
85 JavaScriptOb::ObjectID objectId;
86 {
87 auto new_object = JavaScriptOb::CreateNamed(cjFuncs);
88 objectId = nextObjectId_++;
89 WEBVIEWLOGD("WebviewJavaScriptResultCallBackImpl::AddObject objectId = "
90 "%{public}d",
91 static_cast<int32_t>(objectId));
92 objects_[objectId] = new_object;
93 }
94 return objectId;
95 }
96
AddNamedObject(const std::vector<std::function<char * (const char *)>> & cjFuncs,const std::vector<std::string> & methodList,const std::string & objName)97 JavaScriptOb::ObjectID WebviewJavaScriptResultCallBackImpl::AddNamedObject(
98 const std::vector<std::function<char*(const char*)>>& cjFuncs,
99 const std::vector<std::string>& methodList, const std::string& objName)
100 {
101 JavaScriptOb::ObjectID objectId;
102 NamedObjectMap::iterator iter = namedObjects_.find(objName);
103 bool methodName = FindObjectIdInJsTd(cjFuncs, methodList, objectId);
104 if (methodName && iter != namedObjects_.end() && iter->second == objectId) {
105 // Nothing to do.
106 return objectId;
107 }
108 if (iter != namedObjects_.end()) {
109 RemoveNamedObject(iter->first);
110 }
111 if (methodName && objects_[objectId] != nullptr) {
112 objects_[objectId]->AddName();
113 } else {
114 objectId = AddObject(cjFuncs);
115 }
116 namedObjects_[objName] = objectId;
117 return objectId;
118 }
119
RemoveNamedObject(const std::string & name)120 bool WebviewJavaScriptResultCallBackImpl::RemoveNamedObject(const std::string& name)
121 {
122 WEBVIEWLOGD("WebviewJavaScriptResultCallBackImpl::RemoveNamedObject called, "
123 "name = %{public}s",
124 name.c_str());
125 NamedObjectMap::iterator iter = namedObjects_.find(name);
126 if (iter == namedObjects_.end()) {
127 return false;
128 }
129 if (objects_[iter->second]) {
130 objects_[iter->second]->RemoveName();
131 }
132 namedObjects_.erase(iter);
133 return true;
134 }
135
RegisterJavaScriptProxy(const std::vector<std::function<char * (const char *)>> & cjFuncs,const std::string & objName,const std::vector<std::string> & methodList)136 JavaScriptOb::ObjectID WebviewJavaScriptResultCallBackImpl::RegisterJavaScriptProxy(
137 const std::vector<std::function<char*(const char*)>>& cjFuncs,
138 const std::string& objName, const std::vector<std::string>& methodList)
139 {
140 JavaScriptOb::ObjectID objId = AddNamedObject(cjFuncs, methodList, objName);
141 // set up named object method
142 if (namedObjects_.find(objName) != namedObjects_.end() && objects_[namedObjects_[objName]]) {
143 objects_[namedObjects_[objName]]->SetMethods(methodList);
144 }
145 WEBVIEWLOGD("WebviewJavaScriptResultCallBackImpl::RegisterJavaScriptProxy called, "
146 "objectId = %{public}d",
147 static_cast<int32_t>(objId));
148 return objId;
149 }
150
FindObject(JavaScriptOb::ObjectID objectId)151 std::shared_ptr<JavaScriptOb> WebviewJavaScriptResultCallBackImpl::FindObject(JavaScriptOb::ObjectID objectId)
152 {
153 auto iter = objects_.find(objectId);
154 if (iter != objects_.end()) {
155 return iter->second;
156 }
157 WEBVIEWLOGE("WebviewJavaScriptResultCallBackImpl::FindObject Unknown object: objectId = "
158 "%{public}d",
159 objectId);
160 return nullptr;
161 }
162
GetJavaScriptResultSelf(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int32_t routingId,int32_t objectId)163 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelf(
164 std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName,
165 int32_t routingId, int32_t objectId)
166 {
167 std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
168 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
169 if (!jsObj) {
170 return ret;
171 }
172 WEBVIEWLOGI("WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelf");
173 std::string argv;
174 if (args.size() == 0) {
175 argv = "";
176 } else {
177 argv = args[0]->GetString();
178 }
179 auto callback = jsObj->FindMethod(method);
180 if (!callback) {
181 WEBVIEWLOGE("WebviewJavaScriptResultCallBackImpl::ExecuteGetJavaScriptResult callback null");
182 return ret;
183 }
184 auto argCj = MallocCString(argv);
185 if (argCj == nullptr) {
186 return ret;
187 }
188 char* cjRet = callback(argCj);
189 std::string strVal = std::string(cjRet);
190 free(cjRet);
191 ret->SetType(NWebValue::Type::STRING);
192 ret->SetString(strVal);
193 return ret;
194 }
195
GetJavaScriptResult(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int32_t routingId,int32_t objectId)196 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResult(
197 std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName,
198 int32_t routingId, int32_t objectId)
199 {
200 WEBVIEWLOGD("GetJavaScriptResult method = %{public}s", method.c_str());
201 std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
202 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
203 if (!jsObj || jsObj->HasMethod(method) == -1) {
204 return ret;
205 }
206
207 return GetJavaScriptResultSelf(args, method, objName, routingId, objectId);
208 }
209
FlowbufStrAtIndex(void * mem,int flowbufIndex,int * argIndex,int * strLen)210 char* WebviewJavaScriptResultCallBackImpl::FlowbufStrAtIndex(
211 void* mem, int flowbufIndex, int* argIndex, int* strLen)
212 {
213 int* header = static_cast<int*>(mem); // Cast the memory block to int* for easier access
214 int offset = 0;
215 if (argIndex == nullptr) {
216 return nullptr;
217 }
218 if (flowbufIndex >= MAX_ENTRIES) {
219 *argIndex = -1;
220 return nullptr;
221 }
222
223 int* entry = header + (flowbufIndex * INDEX_SIZE);
224 if (entry == nullptr) {
225 return nullptr;
226 }
227 if (*(entry + 1) == 0) { // Check if length is 0, indicating unused entry
228 *argIndex = -1;
229 return nullptr;
230 }
231
232 int i = 0;
233 for (i = 0; i < flowbufIndex; i++) {
234 offset += *(header + (i * INDEX_SIZE) + 1);
235 }
236 if (strLen == nullptr) {
237 return nullptr;
238 }
239 *strLen = *(header + (i * INDEX_SIZE) + 1) - 1;
240
241 *argIndex = *entry;
242
243 char* dataSegment = static_cast<char*>(mem) + HEADER_SIZE;
244 char* currentString = dataSegment + offset;
245 return currentString;
246 }
247
ConstructArgv(void * ashmem,std::vector<std::shared_ptr<NWebValue>> args,std::vector<std::string> & argv,std::shared_ptr<JavaScriptOb> jsObj,int32_t routingId)248 bool WebviewJavaScriptResultCallBackImpl::ConstructArgv(void* ashmem,
249 std::vector<std::shared_ptr<NWebValue>> args,
250 std::vector<std::string>& argv,
251 std::shared_ptr<JavaScriptOb> jsObj,
252 int32_t routingId)
253 {
254 int argIndex = -1;
255 int currIndex = 0;
256 int flowbufIndex = 0;
257 int strLen = 0;
258 char* flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
259 flowbufIndex++;
260 while (argIndex == currIndex) {
261 argv.push_back(std::string(flowbufStr));
262 currIndex ++;
263 flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
264 flowbufIndex++;
265 }
266
267 for (std::shared_ptr<NWebValue> input : args) {
268 while (argIndex == currIndex) {
269 argv.push_back(std::string(flowbufStr));
270 currIndex ++;
271 flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
272 flowbufIndex++;
273 }
274 argv.push_back(input->GetString());
275 currIndex++;
276 }
277
278 while (argIndex == currIndex) {
279 argv.push_back(std::string(flowbufStr));
280 currIndex ++;
281 flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
282 flowbufIndex++;
283 }
284 return true;
285 }
286
GetJavaScriptResultSelfHelper(std::shared_ptr<JavaScriptOb> jsObj,const std::string & method,int32_t routingId,std::vector<std::string> argv)287 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelfHelper(
288 std::shared_ptr<JavaScriptOb> jsObj,
289 const std::string& method,
290 int32_t routingId,
291 std::vector<std::string> argv)
292 {
293 std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
294 auto callback = jsObj->FindMethod(method);
295 if (!callback) {
296 WEBVIEWLOGE("WebviewJavaScriptResultCallBack::ExecuteGetJavaScriptResult callback null");
297 return ret;
298 }
299 std::string arg;
300 if (argv.size() == 0) {
301 arg = "";
302 } else {
303 arg = argv[0];
304 }
305 auto argCj = MallocCString(arg);
306 if (argCj == nullptr) {
307 return ret;
308 }
309 char* cjRet = callback(argCj);
310 std::string strVal = std::string(cjRet);
311 free(cjRet);
312 ret->SetType(NWebValue::Type::STRING);
313 ret->SetString(strVal);
314 return ret;
315 }
316
GetJavaScriptResultSelfFlowbuf(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int fd,int32_t routingId,int32_t objectId)317 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelfFlowbuf(
318 std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName, int fd,
319 int32_t routingId, int32_t objectId)
320 {
321 std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
322 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
323 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
324 if (!flowbufferAdapter) {
325 return ret;
326 }
327 auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, MAX_FLOWBUF_DATA_SIZE + HEADER_SIZE, PROT_READ);
328 if (!ashmem) {
329 return ret;
330 }
331
332 std::vector<std::string> argv = {};
333 if (!ConstructArgv(ashmem, args, argv, jsObj, routingId)) {
334 return ret;
335 }
336 close(fd);
337
338 ret = GetJavaScriptResultSelfHelper(jsObj, method, routingId, argv);
339 return ret;
340 }
341
GetJavaScriptResultFlowbuf(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int fd,int32_t routingId,int32_t objectId)342 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultFlowbuf(
343 std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName, int fd,
344 int32_t routingId, int32_t objectId)
345 {
346 (void)objName; // to be compatible with older webcotroller, classname may be empty
347 WEBVIEWLOGD("GetJavaScriptResult method = %{public}s", method.c_str());
348 std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
349 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
350 if (!jsObj || jsObj->HasMethod(method) == -1) {
351 return ret;
352 }
353 return GetJavaScriptResultSelfFlowbuf(args, method, objName,fd, routingId, objectId);
354 }
355
GetJavaScriptObjectMethods(int32_t objectId)356 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptObjectMethods(int32_t objectId)
357 {
358 auto ret = std::make_shared<NWebValue>(NWebValue::Type::LIST);
359 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
360 if (!jsObj) {
361 return ret;
362 }
363 auto methods = jsObj->GetMethodNames();
364 for (auto& method : methods) {
365 ret->AddListValue(NWebValue(method));
366 }
367 return ret;
368 }
369
HasJavaScriptObjectMethods(int32_t objectId,const std::string & methodName)370 bool WebviewJavaScriptResultCallBackImpl::HasJavaScriptObjectMethods(int32_t objectId, const std::string& methodName)
371 {
372 bool ret = false;
373 std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
374 if (!jsObj) {
375 return false;
376 }
377 if (jsObj->HasMethod(methodName) != -1) {
378 ret = true;
379 } else {
380 WEBVIEWLOGD("WebviewJavaScriptResultCallBackImpl::HasJavaScriptObjectMethods cannot find "
381 "object");
382 }
383 return ret;
384 }
385
DeleteJavaScriptRegister(const std::string & objName)386 bool WebviewJavaScriptResultCallBackImpl::DeleteJavaScriptRegister(const std::string &objName)
387 {
388 return RemoveNamedObject(objName);
389 }
390
RemoveJavaScriptObjectHolder(int32_t holder,JavaScriptOb::ObjectID objectId)391 void WebviewJavaScriptResultCallBackImpl::RemoveJavaScriptObjectHolder(int32_t holder, JavaScriptOb::ObjectID objectId)
392 {}
393
RemoveTransientJavaScriptObject()394 void WebviewJavaScriptResultCallBackImpl::RemoveTransientJavaScriptObject()
395 {}
396
397 } // namespace OHOS::Webview
398