diff --git a/ReactNative/Views/Web/Events/WebViewLoadingErrorEvent.cs b/ReactNative/Views/Web/Events/WebViewLoadingErrorEvent.cs index 1570c416e15..f22cf2d0282 100644 --- a/ReactNative/Views/Web/Events/WebViewLoadingErrorEvent.cs +++ b/ReactNative/Views/Web/Events/WebViewLoadingErrorEvent.cs @@ -10,11 +10,18 @@ class WebViewLoadingErrorEvent : Event private readonly double _code; private readonly string _description; - public WebViewLoadingErrorEvent(int viewTag, WebErrorStatus error) + public WebViewLoadingErrorEvent(int viewTag, WebErrorStatus error, string description) : base(viewTag, TimeSpan.FromTicks(Environment.TickCount)) { _code = (double)error; - _description = ErrorString(error); + if (description == null) + { + _description = ErrorString(error); + } + else + { + _description = description; + } } public override string EventName diff --git a/ReactNative/Views/Web/Events/WebViewLoadingEvent.cs b/ReactNative/Views/Web/Events/WebViewLoadingEvent.cs index 614b0f60911..50ecdd3f99b 100644 --- a/ReactNative/Views/Web/Events/WebViewLoadingEvent.cs +++ b/ReactNative/Views/Web/Events/WebViewLoadingEvent.cs @@ -6,13 +6,7 @@ namespace ReactNative.Views.Web.Events { class WebViewLoadingEvent : Event { - public enum LoadingEventType - { - Start, - Finish, - }; - - private readonly LoadingEventType _type; + private readonly string _type; private readonly string _url; private readonly bool _loading; @@ -22,7 +16,7 @@ public enum LoadingEventType public WebViewLoadingEvent( int viewTag, - LoadingEventType type, + string type, string url, bool loading, string title, @@ -42,7 +36,7 @@ public override string EventName { get { - if (_type == LoadingEventType.Start) + if (_type.Equals("Start")) { return "topLoadingStart"; } diff --git a/ReactNative/Views/Web/ReactWebViewManager.cs b/ReactNative/Views/Web/ReactWebViewManager.cs index 0bcab3819bf..d583e413f90 100644 --- a/ReactNative/Views/Web/ReactWebViewManager.cs +++ b/ReactNative/Views/Web/ReactWebViewManager.cs @@ -4,9 +4,8 @@ using ReactNative.Views.Web.Events; using System; using System.Collections.Generic; -using Windows.ApplicationModel.Core; -using Windows.UI.Core; using Windows.UI.Xaml.Controls; +using Windows.Web; using Windows.Web.Http; namespace ReactNative.Views.Web @@ -52,9 +51,9 @@ public override IReadOnlyDictionary CommandsMap } /// - /// Sets whether JavaScript is enabled or not. + /// Sets flag signaling whether JavaScript is enabled. /// - /// a webview instance. + /// A webview instance. /// [ReactProp("javaScriptEnabled")] public void SetJavaScriptEnabled(WebView view, bool enabled) @@ -63,9 +62,9 @@ public void SetJavaScriptEnabled(WebView view, bool enabled) } /// - /// CSets whether Indexed DB is enabled or not. + /// Sets flag signaling whether Indexed DB is enabled. /// - /// a webview instance. + /// A webview instance. /// [ReactProp("indexedDbEnabled")] public void SetIndexedDbEnabled(WebView view, bool enabled) @@ -74,27 +73,20 @@ public void SetIndexedDbEnabled(WebView view, bool enabled) } /// - /// Sets the JS to be injected when the webpage loads. + /// Sets the JavaScript to be injected when the webpage loads. /// - /// a webview instance. + /// A webview instance. /// [ReactProp("injectedJavaScript")] public void SetInjectedJavaScript(WebView view, string injectedJavaScript) { - if (_injectedJS.ContainsKey(view.GetTag())) - { - _injectedJS[view.GetTag()] = injectedJavaScript; - } - else - { - _injectedJS.Add(view.GetTag(), injectedJavaScript); - } + _injectedJS[view.GetTag()] = injectedJavaScript; } /// /// Sets webview source. /// - /// a webview instance. + /// A webview instance. /// [ReactProp("source")] public void SetSource(WebView view, JObject source) @@ -133,7 +125,8 @@ public void SetSource(WebView view, JObject source) } else { - return; + throw new InvalidOperationException( + $"Unsupported method '{method}' received by '{typeof(ReactWebViewManager)}'."); } } else @@ -141,7 +134,7 @@ public void SetSource(WebView view, JObject source) request.Method = HttpMethod.Get; } - var headers = source.Value("headers"); + var headers = source.Value("headers"); if (headers != null) { IEnumerator> enumerator = ((JObject)headers).GetEnumerator(); @@ -228,10 +221,10 @@ protected override void AddEventEmitters(ThemedReactContext reactContext, WebVie view.NavigationStarting += OnNavigationStarting; } - private void OnNavigationCompleted(object sender, WebViewNavigationCompletedEventArgs e) + private async void OnNavigationCompleted(object sender, WebViewNavigationCompletedEventArgs e) { var webView = (WebView)sender; - var reactContext = webView.GetReactContext(); + LoadFinished(webView, e.Uri?.ToString()); if (e.IsSuccess) { @@ -240,67 +233,64 @@ private void OnNavigationCompleted(object sender, WebViewNavigationCompletedEven if (_injectedJS.TryGetValue(webView.GetTag(), out script) && script.Length > 0) { string[] args = { script }; - RunOnDispatcher(async () => + try { - try - { - await webView.InvokeScriptAsync("eval", args); - } - catch (Exception) - { - // Invalid script - } - }); - } - - var uri = e.Uri?.ToString(); - - reactContext.GetNativeModule() - .EventDispatcher - .DispatchEvent( - new WebViewLoadingEvent( - webView.GetTag(), - WebViewLoadingEvent.LoadingEventType.Finish, - uri, - false, - webView.DocumentTitle, - webView.CanGoBack, - webView.CanGoForward)); + await webView.InvokeScriptAsync("eval", args); + } + catch (Exception ex) + { + LoadFailed(webView, e.WebErrorStatus, ex.Message); + } + } } else { - reactContext.GetNativeModule() - .EventDispatcher - .DispatchEvent( - new WebViewLoadingErrorEvent( - webView.GetTag(), - e.WebErrorStatus)); + LoadFailed(webView, e.WebErrorStatus, null); } } private void OnNavigationStarting(object sender, WebViewNavigationStartingEventArgs e) { var webView = (WebView)sender; - var reactContext = webView.GetReactContext(); - - var uri = e.Uri?.ToString(); - reactContext.GetNativeModule() + webView.GetReactContext().GetNativeModule() .EventDispatcher .DispatchEvent( new WebViewLoadingEvent( webView.GetTag(), - WebViewLoadingEvent.LoadingEventType.Start, - uri, + "Start", + e.Uri?.ToString(), true, webView.DocumentTitle, webView.CanGoBack, webView.CanGoForward)); } - private static async void RunOnDispatcher(DispatchedHandler action) + private void LoadFinished(WebView webView, string uri) + { + webView.GetReactContext().GetNativeModule() + .EventDispatcher + .DispatchEvent( + new WebViewLoadingEvent( + webView.GetTag(), + "Finish", + uri, + false, + webView.DocumentTitle, + webView.CanGoBack, + webView.CanGoForward)); + } + + private void LoadFailed(WebView webView, WebErrorStatus status, string message) { - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action); + var reactContext = webView.GetReactContext(); + reactContext.GetNativeModule() + .EventDispatcher + .DispatchEvent( + new WebViewLoadingErrorEvent( + webView.GetTag(), + status, + message)); } } } diff --git a/js/Components/WebView/WebView.windows.js b/js/Components/WebView/WebView.windows.js index 0625a329885..481d0630674 100644 --- a/js/Components/WebView/WebView.windows.js +++ b/js/Components/WebView/WebView.windows.js @@ -107,8 +107,9 @@ var WebView = React.createClass({ ]), /** - * Used on Android only, JS is enabled by default for WebView on iOS + * Used on Android and Windows only, JS is enabled by default for WebView on iOS * @platform android + * @platform windows */ javaScriptEnabled: PropTypes.bool, @@ -146,7 +147,6 @@ var WebView = React.createClass({ getDefaultProps: function() { return { javaScriptEnabled : true, - scalesPageToFit: true, }; },