diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index f8a74eca6..b1644c5d4 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -224,7 +224,6 @@ - @@ -253,7 +252,6 @@ - diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index ef7967b7f..0879635f5 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -41,9 +41,6 @@ Source Files - - Source Files - Source Files @@ -115,9 +112,6 @@ Header Files - - Header Files - Header Files diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 5e21ec21a..c45e849e6 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -8,13 +8,14 @@ #include "Internals/CefWebPluginInfoWrapper.h" #include "Internals/CefContextMenuParamsWrapper.h" #include "Internals/JavascriptBinding/BindingHandler.h" -#include "Internals/RequestResponse.h" #include "ClientAdapter.h" #include "Cef.h" #include "DownloadAdapter.h" #include "StreamAdapter.h" +#include "include/wrapper/cef_stream_resource_handler.h" using namespace std; +using namespace CefSharp; using namespace CefSharp::Internals::JavascriptBinding; namespace CefSharp @@ -194,7 +195,7 @@ namespace CefSharp _browserControl->OnLoadError(StringUtils::ToClr(failedUrl), (CefErrorCode)errorCode, StringUtils::ToClr(errorText)); } - bool ClientAdapter:: OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool isRedirect) + bool ClientAdapter::OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool isRedirect) { IRequestHandler^ handler = _browserControl->RequestHandler; if (handler == nullptr) @@ -244,10 +245,46 @@ namespace CefSharp } } + // Called on the IO thread before a resource is loaded. To allow the resource + // to load normally return NULL. To specify a handler for the resource return + // a CefResourceHandler object. The |request| object should not be modified in + // this callback. + CefRefPtr ClientAdapter::GetResourceHandler(CefRefPtr browser, CefRefPtr frame, CefRefPtr request) + { + IRequestHandler^ handler = _browserControl->RequestHandler; + + if (handler == nullptr) + { + return NULL; + } + + auto requestWrapper = gcnew CefRequestWrapper(request); + + auto resourceHandler = handler->GetResourceHandler(_browserControl, requestWrapper); + + if(resourceHandler != nullptr) + { + auto mimeType = StringUtils::ToNative(resourceHandler->MimeType); + auto statusText = StringUtils::ToNative(resourceHandler->StatusText); + + CefRefPtr streamAdapter = new StreamAdapter(resourceHandler->Stream); + + CefRefPtr stream = CefStreamReader::CreateForHandler(static_cast>(streamAdapter)); + if (stream.get()) + { + CefResponse::HeaderMap map = SchemeHandlerWrapper::ToHeaderMap(resourceHandler->Headers); + + //TODO: Investigate crash when using full response + //return new CefStreamResourceHandler(resourceHandler->StatusCode, statusText, mimeType, map, stream); + return new CefStreamResourceHandler(mimeType, stream); + } + } + + return NULL; + } + bool ClientAdapter::OnBeforeResourceLoad(CefRefPtr browser, CefRefPtr frame, CefRefPtr request) { - // TOOD: Try to support with CEF3; seems quite difficult because the method signature has changed greatly with many parts - // seemingly MIA... IRequestHandler^ handler = _browserControl->RequestHandler; if (handler == nullptr) @@ -255,35 +292,18 @@ namespace CefSharp return false; } - CefRequestWrapper^ wrapper = gcnew CefRequestWrapper(request); - RequestResponse^ requestResponse = gcnew RequestResponse(wrapper); + auto requestWrapper = gcnew CefRequestWrapper(request); + auto response = gcnew Response(); - bool ret = handler->OnBeforeResourceLoad(_browserControl, requestResponse); + bool ret = handler->OnBeforeResourceLoad(_browserControl, requestWrapper, response); - if (requestResponse->Action == ResponseAction::Redirect) + if (response->Action == ResponseAction::Redirect) { - request->SetURL(StringUtils::ToNative(requestResponse->RedirectUrl)); + request->SetURL(StringUtils::ToNative(response->RedirectUrl)); } - else if (requestResponse->Action == ResponseAction::Respond) + else if (response->Action == ResponseAction::Cancel) { - throw gcnew NotImplementedException("Respond is not yet supported."); - - //resourceStream = CefStreamReader::CreateForHandler(static_cast>(adapter)); - //response->SetMimeType(StringUtils::ToNative(requestResponse->MimeType)); - //response->SetStatus(requestResponse->StatusCode); - //response->SetStatusText(StringUtils::ToNative(requestResponse->StatusText)); - - //CefResponse::HeaderMap map; - - //if (requestResponse->ResponseHeaders != nullptr) - //{ - // for each (KeyValuePair^ kvp in requestResponse->ResponseHeaders) - // { - // map.insert(pair(StringUtils::ToNative(kvp->Key),StringUtils::ToNative(kvp->Value))); - // } - //} - - //response->SetHeaderMap(map); + return true; } return ret; @@ -340,42 +360,6 @@ namespace CefSharp return handled; } - // TODO: Investigate how we can support in CEF3. - /* - void ClientAdapter::OnResourceResponse(CefRefPtr browser, const CefString& url, CefRefPtr response, CefRefPtr& filter) - { - IRequestHandler^ handler = _browserControl->RequestHandler; - if (handler == nullptr) - { - return; - } - - WebHeaderCollection^ headers = gcnew WebHeaderCollection(); - CefResponse::HeaderMap map; - response->GetHeaderMap(map); - for (CefResponse::HeaderMap::iterator it = map.begin(); it != map.end(); ++it) - { - try - { - headers->Add(StringUtils::ToClr(it->first), StringUtils::ToClr(it->second)); - } - catch (Exception ^ex) - { - // adding a header with invalid characters can cause an exception to be - // thrown. we will drop those headers for now. - // we could eventually use reflection to call headers->AddWithoutValidate(). - } - } - - handler->OnResourceResponse( - _browserControl, - StringUtils::ToClr(url), - response->GetStatus(), - StringUtils::ToClr(response->GetStatusText()), - StringUtils::ToClr(response->GetMimeType()), - headers); - }*/ - void ClientAdapter::OnBeforeContextMenu(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, CefRefPtr model) { diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index c05b1d500..37ecaebb2 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -82,6 +82,7 @@ namespace CefSharp virtual DECL void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE; // CefRequestHandler + virtual DECL CefRefPtr GetResourceHandler(CefRefPtr browser, CefRefPtr frame, CefRefPtr request) OVERRIDE; virtual DECL bool OnBeforeResourceLoad(CefRefPtr browser, CefRefPtr frame, CefRefPtr request) OVERRIDE; virtual DECL bool GetAuthCredentials(CefRefPtr browser, CefRefPtr frame, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr callback) OVERRIDE; diff --git a/CefSharp.Core/Internals/RequestResponse.cpp b/CefSharp.Core/Internals/RequestResponse.cpp deleted file mode 100644 index 9e1a8b97c..000000000 --- a/CefSharp.Core/Internals/RequestResponse.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. -// -// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. - -#include "Stdafx.h" -#include "RequestResponse.h" - -namespace CefSharp -{ - namespace Internals - { - void RequestResponse::Cancel() - { - _action = ResponseAction::Cancel; - } - - void RequestResponse::Redirect(String^ url) - { - _redirectUrl = url; - _action = ResponseAction::Redirect; - } - - void RequestResponse::RespondWith(Stream^ stream, String^ mimeType) - { - RespondWith(stream, mimeType, "OK", 200, nullptr); - } - - void RequestResponse::RespondWith(Stream^ stream, String^ mimeType, String^ statusText, int statusCode, NameValueCollection^ responseHeaders) - { - if (String::IsNullOrEmpty(mimeType)) - { - throw gcnew ArgumentException("must provide a mime type", "mimeType"); - } - - if (stream == nullptr) - { - throw gcnew ArgumentNullException("stream"); - } - - _responseStream = stream; - _mimeType = mimeType; - _statusText = statusText; - _statusCode = statusCode; - _responseHeaders = responseHeaders; - - _action = ResponseAction::Respond; - } - } -} \ No newline at end of file diff --git a/CefSharp.Core/Internals/RequestResponse.h b/CefSharp.Core/Internals/RequestResponse.h deleted file mode 100644 index 2486a646f..000000000 --- a/CefSharp.Core/Internals/RequestResponse.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. -// -// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. - -#pragma once - -#include "Stdafx.h" - -using namespace System; -using namespace System::IO; -using namespace System::Collections::Specialized; - -namespace CefSharp -{ - namespace Internals - { - enum class ResponseAction - { - Continue, - Cancel, - Redirect, - Respond - }; - - private ref class RequestResponse : IRequestResponse - { - IRequest^ _request; - Stream^ _responseStream; - String^ _mimeType; - String^ _redirectUrl; - ResponseAction _action; - String^ _statusText; - int _statusCode; - NameValueCollection^ _responseHeaders; - - internal: - RequestResponse(IRequest^ request) : - _action(ResponseAction::Continue), - _request(request) - { - } - - property Stream^ ResponseStream { Stream^ get() { return _responseStream; } } - property String^ MimeType { String^ get() { return _mimeType; } } - property String^ StatusText { String^ get() { return _statusText; } } - property int StatusCode { int get() { return _statusCode; } } - property NameValueCollection^ ResponseHeaders { NameValueCollection^ get() { return _responseHeaders; } } - property String^ RedirectUrl { String^ get() { return _redirectUrl; } } - property ResponseAction Action { ResponseAction get() { return _action; } } - - public: - virtual void Cancel(); - virtual property IRequest^ Request { IRequest^ get() { return _request; } } - virtual void Redirect(String^ url); - virtual void RespondWith(Stream^ stream, String^ mimeType); - virtual void RespondWith(Stream^ stream, String^ mimeType, String^ statusText, int statusCode, NameValueCollection^ responseHeaders); - }; - } -} \ No newline at end of file diff --git a/CefSharp.Core/Internals/StreamAdapter.cpp b/CefSharp.Core/Internals/StreamAdapter.cpp index d17553d91..8af2857c3 100644 --- a/CefSharp.Core/Internals/StreamAdapter.cpp +++ b/CefSharp.Core/Internals/StreamAdapter.cpp @@ -12,13 +12,15 @@ namespace CefSharp StreamAdapter::~StreamAdapter() { _stream->Close(); + _stream = nullptr; } size_t StreamAdapter::Read(void* ptr, size_t size, size_t n) { AutoLock lock_scope(_syncRoot); - try { + try + { array^ buffer = gcnew array(n * size); int ret = _stream->Read(buffer, 0, n); pin_ptr src = &buffer[0]; @@ -33,17 +35,18 @@ namespace CefSharp int StreamAdapter::Seek(int64 offset, int whence) { - SeekOrigin seekOrigin; + System::IO::SeekOrigin seekOrigin; + switch (whence) { case SEEK_CUR: - seekOrigin = SeekOrigin::Current; + seekOrigin = System::IO::SeekOrigin::Current; break; case SEEK_END: - seekOrigin = SeekOrigin::End; + seekOrigin = System::IO::SeekOrigin::End; break; case SEEK_SET: - seekOrigin = SeekOrigin::Begin; + seekOrigin = System::IO::SeekOrigin::Begin; break; default: return -1; @@ -70,5 +73,10 @@ namespace CefSharp { return _stream->Length == _stream->Position; } + + bool StreamAdapter::MayBlock() + { + return true; + } } } \ No newline at end of file diff --git a/CefSharp.Core/Internals/StreamAdapter.h b/CefSharp.Core/Internals/StreamAdapter.h index 70c882389..8e8b77675 100644 --- a/CefSharp.Core/Internals/StreamAdapter.h +++ b/CefSharp.Core/Internals/StreamAdapter.h @@ -26,6 +26,7 @@ namespace CefSharp virtual int Seek(int64 offset, int whence); virtual int64 Tell(); virtual int Eof(); + virtual bool MayBlock(); IMPLEMENT_REFCOUNTING(StreamAdapter); }; diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index 416b9411c..67f72ec31 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -471,16 +471,10 @@ namespace CefSharp *(CefBrowserSettings*)browserSettings->_internalBrowserSettings, NULL); } - void OnPaint(IntPtr^ sourceHandle) + void Resize(int width, int height) { - HWND hWnd = static_cast(sourceHandle->ToPointer()); - RECT rect; - GetClientRect(hWnd, &rect); - HDWP hdwp = BeginDeferWindowPos(1); - HWND browserHwnd = _renderClientAdapter->GetBrowserHwnd(); - hdwp = DeferWindowPos(hdwp, browserHwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER); - EndDeferWindowPos(hdwp); + SetWindowPos(browserHwnd, NULL, 0, 0, width, height, SWP_NOZORDER); } void RegisterJsObject(String^ name, Object^ object) diff --git a/CefSharp.Core/SchemeHandlerWrapper.h b/CefSharp.Core/SchemeHandlerWrapper.h index b1f349651..a88355d9a 100644 --- a/CefSharp.Core/SchemeHandlerWrapper.h +++ b/CefSharp.Core/SchemeHandlerWrapper.h @@ -29,8 +29,6 @@ namespace CefSharp int _contentLength; bool _closeStream; int SizeFromStream(); - CefResponse::HeaderMap ToHeaderMap(NameValueCollection^ headers); - public: SchemeHandlerWrapper(ISchemeHandler^ handler) : _handler(handler) @@ -47,6 +45,8 @@ namespace CefSharp virtual bool ReadResponse(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr callback); virtual void Cancel(); + static CefResponse::HeaderMap ToHeaderMap(NameValueCollection^ headers); + IMPLEMENT_REFCOUNTING(SchemeHandlerWrapper); }; diff --git a/CefSharp.Example/RequestHandler.cs b/CefSharp.Example/RequestHandler.cs index d4853f011..545ca7f07 100644 --- a/CefSharp.Example/RequestHandler.cs +++ b/CefSharp.Example/RequestHandler.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using System.Text; namespace CefSharp.Example { @@ -21,17 +19,20 @@ namespace CefSharp.Example // TODO: Add your own code here for handling scenarios where a plugin crashed, for one reason or another. } - bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse) + bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response) + { + return false; + } + + public ResourceHandler GetResourceHandler(IWebBrowser browser, IRequest request) { - IRequest request = requestResponse.Request; if (request.Url.StartsWith(ResourceUrl.ToString())) { - Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes( - "

