Skip to content

Commit

Permalink
Enhancement - Simplify Setting of RequestContext Proxy (#3250)
Browse files Browse the repository at this point in the history
Core - Simplify Setting of Proxy for new RequestContext

- Throw exception if requestContext.CanSetPreference returns false.
Proxy likely set via command line args.
- Add IRequestContext.SetPreferenceAsync
-  Test - Add SelfHosted Proxy
Use Titanium.Web.Proxy to self host a proxy for testing proxy settings
Only run Proxy Tests when run locally
- Add RequestContext Builder Fluent API
  • Loading branch information
amaitland authored Oct 13, 2020
1 parent fdba655 commit 3eca8bd
Show file tree
Hide file tree
Showing 24 changed files with 875 additions and 51 deletions.
14 changes: 14 additions & 0 deletions CefSharp.Core.RefAssembly/CefSharp.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public virtual void ClearCertificateExceptions(CefSharp.ICompletionCallback call
public virtual void ClearHttpAuthCredentials(CefSharp.ICompletionCallback callback) { }
public virtual bool ClearSchemeHandlerFactories() { throw null; }
public virtual void CloseAllConnections(CefSharp.ICompletionCallback callback) { }
public static CefSharp.RequestContextBuilder Configure() { throw null; }
public static CefSharp.IRequestContext CreateContext(CefSharp.IRequestContext other, CefSharp.IRequestContextHandler requestContextHandler) { throw null; }
public virtual bool DidLoadExtension(string extensionId) { throw null; }
public void Dispose() { }
Expand All @@ -273,6 +274,19 @@ public virtual void PurgePluginListCache(bool reloadPages) { }
public virtual System.Threading.Tasks.Task<CefSharp.ResolveCallbackResult> ResolveHostAsync(System.Uri origin) { throw null; }
public virtual bool SetPreference(string name, object value, out string error) { throw null; }
}
public partial class RequestContextBuilder
{
public RequestContextBuilder() { }
public CefSharp.IRequestContext Create() { throw null; }
public CefSharp.RequestContextBuilder OnInitialize(System.Action<CefSharp.IRequestContext> action) { throw null; }
public CefSharp.RequestContextBuilder PersistUserPreferences() { throw null; }
public CefSharp.RequestContextBuilder WithCachePath(string cachePath) { throw null; }
public CefSharp.RequestContextBuilder WithPreference(string name, object value) { throw null; }
public CefSharp.RequestContextBuilder WithProxyServer(string host) { throw null; }
public CefSharp.RequestContextBuilder WithProxyServer(string host, int? port) { throw null; }
public CefSharp.RequestContextBuilder WithProxyServer(string scheme, string host, int? port) { throw null; }
public CefSharp.RequestContextBuilder WithSharedSettings(CefSharp.IRequestContext other) { throw null; }
}
public partial class RequestContextSettings : System.IDisposable
{
public RequestContextSettings() { }
Expand Down
7 changes: 3 additions & 4 deletions CefSharp.Core/Cef.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ namespace CefSharp
FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE));

//Allows us to execute Tasks on the CEF UI thread in CefSharp.dll
CefThread::UiThreadTaskFactory = UIThreadTaskFactory;
CefThread::CurrentOnUiThreadDelegate = gcnew Func<bool>(&CurrentOnUiThread); ;
CefThread::Initialize(UIThreadTaskFactory, gcnew Func<bool>(&CurrentOnUiThread));

//To allow FolderSchemeHandlerFactory to access GetMimeType we pass in a Func
CefSharp::SchemeHandler::FolderSchemeHandlerFactory::GetMimeTypeDelegate = gcnew Func<String^, String^>(&GetMimeType);
Expand Down Expand Up @@ -497,8 +496,8 @@ namespace CefSharp
UIThreadTaskFactory = nullptr;
IOThreadTaskFactory = nullptr;
FileThreadTaskFactory = nullptr;
CefThread::UiThreadTaskFactory = nullptr;
CefThread::CurrentOnUiThreadDelegate = nullptr;

CefThread::Shutdown();

