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     WEBVIEWLOGI("WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelf");
170     std::string argv;
171     if (args.size() == 0) {
172         argv = "";
173     } else {
174         argv = args[0]->GetString();
175     }
176     auto callback = jsObj->FindMethod(method);
177     if (!callback) {
178         WEBVIEWLOGE("WebviewJavaScriptResultCallBackImpl::ExecuteGetJavaScriptResult callback null");
179         return ret;
180     }
181     auto argCj = MallocCString(argv);
182     if (argCj == nullptr) {
183         return ret;
184     }
185     char* cjRet = callback(argCj);
186     std::string strVal = std::string(cjRet);
187     free(cjRet);
188     ret->SetType(NWebValue::Type::STRING);
189     ret->SetString(strVal);
190     return ret;
191 }
192 
GetJavaScriptResult(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int32_t routingId,int32_t objectId)193 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResult(
194     std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName,
195     int32_t routingId, int32_t objectId)
196 {
197     WEBVIEWLOGD("GetJavaScriptResult method = %{public}s", method.c_str());
198     std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
199     std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
200     if (!jsObj || jsObj->HasMethod(method) == -1) {
201         return ret;
202     }
203 
204     return GetJavaScriptResultSelf(args, method, objName, routingId, objectId);
205 }
206 
FlowbufStrAtIndex(void * mem,int flowbufIndex,int * argIndex,int * strLen)207 char* WebviewJavaScriptResultCallBackImpl::FlowbufStrAtIndex(
208     void* mem, int flowbufIndex, int* argIndex, int* strLen)
209 {
210     int* header = static_cast<int*>(mem); // Cast the memory block to int* for easier access
211     int offset = 0;
212     if (argIndex == nullptr) {
213         return nullptr;
214     }
215     if (flowbufIndex >=  MAX_ENTRIES) {
216         *argIndex = -1;
217         return nullptr;
218     }
219 
220     int* entry = header + (flowbufIndex * INDEX_SIZE);
221     if (entry == nullptr) {
222         return nullptr;
223     }
224     if (*(entry + 1) == 0) { // Check if length is 0, indicating unused entry
225         *argIndex = -1;
226         return nullptr;
227     }
228 
229     int i = 0;
230     for (i = 0; i < flowbufIndex; i++) {
231         offset += *(header + (i * INDEX_SIZE) + 1);
232     }
233     if (strLen == nullptr) {
234         return nullptr;
235     }
236     *strLen = *(header + (i * INDEX_SIZE) + 1) - 1;
237 
238     *argIndex = *entry;
239 
240     char* dataSegment = static_cast<char*>(mem) + HEADER_SIZE;
241     char* currentString = dataSegment + offset;
242     return currentString;
243 }
244 
ConstructArgv(void * ashmem,std::vector<std::shared_ptr<NWebValue>> args,std::vector<std::string> & argv,std::shared_ptr<JavaScriptOb> jsObj,int32_t routingId)245 bool WebviewJavaScriptResultCallBackImpl::ConstructArgv(void* ashmem,
246     std::vector<std::shared_ptr<NWebValue>> args,
247     std::vector<std::string>& argv,
248     std::shared_ptr<JavaScriptOb> jsObj,
249     int32_t routingId)
250 {
251     int argIndex = -1;
252     int currIndex = 0;
253     int flowbufIndex = 0;
254     int strLen = 0;
255     char* flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
256     flowbufIndex++;
257     while (argIndex == currIndex) {
258         argv.push_back(std::string(flowbufStr));
259         currIndex ++;
260         flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
261         flowbufIndex++;
262     }
263 
264     for (std::shared_ptr<NWebValue> input : args) {
265         while (argIndex == currIndex) {
266             argv.push_back(std::string(flowbufStr));
267             currIndex ++;
268             flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
269             flowbufIndex++;
270         }
271         argv.push_back(input->GetString());
272         currIndex++;
273     }
274 
275     while (argIndex == currIndex) {
276         argv.push_back(std::string(flowbufStr));
277         currIndex ++;
278         flowbufStr = FlowbufStrAtIndex(ashmem, flowbufIndex, &argIndex, &strLen);
279         flowbufIndex++;
280     }
281     return true;
282 }
283 
GetJavaScriptResultSelfHelper(std::shared_ptr<JavaScriptOb> jsObj,const std::string & method,int32_t routingId,std::vector<std::string> argv)284 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelfHelper(
285     std::shared_ptr<JavaScriptOb> jsObj,
286     const std::string& method,
287     int32_t routingId,
288     std::vector<std::string> argv)
289 {
290     std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
291     auto callback = jsObj->FindMethod(method);
292     if (!callback) {
293         WEBVIEWLOGE("WebviewJavaScriptResultCallBack::ExecuteGetJavaScriptResult callback null");
294         return ret;
295     }
296     std::string arg;
297     if (argv.size() == 0) {
298         arg = "";
299     } else {
300         arg = argv[0];
301     }
302     auto argCj = MallocCString(arg);
303     if (argCj == nullptr) {
304         return ret;
305     }
306     char* cjRet = callback(argCj);
307     std::string strVal = std::string(cjRet);
308     free(cjRet);
309     ret->SetType(NWebValue::Type::STRING);
310     ret->SetString(strVal);
311     return ret;
312 }
313 
GetJavaScriptResultSelfFlowbuf(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int fd,int32_t routingId,int32_t objectId)314 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultSelfFlowbuf(
315     std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName, int fd,
316     int32_t routingId, int32_t objectId)
317 {
318     std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
319     std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
320     auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter();
321     if (!flowbufferAdapter) {
322         return ret;
323     }
324     auto ashmem = flowbufferAdapter->CreateAshmemWithFd(fd, MAX_FLOWBUF_DATA_SIZE + HEADER_SIZE, PROT_READ);
325     if (!ashmem) {
326         return ret;
327     }
328 
329     std::vector<std::string> argv = {};
330     if (!ConstructArgv(ashmem, args, argv, jsObj, routingId)) {
331         return ret;
332     }
333     close(fd);
334 
335     ret = GetJavaScriptResultSelfHelper(jsObj, method, routingId, argv);
336     return ret;
337 }
338 
GetJavaScriptResultFlowbuf(std::vector<std::shared_ptr<NWebValue>> args,const std::string & method,const std::string & objName,int fd,int32_t routingId,int32_t objectId)339 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptResultFlowbuf(
340     std::vector<std::shared_ptr<NWebValue>> args, const std::string& method, const std::string& objName, int fd,
341     int32_t routingId, int32_t objectId)
342 {
343     (void)objName; // to be compatible with older webcotroller, classname may be empty
344     WEBVIEWLOGD("GetJavaScriptResult method = %{public}s", method.c_str());
345     std::shared_ptr<NWebValue> ret = std::make_shared<NWebValue>(NWebValue::Type::NONE);
346     std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
347     if (!jsObj || jsObj->HasMethod(method) == -1) {
348         return ret;
349     }
350     return GetJavaScriptResultSelfFlowbuf(args, method, objName, fd, routingId, objectId);
351 }
352 
GetJavaScriptObjectMethods(int32_t objectId)353 std::shared_ptr<NWebValue> WebviewJavaScriptResultCallBackImpl::GetJavaScriptObjectMethods(int32_t objectId)
354 {
355     auto ret = std::make_shared<NWebValue>(NWebValue::Type::LIST);
356     std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
357     if (!jsObj) {
358         return ret;
359     }
360     auto methods = jsObj->GetMethodNames();
361     for (auto& method : methods) {
362         ret->AddListValue(NWebValue(method));
363     }
364     return ret;
365 }
366 
HasJavaScriptObjectMethods(int32_t objectId,const std::string & methodName)367 bool WebviewJavaScriptResultCallBackImpl::HasJavaScriptObjectMethods(int32_t objectId, const std::string& methodName)
368 {
369     bool ret = false;
370     std::shared_ptr<JavaScriptOb> jsObj = FindObject(objectId);
371     if (!jsObj) {
372         return false;
373     }
374     if (jsObj->HasMethod(methodName) != -1) {
375         ret = true;
376     } else {
377         WEBVIEWLOGD("WebviewJavaScriptResultCallBackImpl::HasJavaScriptObjectMethods cannot find "
378                 "object");
379     }
380     return ret;
381 }
382 
RemoveJavaScriptObjectHolder(int32_t holder,JavaScriptOb::ObjectID objectId)383 void WebviewJavaScriptResultCallBackImpl::RemoveJavaScriptObjectHolder(int32_t holder, JavaScriptOb::ObjectID objectId)
384 {}
385 
RemoveTransientJavaScriptObject()386 void WebviewJavaScriptResultCallBackImpl::RemoveTransientJavaScriptObject()
387 {}
388 
389 } // namespace OHOS::Webview
390