[Android] PlatformSpecific for WebView mixed content (#1784)

* add android platform specific for webview mixed content flags

add unit tests and gallery pages

* code style adjustment

* retry update docs

* retry update docs

* Fix bad merge on TestAttributes enum
This commit is contained in:
Alan Grgic 2018-02-15 17:49:22 -06:00 коммит произвёл Jason Smith
Родитель 8df4bf807c
Коммит 8821301e7e
8 изменённых файлов: 320 добавлений и 0 удалений

Просмотреть файл

@ -1,5 +1,6 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
namespace Xamarin.Forms.Controls
@ -56,6 +57,35 @@ namespace Xamarin.Forms.Controls
}
);
// NOTE: Currently the ability to programmatically enable/disable mixed content only exists on Android
if (Device.RuntimePlatform == Device.Android)
{
var mixedContentTestPage = "https://mixed-content-test.appspot.com/";
var mixedContentDisallowedWebView = new WebView() { HeightRequest = 1000 };
mixedContentDisallowedWebView.On<Android>().SetMixedContentMode(MixedContentHandling.NeverAllow);
mixedContentDisallowedWebView.Source = new UrlWebViewSource
{
Url = mixedContentTestPage
};
var mixedContentAllowedWebView = new WebView() { HeightRequest = 1000 };
mixedContentAllowedWebView.On<Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);
mixedContentAllowedWebView.Source = new UrlWebViewSource
{
Url = mixedContentTestPage
};
var mixedContentDisallowedContainer = new ViewContainer<WebView>(Test.WebView.MixedContentDisallowed,
mixedContentDisallowedWebView);
var mixedContentAllowedContainer = new ViewContainer<WebView>(Test.WebView.MixedContentAllowed,
mixedContentAllowedWebView);
Add(mixedContentDisallowedContainer);
Add(mixedContentAllowedContainer);
}
var jsAlertWebView = new WebView
{
Source = new HtmlWebViewSource

Просмотреть файл

@ -4,6 +4,7 @@ using System.Linq;
using NUnit.Framework;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
namespace Xamarin.Forms.Core.UnitTests
@ -92,6 +93,18 @@ namespace Xamarin.Forms.Core.UnitTests
Assert.AreEqual ("http://xamarin.com", urlSource.Url);
}
[Test]
public void TestAndroidMixedContent()
{
var defaultWebView = new WebView();
var mixedContentWebView = new WebView();
mixedContentWebView.On<Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);
Assert.AreEqual(defaultWebView.On<Android>().MixedContentMode(), MixedContentHandling.NeverAllow);
Assert.AreEqual(mixedContentWebView.On<Android>().MixedContentMode(), MixedContentHandling.AlwaysAllow);
}
[Test]
public void TestWindowsSetAllowJavaScriptAlertsFlag()
{

Просмотреть файл

@ -0,0 +1,38 @@

namespace Xamarin.Forms.PlatformConfiguration.AndroidSpecific
{
using FormsElement = Forms.WebView;
public enum MixedContentHandling
{
AlwaysAllow = 0,
NeverAllow = 1,
CompatibilityMode = 2
}
public static class WebView
{
public static readonly BindableProperty MixedContentModeProperty = BindableProperty.Create("MixedContentMode", typeof(MixedContentHandling), typeof(WebView), MixedContentHandling.NeverAllow);
public static MixedContentHandling GetMixedContentMode(BindableObject element)
{
return (MixedContentHandling)element.GetValue(MixedContentModeProperty);
}
public static void SetMixedContentMode(BindableObject element, MixedContentHandling value)
{
element.SetValue(MixedContentModeProperty, value);
}
public static MixedContentHandling MixedContentMode(this IPlatformElementConfiguration<Android, FormsElement> config)
{
return GetMixedContentMode(config.Element);
}
public static IPlatformElementConfiguration<Android, FormsElement> SetMixedContentMode(this IPlatformElementConfiguration<Android, FormsElement> config, MixedContentHandling value)
{
SetMixedContentMode(config.Element, value);
return config;
}
}
}

Просмотреть файл

@ -681,6 +681,8 @@ namespace Xamarin.Forms.CustomAttributes
UrlWebViewSource,
HtmlWebViewSource,
LoadHtml,
MixedContentDisallowed,
MixedContentAllowed,
JavaScriptAlert
}