for each(IDisposable^ diposable in Enumerable::ToList(_disposables))
{
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Core/CefSharp.Core.netcore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<ClCompile Include="Request.cpp" />
<ClCompile Include="RequestContext.cpp" />
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
<ClCompile Include="RequestContextBuilder.cpp" />
<ClCompile Include="Stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
Expand Down Expand Up @@ -302,6 +303,7 @@
<ClInclude Include="PostData.h" />
<ClInclude Include="PostDataElement.h" />
<ClInclude Include="Request.h" />
<ClInclude Include="RequestContextBuilder.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="UrlRequest.h" />
<ClInclude Include="WindowInfo.h" />
Expand Down
6 changes: 6 additions & 0 deletions CefSharp.Core/CefSharp.Core.netcore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RequestContextBuilder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
Expand Down Expand Up @@ -328,6 +331,9 @@
<ClInclude Include="Internals\CefRegistrationWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RequestContextBuilder.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefFrameWrapper.h">
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
<ClCompile Include="Request.cpp" />
<ClCompile Include="RequestContext.cpp" />
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
<ClCompile Include="RequestContextBuilder.cpp" />
<ClCompile Include="Stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
Expand Down Expand Up @@ -307,6 +308,7 @@
<ClInclude Include="PostData.h" />
<ClInclude Include="PostDataElement.h" />
<ClInclude Include="Request.h" />
<ClInclude Include="RequestContextBuilder.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="UrlRequest.h" />
<ClInclude Include="WindowInfo.h" />
Expand Down
6 changes: 6 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RequestContextBuilder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
Expand Down Expand Up @@ -328,6 +331,9 @@
<ClInclude Include="Internals\CefRegistrationWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RequestContextBuilder.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefFrameWrapper.h">
Expand Down
15 changes: 14 additions & 1 deletion CefSharp.Core/RequestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "include\cef_request_context.h"

#include "RequestContextSettings.h"
#include "RequestContextBuilder.h"
#include "Internals\CefRequestContextHandlerAdapter.h"
#include "Internals\CefWrapper.h"

Expand Down Expand Up @@ -112,7 +113,7 @@ namespace CefSharp
/// </summary>
/// <param name="other">shares storage with this RequestContext</param>
/// <param name="requestContextHandler">optional requestContext handler</param>
/// <returns>Returns a nre RequestContext</returns>
/// <returns>Returns a new RequestContext</returns>
static IRequestContext^ CreateContext(IRequestContext^ other, IRequestContextHandler^ requestContextHandler)
{
auto otherRequestContext = static_cast<RequestContext^>(other);
Expand All @@ -122,6 +123,18 @@ namespace CefSharp
return gcnew RequestContext(newContext);
}

/// <summary>
/// Creates a new RequestContextBuilder which can be used to fluently set
/// preferences
/// </summary>
/// <returns>Returns a new RequestContextBuilder</returns>
static RequestContextBuilder^ Configure()
{
auto builder = gcnew RequestContextBuilder();

return builder;
}

/// <summary>
/// Returns true if this object is pointing to the same context object.
/// </summary>
Expand Down
127 changes: 127 additions & 0 deletions CefSharp.Core/RequestContextBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright © 2020 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 "RequestContextBuilder.h"
#include "RequestContext.h"

namespace CefSharp
{
IRequestContext^ RequestContextBuilder::Create()
{
if (_otherContext != nullptr)
{
return gcnew RequestContext(_otherContext, _handler);
}

if(_settings != nullptr)
{
return gcnew RequestContext(_settings, _handler);
}

return gcnew RequestContext(_handler);
}

RequestContextBuilder^ RequestContextBuilder::OnInitialize(Action<IRequestContext^>^ action)
{
if (_handler == nullptr)
{
_handler = gcnew RequestContextHandler();
}

_handler->OnInitialize(action);

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithPreference(String^ name, Object^ value)
{
if (_handler == nullptr)
{
_handler = gcnew RequestContextHandler();
}

_handler->SetPreferenceOnContextInitialized(name, value);

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ host)
{
if (_handler == nullptr)
{
_handler = gcnew RequestContextHandler();
}

_handler->SetProxyOnContextInitialized(host, Nullable<int>());

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ host, Nullable<int> port)
{
if (_handler == nullptr)
{
_handler = gcnew RequestContextHandler();
}

_handler->SetProxyOnContextInitialized(host, port);

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ scheme, String^ host, Nullable<int> port)
{
if (_handler == nullptr)
{
_handler = gcnew RequestContextHandler();
}

_handler->SetProxyOnContextInitialized(scheme, host, port);

return this;
}

RequestContextBuilder^ RequestContextBuilder::PersistUserPreferences()
{
ThrowExceptionIfContextAlreadySet();

if (_settings == nullptr)
{
_settings = gcnew RequestContextSettings();
}

_settings->PersistUserPreferences = true;

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithCachePath(String^ cachePath)
{
ThrowExceptionIfContextAlreadySet();

if (_settings == nullptr)
{
_settings = gcnew RequestContextSettings();
}

_settings->CachePath = cachePath;

return this;
}

RequestContextBuilder^ RequestContextBuilder::WithSharedSettings(IRequestContext^ other)
{
if (other == nullptr)
{
throw gcnew ArgumentNullException("other");
}

ThrowExceptionIfCustomSettingSpecified();

_otherContext = other;

return this;
}
}
Loading

0 comments on commit 3eca8bd

Please sign in to comment.