mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 12:09:40 +00:00
Change PluginVisitor to accept the newly added IWebPluginInfoVisitor
Extract PluginVisitor TaskCompletionSource out into TaskWebPluginInfoVisitor
This commit is contained in:
+11
-2
@@ -433,6 +433,14 @@ namespace CefSharp
|
||||
return CefClearSchemeHandlerFactories();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Visit web plugin information. Can be called on any thread in the browser process.
|
||||
/// </summary>
|
||||
static void VisitWebPluginInfo(IWebPluginInfoVisitor^ visitor)
|
||||
{
|
||||
CefVisitWebPluginInfo(new PluginVisitor(visitor));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Async returns a list containing Plugin Information
|
||||
/// (Wrapper around CefVisitWebPluginInfo)
|
||||
@@ -440,11 +448,12 @@ namespace CefSharp
|
||||
/// <return>Returns List of <see cref="Plugin"/> structs.</return>
|
||||
static Task<List<Plugin>^>^ GetPlugins()
|
||||
{
|
||||
CefRefPtr<PluginVisitor> visitor = new PluginVisitor();
|
||||
auto taskVisitor = gcnew TaskWebPluginInfoVisitor();
|
||||
CefRefPtr<PluginVisitor> visitor = new PluginVisitor(taskVisitor);
|
||||
|
||||
CefVisitWebPluginInfo(visitor);
|
||||
|
||||
return visitor->GetTask();
|
||||
return taskVisitor->Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -7,40 +7,23 @@
|
||||
#include "Stdafx.h"
|
||||
#include "include/cef_web_plugin.h"
|
||||
|
||||
using namespace CefSharp::Internals;
|
||||
using namespace System::Threading::Tasks;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
private class PluginVisitor : public CefWebPluginInfoVisitor
|
||||
{
|
||||
private:
|
||||
gcroot<TaskCompletionSource<List<Plugin>^>^> _taskCompletionSource;
|
||||
gcroot<List<Plugin>^> _list;
|
||||
gcroot<IWebPluginInfoVisitor^> _visitor;
|
||||
|
||||
public:
|
||||
PluginVisitor()
|
||||
PluginVisitor(IWebPluginInfoVisitor^ visitor) : _visitor(visitor)
|
||||
{
|
||||
_list = gcnew List<Plugin>();
|
||||
_taskCompletionSource = gcnew TaskCompletionSource<List<Plugin>^>();
|
||||
|
||||
}
|
||||
|
||||
~PluginVisitor()
|
||||
{
|
||||
//In this case Visit was likely never called, so we set result to complete the task
|
||||
if (_list->Count == 0)
|
||||
{
|
||||
//Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread
|
||||
CefSharp::Internals::TaskExtensions::TrySetResultAsync<List<Plugin>^>(_taskCompletionSource, _list);
|
||||
}
|
||||
|
||||
_list = nullptr;
|
||||
_taskCompletionSource = nullptr;
|
||||
}
|
||||
|
||||
Task<List<Plugin>^>^ GetTask()
|
||||
{
|
||||
return _taskCompletionSource->Task;
|
||||
delete _visitor;
|
||||
_visitor = nullptr;
|
||||
}
|
||||
|
||||
virtual bool Visit(CefRefPtr<CefWebPluginInfo> info, int count, int total) OVERRIDE
|
||||
@@ -51,15 +34,7 @@ namespace CefSharp
|
||||
plugin.Path = StringUtils::ToClr(info->GetPath());
|
||||
plugin.Version = StringUtils::ToClr(info->GetVersion());
|
||||
|
||||
_list->Add(plugin);
|
||||
|
||||
if(count == (total - 1))
|
||||
{
|
||||
//Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread
|
||||
CefSharp::Internals::TaskExtensions::TrySetResultAsync<List<Plugin>^>(_taskCompletionSource, _list);
|
||||
}
|
||||
|
||||
return true;
|
||||
return _visitor->Visit(plugin, count, total);
|
||||
}
|
||||
|
||||
IMPLEMENT_REFCOUNTING(PluginVisitor);
|
||||
|
||||
@@ -264,8 +264,10 @@
|
||||
<Compile Include="Visitor\ICookieVisitor.cs" />
|
||||
<Compile Include="Visitor\INavigationEntryVisitor.cs" />
|
||||
<Compile Include="Visitor\IStringVisitor.cs" />
|
||||
<Compile Include="Visitor\IWebPluginInfoVisitor.cs" />
|
||||
<Compile Include="Visitor\TaskCookieVisitor.cs" />
|
||||
<Compile Include="Visitor\TaskNavigationEntryVisitor.cs" />
|
||||
<Compile Include="Visitor\TaskWebPluginInfoVisitor.cs" />
|
||||
<Compile Include="Visitor\TaskStringVisitor.cs" />
|
||||
<Compile Include="WebBrowserExtensions.cs" />
|
||||
<Compile Include="WebPluginInfo.cs" />
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright © 2010-2017 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;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public interface IWebPluginInfoVisitor : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Method that will be called once for each plugin.
|
||||
/// This method may never be called if no plugins are found.
|
||||
/// </summary>
|
||||
/// <param name="plugin">plugin information</param>
|
||||
/// <param name="count">is the 0-based index for the current plugin</param>
|
||||
/// <param name="total">total is the total number of plugins.</param>
|
||||
/// <returns>Return false to stop visiting plugins otherwise true</returns>
|
||||
bool Visit(Plugin plugin, int count, int total);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright © 2010-2017 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CefSharp.Internals;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public class TaskWebPluginInfoVisitor : IWebPluginInfoVisitor
|
||||
{
|
||||
private TaskCompletionSource<List<Plugin>> taskCompletionSource;
|
||||
private List<Plugin> list;
|
||||
|
||||
public TaskWebPluginInfoVisitor()
|
||||
{
|
||||
taskCompletionSource = new TaskCompletionSource<List<Plugin>>();
|
||||
list = new List<Plugin>();
|
||||
}
|
||||
|
||||
bool IWebPluginInfoVisitor.Visit(Plugin plugin, int count, int total)
|
||||
{
|
||||
list.Add(plugin);
|
||||
|
||||
//Return true to keep visiting plugins
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Task that can be awaited for the result to be retrieved async
|
||||
/// </summary>
|
||||
public Task<List<Plugin>> Task
|
||||
{
|
||||
get { return taskCompletionSource.Task; }
|
||||
}
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
//Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread
|
||||
taskCompletionSource.TrySetResultAsync(list);
|
||||
|
||||
list = null;
|
||||
taskCompletionSource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user