/* * Copyright (c) 2017 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.Collections.Generic; using System.Runtime.InteropServices; using Tizen.NUI.BaseComponents; using Tizen.NUI.Binding; namespace Tizen.NUI { /// /// WebView /// [EditorBrowsable(EditorBrowsableState.Never)] public class WebView : View { private global::System.Runtime.InteropServices.HandleRef swigCPtr; [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void WebViewPageLoadCallbackDelegate(IntPtr data, string pageUrl); [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void WebViewPageLoadErrorCallbackDelegate(IntPtr data, string pageUrl, int errorCode); private readonly WebViewPageLoadSignal pageLoadStartedSignal; private EventHandler pageLoadStartedEventHandler; private WebViewPageLoadCallbackDelegate pageLoadStartedCallback; private readonly WebViewPageLoadSignal pageLoadFinishedSignal; private EventHandler pageLoadFinishedEventHandler; private WebViewPageLoadCallbackDelegate pageLoadFinishedCallback; private readonly WebViewPageLoadErrorSignal pageLoadErrorSignal; private EventHandler pageLoadErrorEventHandler; private WebViewPageLoadErrorCallbackDelegate pageLoadErrorCallback; internal WebView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.WebView.WebView_SWIGUpcast(cPtr), cMemoryOwn) { swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr); pageLoadStartedSignal = new WebViewPageLoadSignal(Interop.WebView.new_WebViewPageLoadSignal_PageLoadStarted(swigCPtr)); pageLoadFinishedSignal = new WebViewPageLoadSignal(Interop.WebView.new_WebViewPageLoadSignal_PageLoadFinished(swigCPtr)); pageLoadErrorSignal = new WebViewPageLoadErrorSignal(Interop.WebView.new_WebViewPageLoadErrorSignal_PageLoadError(swigCPtr)); } internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WebView obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } internal WebView Assign(WebView webView) { WebView ret = new WebView(Interop.WebView.WebView_Assign(swigCPtr, WebView.getCPtr(webView)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } internal static WebView DownCast(BaseHandle handle) { WebView ret = new WebView(Interop.WebView.WebView_DownCast(BaseHandle.getCPtr(handle)), true); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Dispose for IDisposable pattern /// [EditorBrowsable(EditorBrowsableState.Never)] protected override void Dispose(DisposeTypes type) { if (disposed) { return; } if (type == DisposeTypes.Explicit) { //Called by User //Release your own managed resources here. //You should release all of your own disposable objects here. pageLoadStartedSignal.Dispose(); pageLoadFinishedSignal.Dispose(); pageLoadErrorSignal.Dispose(); } //Release your own unmanaged resources here. //You should not access any managed member here except static instance. //because the execution order of Finalizes is non-deterministic. if (swigCPtr.Handle != global::System.IntPtr.Zero) { if (swigCMemOwn) { swigCMemOwn = false; Interop.WebView.delete_WebView(swigCPtr); } swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); } base.Dispose(type); } private void OnPageLoadStarted(IntPtr data, string pageUrl) { WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs(); e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView; e.PageUrl = pageUrl; if (pageLoadStartedEventHandler != null) { pageLoadStartedEventHandler(this, e); } } private void OnPageLoadFinished(IntPtr data, string pageUrl) { WebViewPageLoadEventArgs e = new WebViewPageLoadEventArgs(); e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView; e.PageUrl = pageUrl; if (pageLoadFinishedEventHandler != null) { pageLoadFinishedEventHandler(this, e); } } private void OnPageLoadError(IntPtr data, string pageUrl, int errorCode) { WebViewPageLoadErrorEventArgs e = new WebViewPageLoadErrorEventArgs(); e.WebView = Registry.GetManagedBaseHandleFromNativePtr(data) as WebView; e.PageUrl = pageUrl; e.ErrorCode = (WebViewPageLoadErrorEventArgs.LoadErrorCode)errorCode; if (pageLoadErrorEventHandler != null) { pageLoadErrorEventHandler(this, e); } } internal static new class Property { internal static readonly int URL = Interop.WebView.WebView_Property_URL_get(); internal static readonly int CACHE_MODEL = Interop.WebView.WebView_Property_CACHE_MODEL_get(); internal static readonly int COOKIE_ACCEPT_POLICY = Interop.WebView.WebView_Property_COOKIE_ACCEPT_POLICY_get(); internal static readonly int USER_AGENT = Interop.WebView.WebView_Property_USER_AGENT_get(); internal static readonly int ENABLE_JAVASCRIPT = Interop.WebView.WebView_Property_ENABLE_JAVASCRIPT_get(); internal static readonly int LOAD_IMAGES_AUTOMATICALLY = Interop.WebView.WebView_Property_LOAD_IMAGES_AUTOMATICALLY_get(); internal static readonly int DEFAULT_TEXT_ENCODING_NAME = Interop.WebView.WebView_Property_DEFAULT_TEXT_ENCODING_NAME_get(); internal static readonly int DEFAULT_FONT_SIZE = Interop.WebView.WebView_Property_DEFAULT_FONT_SIZE_get(); } private static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(WebView), string.Empty, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.URL, new Tizen.NUI.PropertyValue((string)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; string temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.URL).Get(out temp); return temp; }); private static readonly BindableProperty CacheModelProperty = BindableProperty.Create(nameof(CacheModel), typeof(CacheModel), typeof(WebView), CacheModel.DocumentViewer, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.CACHE_MODEL, new Tizen.NUI.PropertyValue((int)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; string temp; if (Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.CACHE_MODEL).Get(out temp) == false) { NUILog.Error("CacheModel get error!"); } switch (temp) { case "DOCUMENT_VIEWER": return CacheModel.DocumentViewer; case "DOCUMENT_BROWSER": return CacheModel.DocumentBrowser; default: return CacheModel.PrimaryWebBrowser; } }); private static readonly BindableProperty CookieAcceptPolicyProperty = BindableProperty.Create(nameof(CookieAcceptPolicy), typeof(CookieAcceptPolicy), typeof(WebView), CookieAcceptPolicy.NoThirdParty, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.COOKIE_ACCEPT_POLICY, new Tizen.NUI.PropertyValue((int)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; string temp; if (Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.COOKIE_ACCEPT_POLICY).Get(out temp) == false) { NUILog.Error("CookieAcceptPolicy get error!"); } switch (temp) { case "ALWAYS": return CookieAcceptPolicy.Always; case "NEVER": return CookieAcceptPolicy.Never; default: return CookieAcceptPolicy.NoThirdParty; } }); private static readonly BindableProperty UserAgentProperty = BindableProperty.Create(nameof(UserAgent), typeof(string), typeof(WebView), string.Empty, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.USER_AGENT, new Tizen.NUI.PropertyValue((string)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; string temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.USER_AGENT).Get(out temp); return temp; }); private static readonly BindableProperty EnableJavaScriptProperty = BindableProperty.Create(nameof(EnableJavaScript), typeof(bool), typeof(WebView), true, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.ENABLE_JAVASCRIPT, new Tizen.NUI.PropertyValue((bool)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; bool temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.ENABLE_JAVASCRIPT).Get(out temp); return temp; }); private static readonly BindableProperty LoadImagesAutomaticallyProperty = BindableProperty.Create(nameof(LoadImagesAutomatically), typeof(bool), typeof(WebView), true, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.LOAD_IMAGES_AUTOMATICALLY, new Tizen.NUI.PropertyValue((bool)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; bool temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.LOAD_IMAGES_AUTOMATICALLY).Get(out temp); return temp; }); private static readonly BindableProperty DefaultTextEncodingNameProperty = BindableProperty.Create(nameof(DefaultTextEncodingName), typeof(string), typeof(WebView), string.Empty, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.DEFAULT_TEXT_ENCODING_NAME, new Tizen.NUI.PropertyValue((string)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; string temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.DEFAULT_TEXT_ENCODING_NAME).Get(out temp); return temp; }); private static readonly BindableProperty DefaultFontSizeProperty = BindableProperty.Create(nameof(DefaultFontSize), typeof(int), typeof(WebView), 16, propertyChanged: (bindable, oldValue, newValue) => { var webview = (WebView)bindable; if (newValue != null) { Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.DEFAULT_FONT_SIZE, new Tizen.NUI.PropertyValue((int)newValue)); } }, defaultValueCreator: (bindable) => { var webview = (WebView)bindable; int temp; Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.DEFAULT_FONT_SIZE).Get(out temp); return temp; }); /// /// Creates an uninitialized WebView. /// [EditorBrowsable(EditorBrowsableState.Never)] public WebView() : this(Interop.WebView.WebView_New(), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Creates an uninitialized WebView. /// The locale of Web /// The timezoneId of Web /// [EditorBrowsable(EditorBrowsableState.Never)] public WebView(string locale, string timezoneId) : this(Interop.WebView.WebView_New_2(locale, timezoneId), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Copy constructor. /// WebView to copy. The copied WebView will point at the same implementation /// [EditorBrowsable(EditorBrowsableState.Never)] public WebView(WebView webView) : this(Interop.WebView.new_WebView__SWIG_1(WebView.getCPtr(webView)), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// The url to load. /// [EditorBrowsable(EditorBrowsableState.Never)] public string Url { get { return (string)GetValue(UrlProperty); } set { SetValue(UrlProperty, value); NotifyPropertyChanged(); } } /// /// The cache model of the current WebView. /// [EditorBrowsable(EditorBrowsableState.Never)] public CacheModel CacheModel { get { return (CacheModel)GetValue(CacheModelProperty); } set { SetValue(CacheModelProperty, value); NotifyPropertyChanged(); } } /// /// The cookie acceptance policy. /// [EditorBrowsable(EditorBrowsableState.Never)] public CookieAcceptPolicy CookieAcceptPolicy { get { return (CookieAcceptPolicy)GetValue(CookieAcceptPolicyProperty); } set { SetValue(CookieAcceptPolicyProperty, value); NotifyPropertyChanged(); } } /// /// The user agent string. /// [EditorBrowsable(EditorBrowsableState.Never)] public string UserAgent { get { return (string)GetValue(UserAgentProperty); } set { SetValue(UserAgentProperty, value); NotifyPropertyChanged(); } } /// /// Whether JavaScript is enabled. /// [EditorBrowsable(EditorBrowsableState.Never)] public bool EnableJavaScript { get { return (bool)GetValue(EnableJavaScriptProperty); } set { SetValue(EnableJavaScriptProperty, value); NotifyPropertyChanged(); } } /// /// Whether images can be loaded automatically. /// [EditorBrowsable(EditorBrowsableState.Never)] public bool LoadImagesAutomatically { get { return (bool)GetValue(LoadImagesAutomaticallyProperty); } set { SetValue(LoadImagesAutomaticallyProperty, value); NotifyPropertyChanged(); } } /// /// The default text encoding name.
/// e.g. "UTF-8"
///
[EditorBrowsable(EditorBrowsableState.Never)] public string DefaultTextEncodingName { get { return (string)GetValue(DefaultTextEncodingNameProperty); } set { SetValue(DefaultTextEncodingNameProperty, value); NotifyPropertyChanged(); } } /// /// The default font size in pixel. /// [EditorBrowsable(EditorBrowsableState.Never)] public int DefaultFontSize { get { return (int)GetValue(DefaultFontSizeProperty); } set { SetValue(DefaultFontSizeProperty, value); NotifyPropertyChanged(); } } /// /// Event for the PageLoadStarted signal which can be used to subscribe or unsubscribe the event handler.
/// This signal is emitted when page loading has started.
///
[EditorBrowsable(EditorBrowsableState.Never)] public event EventHandler PageLoadStarted { add { if (pageLoadStartedEventHandler == null) { pageLoadStartedCallback = (OnPageLoadStarted); pageLoadStartedSignal.Connect(pageLoadStartedCallback); } pageLoadStartedEventHandler += value; } remove { pageLoadStartedEventHandler -= value; if (pageLoadStartedEventHandler == null && pageLoadStartedCallback != null) { pageLoadStartedSignal.Disconnect(pageLoadStartedCallback); } } } /// /// Event for the PageLoadFinished signal which can be used to subscribe or unsubscribe the event handler.
/// This signal is emitted when page loading has finished.
///
[EditorBrowsable(EditorBrowsableState.Never)] public event EventHandler PageLoadFinished { add { if (pageLoadFinishedEventHandler == null) { pageLoadFinishedCallback = (OnPageLoadFinished); pageLoadFinishedSignal.Connect(pageLoadFinishedCallback); } pageLoadFinishedEventHandler += value; } remove { pageLoadFinishedEventHandler -= value; if (pageLoadFinishedEventHandler == null && pageLoadFinishedCallback != null) { pageLoadFinishedSignal.Disconnect(pageLoadFinishedCallback); } } } /// /// Event for the PageLoadError signal which can be used to subscribe or unsubscribe the event handler.
/// This signal is emitted when there's an error in page loading.
///
[EditorBrowsable(EditorBrowsableState.Never)] public event EventHandler PageLoadError { add { if (pageLoadErrorEventHandler == null) { pageLoadErrorCallback = (OnPageLoadError); pageLoadErrorSignal.Connect(pageLoadErrorCallback); } pageLoadErrorEventHandler += value; } remove { pageLoadErrorEventHandler -= value; if (pageLoadErrorEventHandler == null && pageLoadErrorCallback != null) { pageLoadErrorSignal.Disconnect(pageLoadErrorCallback); } } } /// /// Loads a html. /// The path of Web /// [EditorBrowsable(EditorBrowsableState.Never)] public void LoadUrl(string url) { Interop.WebView.WebView_LoadUrl(swigCPtr, url); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Returns the URL of the Web /// The data of Web /// [EditorBrowsable(EditorBrowsableState.Never)] public void LoadHTMLString(string data) { Interop.WebView.WebView_LoadHTMLString(swigCPtr, data); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Reloads the Web /// [EditorBrowsable(EditorBrowsableState.Never)] public void Reload() { Interop.WebView.WebView_Reload(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Stops loading the Web /// [EditorBrowsable(EditorBrowsableState.Never)] public void StopLoading() { Interop.WebView.WebView_StopLoading(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Suspends the operation. /// [EditorBrowsable(EditorBrowsableState.Never)] public void Suspend() { Interop.WebView.WebView_Suspend(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Resumes the operation after calling Suspend() /// [EditorBrowsable(EditorBrowsableState.Never)] public void Resume() { Interop.WebView.WebView_Resume(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Goes to the back /// [EditorBrowsable(EditorBrowsableState.Never)] public void GoBack() { Interop.WebView.WebView_GoBack(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Goes to the forward /// [EditorBrowsable(EditorBrowsableState.Never)] public void GoForward() { Interop.WebView.WebView_GoForward(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Returns whether backward is possible. /// True if backward is possible, false otherwise /// [EditorBrowsable(EditorBrowsableState.Never)] public bool CanGoBack() { bool ret = Interop.WebView.WebView_CanGoBack(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Returns whether forward is possible. /// True if forward is possible, false otherwise /// [EditorBrowsable(EditorBrowsableState.Never)] public bool CanGoForward() { bool ret = Interop.WebView.WebView_CanGoForward(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// The callback function that is invoked when the message is received from the script. /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] [EditorBrowsable(EditorBrowsableState.Never)] public delegate void JavaScriptMessageHandler(string message); /// /// Evaluates JavaScript code represented as a string. /// The JavaScript code /// [EditorBrowsable(EditorBrowsableState.Never)] public void EvaluateJavaScript(string script) { Interop.WebView.WebView_EvaluateJavaScript(swigCPtr, script, new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } // For rooting handlers internal Dictionary handlerRootMap = new Dictionary(); /// /// Add a message handler into the WebView. /// The name of exposed object /// The callback function /// [EditorBrowsable(EditorBrowsableState.Never)] public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHandler handler) { if (handlerRootMap.ContainsKey(objectName)) { return; } handlerRootMap.Add(objectName, handler); System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler); Interop.WebView.WebView_AddJavaScriptMessageHandler(swigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Clears the history of current WebView. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ClearHistory() { Interop.WebView.WebView_ClearHistory(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Clears the cache of current WebView. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ClearCache() { Interop.WebView.WebView_ClearCache(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Clears all the cookies of current WebView. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ClearCookies() { Interop.WebView.WebView_ClearCookies(swigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } } }