Messages.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using Xilium.CefGlue.Common.Shared.Serialization;
  3. namespace Xilium.CefGlue.Common.Shared.RendererProcessCommunication
  4. {
  5. internal static class Messages
  6. {
  7. public struct JsEvaluationRequest
  8. {
  9. public const string Name = nameof(JsEvaluationRequest);
  10. public int TaskId;
  11. public string Script;
  12. public string Url;
  13. public int Line;
  14. public CefProcessMessage ToCefProcessMessage()
  15. {
  16. var message = CefProcessMessage.Create(Name);
  17. var arguments = message.Arguments;
  18. arguments.SetInt(0, TaskId);
  19. arguments.SetString(1, Script);
  20. arguments.SetString(2, Url);
  21. arguments.SetInt(3, Line);
  22. return message;
  23. }
  24. public static JsEvaluationRequest FromCefMessage(CefProcessMessage message)
  25. {
  26. var arguments = message.Arguments;
  27. return new JsEvaluationRequest()
  28. {
  29. TaskId = arguments.GetInt(0),
  30. Script = arguments.GetString(1),
  31. Url = arguments.GetString(2),
  32. Line = arguments.GetInt(3)
  33. };
  34. }
  35. }
  36. public struct JsEvaluationResult
  37. {
  38. public const string Name = nameof(JsEvaluationResult);
  39. public int TaskId;
  40. public bool Success;
  41. public CefValueHolder Result;
  42. public string Exception;
  43. public CefProcessMessage ToCefProcessMessage()
  44. {
  45. var message = CefProcessMessage.Create(Name);
  46. var arguments = message.Arguments;
  47. arguments.SetInt(0, TaskId);
  48. arguments.SetBool(1, Success);
  49. Result?.CopyTo(arguments, 2);
  50. arguments.SetString(3, Exception);
  51. return message;
  52. }
  53. public static JsEvaluationResult FromCefMessage(CefProcessMessage message)
  54. {
  55. var arguments = message.Arguments;
  56. return new JsEvaluationResult()
  57. {
  58. TaskId = arguments.GetInt(0),
  59. Success = arguments.GetBool(1),
  60. Result = new CefValueHolder(arguments, 2, isReadOnly: true),
  61. Exception = arguments.GetString(3)
  62. };
  63. }
  64. }
  65. public struct NativeObjectRegistrationRequest
  66. {
  67. public const string Name = nameof(NativeObjectRegistrationRequest);
  68. public string ObjectName;
  69. public string[] MethodsNames;
  70. public CefProcessMessage ToCefProcessMessage()
  71. {
  72. var message = CefProcessMessage.Create(Name);
  73. var arguments = message.Arguments;
  74. arguments.SetString(0, ObjectName);
  75. var methods = CefListValue.Create();
  76. for (var i = 0; i < MethodsNames.Length; i++)
  77. {
  78. methods.SetString(i, MethodsNames[i]);
  79. }
  80. arguments.SetList(1, methods);
  81. return message;
  82. }
  83. public static NativeObjectRegistrationRequest FromCefMessage(CefProcessMessage message)
  84. {
  85. var arguments = message.Arguments;
  86. var methodsNames = arguments.GetList(1);
  87. return new NativeObjectRegistrationRequest()
  88. {
  89. ObjectName = arguments.GetString(0),
  90. MethodsNames = CefValueSerialization.DeserializeCefList<string>(methodsNames)
  91. };
  92. }
  93. }
  94. public struct NativeObjectUnregistrationRequest
  95. {
  96. public const string Name = nameof(NativeObjectUnregistrationRequest);
  97. public string ObjectName;
  98. public CefProcessMessage ToCefProcessMessage()
  99. {
  100. var message = CefProcessMessage.Create(Name);
  101. var arguments = message.Arguments;
  102. arguments.SetString(0, ObjectName);
  103. return message;
  104. }
  105. public static NativeObjectRegistrationRequest FromCefMessage(CefProcessMessage message)
  106. {
  107. var arguments = message.Arguments;
  108. return new NativeObjectRegistrationRequest()
  109. {
  110. ObjectName = arguments.GetString(0),
  111. };
  112. }
  113. }
  114. public struct NativeObjectCallRequest : IDisposable
  115. {
  116. public const string Name = nameof(NativeObjectCallRequest);
  117. public int CallId;
  118. public string ObjectName;
  119. public string MemberName;
  120. public ICefListValue Arguments;
  121. public CefProcessMessage ToCefProcessMessage()
  122. {
  123. var message = CefProcessMessage.Create(Name);
  124. var arguments = message.Arguments;
  125. arguments.SetInt(0, CallId);
  126. arguments.SetString(1, ObjectName);
  127. arguments.SetString(2, MemberName);
  128. arguments.SetList(3, Arguments);
  129. return message;
  130. }
  131. public static NativeObjectCallRequest FromCefMessage(CefProcessMessage message)
  132. {
  133. var arguments = message.Arguments;
  134. return new NativeObjectCallRequest()
  135. {
  136. CallId = arguments.GetInt(0),
  137. ObjectName = arguments.GetString(1),
  138. MemberName = arguments.GetString(2),
  139. Arguments = arguments.GetList(3)
  140. };
  141. }
  142. public void Dispose()
  143. {
  144. Arguments?.Dispose();
  145. }
  146. }
  147. public struct NativeObjectCallResult
  148. {
  149. public const string Name = nameof(NativeObjectCallResult);
  150. public int CallId;
  151. public bool Success;
  152. public CefValueHolder Result;
  153. public string Exception;
  154. public CefProcessMessage ToCefProcessMessage()
  155. {
  156. var message = CefProcessMessage.Create(Name);
  157. var arguments = message.Arguments;
  158. arguments.SetInt(0, CallId);
  159. arguments.SetBool(1, Success);
  160. Result?.CopyTo(arguments, 2);
  161. arguments.SetString(3, Exception);
  162. return message;
  163. }
  164. public static NativeObjectCallResult FromCefMessage(CefProcessMessage message)
  165. {
  166. var arguments = message.Arguments;
  167. return new NativeObjectCallResult() {
  168. CallId = arguments.GetInt(0),
  169. Success = arguments.GetBool(1),
  170. Result = new CefValueHolder(arguments, 2, isReadOnly: true),
  171. Exception = arguments.GetString(3)
  172. };
  173. }
  174. }
  175. public struct JsContextCreated
  176. {
  177. public const string Name = nameof(JsContextCreated);
  178. public CefProcessMessage ToCefProcessMessage()
  179. {
  180. return CefProcessMessage.Create(Name);
  181. }
  182. public static JsContextCreated FromCefMessage(CefProcessMessage message)
  183. {
  184. return new JsContextCreated();
  185. }
  186. }
  187. public struct JsContextReleased
  188. {
  189. public const string Name = nameof(JsContextReleased);
  190. public CefProcessMessage ToCefProcessMessage()
  191. {
  192. return CefProcessMessage.Create(Name);
  193. }
  194. public static JsContextReleased FromCefMessage(CefProcessMessage message)
  195. {
  196. return new JsContextReleased();
  197. }
  198. }
  199. public struct JsUncaughtException
  200. {
  201. public const string Name = nameof(JsUncaughtException);
  202. public string Message;
  203. public JsStackFrame[] StackFrames;
  204. public CefProcessMessage ToCefProcessMessage()
  205. {
  206. var message = CefProcessMessage.Create(Name);
  207. var arguments = message.Arguments;
  208. arguments.SetString(0, Message);
  209. var frames = CefListValue.Create();
  210. for (var i = 0; i < StackFrames.Length; i++)
  211. {
  212. frames.SetList(i, StackFrames[i].ToCefValue());
  213. }
  214. arguments.SetList(1, frames);
  215. return message;
  216. }
  217. public static JsUncaughtException FromCefMessage(CefProcessMessage message)
  218. {
  219. var arguments = message.Arguments;
  220. var cefFrames = arguments.GetList(1);
  221. var frames = new JsStackFrame[cefFrames.Count];
  222. for (var i = 0; i < cefFrames.Count; i++)
  223. {
  224. var cefFrame = cefFrames.GetList(i);
  225. frames[i] = JsStackFrame.FromCefValue(cefFrame);
  226. }
  227. return new JsUncaughtException()
  228. {
  229. Message = arguments.GetString(0),
  230. StackFrames = frames
  231. };
  232. }
  233. }
  234. public struct JsStackFrame
  235. {
  236. public int Column;
  237. public string FunctionName;
  238. public int LineNumber;
  239. public string ScriptNameOrSourceUrl;
  240. internal CefListValue ToCefValue()
  241. {
  242. var result = CefListValue.Create();
  243. result.SetString(0, FunctionName);
  244. result.SetString(1, ScriptNameOrSourceUrl);
  245. result.SetInt(2, LineNumber);
  246. result.SetInt(3, Column);
  247. return result;
  248. }
  249. internal static JsStackFrame FromCefValue(ICefListValue frame)
  250. {
  251. return new JsStackFrame()
  252. {
  253. FunctionName = frame.GetString(0),
  254. ScriptNameOrSourceUrl = frame.GetString(1),
  255. LineNumber = frame.GetInt(2),
  256. Column = frame.GetInt(3)
  257. };
  258. }
  259. }
  260. public struct UnhandledException
  261. {
  262. public const string Name = nameof(UnhandledException);
  263. public string ExceptionType;
  264. public string Message;
  265. public string StackTrace;
  266. public CefProcessMessage ToCefProcessMessage()
  267. {
  268. var message = CefProcessMessage.Create(Name);
  269. var arguments = message.Arguments;
  270. arguments.SetString(0, ExceptionType);
  271. arguments.SetString(1, Message);
  272. arguments.SetString(2, StackTrace);
  273. return message;
  274. }
  275. public static UnhandledException FromCefMessage(CefProcessMessage message)
  276. {
  277. var arguments = message.Arguments;
  278. return new UnhandledException()
  279. {
  280. ExceptionType = arguments.GetString(0),
  281. Message = arguments.GetString(1),
  282. StackTrace = arguments.GetString(2),
  283. };
  284. }
  285. }
  286. }
  287. }