Skip to content

Commit

Permalink
Added example for separated bound objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cefsharp-ms committed Sep 1, 2016
1 parent 6f2a44e commit 8502e67
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 75 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 @@ -102,6 +102,7 @@
</Compile>
<Compile Include="CefSharpSchemeHandler.cs" />
<Compile Include="CefSharpSchemeHandlerFactory.cs" />
<Compile Include="UniqueBoundObject.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
Expand All @@ -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
25 changes: 25 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>
77 changes: 77 additions & 0 deletions CefSharp.Example/Resources/MultitenantTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Multitenant Test</title>
<script>
var windows = [];

window.onmessage = function(e)
{
var data;
try
{
data = JSON.parse(e.data);
} catch (e)
{
// fail silent...?
return;
}
switch (data.event)
{
case "QueryOpenWindows":
if (windows.indexOf(e.source) === -1)
{
windows.push(e.source);
}
break;
}
}

setInterval(function () {
try {
if (window.opener) {
window.opener.postMessage(JSON.stringify({ "event": "QueryOpenWindows"}), "*");
}
}
catch (e) {
}
}, 100);

function onLoad()
{
var p = document.getElementById('p');
var btn = document.getElementById('btn');
if (window.opener) {
btn.parentElement.removeChild(btn);
}

setTimeout(function () {
for (var i = 0; i < windows.length; i++)
{
windows[i].location.reload();
}
try {
uniqueObject.id().then(function (res) {
p.innerText = res;
}, function (error) {
p.innerText = error;
});
} catch (err) {
p.innerText = err;
}
}, 500);
}

function openWindow()
{
window.open(window.location, '', 'width=300,height=300');
window.location.reload();
}
</script>
</head>
<body onload="onLoad()">
<button id="btn" onclick="openWindow()">New Window</button>
<p id="p"></p>
</body>
</html>
24 changes: 24 additions & 0 deletions CefSharp.Example/UniqueBoundObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

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;
}
}
}
4 changes: 2 additions & 2 deletions CefSharp.Wpf.Example/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ protected override void OnStartup(StartupEventArgs e)
"please make sure you compile in `Release` mode.", "Warning");
}
#endif

CefSharpSettings.SeparateBoundObjects = true;
CefExample.Init(true, multiThreadedMessageLoop: true, browserProcessHandler: new BrowserProcessHandler());

base.OnStartup(e);
}
}
}
}
148 changes: 75 additions & 73 deletions CefSharp.Wpf.Example/Handlers/LifespanHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Windows;
using System.Windows.Interop;
using CefSharp.Example;

namespace CefSharp.Wpf.Example.Handlers
{
Expand All @@ -14,79 +15,80 @@ bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser
{
newBrowser = null;

return false;
// 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;
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());

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;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

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

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

bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
Expand All @@ -97,17 +99,17 @@ bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
//NOTE: This is experimental
//var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

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

// if (owner != null && owner.Content == browserControl)
// {
// owner.Close();
// }
//});
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 @@ -21,6 +21,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 8502e67

Please sign in to comment.