#include "stdafx.h" #include "CefWpfWebBrowser.h" #include "JsTask.h" #include "ScriptException.h" namespace CefSharp { void CefWpfWebBrowser::Load(String^ url) { WaitForInitialized(); _loadCompleted->Reset(); _clientAdapter->GetCefBrowser()->GetMainFrame()->LoadURL(toNative(url)); } void CefWpfWebBrowser::WaitForLoadCompletion() { WaitForLoadCompletion(-1); } void CefWpfWebBrowser::WaitForLoadCompletion(int timeout) { _loadCompleted->Wait(timeout); } void CefWpfWebBrowser::Stop() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->StopLoad(); } void CefWpfWebBrowser::Back() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->GoBack(); } void CefWpfWebBrowser::Forward() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->GoForward(); } void CefWpfWebBrowser::Reload() { Reload(false); } void CefWpfWebBrowser::Reload(bool ignoreCache) { WaitForInitialized(); if(ignoreCache) { _clientAdapter->GetCefBrowser()->ReloadIgnoreCache(); } else { _clientAdapter->GetCefBrowser()->Reload(); } } String^ CefWpfWebBrowser::RunScript(String^ script, String^ scriptUrl, int startLine) { WaitForInitialized(); return RunScript(script, scriptUrl, startLine, -1); } String^ CefWpfWebBrowser::RunScript(String^ script, String^ scriptUrl, int startLine, int timeout) { WaitForInitialized(); _jsError = false; _jsResult = nullptr; /* script = "(function() {" " try { " " __js_run_done(" + script + ");" " } catch(e) {" " __js_run_err(e);" " }" "})();"; */ CefRefPtr task = new JsTask(this, toNative(script), toNative(scriptUrl), startLine); _clientAdapter->GetCefBrowser()->GetMainFrame()->ExecuteJavaScriptTask(static_cast>(task)); if(!_runJsFinished->WaitOne(timeout)) { throw gcnew TimeoutException(L"Timed out waiting for JavaScript to return"); } if(_jsError == false) { return _jsResult; } throw gcnew ScriptException("An error occurred during javascript execution"); } void CefWpfWebBrowser::OnInitialized() { _browserInitialized->Set(); } void CefWpfWebBrowser::SetTitle(String^ title) { _title = title; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"Title")); } void CefWpfWebBrowser::SetAddress(String^ address) { _address = address; PropertyChanged(this, gcnew PropertyChangedEventArgs(L"Address")); } void CefWpfWebBrowser::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 CefWpfWebBrowser::AddFrame(CefRefPtr frame) { _loadCompleted->AddCount(); } void CefWpfWebBrowser::FrameLoadComplete(CefRefPtr frame) { _loadCompleted->Signal(); } void CefWpfWebBrowser::SetJsResult(String^ result) { _jsResult = result; _runJsFinished->Set(); } void CefWpfWebBrowser::SetJsError() { _jsError = true; _runJsFinished->Set(); } void CefWpfWebBrowser::RaiseConsoleMessage(String^ message, String^ source, int line) { ConsoleMessage(this, gcnew ConsoleMessageEventArgs(message, source, line)); } void CefWpfWebBrowser::WaitForInitialized() { if (IsInitialized) return; // TODO: risk of infinite lock _browserInitialized->WaitOne(); } void CefWpfWebBrowser::OnApplyTemplate() { ContentControl::OnApplyTemplate(); _image = (Image^)GetTemplateChild("PART_Image"); if (_image == nullptr) { Content = _image = gcnew Image(); _image->Stretch = Stretch::None; _image->HorizontalAlignment = System::Windows::HorizontalAlignment::Left; _image->VerticalAlignment = System::Windows::VerticalAlignment::Top; } } Size CefWpfWebBrowser::ArrangeOverride(Size size) { try { _clientAdapter->GetCefBrowser()->SetSize(PET_VIEW, (int)size.Width, (int)size.Height); } catch (...) { // ArrangeOverride may be called one or more times before Cef is initialized } return ContentControl::ArrangeOverride(size); } void CefWpfWebBrowser::OnGotFocus(RoutedEventArgs^ e) { _clientAdapter->GetCefBrowser()->SendFocusEvent(true); ContentControl::OnGotFocus(e); } void CefWpfWebBrowser::OnLostFocus(RoutedEventArgs^ e) { _clientAdapter->GetCefBrowser()->SendFocusEvent(false); ContentControl::OnLostFocus(e); } void CefWpfWebBrowser::OnMouseMove(MouseEventArgs^ e) { Point point = e->GetPosition(this); _clientAdapter->GetCefBrowser()->SendMouseMoveEvent((int)point.X, (int)point.Y, false); } void CefWpfWebBrowser::OnMouseWheel(MouseWheelEventArgs^ e) { Point point = e->GetPosition(this); _clientAdapter->GetCefBrowser()->SendMouseWheelEvent((int)point.X, (int)point.Y, e->Delta); } void CefWpfWebBrowser::OnMouseLeave(MouseEventArgs^ e) { _clientAdapter->GetCefBrowser()->SendMouseMoveEvent(0, 0, true); } void CefWpfWebBrowser::OnMouseDown(MouseButtonEventArgs^ e) { Keyboard::Focus(this); // XXX: temporary Point point = e->GetPosition(this); CefBrowser::MouseButtonType mbt; if (e->RightButton == MouseButtonState::Pressed) { mbt = CefBrowser::MouseButtonType::MBT_RIGHT; } else if (e->LeftButton == MouseButtonState::Pressed) { mbt = CefBrowser::MouseButtonType::MBT_LEFT; } _clientAdapter->GetCefBrowser()->SendMouseClickEvent((int)point.X, (int)point.Y, mbt, false, 1); } void CefWpfWebBrowser::OnMouseUp(MouseButtonEventArgs^ e) { Point point = e->GetPosition(this); CefBrowser::MouseButtonType mbt; if (e->RightButton == MouseButtonState::Pressed) { mbt = CefBrowser::MouseButtonType::MBT_RIGHT; } else if (e->LeftButton == MouseButtonState::Pressed) { mbt = CefBrowser::MouseButtonType::MBT_LEFT; } _clientAdapter->GetCefBrowser()->SendMouseClickEvent((int)point.X, (int)point.Y, mbt, true, 1); } void CefWpfWebBrowser::SetCursor(CefCursorHandle cursor) { SafeFileHandle^ handle = gcnew SafeFileHandle((IntPtr)cursor, false); Dispatcher->BeginInvoke(DispatcherPriority::Render, gcnew Action(this, &CefWpfWebBrowser::SetCursor), handle); } void CefWpfWebBrowser::SetCursor(SafeFileHandle^ handle) { Cursor = CursorInteropHelper::Create(handle); } IntPtr CefWpfWebBrowser::SourceHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, bool% handled) { handled = false; switch(message) { case WM_KEYDOWN: case WM_KEYUP: case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_CHAR: case WM_SYSCHAR: case WM_IME_CHAR: if (!IsFocused) { break; } CefBrowser::KeyType type = KT_CHAR; bool sysChar = false, imeChar = false; if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN) { type = KT_KEYDOWN; } else if (message == WM_KEYUP || message == WM_SYSKEYUP) { type = KT_KEYUP; } if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP || message == WM_SYSCHAR) { sysChar = true; } if (message == WM_IME_CHAR) { imeChar = true; } _clientAdapter->GetCefBrowser()->SendKeyEvent(type, wParam.ToInt32(), lParam.ToInt32(), sysChar, imeChar); handled = true; } return IntPtr::Zero; } void CefWpfWebBrowser::SetBuffer(int width, int height, const CefRect& dirtyRect, const void* buffer) { _width = width; _height = height; _buffer = (void *)buffer; Dispatcher->Invoke(DispatcherPriority::Render, gcnew Action(this, &CefWpfWebBrowser::SetBitmap), _bitmap); } // XXX: don't know how to Invoke a parameterless delegate... void CefWpfWebBrowser::SetBitmap(WriteableBitmap^ bitmap) { int length = _width * _height * 4; if (length == 0) { return; } if (!_bitmap || _bitmap->PixelWidth != _width || _bitmap->PixelHeight != _height) { _image->Source = _bitmap = gcnew WriteableBitmap(_width, _height, 96 * _transform.M11, 96 * _transform.M22, PixelFormats::Bgr32, nullptr); } Int32Rect rect; rect.X = 0; rect.Y = 0; rect.Width = _width; rect.Height = _height; _bitmap->WritePixels(rect, (IntPtr)_buffer, length, _bitmap->BackBufferStride); } }