protected override bool OnProcessMessageReceived(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message) { if (DumpProcessMessages) { Console.WriteLine("Render::OnProcessMessageReceived: SourceProcess={0}", sourceProcess); Console.WriteLine("Message Name={0} IsValid={1} IsReadOnly={2}", message.Name, message.IsValid, message.IsReadOnly); var arguments = message.Arguments; for (var i = 0; i < arguments.Count; i++) { var type = arguments.GetValueType(i); object value; switch (type) { case CefValueType.Null: value = null; break; case CefValueType.String: value = arguments.GetString(i); break; case CefValueType.Int: value = arguments.GetInt(i); break; case CefValueType.Double: value = arguments.GetDouble(i); break; case CefValueType.Bool: value = arguments.GetBool(i); break; default: value = null; break; } Console.WriteLine(" [{0}] ({1}) = {2}", i, type, value); } } //var handled = MessageRouter.OnProcessMessageReceived(browser, sourceProcess, message); //if (handled) return true; if (message.Name == "myMessage2") { return(true); } var message2 = CefProcessMessage.Create("myMessage2"); frame.SendProcessMessage(CefProcessId.Renderer, message2); Console.WriteLine("Sending myMessage2 to renderer process = {0}"); var message3 = CefProcessMessage.Create("myMessage3"); frame.SendProcessMessage(CefProcessId.Browser, message3); Console.WriteLine("Sending myMessage3 to browser process = {0}"); return(false); }
private static void TestV8ValueTypes(CefFrame frame) { var sb = new StringBuilder(); CefV8Context context = frame.V8Context; if (!context.Enter()) { return; } try { sb.Append("typeof 1 = ").Append(context.Eval("1", null).Type).AppendLine(); sb.Append("typeof true = ").Append(context.Eval("true", null).Type).AppendLine(); sb.Append("typeof 'string' = ").Append(context.Eval("'string'", null).Type).AppendLine(); sb.Append("typeof 2.2 = ").Append(context.Eval("2.2", null).Type).AppendLine(); sb.Append("typeof null = ").Append(context.Eval("null", null).Type).AppendLine(); sb.Append("typeof new Object() = ").Append(context.Eval("new Object()", null).Type).AppendLine(); sb.Append("typeof undefined = ").Append(context.Eval("undefined", null).Type).AppendLine(); sb.Append("typeof new Date() = ").Append(context.Eval("new Date()", null).Type).AppendLine(); sb.Append("(window == window) = ").Append(context.Eval("window", null) == context.Eval("window", null)).AppendLine(); } finally { context.Exit(); } var message = new CefProcessMessage("MessageBox.Show"); message.ArgumentList.SetString(0, sb.ToString()); frame.SendProcessMessage(CefProcessId.Browser, message); }
// Returns the new request ID. private int SendQuery(CefBrowser browser, long frameId, int contextId, string request, bool persistent, CefV8Value successCallback, CefV8Value failureCallback) { Helpers.RequireRendererThread(); var requestId = _requestIdGenerator.GetNextId(); var info = new RequestInfo { Persistent = persistent, SuccessCallback = successCallback, FailureCallback = failureCallback, }; _browserRequestInfoMap.Add(browser.Identifier, new KeyValuePair <int, int>(contextId, requestId), info); var message = CefProcessMessage.Create(_queryMessageName); var args = message.Arguments; args.SetInt(0, Helpers.Int64GetLow(frameId)); args.SetInt(1, Helpers.Int64GetHigh(frameId)); args.SetInt(2, contextId); args.SetInt(3, requestId); args.SetString(4, request); args.SetBool(5, persistent); CefFrame frame = browser.GetFrame(frameId); frame?.SendProcessMessage(CefProcessId.Browser, message); args.Dispose(); message.Dispose(); return(requestId); }
public object SendRequest(XrayAction action, XrayHandle thisArg, params object[] args) { var sqi = new ScriptableRequestInfo(); CefProcessMessage msg = null; try { msg = new CefProcessMessage(CefNetApplication.XrayRequestKey); CefListValue argList = msg.ArgumentList; if (!argList.SetSize(3 + args.Length)) { throw new InvalidOperationException(); } argList.SetInt(0, sqi.RequestId); argList.SetInt(1, (int)action); argList.SetBinary(2, ValidateXrayHandle(thisArg).ToCfxBinaryValue()); AppendArgs(argList, 3, args); CefFrame frame = this.Frame; if (!frame.IsValid || frame.Identifier != _frameId) { throw new ObjectDeadException(); } frame.SendProcessMessage(CefProcessId.Renderer, msg); sqi.Wait(); return(sqi.GetResult()); } finally { msg?.Dispose(); sqi.Dispose(); } }
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception) { var message = default(CefProcessMessage); try { CefListValue args; switch (name) { case ReportDocumentSize: message = CefProcessMessage.Create("document-size-response"); using (args = message.Arguments) { args.SetInt(0, arguments[0].GetIntValue()); args.SetInt(1, arguments[1].GetIntValue()); Frame.SendProcessMessage(CefProcessId.Browser, message); } returnValue = null; exception = null; return(true); default: returnValue = null; exception = null; return(false); } } finally { message?.Dispose(); } }
//public void SendProcessMessage(CefProcessId processId, CefFrame frame, BridgeMessage message) //{ // var msg = CefProcessMessage.Create(PROCESS_MESSAGE_BRIDGE_MESSAGE); // var json = message.ToJson(); // msg.Arguments.SetBinary(0, CefBinaryValue.Create(Encoding.UTF8.GetBytes(json))); // frame.SendProcessMessage(processId, msg); // msg.Dispose(); //} public static void SendProcessMessage(CefProcessId processId, CefFrame frame, BridgeMessage message) { var msg = CefProcessMessage.Create(PROCESS_MESSAGE_BRIDGE_MESSAGE); var json = message.ToJson(); msg.Arguments.SetBinary(0, CefBinaryValue.Create(Encoding.UTF8.GetBytes(json))); frame.SendProcessMessage(processId, msg); msg.Dispose(); }
private bool SendCancel(CefBrowser browser, long frameId, int contextId, int requestId) { Helpers.RequireRendererThread(); var browserId = browser.Identifier; int cancelCount = 0; if (requestId != CefMessageRouter.ReservedId) { // Cancel a single request. bool removed = false; var info = GetRequestInfo(browserId, contextId, requestId, true, ref removed); if (info != null) { Debug.Assert(removed); info.Dispose(); cancelCount = 1; } } else { // Cancel all requests with the specified context ID. BrowserRequestInfoMap.Visitor visitor = (int vBrowserId, KeyValuePair <int, int> vInfoId, RequestInfo vInfo, ref bool vRemove) => { if (vInfoId.Key == contextId) { vRemove = true; vInfo.Dispose(); cancelCount++; } return(true); }; _browserRequestInfoMap.FindAll(browserId, visitor); } if (cancelCount > 0) { var message = CefProcessMessage.Create(_cancelMessageName); var args = message.Arguments; args.SetInt(0, contextId); args.SetInt(1, requestId); CefFrame frame = browser.GetFrame(frameId); frame?.SendProcessMessage(CefProcessId.Browser, message); return(true); } return(false); }
private void SendQuerySuccess(CefFrame frame, int contextId, int requestId, string response) { var message = CefProcessMessage.Create(_queryMessageName); var args = message.Arguments; args.SetInt(0, contextId); args.SetInt(1, requestId); args.SetBool(2, true); // Indicates a success result. args.SetString(3, response); frame?.SendProcessMessage(CefProcessId.Renderer, message); args.Dispose(); message.Dispose(); }
private void SendQueryFailure(CefFrame frame, int contextId, int requestId, int errorCode, string errorMessage) { var message = CefProcessMessage.Create(_queryMessageName); var args = message.Arguments; args.SetInt(0, contextId); args.SetInt(1, requestId); args.SetBool(2, false); // Indicates a failure result. args.SetInt(3, errorCode); args.SetString(4, errorMessage); frame?.SendProcessMessage(CefProcessId.Renderer, message); args.Dispose(); message.Dispose(); }
public void ReleaseObject(XrayHandle handle) { if (CefNetApplication.ProcessType == ProcessType.Renderer) { if (CefApi.CurrentlyOn(CefThreadId.Renderer)) { handle.Release(); } else { CefApi.PostTask(CefThreadId.Renderer, new V8CallTask(handle.Release)); } return; } CefBinaryValue obj = null; CefProcessMessage msg = null; try { if (_frameId != handle.frame) { return; } obj = handle.ToCfxBinaryValue(); msg = new CefProcessMessage(CefNetApplication.XrayReleaseKey); using (CefListValue args = msg.ArgumentList) { if (!args.SetSize(1)) { return; } args.SetBinary(0, obj); } _frame.SendProcessMessage(CefProcessId.Renderer, msg); } finally { if (msg != null) { msg.Dispose(); } if (obj != null) { obj.Dispose(); } } }
private static XrayHandle GetGlobal(CefFrame frame) { if (CefNetApplication.ProcessType == ProcessType.Renderer) { return(GetGlobalInternal(frame)); } else { var sqi = new ScriptableRequestInfo(); CefProcessMessage msg = null; try { msg = new CefProcessMessage(CefNetApplication.XrayRequestKey); using (CefListValue args = msg.ArgumentList) { if (!args.SetSize(3)) { throw new InvalidOperationException(); } args.SetInt(0, sqi.RequestId); args.SetInt(1, (int)XrayAction.GetGlobal); } if (!frame.IsValid) { throw new ObjectDeadException(); } frame.SendProcessMessage(CefProcessId.Renderer, msg); sqi.Wait(); return((XrayHandle)sqi.GetResult()); } finally { msg?.Dispose(); sqi.Dispose(); } } }
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context) { if (frame.IsMain) { // Retrieve the context's window object and install the "vvvvReportDocumentSize" function // used to tell the node about the document size as well as the "vvvvSend" function // used to tell the node about variables computed inside the frame. using (var window = context.GetGlobal()) { var handler = new CustomCallbackHandler(browser, frame); var reportDocumentSizeFunc = CefV8Value.CreateFunction(CustomCallbackHandler.ReportDocumentSize, handler); window.SetValue(CustomCallbackHandler.ReportDocumentSize, reportDocumentSizeFunc); window.SetValue("vvvvQuery", CefV8Value.CreateFunction("vvvvQuery", new DelegatingHandler((name, obj, args) => { const string argForm = "{ request: 'NAME', arguments: {}, onSuccess: function(response) {}, onError: function(error_message) {} }"; string invalidArg = $"vvvvQuery expects one argument of the form {argForm}"; if (args.Length != 1) { throw new Exception($"vvvvQuery expects one argument"); } var x = args[0]; if (x is null || !x.IsObject) { throw new Exception($"The argument must be of the form {argForm}"); } var requestValue = x.GetValue("request"); if (requestValue.IsUndefined) { throw new Exception($"The request entry is missing"); } if (!requestValue.IsString) { throw new Exception($"The request must be a string"); } var onSuccessValue = x.GetValue("onSuccess"); if (!onSuccessValue.IsUndefined && !onSuccessValue.IsFunction) { throw new Exception($"The onSuccess entry must be a function"); } var onErrorValue = x.GetValue("onError"); if (!onErrorValue.IsUndefined && !onErrorValue.IsFunction) { throw new Exception($"The onError entry must be a function"); } var request = requestValue.GetStringValue(); var id = queryCount++; if (queries.TryAdd((request, id), (context, onSuccessValue, onErrorValue))) { using var message = CefProcessMessage.Create("query-request"); message.Arguments.SetString(0, request); message.Arguments.SetInt(1, id); message.Arguments.SetValue(2, ToValue(x.GetValue("arguments"))); frame.SendProcessMessage(CefProcessId.Browser, message); } return(default);
public static void SendGCCollectToRenderer(CefFrame frame) { frame.SendProcessMessage(CefProcessId.Renderer, new CefProcessMessage("call GC.Collect()")); }
public static void SendTestScriptableObjectToRenderer(CefFrame frame) { frame.SendProcessMessage(CefProcessId.Renderer, new CefProcessMessage("test scriptableobject")); }