• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef WEBVIEW_CONTROLLER_IMPL_FFI_H
17 #define WEBVIEW_CONTROLLER_IMPL_FFI_H
18 
19 #include <cstdint>
20 #include <map>
21 #include "ffi_remote_data.h"
22 #include "webview_utils.h"
23 #include "web_errors.h"
24 #include "webview_javascript_result_callback.h"
25 #include "nweb.h"
26 #include "nweb_helper.h"
27 #include "nweb_web_message.h"
28 #include "nweb_message_ext.h"
29 #include "web_scheme_handler_request.h"
30 
31 namespace OHOS::Webview {
32 enum class WebHitTestType : int {
33     EDIT = 0,
34     EMAIL,
35     HTTP,
36     HTTP_IMG,
37     IMG,
38     MAP,
39     PHONE,
40     UNKNOWN
41 };
42 
43 enum class SecurityLevel : int {
44     NONE = 0,
45     SECURE,
46     WARNING,
47     DANGEROUS
48 };
49 
50 enum class CoreSecurityLevel : int {
51     NONE = 0,
52     SECURE = 3,
53     WARNING = 6,
54     DANGEROUS = 5
55 };
56 
57 enum class WebMessageType : int {
58     NOTSUPPORT = 0,
59     STRING,
60     NUMBER,
61     BOOLEAN,
62     ARRAYBUFFER,
63     ARRAY,
64     ERROR
65 };
66 
67 enum class MediaPlaybackState: int {
68     NONE = 0,
69     PLAYING = 1,
70     PAUSED = 2,
71     STOPPED = 3
72 };
73 
74 enum class RenderProcessMode: int {
75     SINGLE = 0,
76     MULTIPLE = 1
77 };
78 
79 enum class OfflineResourceType : int {
80     IMAGE = 0,
81     CSS,
82     CLASSIC_JS,
83     MODULE_JS
84 };
85 
86 enum class UrlListSetResult : int {
87     INIT_ERROR = -2,
88     PARAM_ERROR = -1,
89     SET_OK = 0
90 };
91 
92 class __attribute__((visibility("default"))) WebviewControllerImpl : public OHOS::FFI::FFIData {
93     DECL_TYPE(WebviewControllerImpl, OHOS::FFI::FFIData)
94 public:
95     explicit WebviewControllerImpl() = default;
96 
97     explicit WebviewControllerImpl(int32_t nwebId);
98 
WebviewControllerImpl(const std::string & webTag)99     explicit WebviewControllerImpl(const std::string& webTag) : webTag_(webTag)
100     {
101         NWeb::NWebHelper::Instance().SetWebTag(-1, webTag_.c_str());
102     };
103 
104     bool IsInit();
105 
106     void SetWebId(int32_t nwebId);
107 
108     void InnerSetHapPath(const std::string& hapPath);
109 
110     int32_t GetWebId() const;
111 
112     int32_t LoadUrl(std::string url);
113 
114     int32_t LoadUrl(std::string url, std::map<std::string, std::string> headers);
115 
116     ErrCode LoadData(
117         std::string data, std::string mimeType, std::string encoding, std::string baseUrl, std::string historyUrl);
118 
119     int32_t PreFetchPage(std::string url);
120 
121     int32_t PreFetchPage(std::string url, std::map<std::string, std::string> headers);
122 
123     int32_t SetAudioMuted(bool mute);
124 
125     void SlideScroll(float vx, float vy);
126 
127     void PutNetworkAvailable(bool enable);
128 
129     void ClearClientAuthenticationCache();
130 
131     void ClearSslCache();
132 
133     void SearchNext(bool forward);
134 
135     void ClearMatches();
136 
137     void SearchAllAsync(std::string str);
138 
139     ErrCode DeleteJavaScriptRegister(const std::string& objName, const std::vector<std::string>& methodList);
140 
141     void Refresh();
142 
143     std::string GetUserAgent();
144 
145     bool AccessForward();
146 
147     bool AccessBackward();
148 
149     int32_t SetCustomUserAgent(const std::string& userAgent);
150 
151     std::string GetCustomUserAgent() const;
152 
153     void RunJavaScript(std::string script, const std::function<void(RetDataCString)>& callbackRef);
154 
155     void RunJavaScriptExt(std::string script, const std::function<void(RetDataI64)>& callbackRef);
156 
157     std::string GetUrl();
158 
159     std::string GetOriginalUrl();
160 
161     void ScrollPageUp(bool top);
162 
163     void ScrollPageDown(bool bottom);
164 
165     void ScrollTo(float x, float y);
166 
167     void ScrollBy(float deltaX, float deltaY);
168 
169     void ScrollToWithAnime(float x, float y, int32_t duration);
170 
171     void ScrollByWithAnime(float deltaX, float deltaY, int32_t duration);
172 
173     void Forward();
174 
175     void Backward();
176 
177     int32_t BackOrForward(int32_t step);
178 
179     int32_t GetProgress();
180 
181     int32_t GetPageHeight();
182 
183     std::string GetTitle();
184 
185     int32_t Zoom(float factor);
186 
187     int32_t ZoomIn();
188 
189     int32_t ZoomOut();
190 
191     int32_t RequestFocus();
192 
193     void ClearHistory();
194 
195     bool AccessStep(int32_t step);
196 
197     void OnActive();
198 
199     void OnInactive();
200 
201     int32_t GetHitTest();
202 
203     std::shared_ptr<NWeb::HitTestResult> GetHitTestValue();
204 
205     void StoreWebArchiveCallback(
206         std::string baseName, bool autoName, const std::function<void(RetDataCString)>& callbackRef);
207 
208     void EnableSafeBrowsing(bool enable);
209 
210     bool IsSafeBrowsingEnabled();
211 
212     int32_t GetSecurityLevel();
213 
214     bool IsIncognitoMode();
215 
216     void RemoveCache(bool includeDiskFiles);
217 
218     std::shared_ptr<OHOS::NWeb::NWebHistoryList> GetHistoryList();
219 
220     bool GetFavicon(const void **data, size_t &width, size_t &height,
221         NWeb::ImageColorType &colorType, NWeb::ImageAlphaType &alphaType) const;
222 
223     void SetNWebJavaScriptResultCallBack();
224 
225     void RegisterJavaScriptProxy(const std::vector<std::function<char*(const char*)>>& cjFuncs,
226         const std::string& objName, const std::vector<std::string>& methodList);
227 
228     void RegisterJavaScriptProxyEx(const std::vector<std::function<char*(const char*)>>& cjFuncs,
229         const std::string& objName, const std::vector<std::string>& methodList, char* permission);
230     void Stop();
231 
232     void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive);
233 
234     int32_t PostUrl(std::string& url, std::vector<char>& postData);
235 
236     std::vector<std::string> CreateWebMessagePorts();
237 
238     ErrCode PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl);
239 
240     std::vector<uint8_t> SerializeWebState();
241 
242     bool RestoreWebState(const std::vector<uint8_t>& state) const;
243 
244     bool GetCertChainDerData(std::vector<std::string>& certChainDerData) const;
245 
246     ErrCode HasImagesCallback(const std::function<void(RetDataBool)>& callbackRef);
247 
248     static int32_t CustomizeSchemesArrayDataHandler(CArrScheme schemes);
249 
250     bool TerminateRenderProcess();
251 
252     void CloseAllMediaPresentations();
253 
254     void PauseAllMedia();
255 
256     void ResumeAllMedia();
257 
258     void StopAllMedia();
259 
260     void SetPrintBackground(bool enable);
261 
262     bool GetPrintBackground();
263 
264     bool GetScrollable();
265 
266     void SetScrollable(bool enable);
267 
268     void SetScrollable(bool enable, int32_t scrollType);
269 
270     void EnableAdsBlock(bool enable);
271 
272     bool IsAdsBlockEnabled();
273 
274     bool IsAdsBlockEnabledForCurPage();
275 
276     bool IsIntelligentTrackingPreventionEnabled();
277 
278     void EnableIntelligentTrackingPrevention(bool enable);
279 
280     int32_t GetMediaPlaybackState();
281 
282     std::string GetLastJavascriptProxyCallingFrameUrl();
283 
284     void StartCamera();
285 
286     void StopCamera();
287 
288     void CloseCamera();
289 
290     std::string GetSurfaceId();
291 
292     void InjectOfflineResources(const std::vector<std::string>& urlList, const std::vector<uint8_t>& resource,
293         const std::map<std::string, std::string>& response_headers, const uint32_t type);
294 
295     int32_t SetUrlTrustList(const std::string& urlTrustList, std::string& detailErrMsg);
296 
297     void SetPathAllowingUniversalAccess(const std::vector<std::string>& pathList, std::string& errorPath);
298 
299     bool SetWebSchemeHandler(const char* scheme, WebSchemeHandlerImpl* handler);
300 
301     int32_t ClearWebSchemeHandler();
302 
303     static bool SetWebServiceWorkerSchemeHandler(const char* scheme, WebSchemeHandlerImpl* handler);
304 
305     static int32_t ClearWebServiceWorkerSchemeHandler();
306 
307     void OnCreateNativeMediaPlayer(std::function<int64_t(int64_t, CMediaInfo)> callback);
308 
309     int32_t PrecompileJavaScript(std::string url, std::string script,
310         std::shared_ptr<OHOS::NWeb::CacheOptions> cacheOptions);
311 
312     int32_t WebPageSnapshot(const char* id, NWeb::PixelUnit type, int32_t width, int32_t height,
313         const NWeb::WebSnapshotCallback callback);
314 
315     std::shared_ptr<NWeb::HitTestResult> GetLastHitTest();
316 
317     void* CreateWebPrintDocumentAdapter(const std::string &jobName);
318 
319     void GetScrollOffset(float* offset_x, float* offset_y);
320 
321     bool ScrollByWithResult(float deltaX, float deltaY) const;
322 
323     int32_t AvoidVisibleViewportBottom(int32_t avoidHeight);
324 
325     int32_t SetErrorPageEnabled(bool enable);
326 
327     bool GetErrorPageEnabled();
328 
329 public:
330     static std::string customeSchemeCmdLine_;
331     static bool existNweb_;
332     static bool webDebuggingAccess_;
333 
334 private:
335     int ConverToWebHitTestType(int hitType);
336     bool GetHapModuleInfo();
337 
338 private:
339     std::mutex webMtx_;
340     int32_t nwebId_ = -1;
341     std::shared_ptr<WebviewJavaScriptResultCallBackImpl> javaScriptResultCb_ = nullptr;
342     std::string hapPath_ = "";
343     std::string webTag_ = "";
344     std::vector<std::string> moduleName_;
345 };
346 
347 class __attribute__((visibility("default"))) WebHistoryListImpl : public OHOS::FFI::FFIData {
DECL_TYPE(WebHistoryListImpl,OHOS::FFI::FFIData)348     DECL_TYPE(WebHistoryListImpl, OHOS::FFI::FFIData)
349 public:
350     explicit WebHistoryListImpl(std::shared_ptr<NWeb::NWebHistoryList> sptrHistoryList)
351         : sptrHistoryList_(sptrHistoryList) {};
352 
353     int32_t GetCurrentIndex();
354 
355     std::shared_ptr<OHOS::NWeb::NWebHistoryItem> GetItem(int32_t index);
356 
357     int32_t GetListSize();
358 
359 private:
360     std::shared_ptr<OHOS::NWeb::NWebHistoryList> sptrHistoryList_ = nullptr;
361 };
362 
363 class WebMessagePortImpl : public OHOS::FFI::FFIData {
364     DECL_TYPE(WebMessagePortImpl, OHOS::FFI::FFIData)
365 public:
366     WebMessagePortImpl(int32_t nwebId, std::string port, bool isExtentionType);
367 
368     ~WebMessagePortImpl() = default;
369 
370     ErrCode ClosePort();
371 
372     ErrCode PostPortMessage(std::shared_ptr<NWeb::NWebMessage> data);
373 
374     ErrCode SetPortMessageCallback(std::shared_ptr<NWeb::NWebMessageValueCallback> callback);
375 
376     std::string GetPortHandle() const;
377 
IsExtentionType()378     bool IsExtentionType()
379     {
380         return isExtentionType_;
381     }
382 
383 private:
384     int32_t nwebId_ = -1;
385     std::string portHandle_;
386     bool isExtentionType_;
387 };
388 
389 class WebMessageExtImpl : public OHOS::FFI::FFIData {
DECL_TYPE(WebMessageExtImpl,OHOS::FFI::FFIData)390     DECL_TYPE(WebMessageExtImpl, OHOS::FFI::FFIData)
391 public:
392     explicit WebMessageExtImpl(std::shared_ptr<NWeb::NWebMessage> data) : data_(data) {}
WebMessageExtImpl(std::shared_ptr<NWeb::NWebHapValue> data)393     explicit WebMessageExtImpl(std::shared_ptr<NWeb::NWebHapValue> data)
394     {
395         data_ = NWeb::ConvertNwebHap2NwebMessage(data);
396     }
397     ~WebMessageExtImpl() = default;
398 
SetType(int type)399     void SetType(int type)
400     {
401         type_ = type;
402         WebMessageType jsType = static_cast<WebMessageType>(type);
403         NWeb::NWebValue::Type nwebType = NWeb::NWebValue::Type::NONE;
404         switch (jsType) {
405             case WebMessageType::STRING: {
406                 nwebType = NWeb::NWebValue::Type::STRING;
407                 break;
408             }
409             case WebMessageType::NUMBER: {
410                 nwebType = NWeb::NWebValue::Type::DOUBLE;
411                 break;
412             }
413             case WebMessageType::BOOLEAN: {
414                 nwebType = NWeb::NWebValue::Type::BOOLEAN;
415                 break;
416             }
417             case WebMessageType::ARRAYBUFFER: {
418                 nwebType = NWeb::NWebValue::Type::BINARY;
419                 break;
420             }
421             case WebMessageType::ARRAY: {
422                 nwebType = NWeb::NWebValue::Type::STRINGARRAY;
423                 break;
424             }
425             case WebMessageType::ERROR: {
426                 nwebType = NWeb::NWebValue::Type::ERROR;
427                 break;
428             }
429             default: {
430                 nwebType = NWeb::NWebValue::Type::NONE;
431                 break;
432             }
433         }
434         if (data_) {
435             data_->SetType(nwebType);
436         }
437     }
438 
ConvertNwebType2JsType(NWeb::NWebValue::Type type)439     int ConvertNwebType2JsType(NWeb::NWebValue::Type type)
440     {
441         WebMessageType jsType = WebMessageType::NOTSUPPORT;
442         switch (type) {
443             case NWeb::NWebValue::Type::STRING: {
444                 jsType = WebMessageType::STRING;
445                 break;
446             }
447             case NWeb::NWebValue::Type::DOUBLE:
448             case NWeb::NWebValue::Type::INTEGER: {
449                 jsType = WebMessageType::NUMBER;
450                 break;
451             }
452             case NWeb::NWebValue::Type::BOOLEAN: {
453                 jsType = WebMessageType::BOOLEAN;
454                 break;
455             }
456             case NWeb::NWebValue::Type::STRINGARRAY:
457             case NWeb::NWebValue::Type::DOUBLEARRAY:
458             case NWeb::NWebValue::Type::INT64ARRAY:
459             case NWeb::NWebValue::Type::BOOLEANARRAY: {
460                 jsType = WebMessageType::ARRAY;
461                 break;
462             }
463             case NWeb::NWebValue::Type::BINARY: {
464                 jsType = WebMessageType::ARRAYBUFFER;
465                 break;
466             }
467             case NWeb::NWebValue::Type::ERROR: {
468                 jsType = WebMessageType::ERROR;
469                 break;
470             }
471             default: {
472                 jsType = WebMessageType::NOTSUPPORT;
473                 break;
474             }
475         }
476         return static_cast<int>(jsType);
477     }
478 
GetType()479     int GetType()
480     {
481         if (data_) {
482             return ConvertNwebType2JsType(data_->GetType());
483         }
484         return static_cast<int>(WebMessageType::NOTSUPPORT);
485     }
486 
SetString(std::string value)487     void SetString(std::string value)
488     {
489         if (data_) {
490             data_->SetType(NWeb::NWebValue::Type::STRING);
491             data_->SetString(value);
492         }
493     }
494 
SetNumber(double value)495     void SetNumber(double value)
496     {
497         if (data_) {
498             data_->SetType(NWeb::NWebValue::Type::DOUBLE);
499             data_->SetDouble(value);
500         }
501     }
502 
SetBoolean(bool value)503     void SetBoolean(bool value)
504     {
505         if (data_) {
506             data_->SetType(NWeb::NWebValue::Type::BOOLEAN);
507             data_->SetBoolean(value);
508         }
509     }
510 
SetArrayBuffer(std::vector<uint8_t> & value)511     void SetArrayBuffer(std::vector<uint8_t>& value)
512     {
513         if (data_) {
514             data_->SetType(NWeb::NWebValue::Type::BINARY);
515             data_->SetBinary(value);
516         }
517     }
518 
SetStringArray(std::vector<std::string> value)519     void SetStringArray(std::vector<std::string> value)
520     {
521         if (data_) {
522             data_->SetType(NWeb::NWebValue::Type::STRINGARRAY);
523             data_->SetStringArray(value);
524         }
525     }
526 
SetDoubleArray(std::vector<double> value)527     void SetDoubleArray(std::vector<double> value)
528     {
529         if (data_) {
530             data_->SetType(NWeb::NWebValue::Type::DOUBLEARRAY);
531             data_->SetDoubleArray(value);
532         }
533     }
534 
SetInt64Array(std::vector<int64_t> value)535     void SetInt64Array(std::vector<int64_t> value)
536     {
537         if (data_) {
538             data_->SetType(NWeb::NWebValue::Type::INT64ARRAY);
539             data_->SetInt64Array(value);
540         }
541     }
542 
SetBooleanArray(std::vector<bool> value)543     void SetBooleanArray(std::vector<bool> value)
544     {
545         if (data_) {
546             data_->SetType(NWeb::NWebValue::Type::BOOLEANARRAY);
547             data_->SetBooleanArray(value);
548         }
549     }
550 
SetError(std::string name,std::string message)551     void SetError(std::string name, std::string message)
552     {
553         if (data_) {
554             data_->SetType(NWeb::NWebValue::Type::ERROR);
555             data_->SetErrName(name);
556             data_->SetErrMsg(message);
557         }
558     }
559 
GetData()560     std::shared_ptr<NWeb::NWebMessage> GetData()
561     {
562         return data_;
563     }
564 
565 private:
566     int type_ = 0;
567     std::shared_ptr<NWeb::NWebMessage> data_;
568 };
569 
570 class NWebMessageCallbackImpl : public NWeb::NWebMessageValueCallback {
571 public:
NWebMessageCallbackImpl(std::function<void (RetWebMessage)> callback)572     NWebMessageCallbackImpl(std::function<void(RetWebMessage)> callback) : callback_(callback) {}
573     ~NWebMessageCallbackImpl() = default;
574     void OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result) override;
575     void OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value) override;
576 
577 private:
578     std::function<void(RetWebMessage)> callback_;
579 };
580 
581 class NWebWebMessageExtCallbackImpl : public NWeb::NWebMessageValueCallback {
582 public:
NWebWebMessageExtCallbackImpl(std::function<void (int64_t)> callback)583     NWebWebMessageExtCallbackImpl(std::function<void(int64_t)> callback) : callback_(callback) {}
584     ~NWebWebMessageExtCallbackImpl() = default;
585     void OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result) override;
586     void OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value) override;
587 
588 private:
589     std::function<void(int64_t)> callback_;
590 };
591 } // namespace OHOS::Webview
592 #endif // WEBVIEW_CONTROLLER_IMPL_FFI_H