diff --git a/source/ChromeDevTools/ChromeProcess.cs b/source/ChromeDevTools/ChromeProcess.cs index 508d13d43c8358f9d65dd492d4bac7770087c862..21dfbd1ea26d6b7aa6213b0194a60b9e201908ac 100644 --- a/source/ChromeDevTools/ChromeProcess.cs +++ b/source/ChromeDevTools/ChromeProcess.cs @@ -4,9 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Net; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -15,9 +13,10 @@ namespace MasterDevs.ChromeDevTools public class ChromeProcess : IChromeProcess { public DirectoryInfo UserDirectory { get; set; } + public Process Process { get; set; } + public string RemoteDebuggingUri { get; set; } - public void Dispose() { @@ -40,14 +39,14 @@ namespace MasterDevs.ChromeDevTools var uriBuilder = new UriBuilder(RemoteDebuggingUri); uriBuilder.Path = "/json"; var remoteSessions = await webClient.DownloadStringTaskAsync(uriBuilder.Uri); - using(var stringReader = new StringReader(remoteSessions)) - using(var jsonReader = new JsonTextReader(stringReader)) + using (var stringReader = new StringReader(remoteSessions)) + using (var jsonReader = new JsonTextReader(stringReader)) { var sessionsObject = JToken.ReadFrom(jsonReader) as JArray; - foreach(var sessionObject in sessionsObject) + foreach (var sessionObject in sessionsObject) { var sessionUrl = sessionObject["webSocketDebuggerUrl"].GetSafeString(); - if(!String.IsNullOrEmpty(sessionUrl)) + if (!String.IsNullOrEmpty(sessionUrl)) { remoteSessionUrls.Add(sessionUrl); } @@ -56,4 +55,4 @@ namespace MasterDevs.ChromeDevTools return remoteSessionUrls.ToArray(); } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/ChromeProcessFactory.cs b/source/ChromeDevTools/ChromeProcessFactory.cs index 01ee183e0262352785a58d693f27b21aeb5dab2b..ba30cdccb5c20eeb723c66ad0271dcd439f1a535 100644 --- a/source/ChromeDevTools/ChromeProcessFactory.cs +++ b/source/ChromeDevTools/ChromeProcessFactory.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Diagnostics; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -27,4 +22,4 @@ namespace MasterDevs.ChromeDevTools }; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/ChromeSession.cs b/source/ChromeDevTools/ChromeSession.cs index 9a949bb3bf099b01b05e5b129aa29138e5107b67..d6c2901a561d6a853c540a7baeb881dd311fa2d5 100644 --- a/source/ChromeDevTools/ChromeSession.cs +++ b/source/ChromeDevTools/ChromeSession.cs @@ -43,11 +43,11 @@ namespace MasterDevs.ChromeDevTools private void EnsureInit() { - if(null == _webSocket) + if (null == _webSocket) { - lock(_Lock) + lock (_Lock) { - if(null == _webSocket) + if (null == _webSocket) { Init().Wait(); } @@ -73,7 +73,6 @@ namespace MasterDevs.ChromeDevTools }); } - public Task<ICommandResponse> SendAsync<T>() { var command = _commandFactory.Create<T>(); @@ -157,7 +156,7 @@ namespace MasterDevs.ChromeDevTools _requestWaitHandles.AddOrUpdate(command.Id, requestResetEvent, (id, r) => requestResetEvent); return Task.Run(() => { - EnsureInit(); + EnsureInit(); _webSocket.Send(requestString); requestResetEvent.WaitOne(); ICommandResponse response = null; @@ -166,6 +165,7 @@ namespace MasterDevs.ChromeDevTools return response; }); } + private bool TryGetCommandResponse(byte[] data, out ICommandResponse response) { response = _responseFactory.Create(data); diff --git a/source/ChromeDevTools/ChromeSessionFactory.cs b/source/ChromeDevTools/ChromeSessionFactory.cs index eb035f2f296d676ebcb6ebb4e995242c657a8a81..343409e05d9719c229c0450289191829595e8c70 100644 --- a/source/ChromeDevTools/ChromeSessionFactory.cs +++ b/source/ChromeDevTools/ChromeSessionFactory.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public class ChromeSessionFactory : IChromeSessionFactory { @@ -20,4 +14,4 @@ namespace MasterDevs.ChromeDevTools return session; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/Command.cs b/source/ChromeDevTools/Command.cs index ebae5261ef7e1fe1337da27d3ff83c789678e372..5a82f5bd71a2f71f27ef2c321d857a0aaab109a1 100644 --- a/source/ChromeDevTools/Command.cs +++ b/source/ChromeDevTools/Command.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface ICommand { @@ -12,6 +6,7 @@ namespace MasterDevs.ChromeDevTools string Method { get; } } + public class Command : ICommand { public long Id @@ -29,10 +24,10 @@ namespace MasterDevs.ChromeDevTools public class Command<T> : Command { - public T Params - { - get; - set; + public T Params + { + get; + set; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/CommandAttribute.cs b/source/ChromeDevTools/CommandAttribute.cs index 71e4c69cd9b613f5162b51916cd6fe4836401857..9e5317862546e10b80f62b11ae4bbf4a4f7db9c1 100644 --- a/source/ChromeDevTools/CommandAttribute.cs +++ b/source/ChromeDevTools/CommandAttribute.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -16,4 +12,4 @@ namespace MasterDevs.ChromeDevTools public string MethodName { get; private set; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/CommandFactory.cs b/source/ChromeDevTools/CommandFactory.cs index 0de72d164ae84e77508342cc3eb94a00fb17c17e..78111054d976895d99e4877edda2063b984c0d81 100644 --- a/source/ChromeDevTools/CommandFactory.cs +++ b/source/ChromeDevTools/CommandFactory.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Concurrent; using System.Threading; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -45,4 +40,4 @@ namespace MasterDevs.ChromeDevTools return result; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/CommandResponse.cs b/source/ChromeDevTools/CommandResponse.cs index 91a8b66d6fa9e55c11ef144be387f5989b7ad4d4..57a42deabad225bc7c8cfd4086417b0b4d57a9ca 100644 --- a/source/ChromeDevTools/CommandResponse.cs +++ b/source/ChromeDevTools/CommandResponse.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface ICommandResponse { @@ -12,6 +6,7 @@ namespace MasterDevs.ChromeDevTools string Method { get; } } + public class CommandResponse : ICommandResponse { public long Id @@ -35,4 +30,4 @@ namespace MasterDevs.ChromeDevTools private set; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/CommandResponseAttribute.cs b/source/ChromeDevTools/CommandResponseAttribute.cs index f4dfafe06bdf096b893d2592decc25ddf62cbf28..bf0ce94103e37f24ca39b32ace07625076d37c43 100644 --- a/source/ChromeDevTools/CommandResponseAttribute.cs +++ b/source/ChromeDevTools/CommandResponseAttribute.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -16,4 +12,4 @@ namespace MasterDevs.ChromeDevTools public string MethodName { get; private set; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/CommandResponseFactory.cs b/source/ChromeDevTools/CommandResponseFactory.cs index 3e4ee8780516703dbde018389ff79c35a2f79ed6..562dacaaa520bfa9f1b444b652ff1136c1bd65fc 100644 --- a/source/ChromeDevTools/CommandResponseFactory.cs +++ b/source/ChromeDevTools/CommandResponseFactory.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq; +using System; namespace MasterDevs.ChromeDevTools { @@ -32,12 +27,12 @@ namespace MasterDevs.ChromeDevTools return jObject.ToObject<ErrorResponse>(); } var methodString = GetMethod(jObject); - if(null == methodString) + if (null == methodString) { return null; } var typeInferredFromMethod = _methodTypeMap.GetCommandResponse(methodString); - if(null == typeInferredFromMethod) + if (null == typeInferredFromMethod) { return null; } @@ -62,4 +57,4 @@ namespace MasterDevs.ChromeDevTools return methodString; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/ErrorResponse.cs b/source/ChromeDevTools/ErrorResponse.cs index 96e9911f97418d4819bb8ec7d296677cb9363b0c..8f4aaaf03b7c16d762c56c0153cf086cb97220d5 100644 --- a/source/ChromeDevTools/ErrorResponse.cs +++ b/source/ChromeDevTools/ErrorResponse.cs @@ -1,21 +1,21 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { public interface IErrorResponse { Error Error { get; } + long Id { get; } } + public class Error { public int Code { get; set; } + public string Message { get; set; } } + public class ErrorResponse : IErrorResponse, ICommandResponse { public Error Error @@ -35,4 +35,4 @@ namespace MasterDevs.ChromeDevTools get { throw new NotSupportedException(); } } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/Event.cs b/source/ChromeDevTools/Event.cs index d1cd1321d91d7370ac76b86a1fcb35cee3c02dec..5e3c8aa2433c61411ee2235de011183515f2d59c 100644 --- a/source/ChromeDevTools/Event.cs +++ b/source/ChromeDevTools/Event.cs @@ -1,22 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface IEvent { string Method { get; set; } } + public class Event : IEvent { public string Method { get; set; } } + public class Event<T> : Event { public T Params { get; set; } } - -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/EventAttribute.cs b/source/ChromeDevTools/EventAttribute.cs index 60f35e7da5b514927e942480b4d033e4a642a818..2d8e5e0e03c578dc77de2dfcf5217b9840862368 100644 --- a/source/ChromeDevTools/EventAttribute.cs +++ b/source/ChromeDevTools/EventAttribute.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -16,4 +12,4 @@ namespace MasterDevs.ChromeDevTools public string MethodName { get; private set; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/EventFactory.cs b/source/ChromeDevTools/EventFactory.cs index c47433884c3613f0e93df9043a4a535c41789d2b..4fd49bf81a504718580ed28e2df1f5a70076d9a3 100644 --- a/source/ChromeDevTools/EventFactory.cs +++ b/source/ChromeDevTools/EventFactory.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq; +using System; namespace MasterDevs.ChromeDevTools { @@ -40,4 +36,4 @@ namespace MasterDevs.ChromeDevTools return result as IEvent; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/EventHandler.cs b/source/ChromeDevTools/EventHandler.cs index 5cb000bc920e605e00bef27a22b4cbfd6574155a..f4f65d8bb024730853a8018aaaff260f84caf8b1 100644 --- a/source/ChromeDevTools/EventHandler.cs +++ b/source/ChromeDevTools/EventHandler.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public delegate void EventHandler(object sender, IEvent response); -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/Extensions/JTokenExtensions.cs b/source/ChromeDevTools/Extensions/JTokenExtensions.cs index 41e2d37413b614de3daaa0b27b037c956ff507a3..7b5770336a0c23888090d1c6db530dd353b857b6 100644 --- a/source/ChromeDevTools/Extensions/JTokenExtensions.cs +++ b/source/ChromeDevTools/Extensions/JTokenExtensions.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Newtonsoft.Json.Linq +namespace Newtonsoft.Json.Linq { public static class JTokenExtensions { @@ -14,4 +8,4 @@ namespace Newtonsoft.Json.Linq return jtoken.ToString(); } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/Extensions/ObjectExtensions.cs b/source/ChromeDevTools/Extensions/ObjectExtensions.cs index 74cba8a1ace0772a7d173c2378270f9e1a75fc88..a3ce9b949ec89245270c4276509ff4344903d62d 100644 --- a/source/ChromeDevTools/Extensions/ObjectExtensions.cs +++ b/source/ChromeDevTools/Extensions/ObjectExtensions.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -26,9 +23,9 @@ namespace MasterDevs.ChromeDevTools var commandResponseAttribute = type.GetCustomAttributes(typeof(CommandResponseAttribute), true) .FirstOrDefault() as CommandResponseAttribute; if (null != commandResponseAttribute) return commandResponseAttribute.MethodName; - + // maybe it's generic parameter has a method - if(type.IsGenericType) + if (type.IsGenericType) { return type.GenericTypeArguments .FirstOrDefault() @@ -38,4 +35,4 @@ namespace MasterDevs.ChromeDevTools throw new Exception("Could not determine the method type for " + type); } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IChromeProcess.cs b/source/ChromeDevTools/IChromeProcess.cs index d89e9ab779d7de16b59c60ae74722a3ead2420dc..a6ee6e57458bba29956fb16cb097520cfcafa5b9 100644 --- a/source/ChromeDevTools/IChromeProcess.cs +++ b/source/ChromeDevTools/IChromeProcess.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools @@ -18,4 +15,4 @@ namespace MasterDevs.ChromeDevTools string RemoteDebuggingUri { get; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IChromeProcessFactory.cs b/source/ChromeDevTools/IChromeProcessFactory.cs index 8012c8a3dc9b8633e4c19ba87baada32190e251e..ddf57955e97a993ed0f5341c94e362af2c9d86ee 100644 --- a/source/ChromeDevTools/IChromeProcessFactory.cs +++ b/source/ChromeDevTools/IChromeProcessFactory.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface IChromeProcessFactory { IChromeProcess Create(int port); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IChromeSession.cs b/source/ChromeDevTools/IChromeSession.cs index 487ecc4118261d717707c80f2968c7f8f14491bf..dd7f98979bbf31577df59f9ab908943dd3dc3d06 100644 --- a/source/ChromeDevTools/IChromeSession.cs +++ b/source/ChromeDevTools/IChromeSession.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -14,4 +10,4 @@ namespace MasterDevs.ChromeDevTools void Subscribe<T>(EventHandler handler); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IChromeSessionFactory.cs b/source/ChromeDevTools/IChromeSessionFactory.cs index 9489ba69163e4c956a07ef4969396c91f170de36..286019d79236814f254fba53052cdfa3576e04ac 100644 --- a/source/ChromeDevTools/IChromeSessionFactory.cs +++ b/source/ChromeDevTools/IChromeSessionFactory.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface IChromeSessionFactory { IChromeSession Create(string endpointUrl); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/ICommandFactory.cs b/source/ChromeDevTools/ICommandFactory.cs index 2bc4b7df8ba29d49006b546c5b28e82eca2202da..afc94d02193f8d133d4a079461c7f46a1fc67e74 100644 --- a/source/ChromeDevTools/ICommandFactory.cs +++ b/source/ChromeDevTools/ICommandFactory.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface ICommandFactory { @@ -14,4 +8,4 @@ namespace MasterDevs.ChromeDevTools Command<T> Create<T>(T parameter); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/ICommandResponseFactory.cs b/source/ChromeDevTools/ICommandResponseFactory.cs index 34795c101af4b201d7e68c6d9d03285588fa43e5..3956cf784adf10ccf0306941f100a396c5b50aa6 100644 --- a/source/ChromeDevTools/ICommandResponseFactory.cs +++ b/source/ChromeDevTools/ICommandResponseFactory.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface ICommandResponseFactory { @@ -12,4 +6,4 @@ namespace MasterDevs.ChromeDevTools ICommandResponse Create(string responseText); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IEventFactory.cs b/source/ChromeDevTools/IEventFactory.cs index c2da19ee8f994880c1486e67298d1b9e7fed3b0c..be1c5ababd21e05e54a328fb9d8206e29191e984 100644 --- a/source/ChromeDevTools/IEventFactory.cs +++ b/source/ChromeDevTools/IEventFactory.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface IEventFactory { @@ -12,4 +6,4 @@ namespace MasterDevs.ChromeDevTools IEvent Create(string responseText); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IMethodNameAttribute.cs b/source/ChromeDevTools/IMethodNameAttribute.cs index e42d733212725919d84309976616b38a196d4d4e..d8aabc3c7108f4fc2b8d8f2b10cb5294dd87b2a3 100644 --- a/source/ChromeDevTools/IMethodNameAttribute.cs +++ b/source/ChromeDevTools/IMethodNameAttribute.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MasterDevs.ChromeDevTools +namespace MasterDevs.ChromeDevTools { public interface IMethodNameAttribute { string MethodName { get; } } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/IMethodTypeMap.cs b/source/ChromeDevTools/IMethodTypeMap.cs index f0f0967f2ef4389a48725abbf20c5a2a036a3499..57aa28980ddc282db318a1032f48b0e3ca2a56fe 100644 --- a/source/ChromeDevTools/IMethodTypeMap.cs +++ b/source/ChromeDevTools/IMethodTypeMap.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -14,4 +10,4 @@ namespace MasterDevs.ChromeDevTools Type GetEvent(string method); } -} +} \ No newline at end of file diff --git a/source/ChromeDevTools/MethodTypeMap.cs b/source/ChromeDevTools/MethodTypeMap.cs index ef256d8e2d898dd72cd498241fad4093192abb0b..3f96d98c75d1fbd43b6379ccb81e303456908490 100644 --- a/source/ChromeDevTools/MethodTypeMap.cs +++ b/source/ChromeDevTools/MethodTypeMap.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -17,14 +15,15 @@ namespace MasterDevs.ChromeDevTools { LoadMethodTypeMap(); } + private void LoadMethodTypeMap() { var assembly = Assembly.GetExecutingAssembly(); var assemblyTypes = assembly.GetTypes(); - foreach(var type in assemblyTypes) + foreach (var type in assemblyTypes) { - if(!type.IsClass) continue; - if(type.Name.EndsWith("CommandResponse")) + if (!type.IsClass) continue; + if (type.Name.EndsWith("CommandResponse")) { var methodName = GetMethodName<CommandResponseAttribute>(type); if (null == methodName) continue; @@ -76,4 +75,4 @@ namespace MasterDevs.ChromeDevTools return type; } } -} +} \ No newline at end of file diff --git a/source/ProtocolGenerator/Program.cs b/source/ProtocolGenerator/Program.cs index f7a93161bea2bcc0d7de31c61722f41f3c7f1a02..194d205c5264133252d022a023569fa95a7a024c 100644 --- a/source/ProtocolGenerator/Program.cs +++ b/source/ProtocolGenerator/Program.cs @@ -5,37 +5,35 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools.ProtocolGenerator { - class Program + internal class Program { - const string CommandAttribute = "Command"; - const string CommandResponseAttribute = "CommandResponse"; - const string EventAttribute = "Event"; - const string ProtocolNameClass = "ProtocolName"; - const string RootNamespace = "MasterDevs.ChromeDevTools.Protocol"; - const string CommandSubclass = "Command"; - const string CommandResponseSubclass = CommandSubclass + "Response"; - const string EventSubclass = "Event"; - private static Dictionary<string, Dictionary<string, string>> _DomainPropertyTypes = new Dictionary<string,Dictionary<string,string>>(); + private const string CommandAttribute = "Command"; + private const string CommandResponseAttribute = "CommandResponse"; + private const string EventAttribute = "Event"; + private const string ProtocolNameClass = "ProtocolName"; + private const string RootNamespace = "MasterDevs.ChromeDevTools.Protocol"; + private const string CommandSubclass = "Command"; + private const string CommandResponseSubclass = CommandSubclass + "Response"; + private const string EventSubclass = "Event"; + private static Dictionary<string, Dictionary<string, string>> _DomainPropertyTypes = new Dictionary<string, Dictionary<string, string>>(); private static Dictionary<string, List<string>> _DomainCommands = new Dictionary<string, List<string>>(); private static Dictionary<string, List<string>> _DomainEvents = new Dictionary<string, List<string>>(); private static Dictionary<string, string> _SimpleTypes = new Dictionary<string, string>(); - - static void Main(string[] args) + private static void Main(string[] args) { var filePath = "protocol.json"; JObject protocolObject = null; - using(var fileStream = File.OpenText(filePath)) - using(var reader = new JsonTextReader(fileStream)) + using (var fileStream = File.OpenText(filePath)) + using (var reader = new JsonTextReader(fileStream)) { protocolObject = (JObject)JToken.ReadFrom(reader); } var outputFolder = "OutputProtocol"; - if(Directory.Exists(outputFolder)) + if (Directory.Exists(outputFolder)) { Directory.Delete(outputFolder, true); } @@ -50,7 +48,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator { AddPropertyTypes(domain["domain"].ToString(), domain["types"] as JArray); } - foreach(var domain in domains) + foreach (var domain in domains) { var domainName = domain["domain"].ToString(); var types = domain["types"] as JArray; @@ -71,8 +69,8 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator { var propertyType = type["type"].ToString(); var typeName = GetString(type["id"]); - if (null != type["enum"] - || null != type["properties"] + if (null != type["enum"] + || null != type["properties"] || "object" == propertyType) { propertyType = domain + "." + typeName; @@ -107,7 +105,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator private static void WriteProtocolClasses(DirectoryInfo directory, string domainName, JArray types, JArray commands, JArray events) { var domainDirectoryInfo = CreateDomainFolder(directory, domainName); - foreach(var type in types ?? new JArray()) + foreach (var type in types ?? new JArray()) { WriteType(domainDirectoryInfo, type as JObject); } @@ -120,7 +118,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator WriteEvent(domainDirectoryInfo, evnt as JObject); } } - + private static void WriteMethodConstants(DirectoryInfo domainDirectoryInfo) { var sb = new StringBuilder(); @@ -135,24 +133,24 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator sb.AppendLine("\t{"); var domains = _DomainCommands.Keys.Union(_DomainEvents.Keys).Distinct(); - foreach(var domain in domains) + foreach (var domain in domains) { sb.AppendFormat("\t\tpublic static class {0}", domain); sb.AppendLine(); sb.AppendLine("\t\t{"); List<string> commands; - if(_DomainCommands.TryGetValue(domain, out commands)) + if (_DomainCommands.TryGetValue(domain, out commands)) { - foreach(var commandName in commands) + foreach (var commandName in commands) { sb.AppendFormat("\t\t\tpublic const string {0} = \"{1}.{2}\";", ToCamelCase(commandName), domain, commandName); sb.AppendLine(); } } List<string> events; - if(_DomainEvents.TryGetValue(domain, out events)) + if (_DomainEvents.TryGetValue(domain, out events)) { - foreach(var eventName in events) + foreach (var eventName in events) { sb.AppendFormat("\t\t\tpublic const string {0} = \"{1}.{2}\";", ToCamelCase(eventName), domain, eventName); sb.AppendLine(); @@ -162,7 +160,6 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator sb.AppendLine(); } - sb.AppendLine("\t}"); sb.AppendLine("}"); WriteToFile(domainDirectoryInfo, ProtocolNameClass, sb.ToString()); @@ -238,8 +235,8 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator sb.AppendFormat("namespace {0}.{1}", RootNamespace, domainDirectoryInfo.Name); sb.AppendLine(); sb.AppendLine("{"); - if(!String.IsNullOrEmpty(description)) - { + if (!String.IsNullOrEmpty(description)) + { sb.AppendLine("\t/// <summary>"); sb.AppendFormat("\t/// {0}", description); sb.AppendLine(); @@ -338,49 +335,49 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator { var propertyName = GeneratePropertyName(property["name"].ToString()); string propertyType = GetString(property["type"]); - if(null != property["$ref"]) + if (null != property["$ref"]) { propertyType = GeneratePropertyTypeFromReference(domain, property["$ref"].ToString()); } - else if("array" == propertyType) + else if ("array" == propertyType) { var arrayDescription = property["items"] as JObject; - if(null != arrayDescription["$ref"]) + if (null != arrayDescription["$ref"]) { propertyType = GeneratePropertyTypeFromReference(domain, arrayDescription["$ref"].ToString()) + "[]"; - } + } else { var arrayType = GetString(arrayDescription["type"]); - if("object" == arrayType) + if ("object" == arrayType) { var internalClassName = ToCamelCase(propertyName) + "Array"; propertyType = internalClassName + "[]"; sb.AppendFormat("\t\tpublic class {0}", internalClassName); sb.AppendLine(); sb.AppendLine("\t\t{"); - foreach(var internalProperty in arrayDescription["properties"] as JArray ?? new JArray()) + foreach (var internalProperty in arrayDescription["properties"] as JArray ?? new JArray()) { WriteProperty(sb, domain, internalClassName, internalProperty as JObject); } sb.AppendLine("\t\t}"); sb.AppendLine(); - } - else + } + else { propertyType = GeneratePropertyType(GetString(arrayDescription["type"])) + "[]"; } } - } + } else { propertyType = GeneratePropertyType(propertyType.ToString()); - } + } sb.AppendLine("\t\t/// <summary>"); sb.AppendFormat("\t\t/// Gets or sets {0}", property["description"] ?? propertyName); sb.AppendLine(); sb.AppendLine("\t\t/// </summary>"); - if(className == propertyName) + if (className == propertyName) { sb.AppendFormat("\t\t[JsonProperty(\"{0}\")]", property["name"]); sb.AppendLine(); @@ -398,10 +395,10 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator { Dictionary<string, string> domainDictionary; string inDomainType; - if(_DomainPropertyTypes.TryGetValue(domain, out domainDictionary) + if (_DomainPropertyTypes.TryGetValue(domain, out domainDictionary) && domainDictionary.TryGetValue(propertyPaths[0], out inDomainType)) { - if(inDomainType.StartsWith(domain + ".")) + if (inDomainType.StartsWith(domain + ".")) { return inDomainType.Substring(inDomainType.IndexOf('.') + 1); } @@ -419,7 +416,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator private static string GeneratePropertyType(string propertyType) { - switch(propertyType) + switch (propertyType) { case "number": return "double"; case "integer": return "long"; @@ -455,7 +452,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator sb.AppendFormat("\tpublic enum {0}", enumName); sb.AppendLine(); sb.AppendLine("\t{"); - foreach(var enumValueName in type["enum"] as JArray ?? new JArray()) + foreach (var enumValueName in type["enum"] as JArray ?? new JArray()) { sb.AppendFormat("\t\t\t{0},", ToCamelCase(enumValueName.ToString().Replace("-", "_"))); sb.AppendLine(); @@ -474,7 +471,7 @@ namespace MasterDevs.ChromeDevTools.ProtocolGenerator private static DirectoryInfo CreateDomainFolder(DirectoryInfo parentDirectory, string domainName) { - return parentDirectory.CreateSubdirectory(domainName); + return parentDirectory.CreateSubdirectory(domainName); } } -} +} \ No newline at end of file diff --git a/source/Sample/Program.cs b/source/Sample/Program.cs index 9e4b2c35afe5ab8d762a188667414a6ee5f62b77..c8fefbed4c9a1c8700a1592d7a734ea4778dd6b3 100644 --- a/source/Sample/Program.cs +++ b/source/Sample/Program.cs @@ -1,21 +1,12 @@ -using MasterDevs.ChromeDevTools; +using MasterDevs.ChromeDevTools.Protocol.Page; using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using MasterDevs.ChromeDevTools.Protocol; -using WebSocket4Net; -using MasterDevs.ChromeDevTools.Protocol.Page; namespace MasterDevs.ChromeDevTools.Sample { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { // STEP 1 - Run Chrome var chromeProcessFactory = new ChromeProcessFactory(); @@ -27,7 +18,7 @@ namespace MasterDevs.ChromeDevTools.Sample var chromeSession = chromeSessionFactory.Create(endpointUrl); // STEP 3 - Send a command - // + // // Here we are sending a command to tell chrome to navigate to // the specified URL var navigateResponse = chromeSession.SendAsync(new NavigateCommand @@ -56,8 +47,6 @@ namespace MasterDevs.ChromeDevTools.Sample Console.ReadLine(); } - } - } -} +} \ No newline at end of file