public override void OnFilterUrl(NativeCallArgs args) { string reqUrl = args.GetArgAsString(0); if (reqUrl.StartsWith("http://localhost/index2")) { //eg. how to fix request url args.SetOutput(0, 1); //return url-in ascii form var utf8Buffer = Encoding.ASCII.GetBytes("http://localhost/index2.html"); args.SetOutput(1, utf8Buffer); } }
public override void OnCefQuery(NativeCallArgs args, QueryRequestArgs reqArgs) { string frameUrl = reqArgs.GetFrameUrl(); string getRequest = reqArgs.GetRequest(); string result = "hello!"; byte[] resultBuffer = Encoding.UTF8.GetBytes(result); args.SetOutput(0, resultBuffer); }
void Test001(int id, IntPtr argsPtr) { #if DEBUG //if (Cef3Binder.s_dbugIsRendererProcess) //{ // System.Diagnostics.Debugger.Break(); //} #endif var nativeCallArgs = new NativeCallArgs(argsPtr); nativeCallArgs.SetOutput(0, "hello from managed side !"); }
public override void OnRequestForBinaryResource(NativeCallArgs args) { string url = args.GetArgAsString(0); if (url == "http://localhost/hello_img" && File.Exists("prepare.jpg")) { //load sample image and the send to client byte[] img = File.ReadAllBytes("prepare.jpg"); int imgLen = img.Length; //TODO: review here, who will destroy this mem IntPtr unmanagedPtr = Marshal.AllocHGlobal(imgLen); Marshal.Copy(img, 0, unmanagedPtr, imgLen); args.SetOutput(0, 1); args.UnsafeSetOutput(1, unmanagedPtr, imgLen); args.SetOutputAsAsciiString(2, "image/jpeg"); } else { //intercept any request //args.SetOutput(0, 1); //args.SetOutputAsAsciiString(1, "OKOK!!!!"); //args.SetOutputAsAsciiString(2, "text/plain"); } }
void MxCallBack(int id, IntPtr argsPtr) { switch ((MyCefMsg)id) { case MyCefMsg.CEF_MSG_ClientHandler_NotifyBrowserCreated: { IsBrowserCreated = true; } break; case MyCefMsg.CEF_MSG_ClientHandler_NotifyBrowserClosing: { } break; case MyCefMsg.CEF_MSG_ClientHandler_NotifyBrowserClosed: { if (this.devForm != null) { this.devForm.Close(); ((IDisposable)this.devForm).Dispose(); this.devForm = null; } if (this.BrowserDisposed != null) { this.BrowserDisposed(this, EventArgs.Empty); } } break; case MyCefMsg.CEF_MSG_ClientHandler_OnBeforeContextMenu: { } break; case MyCefMsg.CEF_MSG_ClientHandler_BeforeDownload: { //handle download path here NativeCallArgs metArgs = new NativeCallArgs(argsPtr); string pathName = metArgs.GetArgAsString(2); } break; case MyCefMsg.CEF_MSG_ClientHandler_DownloadUpdated: { //this version we notify back //when NativeCallArgs metArgs = new NativeCallArgs(argsPtr); if (browserProcessListener != null) { browserProcessListener.OnDownloadCompleted(metArgs); } } break; case MyCefMsg.CEF_MSG_ClientHandler_OnBeforePopup: { NativeCallArgs args = new NativeCallArgs(argsPtr); //open new form with specific url string url = args.GetArgAsString(0); Cef3Binder.SafeUIInvoke(() => { IWindowForm form = Cef3Binder.CreateNewBrowserWindow(800, 600); form.Show(); //and navigate to a specific url }); } break; case MyCefMsg.CEF_MSG_ClientHandler_OnConsoleMessage: { //console.log ... if (browserProcessListener != null) { NativeCallArgs args = new NativeCallArgs(argsPtr); browserProcessListener.OnConsoleLog(args); } } break; case MyCefMsg.CEF_MSG_ClientHandler_ShowDevTools: { //show dev tools Cef3Binder.SafeUIInvoke(() => { IWindowForm newPopupForm = Cef3Binder.CreateNewBrowserWindow(800, 600); newPopupForm.Show(); }); } break; case MyCefMsg.CEF_MSG_ClientHandler_OnLoadError: { //load page error //ui process var args = new NativeCallArgs(argsPtr); IntPtr cefBrowser = args.GetArgAsNativePtr(0); IntPtr cefFrame = args.GetArgAsNativePtr(1); int errorCode = args.GetArgAsInt32(2); //error code string errorText = args.GetArgAsString(3); //errorText string failedUrl = args.GetArgAsString(4); //failedUrl //--------------------------- //load error page LoadErrorPage(cefBrowser, cefFrame, errorCode, errorText, failedUrl); } break; case MyCefMsg.CEF_MSG_ClientHandler_OnCertError: { var args = new NativeCallArgs(argsPtr); string certErrMsg = args.GetArgAsString(0); args.SetOutput(0, 1); //true } break; case MyCefMsg.CEF_MSG_ClientHandler_ExecCustomProtocol: { //disable all protocol var args = new NativeCallArgs(argsPtr); if (browserProcessListener != null) { browserProcessListener.OnExecProtocol(args); } else { args.SetOutput(0, 0); //disable all protocol } } break; case MyCefMsg.CEF_MSG_ClientHandler_SetResourceManager: { //setup resource mx if (browserProcessListener != null) { var args = new NativeCallArgs(argsPtr); var resourceMx = new NativeResourceMx(args.GetArgAsNativePtr(0)); browserProcessListener.OnAddResourceMx(resourceMx); } } break; case MyCefMsg.CEF_MSG_RequestUrlFilter2: { //filter url name if (browserProcessListener != null) { var args = new NativeCallArgs(argsPtr); browserProcessListener.OnFilterUrl(args); } } break; case MyCefMsg.CEF_MSG_BinaryResouceProvider_OnRequest: { //request for binary resource if (browserProcessListener != null) { var args = new NativeCallArgs(argsPtr); browserProcessListener.OnRequestForBinaryResource(args); } } break; //------------------------------ //eg. from cefQuery --> case MyCefMsg.CEF_MSG_OnQuery: { if (browserProcessListener != null) { var args = new NativeCallArgs(argsPtr); QueryRequestArgs reqArgs = QueryRequestArgs.CreateRequest(args.GetArgAsNativePtr(0)); browserProcessListener.OnCefQuery(args, reqArgs); } } break; //------------------------------ case MyCefMsg.CEF_MSG_ClientHandler_OnPreKeyEvent: //on PreKey { // Console.WriteLine("on pre key"); NavigateTo("https://html5test.com"); } break; //------------------------------ case MyCefMsg.CEF_MSG_ClientHandler_NotifyTitle: { //title changed var args = new NativeCallArgs(argsPtr); string newtitle = args.GetArgAsString(0); // Console.WriteLine("title changed:" + newtitle); } break; case MyCefMsg.CEF_MSG_ClientHandler_NotifyAddress: { //address changed var args = new NativeCallArgs(argsPtr); string newtitle = args.GetArgAsString(0); // Console.WriteLine("address changed:" + newtitle); } break; //------------------------------ case MyCefMsg.CEF_MSG_OSR_Render: { //receive rendere msg var args = new NativeCallArgs(argsPtr); //copy bits buffer and store to files if (cefOsrListener != null) { cefOsrListener.OnRender(args); } } break; } }