Skip to content

Commit

Permalink
added example for multitenant subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
cefsharp-ms committed Aug 29, 2016
1 parent 48b1258 commit b0d9cde
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 83 deletions.
1 change: 1 addition & 0 deletions CefSharp.Example/CefExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class CefExample
public const string BasicSchemeTestUrl = "custom://cefsharp/SchemeTest.html";
public const string ResponseFilterTestUrl = "custom://cefsharp/ResponseFilterTest.html";
public const string DraggableRegionTestUrl = "custom://cefsharp/DraggableRegionTest.html";
public const string MultitenantTestUrl = "custom://cefsharp/MultitenantTest.html";
public const string TestResourceUrl = "http://test/resource/load";
public const string RenderProcessCrashedUrl = "http://processcrashed";
public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode";
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="SimpleClass.cs" />
<Compile Include="SimpleSubClass.cs" />
<Compile Include="SubBoundObject.cs" />
<Compile Include="UniqueBoundObject.cs" />
<Compile Include="TempFileDialogHandler.cs" />
<Compile Include="CefExample.cs" />
<Compile Include="DownloadHandler.cs" />
Expand Down Expand Up @@ -128,6 +129,7 @@
<Content Include="Resources\MultiBindingTest.html" />
<Content Include="Resources\home.html" />
<Content Include="Resources\FramedWebGLTest.html" />
<Content Include="Resources\MultitenantTest.html" />
<Content Include="Resources\PopupTest.html" />
<Content Include="Resources\DraggableRegionTest.html" />
<Content Include="Resources\ResponseFilterTest.html" />
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Example/CefSharpSchemeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static CefSharpSchemeHandler()
{ "/bootstrap/bootstrap.min.js", Resources.bootstrap_min_js },

{ "/BindingTest.html", Resources.BindingTest },
{ "/MultitenantTest.html", Resources.MultitenantTest },
{ "/ExceptionTest.html", Resources.ExceptionTest },
{ "/PopupTest.html", Resources.PopupTest },
{ "/SchemeTest.html", Resources.SchemeTest },
Expand Down
26 changes: 26 additions & 0 deletions CefSharp.Example/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CefSharp.Example/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@
<data name="DraggableRegionTest" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\DraggableRegionTest.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="MultitenantTest" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\multitenanttest.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>
31 changes: 31 additions & 0 deletions CefSharp.Example/Resources/MultitenantTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Multitenant Test</title>
<script>
function onLoad()
{
var p = document.getElementById('p');
var btn = document.getElementById('btn');
if (window.opener)
{
btn.parentElement.removeChild(btn);
}
uniqueObject.id().then(function(res) {
p.innerText = res;
});
}

function openWindow()
{
window.open(window.location, '', 'width=300,height=300');
}
</script>
</head>
<body onload="onLoad()">
<button id="btn" onclick="openWindow()">New Window</button>
<p id="p"></p>
</body>
</html>
20 changes: 20 additions & 0 deletions CefSharp.Example/UniqueBoundObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading;

namespace CefSharp.Example
{
public class UniqueBoundObject
{
private static int lastId = 0;
private int id;

public UniqueBoundObject()
{
id = Interlocked.Increment(ref lastId);
}

public int Id()
{
return id;
}
}
}
149 changes: 66 additions & 83 deletions CefSharp.Wpf.Example/Handlers/LifespanHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,75 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Windows;
using System.Windows.Interop;
using CefSharp.Example;

