public override void OnRender(NativeCallArgs args) { //if not set to 0 //images not render to native cef win return; if (n > 100) { return; } //---------------------- IntPtr rawBitBuffer = args.GetArgAsNativePtr(0); int width = args.GetArgAsInt32(1); int height = args.GetArgAsInt32(2); unsafe { using (Bitmap bmp = new Bitmap(width, height)) { var bmpdata = bmp.LockBits( new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb); CopyMemory(bmpdata.Scan0, rawBitBuffer, width * height * 4); bmp.UnlockBits(bmpdata); bmp.Save("d:\\WImageTest\\snap02" + (n++) + ".jpg"); } } }
public override void OnConsoleLog(NativeCallArgs args) { string msg = args.GetArgAsString(0); string src = args.GetArgAsString(1); string location = args.GetArgAsString(2); Console.WriteLine(msg); }
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); }
public void GetSource(Action <string> strCallback) { //keep alive callback InternalGetSource((id, nativePtr) => { var args = new NativeCallArgs(nativePtr); strCallback(args.GetArgAsString(0)); }); }
public void GetText(Action <string> strCallback) { //keep alive callback InternalGetText((id, nativePtr) => { var args = new NativeCallArgs(nativePtr); strCallback(args.GetArgAsString(0)); }); //Cef3Binder.MyCefDomGetTextWalk(this.myCefBrowser, strCallback); }
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 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 void PrintToPdf(string filename) { MyCefCallback cb = null; cb = new MyCefCallback((id, args) => { //remove after finish var metArg = new NativeCallArgs(args); int isOK = metArg.GetArgAsInt32(1); tmpCallbacks.Remove(cb); }); tmpCallbacks.Add(cb); // Cef3Binder.MyCefPrintToPdf(_myCefBw.ptr, IntPtr.Zero, filename, cb); }
public void PrintToPdf(string pdfConfig, string filename) { IntPtr nativePdfConfig = Cef3Binder.MyCefCreatePdfPrintSetting(pdfConfig); MyCefCallback cb = null; cb = new MyCefCallback((id, args) => { //remove after finish var metArg = new NativeCallArgs(args); int isOK = metArg.GetArgAsInt32(1); tmpCallbacks.Remove(cb); }); tmpCallbacks.Add(cb); // Cef3Binder.MyCefPrintToPdf(_myCefBw.ptr, nativePdfConfig, filename, cb); }
/// <summary> /// handle native reqiest , this is called by native side. /// </summary> /// <param name="id"></param> /// <param name="argsPtr"></param> void HandleNativeReq(int met_id, IntPtr argsPtr) { if (renderProcessHandler == null) { //check if the render process handler is create or not //this method is called from native side //must check here! renderProcessHandler = new RenderProcessHandler(); } //main raw msg switch table if ((met_id >> 16) > 0) { //built in object CefNativeRequestHandlers.HandleNativeReq_I0(renderProcessHandler, met_id, argsPtr); return; } //this is custom msg dbugRenderProcessLog.WriteLine("custom_msg"); switch ((MyCefMsg)met_id) { default: break; case MyCefMsg.CEF_MSG_CefSettings_Init: { InitCefSettings(new CefSettings(argsPtr)); } break; case MyCefMsg.CEF_MSG_MainContext_GetConsoleLogPath: { NativeCallArgs nat1 = new NativeCallArgs(argsPtr); nat1.SetOutputAsAsciiString(0, ReferencePaths.LOG_PATH); } break; case MyCefMsg.CEF_MSG_OSR_Render: { } break; } }
public override void OnWebKitInitialized(NativeCallArgs nativeCallArgs) { //sample!!! string extensionCode = "var test;" + "if (!test)" + " test = {};" + "(function() {" + " test.myfunc = function() {" + "return 2;" + //" native function myfunc();" + //" return myfunc();" + " };" + "})();"; //test regsiter extension CefBinder2.RegisterCefExtension("v8/test", extensionCode); }
public override void OnRender(NativeCallArgs args) { //if not set to 0 //images not render to native cef win return; //just return if (n > 100) { return; } //---------------------- //INIT_MY_MET_ARGS(metArgs, 3) //MyCefSetVoidPtr2(&vargs[1], buffer); //MyCefSetInt32(&vargs[2], width); //MyCefSetInt32(&vargs[3], height); IntPtr rawBitBuffer = args.GetArgAsNativePtr(1); int width = args.GetArgAsInt32(2); int height = args.GetArgAsInt32(3); unsafe { using (Bitmap bmp = new Bitmap(width, height)) { var bmpdata = bmp.LockBits( new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb); CopyMemory(bmpdata.Scan0, rawBitBuffer, width * height * 4); bmp.UnlockBits(bmpdata); bmp.Save(ReferencePaths.SAVE_IMAGE_PATH + (n++) + ".jpg"); } } }
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"); } }
public override void OnDownloadCompleted(NativeCallArgs args) { string downloadFullPath = args.GetArgAsString(2); Console.WriteLine("download complete :" + downloadFullPath); }
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; } }
void MxCallBack(int id, IntPtr argsPtr) { switch ((MyCefMsg)id) { case MyCefMsg.CEF_MSG_ClientHandler_OnBeforePopup: { NativeCallArgs args = new NativeCallArgs(argsPtr); string url = args.GetArgAsString(0); Cef3Binder.SafeUIInvoke(() => { IWindowForm popupWin = Cef3Binder.CreateNewBrowserWindow(600, 450); popupWin.Show(); }); } break; case MyCefMsg.CEF_MSG_ClientHandler_BeforeDownload: { } 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_CefSettings_Init: { InitCefSettings(new CefSettings(argsPtr)); } break; case MyCefMsg.CEF_MSG_MainContext_GetConsoleLogPath: { NativeCallArgs nat1 = new NativeCallArgs(argsPtr); nat1.SetOutputAsAsciiString(0, "d:\\WImageTest\\cef_console.log"); } break; case MyCefMsg.CEF_MSG_OSR_Render: { //not visit here? } break; case MyCefMsg.CEF_MSG_RenderDelegate_OnContextCreated: { //client app callback //eg. from RenderClientApp //in render process *** //we can register external methods for window object here. //NativeMethods.MessageBox(IntPtr.Zero, id.ToString(), "NN2", 0); if (renderProcessListener != null) { NativeCallArgs args = new NativeCallArgs(argsPtr); MyCefContextArgs cefContextArgs = new MyCefContextArgs(args); //var clientRenderApp = new NativeRendererApp(args.GetArgAsNativePtr(0)); //var browser = new NativeBrowser(args.GetArgAsNativePtr(1)); //var context = new NativeJsContext(args.GetArgAsNativePtr(2)); renderProcessListener.OnContextCreated(cefContextArgs); } } break; case MyCefMsg.CEF_MSG_RenderDelegate_OnContextReleased: { if (renderProcessListener != null) { NativeCallArgs args = new NativeCallArgs(argsPtr); MyCefContextArgs cefContextArgs = new MyCefContextArgs(args); //var clientRenderApp = new NativeRendererApp(args.GetArgAsNativePtr(0)); //var browser = new NativeBrowser(args.GetArgAsNativePtr(1)); //var context = new NativeJsContext(args.GetArgAsNativePtr(2)); renderProcessListener.OnContextReleased(cefContextArgs); } } break; case MyCefMsg.CEF_MSG_RenderDelegate_OnWebKitInitialized: { if (renderProcessListener != null) { NativeCallArgs args = new NativeCallArgs(argsPtr); renderProcessListener.OnWebKitInitialized(args); } } break; } }
public static void DisposeNativeCallArgs(NativeCallArgs nativeCallArgs) { Cef3Binder.DisposeMethodArgs(nativeCallArgs._argPtr); }
void HandleNativeReq(int met_id, IntPtr argsPtr) { //main raw msg switch table if ((met_id >> 16) > 0) { //built in object CefNativeRequestHandlers.HandleNativeReq_I0(myBwHandler, met_id, argsPtr); return; } //else this is custom msg switch ((MyCefMsg)met_id) { default: break; 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_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; //---- //TODO: review here again //case MyCefMsg.CEF_MSG_ClientHandler_SetResourceManager: // { // //setup resource mx // if (browserProcessListener != null) // { // //INIT_MY_MET_ARGS(metArgs, 1) // // MyCefSetVoidPtr2(&vargs[1], resource_manager_); // var args = new NativeCallArgs(argsPtr); // var resourceMx = new NativeResourceMx(args.GetArgAsNativePtr(1)); // 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); //} //TODO: review here again //QueryRequestArgs queryReq; //memset(&queryReq, 0, sizeof(QueryRequestArgs)); //queryReq.browser = browser.get(); //queryReq.frame = frame.get(); //queryReq.query_id = query_id; //MyCefStringHolder mystr; //mystr.value = request; //queryReq.request = &mystr; //queryReq.persistent = persistent; //queryReq.callback = callback.get(); //MethodArgs args; //memset(&args, 0, sizeof(MethodArgs)); //args.SetArgAsNativeObject(0, &queryReq); //this->mcallback_(CEF_MSG_OnQuery, &args); ////auto result = args.ReadOutputAsString(0); //CefString cefstr = args.ReadOutputAsString(0); //callback->Success(cefstr); } break; //------------------------------ case MyCefMsg.CEF_MSG_ClientHandler_NotifyTitle: { //INIT_MY_MET_ARGS(metArgs, 1) //SetCefStringToJsValue2(&vargs[1], string); //title changed var args = new NativeCallArgs(argsPtr); string newtitle = args.GetArgAsString(1); // Console.WriteLine("title changed:" + newtitle); } break; case MyCefMsg.CEF_MSG_ClientHandler_NotifyAddress: { //INIT_MY_MET_ARGS(metArgs, 1) //SetCefStringToJsValue2(&vargs[1], string); //address changed var args = new NativeCallArgs(argsPtr); string newtitle = args.GetArgAsString(1); // 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; } }