Skip to content

Commit

Permalink
Add a timeout to expand browser tabs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Sep 2, 2014
1 parent f02c305 commit 4b8b717
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions GoToWindow.Plugins.ExpandBrowsersTabs/ExpandBrowsersTabsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GoToWindow.Extensibility;
using GoToWindow.Extensibility.Controls;
using GoToWindow.Extensibility.ViewModel;
Expand All @@ -10,12 +12,17 @@
using GoToWindow.Plugins.ExpandBrowsersTabs.Firefox;
using GoToWindow.Plugins.ExpandBrowsersTabs.InternetExplorer;
using GoToWindow.Plugins.ExpandBrowsersTabs.ViewModel;
using log4net;

namespace GoToWindow.Plugins.ExpandBrowsersTabs
{
[Export(GoToWindowConstants.PluginContractName, typeof(IGoToWindowPlugin))]
public class ExpandBrowsersTabsPlugin : IGoToWindowPlugin
{
private const int TimeoutMilliseconds = 1500;

private static readonly ILog Log = LogManager.GetLogger(typeof(ExpandBrowsersTabsPlugin).Assembly, "GoToWindow");

public string Id { get { return "GoToWindow.ExpandBrowsersTabs"; } }

public string Title { get { return "GoToWindow Expand Browser Tabs"; } }
Expand All @@ -33,24 +40,36 @@ public void BuildList(List<ISearchResult> list)
{
var finders = new Dictionary<string, ITabsFinder>();

for(var index = list.Count - 1; index >= 0; index--)
for (var index = list.Count - 1; index >= 0; index--)
{
var item = list[index] as IWindowSearchResult;

if (item == null)
continue;

ITabsFinder finder;
if(!finders.TryGetValue(item.ProcessName, out finder))
var browserName = item.ProcessName;
if (!finders.TryGetValue(browserName, out finder))
{
Func<ITabsFinder> finderCtor;
if (TabsFinders.TryGetValue(item.ProcessName, out finderCtor))
if (TabsFinders.TryGetValue(browserName, out finderCtor))
finder = finderCtor();
else
continue;
}

var tabs = finder.GetTabsOfWindow(item.HWnd);
IEnumerable<ITab> tabs = null;

var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;

var task = Task.Factory.StartNew(() => tabs = finder.GetTabsOfWindow(item.HWnd).ToArray(), token);

if (!task.Wait(TimeoutMilliseconds, token))
{
Log.WarnFormat("Timeout trying to get tabs for '{0}'", browserName);
continue;
}

var tabResults = tabs.Select(tab => ConvertTabToResult(item, tab)).ToArray();

Expand Down

0 comments on commit 4b8b717

Please sign in to comment.