protected override bool ProcessRequest(CefRequest request, CefCallback callback) { if (request == null) { throw new ArgumentNullException("request"); } if (callback == null) { throw new ArgumentNullException("callback"); } Url = request.Url; Path = Application.GetNativeRequestPath(Url); Logger.Debug(string.Format("Native request: {0}", Url)); try { var nativeFunction = FindNativeFunction(Path); if (nativeFunction != null) { ResponseValue = nativeFunction(request); } else { Exception = new NativeNotFoundException(Path); } } catch (Exception e) { ResponseValue = null; Exception = e; } callback.Continue(); return(true); }
/// <summary> /// Processes the request for the view. /// </summary> /// <param name="request"></param> /// <param name="callback"></param> /// <returns></returns> protected override bool ProcessRequest(CefRequest request, CefCallback callback) { if (request == null) { throw new ArgumentNullException("request"); } if (callback == null) { throw new ArgumentNullException("callback"); } Url = request.Url; Path = BaseMainApplication.Current.GetNativeRequestPath(Url); try { var nativeMethod = FindNativeMethod(Path); if (nativeMethod != null) { ResponseValue = nativeMethod(request); } else { Exception = new NativeNotFoundException(Path); } } catch (Exception e) { ResponseValue = null; Exception = e; } callback.Continue(); return(true); }
public bool ProcessMessage(CefBrowser browser, CefProcessId sourceProcess, CefProcessMessage processMessage) { if (processMessage.Name == "native") { var message = MessageUtility.DeserializeMessage <CallNative>(processMessage); BaseMainApplication.Current.InvokeOnMainAsync(() => { var returnData = (object)null; var exception = (Exception)null; // native found if (ProcessMessages.ContainsKey(message.Data.Name)) { try { returnData = ProcessMessages[message.Data.Name](message.Data.Json); } catch (Exception e) { exception = e; returnData = null; } } else { exception = new NativeNotFoundException(message.Data.Name); } // callback if (message.CallbackId != null) { var nativeResponse = new NativeResponse(); if (exception != null) { nativeResponse.Exception = ExceptionUtility.CreateJavascriptException(exception); nativeResponse.Type = NativeResponseType.Exception; nativeResponse.Value = null; } else { if (returnData == Undefined.Value) { nativeResponse.Exception = null; nativeResponse.Type = NativeResponseType.Undefined; nativeResponse.Value = null; } else { nativeResponse.Exception = null; nativeResponse.Type = NativeResponseType.Value; nativeResponse.Value = returnData; } } var returnJson = JsonUtility.SerializeToJson(nativeResponse); MessageUtility.SendMessage(browser, "native", message.CallbackId, returnJson); } }).ContinueWith(t => { GeneralLog.Error("Native call exception.", t.Exception); }, TaskContinuationOptions.OnlyOnFaulted); return(true); } return(false); }
private void ProcessMessageNative(CefBrowser browser, CefProcessId sourceProcess, CefProcessMessage processMessage) { var callNative = MessageUtility.DeserializeMessage <CallNative>(processMessage); Application.Current.InvokeOnMainAsync(() => { object returnData = null; Exception exception = null; NativeFunctionDelegate handler; NativeFunctionDelegates.TryGetValue(callNative.Name, out handler); // function call if (handler != null) { try { returnData = handler(callNative.Json); } catch (Exception e) { exception = e; } } else { exception = new NativeNotFoundException(callNative.Name); } // callback if (callNative.CallbackId != null) { var nativeResponse = new NativeResponse(); if (exception != null) { nativeResponse.Exception = ExceptionUtility.CreateJavascriptException(exception); nativeResponse.Type = NativeResponseType.Exception; nativeResponse.Value = null; } else { if (returnData == Value.Undefined) { nativeResponse.Exception = null; nativeResponse.Type = NativeResponseType.Undefined; nativeResponse.Value = null; } else { nativeResponse.Exception = null; nativeResponse.Type = NativeResponseType.Value; nativeResponse.Value = returnData; } } var returnJson = JsonUtility.SerializeToJson(nativeResponse); MessageUtility.SendMessage(CefProcessId.Renderer, browser, "native", new CallNativeResult { JsonResult = returnJson, CallbackId = callNative.CallbackId }); } }).ContinueWith(t => { Logger.Error("Native call exception.", t.Exception); }, TaskContinuationOptions.OnlyOnFaulted); }