Skip to content

Commit 0a8e7d2

Browse files
committed
Enhancement - Simplify Setting of RequestContext Proxy (#3250)
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
1 parent deca495 commit 0a8e7d2

24 files changed

+875
-51
lines changed

CefSharp.Core.RefAssembly/CefSharp.Core.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ public virtual void ClearCertificateExceptions(CefSharp.ICompletionCallback call
253253
public virtual void ClearHttpAuthCredentials(CefSharp.ICompletionCallback callback) { }
254254
public virtual bool ClearSchemeHandlerFactories() { throw null; }
255255
public virtual void CloseAllConnections(CefSharp.ICompletionCallback callback) { }
256+
public static CefSharp.RequestContextBuilder Configure() { throw null; }
256257
public static CefSharp.IRequestContext CreateContext(CefSharp.IRequestContext other, CefSharp.IRequestContextHandler requestContextHandler) { throw null; }
257258
public virtual bool DidLoadExtension(string extensionId) { throw null; }
258259
public void Dispose() { }
@@ -273,6 +274,19 @@ public virtual void PurgePluginListCache(bool reloadPages) { }
273274
public virtual System.Threading.Tasks.Task<CefSharp.ResolveCallbackResult> ResolveHostAsync(System.Uri origin) { throw null; }
274275
public virtual bool SetPreference(string name, object value, out string error) { throw null; }
275276
}
277+
public partial class RequestContextBuilder
278+
{
279+
public RequestContextBuilder() { }
280+
public CefSharp.IRequestContext Create() { throw null; }
281+
public CefSharp.RequestContextBuilder OnInitialize(System.Action<CefSharp.IRequestContext> action) { throw null; }
282+
public CefSharp.RequestContextBuilder PersistUserPreferences() { throw null; }
283+
public CefSharp.RequestContextBuilder WithCachePath(string cachePath) { throw null; }
284+
public CefSharp.RequestContextBuilder WithPreference(string name, object value) { throw null; }
285+
public CefSharp.RequestContextBuilder WithProxyServer(string host) { throw null; }
286+
public CefSharp.RequestContextBuilder WithProxyServer(string host, int? port) { throw null; }
287+
public CefSharp.RequestContextBuilder WithProxyServer(string scheme, string host, int? port) { throw null; }
288+
public CefSharp.RequestContextBuilder WithSharedSettings(CefSharp.IRequestContext other) { throw null; }
289+
}
276290
public partial class RequestContextSettings : System.IDisposable
277291
{
278292
public RequestContextSettings() { }

CefSharp.Core/Cef.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ namespace CefSharp
238238
FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE));
239239

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

244243
//To allow FolderSchemeHandlerFactory to access GetMimeType we pass in a Func
245244
CefSharp::SchemeHandler::FolderSchemeHandlerFactory::GetMimeTypeDelegate = gcnew Func<String^, String^>(&GetMimeType);
@@ -497,8 +496,8 @@ namespace CefSharp
497496
UIThreadTaskFactory = nullptr;
498497
IOThreadTaskFactory = nullptr;
499498
FileThreadTaskFactory = nullptr;
500-
CefThread::UiThreadTaskFactory = nullptr;
501-
CefThread::CurrentOnUiThreadDelegate = nullptr;
499+
500+
CefThread::Shutdown();
502501

