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