From 6173f4db43feb495f7bd9af53dfd42c5908f47a7 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 10 Mar 2017 15:31:25 +1000 Subject: [PATCH] Change PluginVisitor to accept the newly added IWebPluginInfoVisitor Extract PluginVisitor TaskCompletionSource out into TaskWebPluginInfoVisitor --- CefSharp.Core/Cef.h | 13 +++++- CefSharp.Core/Internals/PluginVisitor.h | 37 +++------------ CefSharp/CefSharp.csproj | 2 + CefSharp/Visitor/IWebPluginInfoVisitor.cs | 21 +++++++++ CefSharp/Visitor/TaskWebPluginInfoVisitor.cs | 48 ++++++++++++++++++++ 5 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 CefSharp/Visitor/IWebPluginInfoVisitor.cs create mode 100644 CefSharp/Visitor/TaskWebPluginInfoVisitor.cs diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 349fcb936..2c679a5f2 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -433,6 +433,14 @@ namespace CefSharp return CefClearSchemeHandlerFactories(); } + /// + /// Visit web plugin information. Can be called on any thread in the browser process. + /// + static void VisitWebPluginInfo(IWebPluginInfoVisitor^ visitor) + { + CefVisitWebPluginInfo(new PluginVisitor(visitor)); + } + /// /// Async returns a list containing Plugin Information /// (Wrapper around CefVisitWebPluginInfo) @@ -440,11 +448,12 @@ namespace CefSharp /// Returns List of structs. static Task^>^ GetPlugins() { - CefRefPtr visitor = new PluginVisitor(); + auto taskVisitor = gcnew TaskWebPluginInfoVisitor(); + CefRefPtr visitor = new PluginVisitor(taskVisitor); CefVisitWebPluginInfo(visitor); - return visitor->GetTask(); + return taskVisitor->Task; } /// diff --git a/CefSharp.Core/Internals/PluginVisitor.h b/CefSharp.Core/Internals/PluginVisitor.h index 7a188affa..2676eb3de 100644 --- a/CefSharp.Core/Internals/PluginVisitor.h +++ b/CefSharp.Core/Internals/PluginVisitor.h @@ -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; - gcroot^> _list; + gcroot _visitor; public: - PluginVisitor() + PluginVisitor(IWebPluginInfoVisitor^ visitor) : _visitor(visitor) { - _list = gcnew List(); - _taskCompletionSource = gcnew TaskCompletionSource^>(); + } ~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^>(_taskCompletionSource, _list); - } - - _list = nullptr; - _taskCompletionSource = nullptr; - } - - Task^>^ GetTask() - { - return _taskCompletionSource->Task; + delete _visitor; + _visitor = nullptr; } virtual bool Visit(CefRefPtr 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^>(_taskCompletionSource, _list); - } - - return true; + return _visitor->Visit(plugin, count, total); } IMPLEMENT_REFCOUNTING(PluginVisitor); diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 63853abe4..112630b7c 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -264,8 +264,10 @@ + + diff --git a/CefSharp/Visitor/IWebPluginInfoVisitor.cs b/CefSharp/Visitor/IWebPluginInfoVisitor.cs new file mode 100644 index 000000000..a838afe36 --- /dev/null +++ b/CefSharp/Visitor/IWebPluginInfoVisitor.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 + { + /// + /// Method that will be called once for each plugin. + /// This method may never be called if no plugins are found. + /// + /// plugin information + /// is the 0-based index for the current plugin + /// total is the total number of plugins. + /// Return false to stop visiting plugins otherwise true + bool Visit(Plugin plugin, int count, int total); + } +} diff --git a/CefSharp/Visitor/TaskWebPluginInfoVisitor.cs b/CefSharp/Visitor/TaskWebPluginInfoVisitor.cs new file mode 100644 index 000000000..b703fbfc2 --- /dev/null +++ b/CefSharp/Visitor/TaskWebPluginInfoVisitor.cs @@ -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> taskCompletionSource; + private List list; + + public TaskWebPluginInfoVisitor() + { + taskCompletionSource = new TaskCompletionSource>(); + list = new List(); + } + + bool IWebPluginInfoVisitor.Visit(Plugin plugin, int count, int total) + { + list.Add(plugin); + + //Return true to keep visiting plugins + return true; + } + + /// + /// Task that can be awaited for the result to be retrieved async + /// + public Task> 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; + } + } +}