PowerToys/Wox.Core/Plugin/JsonRPCPlugin.cs

166 lines
6.0 KiB
C#
Raw Normal View History

2016-01-07 05:34:42 +08:00
using System;
using System.Collections.Generic;
2014-07-06 22:57:11 +08:00
using System.Diagnostics;
using System.IO;
2014-07-10 23:57:08 +08:00
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
2014-12-26 22:51:04 +08:00
using System.Windows.Forms;
2014-07-06 22:57:11 +08:00
using Newtonsoft.Json;
2015-11-09 09:32:33 +08:00
using Wox.Infrastructure.Exception;
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
/// every JsonRPC plugin should has its own plugin instance
2014-12-26 19:36:43 +08:00
/// </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>
public abstract string SupportedLanguage { get; set; }
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);
if (!String.IsNullOrEmpty(output))
2014-07-06 22:57:11 +08:00
{
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;
2016-01-07 05:34:42 +08:00
result.Action = c =>
2014-07-06 22:57:11 +08:00
{
if (result1.JsonRPCAction == null) return false;
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
{
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
2014-07-10 23:57:08 +08:00
{
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
2014-07-10 23:57:08 +08:00
}
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
2014-07-06 22:57:11 +08:00
results.Add(result);
}
return results;
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2014-07-06 22:57:11 +08:00
{
Log.Error(e);
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)
{
2014-12-26 22:51:04 +08:00
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method);
2015-02-03 18:32:16 +08:00
if (methodInfo != null)
2014-07-10 23:57:08 +08:00
{
try
{
2014-12-26 22:51:04 +08:00
methodInfo.Invoke(PluginManager.API, parameters);
2014-07-10 23:57:08 +08:00
}
2016-01-07 05:34:42 +08:00
catch (Exception)
2014-07-10 23:57:08 +08:00
{
#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)
2014-07-06 22:57:11 +08:00
{
using (StreamReader reader = process.StandardOutput)
{
2014-07-18 20:00:55 +08:00
string result = reader.ReadToEnd();
if (result.StartsWith("DEBUG:"))
{
2014-12-26 22:51:04 +08:00
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
2014-07-18 20:00:55 +08:00
return "";
}
if (String.IsNullOrEmpty(result))
2014-07-18 20:00:55 +08:00
{
using (StreamReader errorReader = process.StandardError)
{
string error = errorReader.ReadToEnd();
if (!String.IsNullOrEmpty(error))
2014-07-18 20:00:55 +08:00
{
2015-02-03 18:32:16 +08:00
throw new WoxJsonRPCException(error);
2014-07-18 20:00:55 +08:00
}
}
}
return result;
2014-07-06 22:57:11 +08:00
}
}
}
}
catch (Exception e)
2014-07-06 22:57:11 +08:00
{
2015-02-03 18:32:16 +08:00
throw new WoxJsonRPCException(e.Message);
2014-07-06 22:57:11 +08:00
}
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
}
}
}