Skip to content

Commit

Permalink
Core - FrameHandler track frame lifecycle
Browse files Browse the repository at this point in the history
- FrameHandler now tracks frame creation and removal and reuses the IFrame reference
- CefFrameWrapper no longer throws exception if accessing Identifier for invalid frame, this appears
to be a valid use case now which makes sense otherwise we'd never be able to identify the frame.

Resolves #3588
  • Loading branch information
amaitland committed Sep 10, 2021
1 parent 0804a40 commit 7f870db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 0 additions & 1 deletion CefSharp.Core.Runtime/Internals/CefFrameWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ String^ CefFrameWrapper::Name::get()
Int64 CefFrameWrapper::Identifier::get()
{
ThrowIfDisposed();
ThrowIfFrameInvalid();

return _frame->GetIdentifier();
}
Expand Down
1 change: 1 addition & 0 deletions CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
browser.JsDialogHandler = new JsDialogHandler();
browser.DownloadHandler = new DownloadHandler();
browser.AudioHandler = new CefSharp.Handler.AudioHandler();
browser.FrameHandler = new CefSharp.Handler.FrameHandler();

if (multiThreadedMessageLoopEnabled)
{
Expand Down
36 changes: 31 additions & 5 deletions CefSharp/Handler/FrameHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System.Collections.Generic;

namespace CefSharp.Handler
{
/// <summary>
Expand All @@ -10,10 +12,16 @@ namespace CefSharp.Handler
/// </summary>
public class FrameHandler : IFrameHandler
{
private Dictionary<long, IFrame> frames = new Dictionary<long, IFrame>();

/// <inheritdoc/>
void IFrameHandler.OnFrameAttached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
OnFrameAttached(chromiumWebBrowser, browser, frame);
//If we have a reference to the frame then we'll reuse that for memory management purposes
//The frame param massed on here is stack allocated so will be disposed when out of scope.
var storedFrame = GetFrameById(frame.Identifier);

OnFrameAttached(chromiumWebBrowser, browser, storedFrame ?? frame);
}

/// <summary>
Expand All @@ -25,12 +33,14 @@ void IFrameHandler.OnFrameAttached(IWebBrowser chromiumWebBrowser, IBrowser brow
/// <param name="frame">the frame object</param>
protected virtual void OnFrameAttached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{

}

/// <inheritdoc/>
void IFrameHandler.OnFrameCreated(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
frames.Add(frame.Identifier, frame);

OnFrameCreated(chromiumWebBrowser, browser, frame);
}

Expand All @@ -45,12 +55,14 @@ void IFrameHandler.OnFrameCreated(IWebBrowser chromiumWebBrowser, IBrowser brows
/// <param name="frame">the frame object</param>
protected virtual void OnFrameCreated(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{

}

/// <inheritdoc/>
void IFrameHandler.OnFrameDetached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
frames.Remove(frame.Identifier);

OnFrameDetached(chromiumWebBrowser, browser, frame);
}

Expand All @@ -66,13 +78,17 @@ void IFrameHandler.OnFrameDetached(IWebBrowser chromiumWebBrowser, IBrowser brow
/// <param name="frame">the frame object</param>
protected virtual void OnFrameDetached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{

}

/// <inheritdoc/>
void IFrameHandler.OnMainFrameChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame oldFrame, IFrame newFrame)
{
OnMainFrameChanged(chromiumWebBrowser, browser, oldFrame, newFrame);
//If we have a reference to the frame then we'll reuse that for memory management purposes
//The frame param massed on here is stack allocated so will be disposed when out of scope.
var storedFrame = newFrame == null ? null : GetFrameById(newFrame.Identifier);

OnMainFrameChanged(chromiumWebBrowser, browser, oldFrame, storedFrame ?? newFrame);
}

/// <summary>
Expand Down Expand Up @@ -101,5 +117,15 @@ protected virtual void OnMainFrameChanged(IWebBrowser chromiumWebBrowser, IBrows
{

}

private IFrame GetFrameById(long frameId)
{
if(frames.TryGetValue(frameId, out var frame))
{
return frame;
}

return null;
}
}
}

0 comments on commit 7f870db

Please sign in to comment.