**OnAfterCreated:
GetLoadHandler:
OnLoadingStateChange:Loading
[0817/112657.533:ERROR:broker_host.cc(54)] Failed to rewrite one or more handles to broker client.**
Can you help me ? Thanks
The following is dll code:
- Code: Select all
class ClientApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler {
public:
ClientApp() {};
CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() OVERRIDE
{
return this;
}
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()OVERRIDE{ return this; }
void OnWebKitInitialized() OVERRIDE {}
IMPLEMENT_REFCOUNTING(ClientApp);
};
class ClientHandler : public CefClient, public CefLifeSpanHandler, public CefLoadHandler , public CefDisplayHandler {
public:
ClientHandler() {};
void SetJsInvokeNativeFunc(JsInvokeNativeFunc jsInvokeNativeFunc) { _jsInvokeNativeFunc = jsInvokeNativeFunc; }
CefRefPtr<CefBrowser> GetBrowser()
{
return m_Browser;
}
CefWindowHandle GetBrowserHwnd()
{
return m_BrowserHwnd;
}
// CefClient methods
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
{
return this;
}
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE
{
return this;
}
// CefDisplayHandler methods
void OnAddressChange(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,const CefString& url) OVERRIDE
{
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnAddressChange", url.ToString().c_str());
}
void OnTitleChange(CefRefPtr<CefBrowser> browser,const CefString& title) OVERRIDE
{
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnTitleChange", title.ToString().c_str());
}
// Virutal on CefLifeSpanHandler
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE { return false; }
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
{
if (!m_Browser.get()) {
// We need to keep the main child window, but not popup windows
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnAfterCreated", "");
m_Browser = browser;
m_BrowserHwnd = browser->GetHost()->GetWindowHandle();
}
}
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
{
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnBeforeClose", "");
if (m_BrowserHwnd == browser->GetHost()->GetWindowHandle()) {
// Free the browser pointer so that the browser can be destroyed
m_Browser = NULL;
}
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE
{
#ifdef __LOG__
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("GetLoadHandler", "");
#endif
return this;
}
//CefLoadHandler
virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE
{
#ifdef __LOG__
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnLoadingStateChange", isLoading ? "Loading" : "Finishing");
#endif
//CefString url = browser->GetMainFrame()->GetURL();
}
virtual void OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, TransitionType transition_type) OVERRIDE
{
#ifdef __LOG__
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnLoadStart", frame->GetURL().ToString().c_str());
#endif
}
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) OVERRIDE
{
#ifdef __LOG__
if (_jsInvokeNativeFunc != NULL) _jsInvokeNativeFunc("OnLoadEnd", frame->GetURL().ToString().c_str());
#endif
}
protected:
JsInvokeNativeFunc _jsInvokeNativeFunc;
// The child browser window
CefRefPtr<CefBrowser> m_Browser;
// The child browser window handle
CefWindowHandle m_BrowserHwnd;
// /
// Macro that provides a reference counting implementation for classes extending
// CefBase.
// /
IMPLEMENT_REFCOUNTING(ClientHandler);
};
CefMainArgs _main_args;
CefRefPtr<ClientApp> _app(new ClientApp);
void InitRender()
{
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(_main_args, _app.get(), NULL);
if (exit_code >= 0) {
exit(exit_code);
}
}
void InitBrowser(CefWindowHandle parentHandle, int width, int height, const char* url, JsInvokeNativeFunc jsInvokeNativeFunc)
{
try
{
_jsInvokeNativeFunc = jsInvokeNativeFunc;
CefSettings settings;
//settings.single_process = true;
settings.log_severity = LOGSEVERITY_ERROR;
CefInitialize(_main_args, settings, _app.get(), NULL);
CefWindowInfo info;
CefBrowserSettings b_settings;
CefRefPtr<ClientHandler> clientHandler = new ClientHandler();
clientHandler->SetJsInvokeNativeFunc(jsInvokeNativeFunc);
CefRefPtr<CefClient> client(clientHandler);
RECT rect;
rect.left = 0; rect.top = 0; rect.right = width, rect.bottom = height;
info.SetAsChild(parentHandle, rect);
CefBrowserHost::CreateBrowser(info, client.get(), url, b_settings, NULL);
CefRunMessageLoop();
CefShutdown();
}
catch (std::exception exception)
{
jsInvokeNativeFunc("InitBrowserException", exception.what());
_init = false;
}
}
then in wpf Main function :
- Code: Select all
class Program
{
const string CefDllFile = "CefDll.dll";
[DllImport(CefDllFile, CallingConvention = CallingConvention.Cdecl)]
extern static void InitRender();
[STAThread]
public static void Main(string[] args)
{
InitRender();
App app = new App();
app.InitializeComponent();
app.Run();
}
}
in window code:
- Code: Select all
[DllImport(CefDllFile, CallingConvention = CallingConvention.Cdecl)]
extern static void InitBrowser(System.IntPtr windowHandle, int height, int width, string url, InvokeDelegate invokeFunc);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSource hwndSource = PresentationSource.FromVisual(mainGrid) as HwndSource;
if (hwndSource != null)
{
IntPtr handle = hwndSource.Handle;
InitBrowser(handle, 1280, 1024, "http://www.google.com", InteropInvoke);
}
}