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 
16 #include "webview_ffi.h"
17 #include "webview_controller_impl.h"
18 #include "nweb_helper.h"
19 #include "nweb_init_params.h"
20 #include "web_errors.h"
21 #include "application_context.h"
22 #include "webview_log.h"
23 #include "parameters.h"
24 #include "web_cookie_manager.h"
25 #include "pixel_map.h"
26 #include "cj_lambda.h"
27 #include "pixel_map_impl.h"
28 
29 using namespace OHOS::FFI;
30 using namespace OHOS::NWeb;
31 
32 namespace OHOS {
33 namespace Webview {
34 
35 extern "C" {
FfiOHOSWebviewCtlConstructor()36     int64_t FfiOHOSWebviewCtlConstructor()
37     {
38         auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>();
39         if (nativeWebviewCtl == nullptr) {
40             WEBVIEWLOGE("new webview controller failed");
41             return -1;
42         }
43         WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
44         return nativeWebviewCtl->GetID();
45     }
46 
FfiOHOSWebviewCtlConstructorWithWebTag(char * cWebTag)47     int64_t FfiOHOSWebviewCtlConstructorWithWebTag(char *cWebTag)
48     {
49         std::string webTag = cWebTag;
50         auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>(webTag);
51         if (nativeWebviewCtl == nullptr) {
52             WEBVIEWLOGE("new webview controller failed");
53             return -1;
54         }
55         WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
56         return nativeWebviewCtl->GetID();
57     }
58 
FfiOHOSWebviewCtlInitializeWebEngine()59     void FfiOHOSWebviewCtlInitializeWebEngine()
60     {
61         std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
62             AbilityRuntime::ApplicationContext::GetApplicationContext();
63         if (ctx == nullptr) {
64             WEBVIEWLOGE("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to ctx is null.");
65             return;
66         }
67         const std::string& bundle_path = ctx->GetBundleCodeDir();
68         NWebHelper::Instance().SetBundlePath(bundle_path);
69         if (!NWebHelper::Instance().InitAndRun(true)) {
70             WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to NWebHelper failure.");
71         }
72         WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine NWebHelper initialized, \
73             init web engine done, bundle_path: %{public}s", bundle_path.c_str());
74         return;
75     }
76 
FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode,char * secureDnsConfig)77     void FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode, char* secureDnsConfig)
78     {
79         std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
80         config->SetMode(secureDnsMode);
81         config->SetConfig(secureDnsConfig);
82         WEBVIEWLOGI("set http dns mode:%{public}d doh_config:%{public}s", secureDnsMode, secureDnsConfig);
83         NWebHelper::Instance().SetHttpDns(config);
84     }
85 
FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)86     void FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)
87     {
88         if (OHOS::system::GetBoolParameter("web.debug.devtools", false)) {
89             return;
90         }
91 
92         WebviewControllerImpl::webDebuggingAccess_ = webDebuggingAccess;
93         return;
94     }
95 
FfiOHOSWebviewCtlLoadUrl(int64_t id,char * url)96     int32_t FfiOHOSWebviewCtlLoadUrl(int64_t id, char *url)
97     {
98         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
99         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
100             return NWebError::INIT_ERROR;
101         }
102         std::string webSrc = url;
103 
104         return nativeWebviewCtl->LoadUrl(webSrc);
105     }
106 
FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id,char * url,ArrWebHeader headers)107     int32_t FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id, char *url, ArrWebHeader headers)
108     {
109         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
110         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
111             return NWebError::INIT_ERROR;
112         }
113 
114         std::map<std::string, std::string> httpHeaders;
115         uint32_t arrayLength = static_cast<uint32_t>(headers.size);
116         for (uint32_t i = 0; i < arrayLength; ++i) {
117             std::string key = headers.head[i].headerKey;
118             std::string value = headers.head[i].headerValue;
119             httpHeaders[key] = value;
120         }
121 
122         return nativeWebviewCtl->LoadUrl(url, httpHeaders);
123     }
124 
FfiOHOSWebviewCtlLoadData(int64_t id,LoadDatas loadDatas)125     int32_t FfiOHOSWebviewCtlLoadData(int64_t id, LoadDatas loadDatas)
126     {
127         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
128         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
129             return NWebError::INIT_ERROR;
130         }
131         std::string data = loadDatas.cData;
132         std::string mimeType = loadDatas.cMimeType;
133         std::string encoding = loadDatas.cEncoding;
134         std::string baseUrl = loadDatas.cBaseUrl;
135         std::string historyUrl = loadDatas.cHistoryUrl;
136         return nativeWebviewCtl->LoadData(data, mimeType, encoding, baseUrl, historyUrl);
137     }
138 
FfiOHOSWebviewCtlRefresh(int64_t id)139     int32_t FfiOHOSWebviewCtlRefresh(int64_t id)
140     {
141         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
142         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
143             return NWebError::INIT_ERROR;
144         }
145         nativeWebviewCtl->Refresh();
146         return NWebError::NO_ERROR;
147     }
148 
FfiOHOSWebviewCtlGetUserAgent(int64_t id,int32_t * errCode)149     char *FfiOHOSWebviewCtlGetUserAgent(int64_t id, int32_t *errCode)
150     {
151         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
152         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
153             *errCode = NWebError::INIT_ERROR;
154             return nullptr;
155         }
156         std::string userAgent = nativeWebviewCtl->GetUserAgent();
157         *errCode = NWebError::NO_ERROR;
158         return MallocCString(userAgent);
159     }
160 
FfiOHOSWebviewCtlAccessForward(int64_t id,int32_t * errCode)161     bool FfiOHOSWebviewCtlAccessForward(int64_t id, int32_t *errCode)
162     {
163         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
164         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
165             *errCode = NWebError::INIT_ERROR;
166             return false;
167         }
168         bool access = nativeWebviewCtl->AccessForward();
169         *errCode = NWebError::NO_ERROR;
170         return access;
171     }
172 
FfiOHOSWebviewCtlAccessBackward(int64_t id,int32_t * errCode)173     bool FfiOHOSWebviewCtlAccessBackward(int64_t id, int32_t *errCode)
174     {
175         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
176         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
177             *errCode = NWebError::INIT_ERROR;
178             return false;
179         }
180         bool access = nativeWebviewCtl->AccessBackward();
181         *errCode = NWebError::NO_ERROR;
182         return access;
183     }
184 
FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id,char * cUserAgent)185     int32_t FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id, char *cUserAgent)
186     {
187         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
188         std::string userAgent = cUserAgent;
189         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
190             return NWebError::INIT_ERROR;
191         }
192         return nativeWebviewCtl->SetCustomUserAgent(userAgent);
193     }
194 
FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)195     RetDataCString FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)
196     {
197         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
198         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
199         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
200             return ret;
201         }
202         std::string userAgent = "";
203         userAgent = nativeWebviewCtl->GetCustomUserAgent();
204         ret.code = NWebError::NO_ERROR;
205         ret.data = MallocCString(userAgent);
206         return ret;
207     }
208 
FfiOHOSWebviewCtlRunJavaScript(int64_t id,char * cScript,void (* callbackRef)(RetDataCString infoRef))209     int32_t FfiOHOSWebviewCtlRunJavaScript(int64_t id, char* cScript,
210         void (*callbackRef)(RetDataCString infoRef))
211     {
212         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
213         std::string script = std::string(cScript);
214         auto onChange = [lambda = CJLambda::Create(callbackRef)]
215             (RetDataCString infoRef) -> void { lambda(infoRef); };
216         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
217             return NWebError::INIT_ERROR;
218         }
219         nativeWebviewCtl->RunJavaScript(script, onChange);
220         return NWebError::NO_ERROR;
221     }
222 
FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id,CArrI64 cFuncIds,const char * cName,CArrString cMethodList)223     int32_t FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id,
224         CArrI64 cFuncIds,  const char* cName, CArrString cMethodList)
225     {
226         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
227         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
228             return NWebError::INIT_ERROR;
229         }
230         std::string objName = std::string(cName);
231         std::vector<std::string> methodList;
232         for (int64_t i = 0; i < cMethodList.size; i++) {
233             methodList.push_back(std::string(cMethodList.head[i]));
234         }
235         std::vector<std::function<char*(const char*)>> cjFuncs;
236         for (int64_t i = 0; i < cFuncIds.size; i++) {
237             auto cFunc = reinterpret_cast<char*(*)(const char*)>(cFuncIds.head[i]);
238             auto onChange = [lambda = CJLambda::Create(cFunc)]
239                 (const char* infoRef) -> char* { return lambda(infoRef); };
240             cjFuncs.push_back(onChange);
241         }
242         nativeWebviewCtl->SetNWebJavaScriptResultCallBack();
243         nativeWebviewCtl->RegisterJavaScriptProxy(cjFuncs, objName, methodList);
244         return NWebError::NO_ERROR;
245     }
246 
FfiOHOSWebviewCtlGetUrl(int64_t id)247     RetDataCString FfiOHOSWebviewCtlGetUrl(int64_t id)
248     {
249         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
250         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
251         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
252             return ret;
253         }
254         std::string url = "";
255         url = nativeWebviewCtl->GetUrl();
256         ret.code = NWebError::NO_ERROR;
257         ret.data = MallocCString(url);
258         return ret;
259     }
260 
FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)261     RetDataCString FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)
262     {
263         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
264         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
265         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
266             return ret;
267         }
268         std::string url = "";
269         url = nativeWebviewCtl->GetOriginalUrl();
270         ret.code = NWebError::NO_ERROR;
271         ret.data = MallocCString(url);
272         return ret;
273     }
274 
FfiOHOSWebviewCtlPageUp(int64_t id,bool top)275     int32_t FfiOHOSWebviewCtlPageUp(int64_t id, bool top)
276     {
277         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
278         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
279             return NWebError::INIT_ERROR;
280         }
281         nativeWebviewCtl->ScrollPageUp(top);
282         return NWebError::NO_ERROR;
283     }
284 
FfiOHOSWebviewCtlPageDown(int64_t id,bool bottom)285     int32_t FfiOHOSWebviewCtlPageDown(int64_t id, bool bottom)
286     {
287         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
288         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
289             return NWebError::INIT_ERROR;
290         }
291         nativeWebviewCtl->ScrollPageDown(bottom);
292         return NWebError::NO_ERROR;
293     }
294 
295     // cookie_manager
FfiOHOSCookieMgrFetchCookieSync(const char * url,bool incognitoMode,int32_t * errCode)296     const char* FfiOHOSCookieMgrFetchCookieSync(const char *url, bool incognitoMode, int32_t* errCode)
297     {
298         std::string curl = url;
299         std::string value = OHOS::NWeb::WebCookieManager::CjGetCookie(curl, incognitoMode, *errCode);
300         const char* res = MallocCString(value);
301         return res;
302     }
303 
FfiOHOSCookieMgrConfigCookieSync(const char * url,const char * value,bool incognitoMode)304     int32_t FfiOHOSCookieMgrConfigCookieSync(const char* url, const char* value, bool incognitoMode)
305     {
306         std::string curl = url;
307         std::string cvalue = value;
308         return OHOS::NWeb::WebCookieManager::CjSetCookie(curl, cvalue, incognitoMode);
309     }
310 
FfiOHOSCookieMgrPutAcceptCookieEnabled(bool accept)311     void FfiOHOSCookieMgrPutAcceptCookieEnabled(bool accept)
312     {
313         return OHOS::NWeb::WebCookieManager::CjPutAcceptCookieEnabled(accept);
314     }
315 
FfiOHOSCookieMgrIsCookieAllowed()316     bool FfiOHOSCookieMgrIsCookieAllowed()
317     {
318         return OHOS::NWeb::WebCookieManager::CjIsCookieAllowed();
319     }
320 
FfiOHOSCookieMgrPutAcceptThirdPartyCookieEnabled(bool accept)321     void FfiOHOSCookieMgrPutAcceptThirdPartyCookieEnabled(bool accept)
322     {
323         return OHOS::NWeb::WebCookieManager::CjPutAcceptThirdPartyCookieEnabled(accept);
324     }
325 
FfiOHOSCookieMgrIsThirdPartyCookieAllowed()326     bool FfiOHOSCookieMgrIsThirdPartyCookieAllowed()
327     {
328         return OHOS::NWeb::WebCookieManager::CjIsThirdPartyCookieAllowed();
329     }
330 
FfiOHOSCookieMgrExistCookie(bool incognitoMode)331     bool FfiOHOSCookieMgrExistCookie(bool incognitoMode)
332     {
333         return OHOS::NWeb::WebCookieManager::CjExistCookie(incognitoMode);
334     }
335 
FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode)336     void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode)
337     {
338         return OHOS::NWeb::WebCookieManager::CjDeleteEntireCookie(incognitoMode);
339     }
340 
FfiOHOSCookieMgrClearSessionCookieSync()341     void FfiOHOSCookieMgrClearSessionCookieSync()
342     {
343         return OHOS::NWeb::WebCookieManager::CjDeleteSessionCookie();
344     }
345 
FfiOHOSWebviewCtlScrollTo(int64_t id,float x,float y)346     int32_t FfiOHOSWebviewCtlScrollTo(int64_t id, float x, float y)
347     {
348         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
349         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
350             return NWebError::INIT_ERROR;
351         }
352         nativeWebviewCtl->ScrollTo(x, y);
353         return NWebError::NO_ERROR;
354     }
355 
FfiOHOSWebviewCtlScrollBy(int64_t id,float deltaX,float deltaY)356     int32_t FfiOHOSWebviewCtlScrollBy(int64_t id, float deltaX, float deltaY)
357     {
358         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
359         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
360             return NWebError::INIT_ERROR;
361         }
362         nativeWebviewCtl->ScrollBy(deltaX, deltaY);
363         return NWebError::NO_ERROR;
364     }
365 
FfiOHOSWebviewCtlForward(int64_t id)366     int32_t FfiOHOSWebviewCtlForward(int64_t id)
367     {
368         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
369         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
370             return NWebError::INIT_ERROR;
371         }
372         nativeWebviewCtl->Forward();
373         return NWebError::NO_ERROR;
374     }
375 
FfiOHOSWebviewCtlBackward(int64_t id)376     int32_t FfiOHOSWebviewCtlBackward(int64_t id)
377     {
378         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
379         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
380             return NWebError::INIT_ERROR;
381         }
382         nativeWebviewCtl->Backward();
383         return NWebError::NO_ERROR;
384     }
385 
FfiOHOSWebviewCtlBackOrForward(int64_t id,int32_t step)386     int32_t FfiOHOSWebviewCtlBackOrForward(int64_t id, int32_t step)
387     {
388         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
389         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
390             return NWebError::INIT_ERROR;
391         }
392         return nativeWebviewCtl->BackOrForward(step);
393     }
394 
FfiOHOSWebviewCtlGetPageHeight(int64_t id,int32_t * errCode)395     int32_t FfiOHOSWebviewCtlGetPageHeight(int64_t id, int32_t *errCode)
396     {
397         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
398         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
399             *errCode = NWebError::INIT_ERROR;
400             return -1;
401         }
402         int32_t pageHeight = nativeWebviewCtl->GetPageHeight();
403         *errCode = NWebError::NO_ERROR;
404         return pageHeight;
405     }
406 
FfiOHOSWebviewCtlGetTitle(int64_t id)407     RetDataCString FfiOHOSWebviewCtlGetTitle(int64_t id)
408     {
409         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
410         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
411         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
412             return ret;
413         }
414         std::string title = "";
415         title = nativeWebviewCtl->GetTitle();
416         ret.code = NWebError::NO_ERROR;
417         ret.data = MallocCString(title);
418         return ret;
419     }
420 
FfiOHOSWebviewCtlZoom(int64_t id,float factor)421     int32_t FfiOHOSWebviewCtlZoom(int64_t id, float factor)
422     {
423         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
424         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
425             return NWebError::INIT_ERROR;
426         }
427         int32_t ret = nativeWebviewCtl->Zoom(factor);
428         return ret;
429     }
430 
FfiOHOSWebviewCtlZoomIn(int64_t id)431     int32_t FfiOHOSWebviewCtlZoomIn(int64_t id)
432     {
433         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
434         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
435             return NWebError::INIT_ERROR;
436         }
437         int32_t ret = nativeWebviewCtl->ZoomIn();
438         return ret;
439     }
440 
FfiOHOSWebviewCtlZoomOut(int64_t id)441     int32_t FfiOHOSWebviewCtlZoomOut(int64_t id)
442     {
443         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
444         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
445             return NWebError::INIT_ERROR;
446         }
447         int32_t ret = nativeWebviewCtl->ZoomOut();
448         return ret;
449     }
450 
FfiOHOSWebviewCtlClearHistory(int64_t id)451     int32_t FfiOHOSWebviewCtlClearHistory(int64_t id)
452     {
453         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
454         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
455             return NWebError::INIT_ERROR;
456         }
457         nativeWebviewCtl->ClearHistory();
458         return NWebError::NO_ERROR;
459     }
460 
FfiOHOSWebviewCtlAccessStep(int64_t id,int32_t * errCode,int32_t step)461     bool FfiOHOSWebviewCtlAccessStep(int64_t id, int32_t *errCode, int32_t step)
462     {
463         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
464         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
465             *errCode = NWebError::INIT_ERROR;
466             return false;
467         }
468         bool access = nativeWebviewCtl->AccessStep(step);
469         *errCode = NWebError::NO_ERROR;
470         return access;
471     }
472 
FfiOHOSWebviewCtlOnActive(int64_t id)473     int32_t FfiOHOSWebviewCtlOnActive(int64_t id)
474     {
475         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
476         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
477             return NWebError::INIT_ERROR;
478         }
479         nativeWebviewCtl->OnActive();
480         return NWebError::NO_ERROR;
481     }
482 
FfiOHOSWebviewCtlOnInactive(int64_t id)483     int32_t FfiOHOSWebviewCtlOnInactive(int64_t id)
484     {
485         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
486         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
487             return NWebError::INIT_ERROR;
488         }
489         nativeWebviewCtl->OnInactive();
490         return NWebError::NO_ERROR;
491     }
492 
FfiOHOSWebviewCtlGetHitTest(int64_t id,int32_t * errCode)493     int32_t FfiOHOSWebviewCtlGetHitTest(int64_t id, int32_t *errCode)
494     {
495         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
496         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
497             *errCode = NWebError::INIT_ERROR;
498             return -1;
499         }
500         int32_t type = nativeWebviewCtl->GetHitTest();
501         *errCode = NWebError::NO_ERROR;
502         return type;
503     }
504 
FfiOHOSWebviewCtlGetHitTestValue(int64_t id,int32_t * errCode)505     RetDataCString FfiOHOSWebviewCtlGetHitTestValue(int64_t id, int32_t *errCode)
506     {
507         RetDataCString ret = { .code = NWeb::HitTestResult::UNKNOWN_TYPE, .data = nullptr };
508         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
509         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
510             *errCode = NWebError::INIT_ERROR;
511             return ret;
512         }
513         std::shared_ptr<NWeb::HitTestResult> nwebResult = nativeWebviewCtl->GetHitTestValue();
514         *errCode = NWebError::NO_ERROR;
515         ret.code = nwebResult->GetType();
516         ret.data = MallocCString(nwebResult->GetExtra());
517         return ret;
518     }
519 
FfiOHOSWebviewCtlStoreWebArchive(int64_t id,const char * cBaseName,bool autoName,void (* callbackRef)(RetDataCString infoRef))520     int32_t FfiOHOSWebviewCtlStoreWebArchive(int64_t id, const char* cBaseName,
521         bool autoName, void (*callbackRef)(RetDataCString infoRef))
522     {
523         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
524         std::string baseName = std::string(cBaseName);
525         auto onChange = [lambda = CJLambda::Create(callbackRef)]
526             (RetDataCString infoRef) -> void { lambda(infoRef); };
527         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
528             return NWebError::INIT_ERROR;
529         }
530         nativeWebviewCtl->StoreWebArchiveCallback(baseName, autoName, onChange);
531         return NWebError::NO_ERROR;
532     }
533 
FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id,bool enable)534     int32_t FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id, bool enable)
535     {
536         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
537         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
538             return NWebError::INIT_ERROR;
539         }
540         nativeWebviewCtl->EnableSafeBrowsing(enable);
541         return NWebError::NO_ERROR;
542     }
543 
FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id,int32_t * errCode)544     bool FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id, int32_t *errCode)
545     {
546         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
547         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
548             *errCode = NWebError::INIT_ERROR;
549             return false;
550         }
551         bool isSafeBrowsingEnabled = nativeWebviewCtl->IsSafeBrowsingEnabled();
552         *errCode = NWebError::NO_ERROR;
553         return isSafeBrowsingEnabled;
554     }
555 
FfiOHOSWebviewCtlGetSecurityLevel(int64_t id,int32_t * errCode)556     int32_t FfiOHOSWebviewCtlGetSecurityLevel(int64_t id, int32_t *errCode)
557     {
558         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
559         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
560             *errCode = NWebError::INIT_ERROR;
561             return -1;
562         }
563         int32_t securityLevel = nativeWebviewCtl->GetSecurityLevel();
564         *errCode = NWebError::NO_ERROR;
565         return securityLevel;
566     }
567 
FfiOHOSWebviewCtlIsIncognitoMode(int64_t id,int32_t * errCode)568     bool FfiOHOSWebviewCtlIsIncognitoMode(int64_t id, int32_t *errCode)
569     {
570         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
571         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
572             *errCode = NWebError::INIT_ERROR;
573             return false;
574         }
575         bool incognitoMode = nativeWebviewCtl->IsIncognitoMode();
576         *errCode = NWebError::NO_ERROR;
577         return incognitoMode;
578     }
579 
FfiOHOSWebviewCtlRemoveCache(int64_t id,bool clearRom)580     int32_t FfiOHOSWebviewCtlRemoveCache(int64_t id, bool clearRom)
581     {
582         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
583         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
584             return NWebError::INIT_ERROR;
585         }
586         nativeWebviewCtl->RemoveCache(clearRom);
587         return NWebError::NO_ERROR;
588     }
589 
FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id,int32_t * errCode)590     int64_t FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id, int32_t *errCode)
591     {
592         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
593         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
594             *errCode = NWebError::INIT_ERROR;
595             return -1;
596         }
597 
598         std::shared_ptr<NWebHistoryList> list = nativeWebviewCtl->GetHistoryList();
599         if (!list) {
600             *errCode = NWebError::INIT_ERROR;
601             return -1;
602         }
603 
604         auto nativeWebHistoryList = FFIData::Create<WebHistoryListImpl>(list);
605         if (nativeWebHistoryList == nullptr) {
606             *errCode = NWebError::INIT_ERROR;
607             WEBVIEWLOGE("new WebHistoryList failed");
608             return -1;
609         }
610         *errCode = NWebError::NO_ERROR;
611         return nativeWebHistoryList->GetID();
612     }
613 
FfiOHOSWebviewCtlStop(int64_t id)614     int32_t FfiOHOSWebviewCtlStop(int64_t id)
615     {
616         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
617         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
618             return NWebError::INIT_ERROR;
619         }
620         nativeWebviewCtl->Stop();
621         return NWebError::NO_ERROR;
622     }
623 
624     // BackForwardList
FfiOHOSBackForwardListCurrentIndex(int64_t id,int32_t * errCode)625     int32_t FfiOHOSBackForwardListCurrentIndex(int64_t id, int32_t *errCode)
626     {
627         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
628         if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
629             *errCode = NWebError::INIT_ERROR;
630             return -1;
631         }
632         *errCode = NWebError::NO_ERROR;
633         return nativeWebHistoryListImpl->GetCurrentIndex();
634     }
635 
FfiOHOSBackForwardListSize(int64_t id,int32_t * errCode)636     int32_t FfiOHOSBackForwardListSize(int64_t id, int32_t *errCode)
637     {
638         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
639         if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
640             *errCode = NWebError::INIT_ERROR;
641             return -1;
642         }
643         *errCode = NWebError::NO_ERROR;
644         return nativeWebHistoryListImpl->GetListSize();
645     }
646 
GetColorType(ImageColorType colorType)647     Media::PixelFormat GetColorType(ImageColorType colorType)
648     {
649         Media::PixelFormat pixelFormat;
650         switch (colorType) {
651             case ImageColorType::COLOR_TYPE_UNKNOWN:
652                 pixelFormat = Media::PixelFormat::UNKNOWN;
653                 break;
654             case ImageColorType::COLOR_TYPE_RGBA_8888:
655                 pixelFormat = Media::PixelFormat::RGBA_8888;
656                 break;
657             case ImageColorType::COLOR_TYPE_BGRA_8888:
658                 pixelFormat = Media::PixelFormat::BGRA_8888;
659                 break;
660             default:
661                 pixelFormat = Media::PixelFormat::UNKNOWN;
662                 break;
663         }
664         return pixelFormat;
665     }
666 
GetAlphaType(ImageAlphaType imageAlphaType)667     Media::AlphaType GetAlphaType(ImageAlphaType imageAlphaType)
668     {
669         Media::AlphaType alphaType;
670         switch (imageAlphaType) {
671             case ImageAlphaType::ALPHA_TYPE_UNKNOWN:
672                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
673                 break;
674             case ImageAlphaType::ALPHA_TYPE_OPAQUE:
675                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
676                 break;
677             case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
678                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
679                 break;
680             case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
681                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
682                 break;
683             default:
684                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
685                 break;
686         }
687         return alphaType;
688     }
689 
GetFavicon(std::shared_ptr<NWebHistoryItem> item)690     int64_t GetFavicon(std::shared_ptr<NWebHistoryItem> item)
691     {
692         void *data = nullptr;
693         int32_t width = 0;
694         int32_t height = 0;
695         ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
696         ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
697         bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType);
698         if (!isGetFavicon) {
699             return -1;
700         }
701         OHOS::Media::InitializationOptions opt;
702         opt.size.width = width;
703         opt.size.height = height;
704         opt.pixelFormat = GetColorType(colorType);
705         opt.alphaType = GetAlphaType(alphaType);
706         opt.editable = true;
707         std::unique_ptr<Media::PixelMap> pixelMap = Media::PixelMapImpl::CreatePixelMap(opt);
708         if (pixelMap == nullptr) {
709             return -1;
710         }
711         uint64_t stride = static_cast<uint64_t>(width) << 2;
712         uint64_t bufferSize = stride * static_cast<uint64_t>(height);
713         pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
714         auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
715         if (nativeImage == nullptr) {
716             return -1;
717         }
718         WEBVIEWLOGI("[PixelMap] create PixelMap success");
719         return nativeImage->GetID();
720     }
721 
FfiOHOSGetItemAtIndex(int64_t id,int32_t index,int32_t * errCode)722     CHistoryItem FfiOHOSGetItemAtIndex(int64_t id, int32_t index, int32_t *errCode)
723     {
724         CHistoryItem ret = {.icon = -1, .historyUrl = nullptr, .historyRawUrl = nullptr, .title = nullptr};
725         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
726         if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
727             *errCode = NWebError::INIT_ERROR;
728             return ret;
729         }
730         if (index >= nativeWebHistoryListImpl->GetListSize() || index < 0) {
731             *errCode = NWebError::PARAM_CHECK_ERROR;
732             return ret;
733         }
734         std::shared_ptr<NWebHistoryItem> item = nativeWebHistoryListImpl->GetItem(index);
735         if (!item) {
736             *errCode = NWebError::NWEB_ERROR;
737             return ret;
738         }
739         ret.historyUrl = MallocCString(item->GetHistoryUrl());
740         ret.historyRawUrl = MallocCString(item->GetHistoryRawUrl());
741         ret.title = MallocCString(item->GetHistoryTitle());
742         ret.icon = GetFavicon(item);
743         *errCode = NWebError::NO_ERROR;
744         return ret;
745     }
746 }
747 }
748 }
749