503502
for each(IDisposable^ diposable in Enumerable::ToList(_disposables))
504503
{

CefSharp.Core/CefSharp.Core.netcore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<ClCompile Include="Request.cpp" />
246246
<ClCompile Include="RequestContext.cpp" />
247247
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
248+
<ClCompile Include="RequestContextBuilder.cpp" />
248249
<ClCompile Include="Stdafx.cpp">
249250
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
250251
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
@@ -302,6 +303,7 @@
302303
<ClInclude Include="PostData.h" />
303304
<ClInclude Include="PostDataElement.h" />
304305
<ClInclude Include="Request.h" />
306+
<ClInclude Include="RequestContextBuilder.h" />
305307
<ClInclude Include="resource.h" />
306308
<ClInclude Include="UrlRequest.h" />
307309
<ClInclude Include="WindowInfo.h" />

CefSharp.Core/CefSharp.Core.netcore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp">
8484
<Filter>Source Files</Filter>
8585
</ClCompile>
86+
<ClCompile Include="RequestContextBuilder.cpp">
87+
<Filter>Source Files</Filter>
88+
</ClCompile>
8689
</ItemGroup>
8790
<ItemGroup>
8891
<ClInclude Include="vcclr_local.h">
@@ -328,6 +331,9 @@
328331
<ClInclude Include="Internals\CefRegistrationWrapper.h">
329332
<Filter>Header Files</Filter>
330333
</ClInclude>
334+
<ClInclude Include="RequestContextBuilder.h">
335+
<Filter>Header Files</Filter>
336+
</ClInclude>
331337
</ItemGroup>
332338
<ItemGroup>
333339
<ClInclude Include="Internals\CefFrameWrapper.h">

CefSharp.Core/CefSharp.Core.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
<ClCompile Include="Request.cpp" />
251251
<ClCompile Include="RequestContext.cpp" />
252252
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
253+
<ClCompile Include="RequestContextBuilder.cpp" />
253254
<ClCompile Include="Stdafx.cpp">
254255
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
255256
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
@@ -307,6 +308,7 @@
307308
<ClInclude Include="PostData.h" />
308309
<ClInclude Include="PostDataElement.h" />
309310
<ClInclude Include="Request.h" />
311+
<ClInclude Include="RequestContextBuilder.h" />
310312
<ClInclude Include="resource.h" />
311313
<ClInclude Include="UrlRequest.h" />
312314
<ClInclude Include="WindowInfo.h" />

CefSharp.Core/CefSharp.Core.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp">
8484
<Filter>Source Files</Filter>
8585
</ClCompile>
86+
<ClCompile Include="RequestContextBuilder.cpp">
87+
<Filter>Source Files</Filter>
88+
</ClCompile>
8689
</ItemGroup>
8790
<ItemGroup>
8891
<ClInclude Include="vcclr_local.h">
@@ -328,6 +331,9 @@
328331
<ClInclude Include="Internals\CefRegistrationWrapper.h">
329332
<Filter>Header Files</Filter>
330333
</ClInclude>
334+
<ClInclude Include="RequestContextBuilder.h">
335+
<Filter>Header Files</Filter>
336+
</ClInclude>
331337
</ItemGroup>
332338
<ItemGroup>
333339
<ClInclude Include="Internals\CefFrameWrapper.h">

CefSharp.Core/RequestContext.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "include\cef_request_context.h"
1212

1313
#include "RequestContextSettings.h"
14+
#include "RequestContextBuilder.h"
1415
#include "Internals\CefRequestContextHandlerAdapter.h"
1516
#include "Internals\CefWrapper.h"
1617

@@ -112,7 +113,7 @@ namespace CefSharp
112113
/// </summary>
113114
/// <param name="other">shares storage with this RequestContext</param>
114115
/// <param name="requestContextHandler">optional requestContext handler</param>
115-
/// <returns>Returns a nre RequestContext</returns>
116+
/// <returns>Returns a new RequestContext</returns>
116117
static IRequestContext^ CreateContext(IRequestContext^ other, IRequestContextHandler^ requestContextHandler)
117118
{
118119
auto otherRequestContext = static_cast<RequestContext^>(other);
@@ -122,6 +123,18 @@ namespace CefSharp
122123
return gcnew RequestContext(newContext);
123124
}
124125

126+
/// <summary>
127+
/// Creates a new RequestContextBuilder which can be used to fluently set
128+
/// preferences
129+
/// </summary>
130+
/// <returns>Returns a new RequestContextBuilder</returns>
131+
static RequestContextBuilder^ Configure()
132+
{
133+
auto builder = gcnew RequestContextBuilder();
134+
135+
return builder;
136+
}
137+
125138
/// <summary>
126139
/// Returns true if this object is pointing to the same context object.
127140
/// </summary>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#include "Stdafx.h"
6+
#include "RequestContextBuilder.h"
7+
#include "RequestContext.h"
8+
9+
namespace CefSharp
10+
{
11+
IRequestContext^ RequestContextBuilder::Create()
12+
{
13+
if (_otherContext != nullptr)
14+
{
15+
return gcnew RequestContext(_otherContext, _handler);
16+
}
17+
18+
if(_settings != nullptr)
19+
{
20+
return gcnew RequestContext(_settings, _handler);
21+
}
22+
23+
return gcnew RequestContext(_handler);
24+
}
25+
26+
RequestContextBuilder^ RequestContextBuilder::OnInitialize(Action<IRequestContext^>^ action)
27+
{
28+
if (_handler == nullptr)
29+
{
30+
_handler = gcnew RequestContextHandler();
31+
}
32+
33+
_handler->OnInitialize(action);
34+
35+
return this;
36+
}
37+
38+
RequestContextBuilder^ RequestContextBuilder::WithPreference(String^ name, Object^ value)
39+
{
40+
if (_handler == nullptr)
41+
{
42+
_handler = gcnew RequestContextHandler();
43+
}
44+
45+
_handler->SetPreferenceOnContextInitialized(name, value);
46+
47+
return this;
48+
}
49+
50+
RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ host)
51+
{
52+
if (_handler == nullptr)
53+
{
54+
_handler = gcnew RequestContextHandler();
55+
}
56+
57+
_handler->SetProxyOnContextInitialized(host, Nullable<int>());
58+
59+
return this;
60+
}
61+
62+
RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ host, Nullable<int> port)
63+
{
64+
if (_handler == nullptr)
65+
{
66+
_handler = gcnew RequestContextHandler();
67+
}
68+
69+
_handler->SetProxyOnContextInitialized(host, port);
70+
71+
return this;
72+
}
73+
74+
RequestContextBuilder^ RequestContextBuilder::WithProxyServer(String^ scheme, String^ host, Nullable<int> port)
75+
{
76+
if (_handler == nullptr)
77+
{
78+
_handler = gcnew RequestContextHandler();
79+
}
80+
81+
_handler->SetProxyOnContextInitialized(scheme, host, port);
82+
83+
return this;
84+
}
85+
86+
RequestContextBuilder^ RequestContextBuilder::PersistUserPreferences()
87+
{
88+
ThrowExceptionIfContextAlreadySet();
89+
90+
if (_settings == nullptr)
91+
{
92+
_settings = gcnew RequestContextSettings();
93+
}
94+
95+
_settings->PersistUserPreferences = true;
96+
97+
return this;
98+
}
99+
100+
RequestContextBuilder^ RequestContextBuilder::WithCachePath(String^ cachePath)
101+
{
102+
ThrowExceptionIfContextAlreadySet();
103+
104+
if (_settings == nullptr)
105+
{
106+
_settings = gcnew RequestContextSettings();
107+
}
108+
109+
_settings->CachePath = cachePath;
110+
111+
return this;
112+
}
113+
114+
RequestContextBuilder^ RequestContextBuilder::WithSharedSettings(IRequestContext^ other)
115+
{
116+
if (other == nullptr)
117+
{
118+
throw gcnew ArgumentNullException("other");
119+
}
120+
121+
ThrowExceptionIfCustomSettingSpecified();
122+
123+
_otherContext = other;
124+
125+
return this;
126+
}
127+
}

0 commit comments

Comments
 (0)