PowerToys/Wox.Core/Plugin/JsonRPCPlugin.cs

167 lines
6.1 KiB
C#
Raw Normal View History

2014-07-06 22:57:11 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2014-07-10 23:57:08 +08:00
using System.Reflection;
using System.Threading;
2014-07-06 22:57:11 +08:00
using Newtonsoft.Json;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Exceptions;
2014-09-19 16:57:48 +08:00
using Wox.Infrastructure.Logger;
2014-07-06 22:57:11 +08:00
using Wox.Plugin;
2014-12-26 19:36:43 +08:00
namespace Wox.Core.Plugin
2014-07-06 22:57:11 +08:00
{
2014-12-26 19:36:43 +08:00
/// <summary>
/// Represent the plugin that using JsonPRC
/// </summary>
internal abstract class JsonRPCPlugin : IPlugin
2014-07-06 22:57:11 +08:00
{
protected PluginInitContext context;
2014-12-26 19:36:43 +08:00
/// <summary>
/// The language this JsonRPCPlugin support
/// </summary>
2014-07-09 23:44:57 +08:00
public abstract string SupportedLanguage { get; }
2014-07-06 22:57:11 +08:00
2014-07-07 23:05:06 +08:00
protected abstract string ExecuteQuery(Query query);
2014-12-26 19:36:43 +08:00
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
2014-07-06 22:57:11 +08:00
public List<Result> Query(Query query)
{
2014-07-07 23:05:06 +08:00
string output = ExecuteQuery(query);
2014-07-06 22:57:11 +08:00
if (!string.IsNullOrEmpty(output))
{
try
{
List<Result> results = new List<Result>();
2014-07-07 23:05:06 +08:00
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
2014-07-18 23:12:50 +08:00
if (queryResponseModel.Result == null) return null;
2014-07-09 18:15:23 +08:00
foreach (JsonRPCResult result in queryResponseModel.Result)
2014-07-06 22:57:11 +08:00
{
2014-07-10 23:57:08 +08:00
JsonRPCResult result1 = result;
result.Action = (c) =>
2014-07-06 22:57:11 +08:00
{
if (result1.JsonRPCAction == null) return false;
2014-07-10 23:57:08 +08:00
if (!string.IsNullOrEmpty(result1.JsonRPCAction.Method))
2014-07-06 22:57:11 +08:00
{
2014-07-10 23:57:08 +08:00
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
{
ThreadPool.QueueUserWorkItem(state =>
{
2014-12-26 19:36:43 +08:00
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
2014-07-10 23:57:08 +08:00
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
2014-07-18 20:00:55 +08:00
if (jsonRpcRequestModel != null
2014-07-18 23:12:50 +08:00
&& !string.IsNullOrEmpty(jsonRpcRequestModel.Method)
2014-07-10 23:57:08 +08:00
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
});
}
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
2014-07-06 22:57:11 +08:00
results.Add(result);
}
return results;
}
2014-07-18 20:00:55 +08:00
catch (Exception e)
2014-07-06 22:57:11 +08:00
{
2014-07-18 20:00:55 +08:00
ErrorReporting.TryShowErrorMessageBox(e.Message, e);
2014-09-19 16:57:48 +08:00
Log.Error(e.Message);
2014-07-06 22:57:11 +08:00
}
}
return null;
}
2014-07-10 23:57:08 +08:00
private void ExecuteWoxAPI(string method, object[] parameters)
{
MethodInfo methodInfo = App.Window.GetType().GetMethod(method);
if (methodInfo != null)
{
try
{
methodInfo.Invoke(App.Window, parameters);
}
catch (Exception)
{
#if (DEBUG)
{
throw;
}
#endif
}
}
}
2014-07-07 23:05:06 +08:00
/// <summary>
/// Execute external program and return the output
/// </summary>
/// <param name="fileName"></param>
/// <param name="arguments"></param>
/// <returns></returns>
protected string Execute(string fileName, string arguments)
2014-07-18 20:00:55 +08:00
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = fileName;
start.Arguments = arguments;
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
return Execute(start);
}
protected string Execute(ProcessStartInfo startInfo)
2014-07-06 22:57:11 +08:00
{
try
{
2014-07-18 20:00:55 +08:00
using (Process process = Process.Start(startInfo))
2014-07-06 22:57:11 +08:00
{
if (process != null)
{
using (StreamReader reader = process.StandardOutput)
{
2014-07-18 20:00:55 +08:00
string result = reader.ReadToEnd();
if (result.StartsWith("DEBUG:"))
{
System.Windows.Forms.MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
return "";
}
if (string.IsNullOrEmpty(result))
{
using (StreamReader errorReader = process.StandardError)
{
string error = errorReader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
2014-12-21 22:03:03 +08:00
ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error));
2014-07-18 20:00:55 +08:00
}
}
}
return result;
2014-07-06 22:57:11 +08:00
}
}
}
}
catch
{
return null;
}
return null;
}
public void Init(PluginInitContext ctx)
{
2014-12-26 19:36:43 +08:00
context = ctx;
2014-07-06 22:57:11 +08:00
}
}
}