namespace CefSharp.Wpf.Example.Handlers
{
public class LifespanHandler : ILifeSpanHandler
{
bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
newBrowser = null;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

ChromiumWebBrowser chromiumBrowser = null;

var windowX = (windowInfo.X == int.MinValue) ? double.NaN : windowInfo.X;
var windowY = (windowInfo.Y == int.MinValue) ? double.NaN : windowInfo.Y;
var windowWidth = (windowInfo.Width == int.MinValue) ? double.NaN : windowInfo.Width;
var windowHeight = (windowInfo.Height == int.MinValue) ? double.NaN : windowInfo.Height;

chromiumWebBrowser.Dispatcher.Invoke(() =>
{
var owner = Window.GetWindow(chromiumWebBrowser);
chromiumBrowser = new ChromiumWebBrowser
{
Address = targetUrl,
};
chromiumBrowser.RegisterAsyncJsObject("uniqueObject", new UniqueBoundObject());

var popup = new Window
{
Left = windowX,
Top = windowY,
Width = windowWidth,
Height = windowHeight,
Content = chromiumBrowser,
Owner = owner,
Title = targetFrameName
};

popup.Closed += (o, e) =>
{
var w = o as Window;
if (w != null && w.Content is IWebBrowser)
{
(w.Content as IWebBrowser).Dispose();
w.Content = null;
}
};

chromiumBrowser.LifeSpanHandler = this;
});

newBrowser = chromiumBrowser;

return false;

//NOTE: This is experimental
//var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

//ChromiumWebBrowser chromiumBrowser = null;

//var windowX = (windowInfo.X == int.MinValue) ? double.NaN : windowInfo.X;
//var windowY = (windowInfo.Y == int.MinValue) ? double.NaN : windowInfo.Y;
//var windowWidth = (windowInfo.Width == int.MinValue) ? double.NaN : windowInfo.Width;
//var windowHeight = (windowInfo.Height == int.MinValue) ? double.NaN : windowInfo.Height;

//chromiumWebBrowser.Dispatcher.Invoke(() =>
//{
// var owner = Window.GetWindow(chromiumWebBrowser);
// chromiumBrowser = new ChromiumWebBrowser
// {
// Address = targetUrl,
// };

// chromiumBrowser.SetAsPopup();
// chromiumBrowser.LifeSpanHandler = this;

// var popup = new Window
// {
// Left = windowX,
// Top = windowY,
// Width = windowWidth,
// Height = windowHeight,
// Content = chromiumBrowser,
// Owner = owner,
// Title = targetFrameName
// };

// var windowInteropHelper = new WindowInteropHelper(popup);
// //Create the handle Window handle (In WPF there's only one handle per window, not per control)
// var handle = windowInteropHelper.EnsureHandle();

// //The parentHandle value will be used to identify monitor info and to act as the parent window for dialogs,
// //context menus, etc. If parentHandle is not provided then the main screen monitor will be used and some
// //functionality that requires a parent window may not function correctly.
// windowInfo.SetAsWindowless(handle, true);

// popup.Closed += (o, e) =>
// {
// var w = o as Window;
// if (w != null && w.Content is IWebBrowser)
// {
// (w.Content as IWebBrowser).Dispose();
// w.Content = null;
// }
// };
//});

//newBrowser = chromiumBrowser;

//return false;
}

void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{
//NOTE: This is experimental
//var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

//chromiumWebBrowser.Dispatcher.Invoke(() =>
//{
// var owner = Window.GetWindow(chromiumWebBrowser);

// if (owner != null && owner.Content == browserControl)
// {
// owner.Show();
// }
//});
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

chromiumWebBrowser.Dispatcher.Invoke(() =>
{
var owner = Window.GetWindow(chromiumWebBrowser);

if (owner != null && owner.Content == browserControl)
{
owner.Show();
}
});
}

bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
Expand All @@ -96,18 +80,17 @@ bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)

void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
//NOTE: This is experimental
//var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

//chromiumWebBrowser.Dispatcher.Invoke(() =>
//{
// var owner = Window.GetWindow(chromiumWebBrowser);

// if (owner != null && owner.Content == browserControl)
// {
// owner.Close();
// }
//});
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

chromiumWebBrowser.Dispatcher.Invoke(() =>
{
var owner = Window.GetWindow(chromiumWebBrowser);

if (owner != null && owner.Content == browserControl)
{
owner.Close();
}
});
}
}
}
1 change: 1 addition & 0 deletions CefSharp.Wpf.Example/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</MenuItem>
<MenuItem Header="_Tests">
<MenuItem Header="_Binding Test" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="{Binding Source={x:Static ex:CefExample.BindingTestUrl}}"/>
<MenuItem Header="M_ultitenant Test" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="{Binding Source={x:Static ex:CefExample.MultitenantTestUrl}}"/>
<MenuItem Header="_List Plugins" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="{Binding Source={x:Static ex:CefExample.PluginsTestUrl}}"/>
<MenuItem Header="_Tooltip Test" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="{Binding Source={x:Static ex:CefExample.TooltipTestUrl}}"/>
<MenuItem Header="_Popup Test" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="{Binding Source={x:Static ex:CefExample.PopupParentUrl}}"/>
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public BrowserTabView()
browser.RequestHandler = new RequestHandler();
browser.RegisterJsObject("bound", new BoundObject(), BindingOptions.DefaultBinder);
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject());
browser.RegisterAsyncJsObject("uniqueObject", new UniqueBoundObject());
// Enable touch scrolling - once properly tested this will likely become the default
//browser.IsManipulationEnabled = true;

Expand Down

0 comments on commit b0d9cde

Please sign in to comment.