mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 10:22:55 +00:00
222 lines
5.7 KiB
C++
222 lines
5.7 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "CefWebBrowser.h"
|
|
#include "ScriptException.h"
|
|
|
|
namespace CefSharp
|
|
{
|
|
void CefWebBrowser::Load(String^ url)
|
|
{
|
|
WaitForInitialized();
|
|
|
|
_loadCompleted->Reset();
|
|
_handlerAdapter->GetCefBrowser()->GetMainFrame()->LoadURL(convertFromString(url));
|
|
}
|
|
|
|
void CefWebBrowser::Stop()
|
|
{
|
|
WaitForInitialized();
|
|
|
|
_handlerAdapter->GetCefBrowser()->StopLoad();
|
|
}
|
|
|
|
void CefWebBrowser::Back()
|
|
{
|
|
WaitForInitialized();
|
|
|
|
_handlerAdapter->GetCefBrowser()->GoBack();
|
|
}
|
|
|
|
void CefWebBrowser::Forward()
|
|
{
|
|
WaitForInitialized();
|
|
|
|
_handlerAdapter->GetCefBrowser()->GoForward();
|
|
}
|
|
|
|
void CefWebBrowser::Reload()
|
|
{
|
|
Reload(false);
|
|
}
|
|
|
|
void CefWebBrowser::Reload(bool ignoreCache)
|
|
{
|
|
WaitForInitialized();
|
|
|
|
if(ignoreCache)
|
|
{
|
|
_handlerAdapter->GetCefBrowser()->ReloadIgnoreCache();
|
|
}
|
|
else
|
|
{
|
|
_handlerAdapter->GetCefBrowser()->Reload();
|
|
}
|
|
}
|
|
|
|
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);"
|
|
" }"
|
|
"})();";
|
|
|
|
|
|
CefString scriptStr = convertFromString(script);
|
|
CefString scriptUrlStr = convertFromString(scriptUrl);
|
|
|
|
_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<EventArgs^>(this, &CefWebBrowser::OnSizeChanged), EventArgs::Empty);
|
|
_browserInitialized->Set();
|
|
}
|
|
|
|
void CefWebBrowser::OnHandleCreated(EventArgs^ e)
|
|
{
|
|
if (DesignMode == false)
|
|
{
|
|
_handlerAdapter = new HandlerAdapter(this);
|
|
CefRefPtr<HandlerAdapter> ptr = _handlerAdapter.get();
|
|
|
|
CefString urlStr = convertFromString(_address);
|
|
|
|
CefWindowInfo windowInfo;
|
|
|
|
HWND hWnd = static_cast<HWND>(Handle.ToPointer());
|
|
RECT rect;
|
|
GetClientRect(hWnd, &rect);
|
|
windowInfo.SetAsChild(hWnd, rect);
|
|
|
|
CefBrowser::CreateBrowser(windowInfo, false, static_cast<CefRefPtr<CefHandler>>(ptr), urlStr);
|
|
}
|
|
}
|
|
|
|
void CefWebBrowser::OnSizeChanged(EventArgs^ e)
|
|
{
|
|
if (DesignMode == false && IsInitialized)
|
|
{
|
|
HWND hWnd = static_cast<HWND>(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::OnGotFocus(EventArgs^ e)
|
|
{
|
|
if (IsInitialized && !DesignMode)
|
|
{
|
|
_handlerAdapter->GetCefBrowser()->SetFocus(true);
|
|
}
|
|
}
|
|
|
|
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<CefFrame> frame)
|
|
{
|
|
_loadCompleted->AddCount();
|
|
}
|
|
|
|
void CefWebBrowser::FrameLoadComplete(CefRefPtr<CefFrame> frame)
|
|
{
|
|
_loadCompleted->Signal();
|
|
}
|
|
|
|
void CefWebBrowser::WaitForLoadCompletion()
|
|
{
|
|
WaitForLoadCompletion(-1);
|
|
}
|
|
|
|
void CefWebBrowser::WaitForLoadCompletion(int timeout)
|
|
{
|
|
_loadCompleted->Wait(timeout);
|
|
}
|
|
|
|
void CefWebBrowser::SetJsResult(const CefString& result)
|
|
{
|
|
_jsResult = convertToString(result);
|
|
_runJsFinished->Set();
|
|
}
|
|
|
|
void CefWebBrowser::SetJsError(const CefString& error)
|
|
{
|
|
_jsError = convertToString(error);
|
|
_runJsFinished->Set();
|
|
}
|
|
|
|
void CefWebBrowser::RaiseConsoleMessage(String^ message, String^ source, int line)
|
|
{
|
|
ConsoleMessage(this, gcnew ConsoleMessageEventArgs(message, source, line));
|
|
}
|
|
|
|
void CefWebBrowser::WaitForInitialized()
|
|
{
|
|
if (IsInitialized) return;
|
|
|
|
// TODO: risk of infinite lock
|
|
_browserInitialized->WaitOne();
|
|
}
|
|
} |