Skip to content

Commit d0c9adc

Browse files
committed
Core - Add TryGetBrowserCoreById
- Can be used to obtain an IBrowser reference by Browser Identifier (Id). - Added a couple of basic unit tests.
1 parent 27816d5 commit d0c9adc

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ namespace CefSharp
195195

196196
IBrowser^ ManagedCefBrowserAdapter::GetBrowser(int browserId)
197197
{
198-
return _clientAdapter->GetBrowserWrapper(browserId);
198+
if (_clientAdapter.get())
199+
{
200+
return _clientAdapter->GetBrowserWrapper(browserId);
201+
}
202+
203+
return nullptr;
199204
}
200205

201206
IJavascriptCallbackFactory^ ManagedCefBrowserAdapter::JavascriptCallbackFactory::get()

CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,35 @@ public async Task CanWaitForBrowserInitialLoadAfterLoad()
745745
}
746746
}
747747

748+
[Fact]
749+
public async Task CanCallTryGetBrowserCoreByIdWithInvalidId()
750+
{
751+
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
752+
{
753+
var response = await browser.WaitForInitialLoadAsync();
754+
755+
var result = browser.TryGetBrowserCoreById(100, out IBrowser browserCore);
756+
757+
Assert.False(result);
758+
Assert.Null(browserCore);
759+
}
760+
}
761+
762+
[Fact]
763+
public async Task CanCallTryGetBrowserCoreByIdWithOwnId()
764+
{
765+
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
766+
{
767+
var response = await browser.WaitForInitialLoadAsync();
768+
769+
var result = browser.TryGetBrowserCoreById(browser.BrowserCore.Identifier, out IBrowser browserCore);
770+
771+
Assert.True(result);
772+
Assert.NotNull(browserCore);
773+
Assert.Equal(browser.BrowserCore.Identifier, browserCore.Identifier);
774+
}
775+
}
776+
748777
#if DEBUG
749778
[Fact]
750779
public async Task CanLoadMultipleBrowserInstancesSequentially()

CefSharp/IWebBrowser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,14 @@ public interface IWebBrowser : IChromiumWebBrowserBase
160160
/// </summary>
161161
/// <returns>browser instance or null</returns>
162162
IBrowser GetBrowser();
163+
164+
/// <summary>
165+
/// Try and get a reference to the <see cref="IBrowser"/> instance that matches the <paramref name="browserId"/>.
166+
/// Primarily used for geting a reference to the <see cref="IBrowser"/> used by popups.
167+
/// </summary>
168+
/// <param name="browserId">browser Id</param>
169+
/// <param name="browser">When this method returns, contains the <see cref="IBrowser"/> object reference that matches the specified <paramref name="browserId"/>, or null if no matching instance found.</param>
170+
/// <returns>true if a <see cref="IBrowser"/> instance was found matching <paramref name="browserId"/>; otherwise, false.</returns>
171+
bool TryGetBrowserCoreById(int browserId, out IBrowser browser);
163172
}
164173
}

CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,23 @@ public Task<LoadUrlAsyncResponse> WaitForInitialLoadAsync()
375375
return initialLoadTaskCompletionSource.Task;
376376
}
377377

378+
/// <inheritdoc/>
379+
public bool TryGetBrowserCoreById(int browserId, out IBrowser browser)
380+
{
381+
var browserAdapter = managedCefBrowserAdapter;
382+
383+
if (IsDisposed || browserAdapter == null || browserAdapter.IsDisposed)
384+
{
385+
browser = null;
386+
387+
return false;
388+
}
389+
390+
browser = browserAdapter.GetBrowser(browserId);
391+
392+
return browser != null;
393+
}
394+
378395
private void InitialLoad(bool? isLoading, CefErrorCode? errorCode)
379396
{
380397
if(IsDisposed)

0 commit comments

Comments
 (0)