func toCefStringCopy(s string, out *C.cef_string_t) { var asC *C.char = C.CString(s) defer C.free(unsafe.Pointer(asC)) C.cef_string_from_utf8( asC, C.strlen(asC), C.cefStringCastToCefString16(out), ) }
func CreateBrowser(hwnd unsafe.Pointer, clientHandler ClientHandler, browserSettings BrowserSettings, url string) bool { Logger.Infof("CreateBrowser, url=%s\n", url) // Initialize cef_window_info_t structure. var windowInfo *C.cef_window_info_t windowInfo = (*C.cef_window_info_t)( C.calloc(1, C.sizeof_cef_window_info_t)) FillWindowInfo(windowInfo, hwnd) // url var cefUrl *C.cef_string_t cefUrl = (*C.cef_string_t)( C.calloc(1, C.sizeof_cef_string_t)) var charUrl *C.char = C.CString(url) defer C.free(unsafe.Pointer(charUrl)) C.cef_string_from_utf8(charUrl, C.strlen(charUrl), C.cefStringCastToCefString16(cefUrl)) // Initialize cef_browser_settings_t structure. cefBrowserSettings := browserSettings.toC() // Do not create the browser synchronously using the // cef_browser_host_create_browser_sync() function, as // it is unreliable. Instead obtain browser object in // life_span_handler::on_after_created. In that callback // keep CEF browser objects in a global map (cef window // handle -> cef browser) and introduce // a GetBrowserByWindowHandle() function. This function // will first guess the CEF window handle using for example // WinAPI functions and then search the global map of cef // browser objects. go_AddRef(unsafe.Pointer(clientHandler.GetClientHandlerT().CStruct)) result := C.cef_browser_host_create_browser( windowInfo, clientHandler.GetClientHandlerT().CStruct, cefUrl, cefBrowserSettings, nil, ) return result == C.int(1) }
func Initialize(settings Settings, appHandler AppHandler) int { Logger.Infof("Initialize\n") if _MainArgs == nil { // _MainArgs structure is initialized and filled in ExecuteProcess. // If cef_execute_process is not called, and there is a call // to cef_initialize, then it would result in creation of infinite // number of processes. See Issue 1199 in CEF: // https://code.google.com/p/chromiumembedded/issues/detail?id=1199 Logger.Errorf("ERROR: missing a call to ExecuteProcess\n") return 0 } // Initialize cef_settings_t structure. var cefSettings *C.struct__cef_settings_t cefSettings = (*C.struct__cef_settings_t)( C.calloc(1, C.sizeof_struct__cef_settings_t)) cefSettings.size = C.sizeof_struct__cef_settings_t // cache_path // ---------- if settings.CachePath != "" { Logger.Infof("CachePath=%s\n", settings.CachePath) } var cachePath *C.char = C.CString(settings.CachePath) defer C.free(unsafe.Pointer(cachePath)) C.cef_string_from_utf8(cachePath, C.strlen(cachePath), C.cefStringCastToCefString16(&cefSettings.cache_path)) // log_severity // ------------ cefSettings.log_severity = (C.cef_log_severity_t)(C.int(settings.LogSeverity)) // log_file // -------- if settings.LogFile != "" { Logger.Infof("LogFile=%s\n", settings.LogFile) } var logFile *C.char = C.CString(settings.LogFile) defer C.free(unsafe.Pointer(logFile)) C.cef_string_from_utf8(logFile, C.strlen(logFile), C.cefStringCastToCefString16(&cefSettings.log_file)) // resources_dir_path // ------------------ if settings.ResourcesDirPath == "" && runtime.GOOS != "darwin" { // Setting this path is required for the tests to run fine. cwd, _ := os.Getwd() settings.ResourcesDirPath = cwd } if settings.ResourcesDirPath != "" { Logger.Infof("ResourcesDirPath=%s\n", settings.ResourcesDirPath) } var resourcesDirPath *C.char = C.CString(settings.ResourcesDirPath) defer C.free(unsafe.Pointer(resourcesDirPath)) C.cef_string_from_utf8(resourcesDirPath, C.strlen(resourcesDirPath), C.cefStringCastToCefString16(&cefSettings.resources_dir_path)) // locales_dir_path // ---------------- if settings.LocalesDirPath == "" && runtime.GOOS != "darwin" { // Setting this path is required for the tests to run fine. cwd, _ := os.Getwd() settings.LocalesDirPath = cwd + "/locales" } if settings.LocalesDirPath != "" { Logger.Infof("LocalesDirPath=%s\n", settings.LocalesDirPath) } var localesDirPath *C.char = C.CString(settings.LocalesDirPath) defer C.free(unsafe.Pointer(localesDirPath)) C.cef_string_from_utf8(localesDirPath, C.strlen(localesDirPath), C.cefStringCastToCefString16(&cefSettings.locales_dir_path)) if settings.PersistSessionCookies { cefSettings.persist_session_cookies = 1 } cefSettings.remote_debugging_port = C.int(settings.RemoteDebuggingPort) cefSettings.ignore_certificate_errors = C.int(settings.IgnoreCertificateErrors) // no_sandbox // ---------- cefSettings.no_sandbox = C.int(1) go_AddRef(unsafe.Pointer(_MainArgs)) go_AddRef(unsafe.Pointer(appHandler.GetAppHandlerT().CStruct)) go_AddRef(unsafe.Pointer(_SandboxInfo)) ret := C.cef_initialize(_MainArgs, cefSettings, appHandler.GetAppHandlerT().CStruct, _SandboxInfo) return int(ret) }