/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Tizen.NUI
{
///
/// WebContext is a class for context of web view.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public class WebContext : Disposable
{
private string appId;
private string appVersion;
private float timeOffset;
private ApplicationType applicationType;
private SecurityOriginListAcquiredCallback securityOriginListAcquiredCallback;
private readonly WebContextSecurityOriginListAcquiredProxyCallback securityOriginListAcquiredProxyCallback;
private PasswordDataListAcquiredCallback passwordDataListAcquiredCallback;
private readonly WebContextPasswordDataListAcquiredProxyCallback passwordDataListAcquiredProxyCallback;
internal WebContext(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
{
securityOriginListAcquiredProxyCallback = OnSecurityOriginListAcquired;
passwordDataListAcquiredProxyCallback = OnPasswordDataListAcquired;
}
///
/// The callback function that is invoked when security origin list is acquired.
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void SecurityOriginListAcquiredCallback(WebSecurityOriginList list);
///
/// The callback function that is invoked when storage usage is acquired.
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void StorageUsageAcquiredCallback(ulong usage);
///
/// The callback function that is invoked when security origin list is acquired.
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void PasswordDataListAcquiredCallback(WebPasswordDataList list);
///
/// The callback function that is invoked when download is started.
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate void DownloadStartedCallback(string url);
///
/// The callback function that is invoked when current mime type need be overridden.
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[EditorBrowsable(EditorBrowsableState.Never)]
public delegate bool MimeOverriddenCallback(string url, string currentMime, string newMime);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void WebContextSecurityOriginListAcquiredProxyCallback(IntPtr list);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void WebContextPasswordDataListAcquiredProxyCallback(IntPtr list);
///
/// Cache model
///
[EditorBrowsable(EditorBrowsableState.Never)]
public enum CacheModelType
{
///
/// The smallest cache capacity
///
[EditorBrowsable(EditorBrowsableState.Never)]
DocumentViewer,
///
/// The bigger cache capacity than DocumentBrowser
///
[EditorBrowsable(EditorBrowsableState.Never)]
DocumentBrowser,
///
/// The biggest cache capacity.
///
[EditorBrowsable(EditorBrowsableState.Never)]
PrimaryWebBrowser,
}
///
/// Application type
///
[EditorBrowsable(EditorBrowsableState.Never)]
public enum ApplicationType
{
///
/// Web browser.
///
[EditorBrowsable(EditorBrowsableState.Never)]
WebBrowser,
///
/// Hbb tv.
///
[EditorBrowsable(EditorBrowsableState.Never)]
HbbTv,
///
/// Web runtime.
///
[EditorBrowsable(EditorBrowsableState.Never)]
WebRuntime,
///
/// Other.
///
[EditorBrowsable(EditorBrowsableState.Never)]
Other,
}
///
/// Cache model
///
[EditorBrowsable(EditorBrowsableState.Never)]
public CacheModelType CacheModel
{
get
{
return (CacheModelType)Interop.WebContext.GetCacheModel(SwigCPtr);
}
set
{
Interop.WebContext.SetCacheModel(SwigCPtr, (int)value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Proxy url.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string ProxyUrl
{
get
{
return Interop.WebContext.GetProxyUri(SwigCPtr);
}
set
{
if (value != null)
{
Interop.WebContext.SetProxyUri(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
}
///
/// Certificate file path.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string CertificateFilePath
{
get
{
return Interop.WebContext.GetCertificateFilePath(SwigCPtr);
}
set
{
Interop.WebContext.SetCertificateFilePath(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Enables cache or not.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CacheEnabled
{
get
{
return Interop.WebContext.IsCacheEnabled(SwigCPtr);
}
set
{
Interop.WebContext.EnableCache(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// App id.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string AppId
{
get
{
return appId;
}
set
{
Interop.WebContext.SetAppId(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
appId = value;
}
}
///
/// App version.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string AppVersion
{
get
{
return appVersion;
}
set
{
Interop.WebContext.SetAppVersion(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
appVersion = value;
}
}
///
/// App type.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public ApplicationType AppType
{
get
{
return applicationType;
}
set
{
Interop.WebContext.SetApplicationType(SwigCPtr, (int)value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
applicationType = value;
}
}
///
/// Time offset.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public float TimeOffset
{
get
{
return timeOffset;
}
set
{
Interop.WebContext.SetTimeOffset(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
timeOffset = value;
}
}
///
/// Default zoom factor.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public float DefaultZoomFactor
{
get
{
return Interop.WebContext.GetDefaultZoomFactor(SwigCPtr);
}
set
{
Interop.WebContext.SetDefaultZoomFactor(SwigCPtr, value);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Deprecated. Gets context proxy.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string ContextProxy
{
get
{
return Interop.WebContext.GetProxyUri(SwigCPtr);
}
}
///
/// Gets proxy bypass rule.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public string ProxyBypassRule
{
get
{
return Interop.WebContext.GetProxyBypassRule(SwigCPtr);
}
}
///
/// Sets default proxy auth.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetDefaultProxyAuth(string username, string password)
{
Interop.WebContext.SetDefaultProxyAuth(SwigCPtr, username, password);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deletes all web database.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteAllWebDatabase()
{
Interop.WebContext.DeleteAllWebDatabase(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Gets security origins of web database asynchronously.
/// callback for acquiring security origins
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool GetWebDatabaseOrigins(SecurityOriginListAcquiredCallback callback)
{
securityOriginListAcquiredCallback = callback;
IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
bool result = Interop.WebContext.GetWebDatabaseOrigins(SwigCPtr, new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Deletes web databases by origin.
/// security origin of web database
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DeleteWebDatabase(WebSecurityOrigin origin)
{
bool result = Interop.WebContext.DeleteWebDatabase(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Gets a list of security origins of web storage asynchronously.
/// callback for acquiring security origins
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool GetWebStorageOrigins(SecurityOriginListAcquiredCallback callback)
{
securityOriginListAcquiredCallback = callback;
IntPtr ip = Marshal.GetFunctionPointerForDelegate(securityOriginListAcquiredProxyCallback);
bool result = Interop.WebContext.GetWebStorageOrigins(SwigCPtr, new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Gets a list of security origins of web storage asynchronously.
/// security origin of web storage
/// callback for acquiring storage usage
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool GetWebStorageUsageForOrigin(WebSecurityOrigin origin, StorageUsageAcquiredCallback callback)
{
IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
bool result = Interop.WebContext.GetWebStorageUsageForOrigin(SwigCPtr, WebSecurityOrigin.getCPtr(origin), new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Deletes all web storage.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteAllWebStorage()
{
Interop.WebContext.DeleteAllWebStorage(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deletes web storage by origin.
/// security origin of web storage
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DeleteWebStorage(WebSecurityOrigin origin)
{
bool result = Interop.WebContext.DeleteWebStorage(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Deletes local fileSystem.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteLocalFileSystem()
{
Interop.WebContext.DeleteLocalFileSystem(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Clears cache.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void ClearCache()
{
Interop.WebContext.ClearCache(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deletes web application cache by origin.
/// security origin of web application
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DeleteApplicationCache(WebSecurityOrigin origin)
{
bool result = Interop.WebContext.DeleteApplicationCache(SwigCPtr, WebSecurityOrigin.getCPtr(origin));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return result;
}
///
/// Gets a list of all password data asynchronously.
/// callback for acquiring password data list
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void GetFormPasswordList(PasswordDataListAcquiredCallback callback)
{
passwordDataListAcquiredCallback = callback;
IntPtr ip = Marshal.GetFunctionPointerForDelegate(passwordDataListAcquiredProxyCallback);
Interop.WebContext.GetFormPasswordList(SwigCPtr, new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Registers callback for download started.
/// callback for download started
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void RegisterDownloadStartedCallback(DownloadStartedCallback callback)
{
IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
Interop.WebContext.RegisterDownloadStartedCallback(SwigCPtr, new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Registers callback for overriding mime type.
/// callback for overriding mime type
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void RegisterMimeOverriddenCallback(MimeOverriddenCallback callback)
{
IntPtr ip = Marshal.GetFunctionPointerForDelegate(callback);
Interop.WebContext.RegisterMimeOverriddenCallback(SwigCPtr, new HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Sets context time zone offset.
/// Time offset
/// Daylight saving time
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetTimeZoneOffset(float offset, float time)
{
Interop.WebContext.SetTimeZoneOffset(SwigCPtr, offset, time);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deprecated. Sets context time zone offset.
/// Time offset
/// Daylight saving time
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetContextTimeZoneOffset(float offset, float time)
{
SetTimeZoneOffset(offset, time);
}
///
/// Registers url schemes enabled.
/// The string array of schemes
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void RegisterUrlSchemesAsCorsEnabled(string[] schemes)
{
if (schemes != null)
{
Interop.WebContext.RegisterUrlSchemesAsCorsEnabled(SwigCPtr, schemes, (uint)schemes.Length);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Registers js plugin mime types.
/// The string array of types
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void RegisterJsPluginMimeTypes(string[] mimes)
{
if (mimes != null)
{
Interop.WebContext.RegisterJsPluginMimeTypes(SwigCPtr, mimes, (uint)mimes.Length);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Deletes all application cache.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DeleteAllApplicationCache()
{
bool ret = Interop.WebContext.DeleteAllApplicationCache(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Deletes all web indexed database.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DeleteAllWebIndexedDatabase()
{
bool ret = Interop.WebContext.DeleteAllWebIndexedDatabase(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Deletes password dataList.
/// The string array of data list
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteFormPasswordDataList(string[] passwords)
{
if (passwords != null)
{
Interop.WebContext.DeleteFormPasswordDataList(SwigCPtr, passwords, (uint)passwords.Length);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Deletes all password data.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteAllFormPasswordData()
{
Interop.WebContext.DeleteAllFormPasswordData(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deletes all candidate data.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void DeleteAllFormCandidateData()
{
Interop.WebContext.DeleteAllFormCandidateData(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Sets proxy bypass rule.
/// The proxy string
/// Bypass rule
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetProxyBypassRule(string proxy, string rule)
{
Interop.WebContext.SetProxyBypassRule(SwigCPtr, proxy, rule);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Deprecated. Sets proxy bypass rule.
/// The proxy string
/// Bypass rule
///
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetContextProxy(string proxy, string rule)
{
SetProxyBypassRule(proxy, rule);
}
///
/// Frees unused memory.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool FreeUnusedMemory()
{
bool ret = Interop.WebContext.FreeUnusedMemory(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
private void OnSecurityOriginListAcquired(IntPtr list)
{
WebSecurityOriginList originList = new WebSecurityOriginList(list, true);
securityOriginListAcquiredCallback?.Invoke(originList);
originList.Dispose();
}
private void OnPasswordDataListAcquired(IntPtr list)
{
WebPasswordDataList passwordList = new WebPasswordDataList(list, true);
passwordDataListAcquiredCallback?.Invoke(passwordList);
passwordList.Dispose();
}
}
}