Success

This document is loaded from a System.IO.Stream

")); - requestResponse.RespondWith(resourceStream, "text/html"); + const string responseBody = "

Success

This document is loaded from a System.IO.Stream

"; + return ResourceHandler.FromString(responseBody); } - return false; + return null; } bool IRequestHandler.GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password) diff --git a/CefSharp.WinForms.Example/BrowserForm.cs b/CefSharp.WinForms.Example/BrowserForm.cs index 8b5c85dd9..697bcb2ee 100644 --- a/CefSharp.WinForms.Example/BrowserForm.cs +++ b/CefSharp.WinForms.Example/BrowserForm.cs @@ -40,7 +40,11 @@ namespace CefSharp.WinForms.Example { Dock = DockStyle.Fill }; - + + //This call isn't required for the sample to work. + //It's sole purpose is to demonstrate that #553 has been resolved. + browser.CreateControl(); + tabPage.Controls.Add(browser); if (insertIndex == null) diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index e8c3951e3..c8349b801 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -53,18 +53,11 @@ namespace CefSharp.WinForms Cef.AddDisposable(this); Address = address; - Paint += OnPaint; - - //Redraw on Resize so Cef is notified and updates accordingly - SetStyle(ControlStyles.ResizeRedraw, true); - Dock = DockStyle.Fill; } protected override void Dispose(bool disposing) { - Paint -= OnPaint; - Cef.RemoveDisposable(this); if (disposing) @@ -82,6 +75,8 @@ namespace CefSharp.WinForms { IsBrowserInitialized = true; + ResizeBrowser(); + var handler = IsBrowserInitializedChanged; if (handler != null) @@ -349,12 +344,18 @@ namespace CefSharp.WinForms return taskStringVisitor.Task; } - private void OnPaint(object sender, PaintEventArgs e) + protected override void OnSizeChanged(EventArgs e) { - // Size is 0x0 when we are on a modeless Form which is minimized. - if (!Size.IsEmpty && managedCefBrowserAdapter != null) + base.OnSizeChanged(e); + + ResizeBrowser(); + } + + private void ResizeBrowser() + { + if (IsBrowserInitialized && managedCefBrowserAdapter != null) { - managedCefBrowserAdapter.OnPaint(Handle); + managedCefBrowserAdapter.Resize(Width, Height); } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 5bef8c830..b439c3d43 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -109,6 +109,9 @@ + + + @@ -131,7 +134,7 @@ - + diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs index df10479fe..672a8bc34 100644 --- a/CefSharp/IRequestHandler.cs +++ b/CefSharp/IRequestHandler.cs @@ -29,13 +29,19 @@ namespace CefSharp /// Called before a resource request is loaded. /// /// the browser object - /// the request response object - can be modified in this callback + /// the request object - can be modified in this callback. + /// the request object - can be modified in this callback. /// To cancel loading of the resource return true or false o allow the resource to load normally. - bool OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse); - - // TODO: Investigate how we can support in CEF3. - //void OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, WebHeaderCollection headers); + bool OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response); + /// + /// Called before a resource is loaded. To specify a handler for the resource return a ResourceHandler object + /// + /// the browser object + /// the request object - cannot be modified in this callback + /// To allow the resource to load normally return NULL otherwise return an instance of ResourceHandler with a valid stream + ResourceHandler GetResourceHandler(IWebBrowser browser, IRequest request); + /// /// Called when the browser needs credentials from the user. /// diff --git a/CefSharp/IRequestResponse.cs b/CefSharp/IResponse.cs similarity index 58% rename from CefSharp/IRequestResponse.cs rename to CefSharp/IResponse.cs index d50bf3958..d95b7ede5 100644 --- a/CefSharp/IRequestResponse.cs +++ b/CefSharp/IResponse.cs @@ -2,24 +2,14 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. -using System.IO; - namespace CefSharp { - public interface IRequestResponse + public interface IResponse { - // TODO: Improve these comments. - /// cancel the request, return nothing void Cancel(); - /// the current request - IRequest Request { get; } - /// respond with redirection to the provided URL void Redirect(string url); - - /// respond with data from Stream - void RespondWith(Stream stream, string mimeType); } } diff --git a/CefSharp/ResourceHandler.cs b/CefSharp/ResourceHandler.cs new file mode 100644 index 000000000..f5af6c55c --- /dev/null +++ b/CefSharp/ResourceHandler.cs @@ -0,0 +1,36 @@ +// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System.Collections.Specialized; +using System.IO; +using System.Text; + +namespace CefSharp +{ + public class ResourceHandler + { + public string MimeType { get; set; } + public Stream Stream { get; set; } + public int StatusCode { get; set; } + public string StatusText { get; set; } + public NameValueCollection Headers { get; set; } + + public ResourceHandler() + { + StatusCode = 200; + StatusText = "OK"; + MimeType = "text/html"; + } + + public static ResourceHandler FromFileName(string fileName) + { + return new ResourceHandler { Stream = File.OpenRead(fileName) }; + } + + public static ResourceHandler FromString(string text) + { + return new ResourceHandler { Stream = new MemoryStream(Encoding.UTF8.GetBytes(text)) }; + } + } +} diff --git a/CefSharp/Response.cs b/CefSharp/Response.cs new file mode 100644 index 000000000..a5eda1fca --- /dev/null +++ b/CefSharp/Response.cs @@ -0,0 +1,30 @@ +// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; + +namespace CefSharp +{ + public class Response : IResponse + { + public String RedirectUrl { get; private set; } + public ResponseAction Action { get; private set; } + + public Response() + { + Action = ResponseAction.Continue; + } + + public void Cancel() + { + Action = ResponseAction.Cancel; + } + + public void Redirect(String url) + { + RedirectUrl = url; + Action = ResponseAction.Redirect; + } + } +} diff --git a/CefSharp/ResponseAction.cs b/CefSharp/ResponseAction.cs new file mode 100644 index 000000000..9571a014e --- /dev/null +++ b/CefSharp/ResponseAction.cs @@ -0,0 +1,13 @@ +// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp +{ + public enum ResponseAction + { + Continue, + Cancel, + Redirect + } +}