Просмотреть файл

@ -4,7 +4,10 @@ using Android.App;
using Android.Content;
using Android.Webkit;
using Android.Widget;
using Android.OS;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Internals;
using MixedContentHandling = Android.Webkit.MixedContentHandling;
using AWebView = Android.Webkit.WebView;
namespace Xamarin.Forms.Platform.Android
@ -106,6 +109,8 @@ namespace Xamarin.Forms.Platform.Android
newElementController.EvalRequested += OnEvalRequested;
newElementController.GoBackRequested += OnGoBackRequested;
newElementController.GoForwardRequested += OnGoForwardRequested;
UpdateMixedContentMode();
}
Load();
@ -120,6 +125,9 @@ namespace Xamarin.Forms.Platform.Android
case "Source":
Load();
break;
case "MixedContentMode":
UpdateMixedContentMode();
break;
}
}
@ -163,6 +171,14 @@ namespace Xamarin.Forms.Platform.Android
ElementController.CanGoForward = Control.CanGoForward();
}
void UpdateMixedContentMode()
{
if (Control != null && ((int)Build.VERSION.SdkInt >= 21))
{
Control.Settings.MixedContentMode = (MixedContentHandling)Element.OnThisPlatform().MixedContentMode();
}
}
class WebClient : WebViewClient
{
WebNavigationResult _navigationResult = WebNavigationResult.Success;

Просмотреть файл

@ -0,0 +1,59 @@
<Type Name="MixedContentHandling" FullName="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling">
<TypeSignature Language="C#" Value="public enum MixedContentHandling" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed MixedContentHandling extends System.Enum" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Enum</BaseTypeName>
</Base>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members>
<Member MemberName="AlwaysAllow">
<MemberSignature Language="C#" Value="AlwaysAllow" />
<MemberSignature Language="ILAsm" Value=".field public static literal valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling AlwaysAllow = int32(0)" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
</Docs>
</Member>
<Member MemberName="CompatibilityMode">
<MemberSignature Language="C#" Value="CompatibilityMode" />
<MemberSignature Language="ILAsm" Value=".field public static literal valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling CompatibilityMode = int32(2)" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
</Docs>
</Member>
<Member MemberName="NeverAllow">
<MemberSignature Language="C#" Value="NeverAllow" />
<MemberSignature Language="ILAsm" Value=".field public static literal valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling NeverAllow = int32(1)" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
</Docs>
</Member>
</Members>
</Type>

Просмотреть файл

@ -0,0 +1,116 @@
<Type Name="WebView" FullName="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.WebView">
<TypeSignature Language="C#" Value="public static class WebView" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit WebView extends System.Object" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members>
<Member MemberName="GetMixedContentMode">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling GetMixedContentMode (Xamarin.Forms.BindableObject element);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling GetMixedContentMode(class Xamarin.Forms.BindableObject element) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="element" Type="Xamarin.Forms.BindableObject" />
</Parameters>
<Docs>
<param name="element">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="MixedContentMode">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling MixedContentMode (this Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; config);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling MixedContentMode(class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; config) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="config" Type="Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;" RefType="this" />
</Parameters>
<Docs>
<param name="config">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="MixedContentModeProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty MixedContentModeProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty MixedContentModeProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="SetMixedContentMode">
<MemberSignature Language="C#" Value="public static void SetMixedContentMode (Xamarin.Forms.BindableObject element, Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig void SetMixedContentMode(class Xamarin.Forms.BindableObject element, valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="element" Type="Xamarin.Forms.BindableObject" />
<Parameter Name="value" Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling" />
</Parameters>
<Docs>
<param name="element">To be added.</param>
<param name="value">To be added.</param>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="SetMixedContentMode">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; SetMixedContentMode (this Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; config, Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; SetMixedContentMode(class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; config, valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="config" Type="Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;" RefType="this" />
<Parameter Name="value" Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling" />
</Parameters>
<Docs>
<param name="config">To be added.</param>
<param name="value">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
</Members>
</Type>

Просмотреть файл

@ -523,8 +523,10 @@
<Namespace Name="Xamarin.Forms.PlatformConfiguration.AndroidSpecific">
<Type Name="Application" Kind="Class" />
<Type Name="ListView" Kind="Class" />
<Type Name="MixedContentHandling" Kind="Enumeration" />
<Type Name="TabbedPage" Kind="Class" />
<Type Name="VisualElement" Kind="Class" />
<Type Name="WebView" Kind="Class" />
<Type Name="WindowSoftInputModeAdjust" Kind="Enumeration" />
</Namespace>
<Namespace Name="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat">
@ -2169,6 +2171,50 @@
<Link Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.VisualElement" Member="M:Xamarin.Forms.PlatformConfiguration.AndroidSpecific.VisualElement.SetIsLegacyColorModeEnabled(Xamarin.Forms.IPlatformElementConfiguration{Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.VisualElement},System.Boolean)" />
</Member>
</ExtensionMethod>
<ExtensionMethod>
<Targets>
<Target Type="T:Xamarin.Forms.IPlatformElementConfiguration`2" />
</Targets>
<Member MemberName="MixedContentMode">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling MixedContentMode (this Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; config);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling MixedContentMode(class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; config) cil managed" />
<MemberType>ExtensionMethod</MemberType>
<ReturnValue>
<ReturnType>Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="config" Type="Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;" RefType="this" />
</Parameters>
<Docs>
<param name="config">To be added.</param>
<summary>To be added.</summary>
</Docs>
<Link Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.WebView" Member="M:Xamarin.Forms.PlatformConfiguration.AndroidSpecific.WebView.MixedContentMode(Xamarin.Forms.IPlatformElementConfiguration{Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView})" />
</Member>
</ExtensionMethod>
<ExtensionMethod>
<Targets>
<Target Type="T:Xamarin.Forms.IPlatformElementConfiguration`2" />
</Targets>
<Member MemberName="SetMixedContentMode">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; SetMixedContentMode (this Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt; config, Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; SetMixedContentMode(class Xamarin.Forms.IPlatformElementConfiguration`2&lt;class Xamarin.Forms.PlatformConfiguration.Android, class Xamarin.Forms.WebView&gt; config, valuetype Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling value) cil managed" />
<MemberType>ExtensionMethod</MemberType>
<ReturnValue>
<ReturnType>Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="config" Type="Xamarin.Forms.IPlatformElementConfiguration&lt;Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView&gt;" RefType="this" />
<Parameter Name="value" Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling" />
</Parameters>
<Docs>
<param name="config">To be added.</param>
<param name="value">To be added.</param>
<summary>To be added.</summary>
</Docs>
<Link Type="Xamarin.Forms.PlatformConfiguration.AndroidSpecific.WebView" Member="M:Xamarin.Forms.PlatformConfiguration.AndroidSpecific.WebView.SetMixedContentMode(Xamarin.Forms.IPlatformElementConfiguration{Xamarin.Forms.PlatformConfiguration.Android,Xamarin.Forms.WebView},Xamarin.Forms.PlatformConfiguration.AndroidSpecific.MixedContentHandling)" />
</Member>
</ExtensionMethod>
<ExtensionMethod>
<Targets>
<Target Type="T:Xamarin.Forms.IPlatformElementConfiguration`2" />