#include "stdafx.h" #include "CefWebBrowser.h" #include "ScriptException.h" namespace CefSharp { void CefWebBrowser::Load(String^ url) { WaitForInitialized(); pin_ptr charPtr = PtrToStringChars(url); CefString urlStr = charPtr; _loadCompleted->Reset(); _handlerAdapter->GetCefBrowser()->GetMainFrame()->LoadURL(urlStr); } void CefWebBrowser::Stop() { WaitForInitialized(); _handlerAdapter->GetCefBrowser()->StopLoad(); } void CefWebBrowser::Back() { WaitForInitialized(); _handlerAdapter->GetCefBrowser()->GoBack(); } void CefWebBrowser::Forward() { WaitForInitialized(); _handlerAdapter->GetCefBrowser()->GoForward(); } String^ CefWebBrowser::RunScript(String^ script, String^ scriptUrl, int startLine) { WaitForInitialized(); return RunScript(script, scriptUrl, startLine, -1); } String^ CefWebBrowser::RunScript(String^ script, String^ scriptUrl, int startLine, int timeout) { WaitForInitialized(); _jsError = nullptr; _jsResult = nullptr; script = "(function() {" " try { " " __js_run_done(" + script + ");" " } catch(e) {" " __js_run_err(e);" " }" "})();"; pin_ptr charPtr = PtrToStringChars(script); CefString scriptStr = charPtr; charPtr = PtrToStringChars(scriptUrl); CefString scriptUrlStr = charPtr; _handlerAdapter->GetCefBrowser()->GetMainFrame()->ExecuteJavaScript(scriptStr, scriptUrlStr, startLine); if(!_runJsFinished->WaitOne(timeout)) { throw gcnew TimeoutException(L"Timed out waiting for JavaScript to return"); } if(_jsError == nullptr) { return _jsResult; } throw gcnew ScriptException(_jsError); } void CefWebBrowser::OnInitialized() { BeginInvoke(gcnew Action(this, &CefWebBrowser::OnSizeChanged), EventArgs::Empty); _browserReady->Set(); } void CefWebBrowser::OnHandleCreated(EventArgs^ e) { if (DesignMode == false) { _handlerAdapter = new HandlerAdapter(this); CefRefPtr ptr = _handlerAdapter.get(); pin_ptr charPtr = PtrToStringChars(_address); CefString urlStr = charPtr; CefWindowInfo windowInfo; HWND hWnd = static_cast(Handle.ToPointer()); RECT rect; GetClientRect(hWnd, &rect); windowInfo.SetAsChild(hWnd, rect); CefBrowser::CreateBrowser(windowInfo, false, static_cast>(ptr), urlStr); } } void CefWebBrowser::OnSizeChanged(EventArgs^ e) { if (DesignMode == false && IsReady) { HWND hWnd = static_cast(Handle.ToPointer()); RECT rect; GetClientRect(hWnd, &rect); HDWP hdwp = BeginDeferWindowPos(1); HWND browserHwnd = _handlerAdapter->GetBrowserHwnd(); hdwp = DeferWindowPos(hdwp, browserHwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER); EndDeferWindowPos(hdwp); } } void CefWebBrowser::SetTitle(String^ title) { _title = title; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"Title")); } void CefWebBrowser::SetAddress(String^ address) { _address = address; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"Address")); } void CefWebBrowser::SetNavState(bool isLoading, bool canGoBack, bool canGoForward) { if(isLoading != _isLoading) { _isLoading = isLoading; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"IsLoading")); } if(canGoBack != _canGoBack) { _canGoBack = canGoBack; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"CanGoBack")); } if(canGoForward != _canGoForward) { _canGoForward = canGoForward; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"CanGoForward")); } } void CefWebBrowser::AddFrame(CefRefPtr frame) { _loadCompleted->AddCount(); } void CefWebBrowser::FrameLoadComplete(CefRefPtr frame) { _loadCompleted->Signal(); } void CefWebBrowser::WaitForLoadCompletion() { WaitForLoadCompletion(-1); } void CefWebBrowser::WaitForLoadCompletion(int timeout) { _loadCompleted->Wait(timeout); } void CefWebBrowser::SetJsResult(const CefString& result) { _jsResult = gcnew String(result.c_str()); _runJsFinished->Set(); } void CefWebBrowser::SetJsError(const CefString& error) { _jsError = gcnew String(error.c_str()); _runJsFinished->Set(); } void CefWebBrowser::RaiseConsoleMessage(String^ message, String^ source, int line) { ConsoleMessage(this, gcnew ConsoleMessageEventArgs(message, source, line)); } void CefWebBrowser::WaitForInitialized() { if(IsReady) return; // TODO: risk of infinite lock _browserReady->WaitOne(); } }