2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Runtime.InteropServices;
26 /// WebContext is a class for context of web view.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class WebContext : Disposable
32 private string appVersion;
33 private float timeOffset;
34 private ApplicationType applicationType;
35 private WebSecurityOriginList securityOriginList;
36 private SecurityOriginListAcquiredCallback securityOriginListAcquiredCallback;
37 private readonly WebContextSecurityOriginListAcquiredProxyCallback securityOriginListAcquiredProxyCallback;
38 private WebPasswordDataList passwordDataList;
39 private PasswordDataListAcquiredCallback passwordDataListAcquiredCallback;
40 private readonly WebContextPasswordDataListAcquiredProxyCallback passwordDataListAcquiredProxyCallback;
41 private HttpRequestInterceptedCallback httpRequestInterceptedCallback;
42 private readonly WebContextHttpRequestInterceptedProxyCallback httpRequestInterceptedProxyCallback;
44 internal WebContext(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
46 securityOriginListAcquiredProxyCallback = OnSecurityOriginListAcquired;
47 passwordDataListAcquiredProxyCallback = OnPasswordDataListAcquired;
48 httpRequestInterceptedProxyCallback = OnHttpRequestIntercepted;
52 /// Dispose for IDisposable pattern
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 protected override void Dispose(DisposeTypes type)
62 if (type == DisposeTypes.Explicit)
65 //Release your own managed resources here.
66 //You should release all of your own disposable objects here.
67 if (passwordDataList != null)
69 passwordDataList.Dispose();
71 if (securityOriginList != null)
73 securityOriginList.Dispose();
81 /// The callback function that is invoked when security origin list is acquired.
83 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
84 [EditorBrowsable(EditorBrowsableState.Never)]
85 public delegate void SecurityOriginListAcquiredCallback(IList<WebSecurityOrigin> list);
88 /// The callback function that is invoked when storage usage is acquired.
90 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public delegate void StorageUsageAcquiredCallback(ulong usage);
95 /// The callback function that is invoked when password data list is acquired.
97 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
98 [EditorBrowsable(EditorBrowsableState.Never)]
99 public delegate void PasswordDataListAcquiredCallback(IList<WebPasswordData> list);
102 /// The callback function that is invoked when download is started.
104 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public delegate void DownloadStartedCallback(string url);
109 /// The callback function that is invoked when current mime type need be overridden.
111 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
112 [EditorBrowsable(EditorBrowsableState.Never)]
113 public delegate bool MimeOverriddenCallback(string url, string currentMime, out string newMime);
116 /// The callback function that is invoked when http request need be intercepted.
118 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public delegate void HttpRequestInterceptedCallback(WebHttpRequestInterceptor interceptor);
122 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
123 private delegate void WebContextSecurityOriginListAcquiredProxyCallback(IntPtr list);
125 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
126 private delegate void WebContextPasswordDataListAcquiredProxyCallback(IntPtr list);
128 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
129 private delegate void WebContextHttpRequestInterceptedProxyCallback(IntPtr interceptor);
134 [EditorBrowsable(EditorBrowsableState.Never)]
135 public enum CacheModelType
138 /// The smallest cache capacity
140 [EditorBrowsable(EditorBrowsableState.Never)]
144 /// The bigger cache capacity than DocumentBrowser
146 [EditorBrowsable(EditorBrowsableState.Never)]
150 /// The biggest cache capacity.
152 [EditorBrowsable(EditorBrowsableState.Never)]
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 public enum ApplicationType
165 [EditorBrowsable(EditorBrowsableState.Never)]
171 [EditorBrowsable(EditorBrowsableState.Never)]
177 [EditorBrowsable(EditorBrowsableState.Never)]
183 [EditorBrowsable(EditorBrowsableState.Never)]
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 public CacheModelType CacheModel
195 return (CacheModelType)Interop.WebContext.GetCacheModel(SwigCPtr);
199 Interop.WebContext.SetCacheModel(SwigCPtr, (int)value);
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207 [EditorBrowsable(EditorBrowsableState.Never)]
208 public string ProxyUrl
212 return Interop.WebContext.GetProxyUri(SwigCPtr);
218 Interop.WebContext.SetProxyUri(SwigCPtr, value);
219 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225 /// Certificate file path.
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 public string CertificateFilePath
232 return Interop.WebContext.GetCertificateFilePath(SwigCPtr);
236 Interop.WebContext.SetCertificateFilePath(SwigCPtr, value);
237 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
242 /// Enables cache or not.
244 [EditorBrowsable(EditorBrowsableState.Never)]
245 public bool CacheEnabled
249 return Interop.WebContext.IsCacheEnabled(SwigCPtr);
253 Interop.WebContext.EnableCache(SwigCPtr, value);
254 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
261 [EditorBrowsable(EditorBrowsableState.Never)]
270 Interop.WebContext.SetAppId(SwigCPtr, value);
271 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
279 [EditorBrowsable(EditorBrowsableState.Never)]
280 public string AppVersion
288 Interop.WebContext.SetAppVersion(SwigCPtr, value);
289 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
297 [EditorBrowsable(EditorBrowsableState.Never)]
298 public ApplicationType AppType
302 return applicationType;
306 Interop.WebContext.SetApplicationType(SwigCPtr, (int)value);
307 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
308 applicationType = value;
315 [EditorBrowsable(EditorBrowsableState.Never)]
316 public float TimeOffset
324 Interop.WebContext.SetTimeOffset(SwigCPtr, value);
325 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331 /// Default zoom factor.
333 [EditorBrowsable(EditorBrowsableState.Never)]
334 public float DefaultZoomFactor
338 return Interop.WebContext.GetDefaultZoomFactor(SwigCPtr);
342 Interop.WebContext.SetDefaultZoomFactor(SwigCPtr, value);
343 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
348 /// Deprecated. Gets context proxy.
350 [EditorBrowsable(EditorBrowsableState.Never)]
351 public string ContextProxy
355 return Interop.WebContext.GetProxyUri(SwigCPtr);
360 /// Gets proxy bypass rule.
362 [EditorBrowsable(EditorBrowsableState.Never)]
363 public string ProxyBypassRule
367 return Interop.WebContext.GetProxyBypassRule(SwigCPtr);
372 /// Sets default proxy auth.
374 /// <param name="username">Default username for proxy</param>
375 /// <param name="password">Default password for proxy</param>
376 [EditorBrowsable(EditorBrowsableState.Never)]
377 public void SetDefaultProxyAuth(string username, string password)
379 Interop.WebContext.SetDefaultProxyAuth(SwigCPtr, username, password);
380 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
384 /// Deletes all web database.
386 [EditorBrowsable(EditorBrowsableState.Never)]
387 public void DeleteAllWebDatabase()
389 Interop.WebContext.DeleteAllWebDatabase(SwigCPtr);
390 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394 /// Gets security origins of web database asynchronously.
396 /// <param name="callback">callback for acquiring security origins</param>
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 public bool GetWebDatabaseOrigins(SecurityOriginListAcquiredCallback callback)
400 securityOriginListAcquiredCallback = callback;
401 IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
402 bool result = Interop.WebContext.GetWebDatabaseOrigins(SwigCPtr, new HandleRef(this, ip));
403 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408 /// Deletes web databases by origin.
410 /// <param name="origin">security origin of web database</param>
411 [EditorBrowsable(EditorBrowsableState.Never)]
412 public bool DeleteWebDatabase(WebSecurityOrigin origin)
414 bool result = Interop.WebContext.DeleteWebDatabase(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
415 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
420 /// Gets a list of security origins of web storage asynchronously.
422 /// <param name="callback">callback for acquiring security origins</param>
423 [EditorBrowsable(EditorBrowsableState.Never)]
424 public bool GetWebStorageOrigins(SecurityOriginListAcquiredCallback callback)
426 securityOriginListAcquiredCallback = callback;
427 IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
428 bool result = Interop.WebContext.GetWebStorageOrigins(SwigCPtr, new HandleRef(this, ip));
429 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
434 /// Gets a list of security origins of web storage asynchronously.
436 /// <param name="origin">security origin of web storage</param>
437 /// <param name="callback">callback for acquiring storage usage</param>
438 [EditorBrowsable(EditorBrowsableState.Never)]
439 public bool GetWebStorageUsageForOrigin(WebSecurityOrigin origin, StorageUsageAcquiredCallback callback)
441 IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
442 bool result = Interop.WebContext.GetWebStorageUsageForOrigin(SwigCPtr, WebSecurityOrigin.getCPtr(origin), new HandleRef(this, ip));
443 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
448 /// Deletes all web storage.
450 [EditorBrowsable(EditorBrowsableState.Never)]
451 public void DeleteAllWebStorage()
453 Interop.WebContext.DeleteAllWebStorage(SwigCPtr);
454 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
458 /// Deletes web storage by origin.
460 /// <param name="origin">security origin of web storage</param>
461 [EditorBrowsable(EditorBrowsableState.Never)]
462 public bool DeleteWebStorage(WebSecurityOrigin origin)
464 bool result = Interop.WebContext.DeleteWebStorage(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470 /// Deletes directories and files in local file system.
472 [EditorBrowsable(EditorBrowsableState.Never)]
473 public void DeleteLocalFileSystem()
475 Interop.WebContext.DeleteLocalFileSystem(SwigCPtr);
476 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482 [EditorBrowsable(EditorBrowsableState.Never)]
483 public void ClearCache()
485 Interop.WebContext.ClearCache(SwigCPtr);
486 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
490 /// Deletes web application cache by origin.
492 /// <param name="origin">security origin of web application</param>
493 [EditorBrowsable(EditorBrowsableState.Never)]
494 public bool DeleteApplicationCache(WebSecurityOrigin origin)
496 bool result = Interop.WebContext.DeleteApplicationCache(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
497 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
502 /// Gets a list of all password data asynchronously.
504 /// <param name="callback">callback for acquiring password data list</param>
505 [EditorBrowsable(EditorBrowsableState.Never)]
506 public void GetFormPasswordList(PasswordDataListAcquiredCallback callback)
508 passwordDataListAcquiredCallback = callback;
509 IntPtr ip = Marshal.GetFunctionPointerForDelegate(passwordDataListAcquiredProxyCallback);
510 Interop.WebContext.GetFormPasswordList(SwigCPtr, new HandleRef(this, ip));
511 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 /// Registers callback for download started.
517 /// <param name="callback">callback for download started</param>
518 [EditorBrowsable(EditorBrowsableState.Never)]
519 public void RegisterDownloadStartedCallback(DownloadStartedCallback callback)
521 IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
522 Interop.WebContext.RegisterDownloadStartedCallback(SwigCPtr, new HandleRef(this, ip));
523 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
527 /// Registers callback for overriding MIME type.
529 /// <param name="callback">callback for overriding MIME type</param>
530 [EditorBrowsable(EditorBrowsableState.Never)]
531 public void RegisterMimeOverriddenCallback(MimeOverriddenCallback callback)
533 IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
534 Interop.WebContext.RegisterMimeOverriddenCallback(SwigCPtr, new HandleRef(this, ip));
535 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
539 /// Registers callback for http request interceptor.
541 /// <param name="callback">callback for intercepting http request</param>
542 [EditorBrowsable(EditorBrowsableState.Never)]
543 public void RegisterHttpRequestInterceptedCallback(HttpRequestInterceptedCallback callback)
545 httpRequestInterceptedCallback = callback;
546 IntPtr ip = IntPtr.Zero;
547 if (httpRequestInterceptedCallback != null)
549 ip = Marshal.GetFunctionPointerForDelegate(httpRequestInterceptedProxyCallback);
551 Interop.WebContext.RegisterRequestInterceptedCallback(SwigCPtr, new HandleRef(this, ip));
552 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
556 /// Sets context time zone offset.
558 /// <param name="offset">Time offset</param>
559 /// <param name="time">Daylight saving time</param>
560 [EditorBrowsable(EditorBrowsableState.Never)]
561 public void SetTimeZoneOffset(float offset, float time)
563 Interop.WebContext.SetTimeZoneOffset(SwigCPtr, offset, time);
564 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
568 /// Deprecated. Sets context time zone offset.
570 /// <param name="offset">Time offset</param>
571 /// <param name="time">Daylight saving time</param>
572 [EditorBrowsable(EditorBrowsableState.Never)]
573 public void SetContextTimeZoneOffset(float offset, float time)
575 SetTimeZoneOffset(offset, time);
579 /// Registers URL schemes enabled.
581 /// <param name="schemes">The string array of schemes</param>
582 [EditorBrowsable(EditorBrowsableState.Never)]
583 public void RegisterUrlSchemesAsCorsEnabled(string[] schemes)
587 Interop.WebContext.RegisterUrlSchemesAsCorsEnabled(SwigCPtr, schemes, (uint)schemes.Length);
588 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
593 /// Registers JS plugin mime types.
595 /// <param name="mimes">The string array of types</param>
596 [EditorBrowsable(EditorBrowsableState.Never)]
597 public void RegisterJsPluginMimeTypes(string[] mimes)
601 Interop.WebContext.RegisterJsPluginMimeTypes(SwigCPtr, mimes, (uint)mimes.Length);
602 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
607 /// Deletes all application cache.
609 [EditorBrowsable(EditorBrowsableState.Never)]
610 public bool DeleteAllApplicationCache()
612 bool ret = Interop.WebContext.DeleteAllApplicationCache(SwigCPtr);
613 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
618 /// Deletes all web indexed database.
620 [EditorBrowsable(EditorBrowsableState.Never)]
621 public bool DeleteAllWebIndexedDatabase()
623 bool ret = Interop.WebContext.DeleteAllWebIndexedDatabase(SwigCPtr);
624 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
629 /// Deletes password dataList.
631 /// <param name="passwords">The string array of data list</param>
632 [EditorBrowsable(EditorBrowsableState.Never)]
633 public void DeleteFormPasswordDataList(string[] passwords)
635 if (passwords != null)
637 Interop.WebContext.DeleteFormPasswordDataList(SwigCPtr, passwords, (uint)passwords.Length);
638 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
643 /// Deletes all password data.
645 [EditorBrowsable(EditorBrowsableState.Never)]
646 public void DeleteAllFormPasswordData()
648 Interop.WebContext.DeleteAllFormPasswordData(SwigCPtr);
649 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
653 /// Deletes all candidate data.
655 [EditorBrowsable(EditorBrowsableState.Never)]
656 public void DeleteAllFormCandidateData()
658 Interop.WebContext.DeleteAllFormCandidateData(SwigCPtr);
659 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
663 /// Sets proxy bypass rule.
665 /// <param name="proxy">The proxy string</param>
666 /// <param name="rule">Bypass rule</param>
667 [EditorBrowsable(EditorBrowsableState.Never)]
668 public void SetProxyBypassRule(string proxy, string rule)
670 Interop.WebContext.SetProxyBypassRule(SwigCPtr, proxy, rule);
671 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
675 /// Deprecated. Sets proxy bypass rule.
677 /// <param name="proxy">The proxy string</param>
678 /// <param name="rule">Bypass rule</param>
679 [EditorBrowsable(EditorBrowsableState.Never)]
680 public void SetContextProxy(string proxy, string rule)
682 SetProxyBypassRule(proxy, rule);
686 /// Frees unused memory.
688 [EditorBrowsable(EditorBrowsableState.Never)]
689 public bool FreeUnusedMemory()
691 bool ret = Interop.WebContext.FreeUnusedMemory(SwigCPtr);
692 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
696 private void OnSecurityOriginListAcquired(IntPtr alist)
698 if (securityOriginList != null)
700 securityOriginList.Dispose();
702 securityOriginList = new WebSecurityOriginList(alist, true);
703 List<WebSecurityOrigin> originList = new List<WebSecurityOrigin>();
704 for (uint i = 0; i < securityOriginList.ItemCount; i++)
706 originList.Add(securityOriginList.GetItemAtIndex(i));
708 securityOriginListAcquiredCallback?.Invoke(originList);
711 private void OnPasswordDataListAcquired(IntPtr alist)
713 if (passwordDataList != null)
715 passwordDataList.Dispose();
717 passwordDataList = new WebPasswordDataList(alist, true);
718 List<WebPasswordData> pList = new List<WebPasswordData>();
719 for(uint i = 0; i < passwordDataList.ItemCount; i++)
721 pList.Add(passwordDataList.GetItemAtIndex(i));
723 passwordDataListAcquiredCallback?.Invoke(pList);
726 private void OnHttpRequestIntercepted(IntPtr interceptor)
728 #pragma warning disable CA2000 // Dispose objects before losing scope
729 httpRequestInterceptedCallback?.Invoke(new WebHttpRequestInterceptor(interceptor, true));
730 #pragma warning restore CA2000 // Dispose objects before losing scope