Skip to content

Commit

Permalink
browser(webkit): implement support for proxy (#2436)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Jun 2, 2020
1 parent a644f0a commit c02a862
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 142 deletions.
2 changes: 1 addition & 1 deletion browser_patches/webkit/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1251
1252
129 changes: 92 additions & 37 deletions browser_patches/webkit/patches/bootstrap.diff

Large diffs are not rendered by default.

88 changes: 4 additions & 84 deletions browser_patches/webkit/src/Tools/Playwright/win/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,90 +122,6 @@ void createCrashReport(EXCEPTION_POINTERS* exceptionPointers)
}
}

bool askProxySettings(HWND hwnd, ProxySettings& settings)
{
class ProxyDialog : public Dialog {
public:
ProxyDialog(ProxySettings& settings)
: settings { settings }
{
}

protected:
ProxySettings& settings;

void setup() final
{
auto command = commandForProxyChoice();
proxyChoice().set(command);
setText(IDC_PROXY_URL, settings.url);
setText(IDC_PROXY_EXCLUDE, settings.excludeHosts);
}

void ok() final
{
settings.url = getText(IDC_PROXY_URL);
settings.excludeHosts = getText(IDC_PROXY_EXCLUDE);
updateProxyChoice(proxyChoice().get());
}

bool validate() final
{
bool valid = true;

if (proxyChoice().get() == IDC_PROXY_CUSTOM) {
setEnabled(IDC_PROXY_URL, true);
setEnabled(IDC_PROXY_EXCLUDE, true);

if (!getTextLength(IDC_PROXY_URL))
valid = false;
} else {
setEnabled(IDC_PROXY_URL, false);
setEnabled(IDC_PROXY_EXCLUDE, false);
}

return valid;
}

RadioGroup proxyChoice()
{
return radioGroup(IDC_PROXY_DEFAULT, IDC_PROXY_DISABLE);
}

int commandForProxyChoice()
{
if (!settings.enable)
return IDC_PROXY_DISABLE;
if (settings.custom)
return IDC_PROXY_CUSTOM;
return IDC_PROXY_DEFAULT;
}

void updateProxyChoice(int command)
{
switch (command) {
case IDC_PROXY_DEFAULT:
settings.enable = true;
settings.custom = false;
break;
case IDC_PROXY_CUSTOM:
settings.enable = true;
settings.custom = true;
break;
case IDC_PROXY_DISABLE:
settings.enable = false;
settings.custom = false;
break;
default:
break;
}
}
};

ProxyDialog dialog { settings };
return dialog.run(hInst, hwnd, IDD_PROXY);
}

Optional<Credential> askCredential(HWND hwnd, const std::wstring& realm)
{
struct AuthDialog : public Dialog {
Expand Down Expand Up @@ -274,6 +190,10 @@ CommandLineOptions parseCommandLine()
options.inspectorPipe = true;
else if (!wcsncmp(argv[i], L"--user-data-dir=", 16))
options.userDataDir = argv[i] + 16;
else if (!wcsncmp(argv[i], L"--curl-proxy=", 13))
options.curloptProxy = argv[i] + 13;
else if (!wcsncmp(argv[i], L"--curl-noproxy=", 15))
options.curloptNoproxy = argv[i] + 15;
else if (!wcsicmp(argv[i], L"--headless"))
options.headless = true;
else if (!wcsicmp(argv[i], L"--no-startup-window"))
Expand Down
10 changes: 2 additions & 8 deletions browser_patches/webkit/src/Tools/Playwright/win/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct CommandLineOptions {
bool noStartupWindow { };
_bstr_t requestedURL;
_bstr_t userDataDir;
_bstr_t curloptProxy;
_bstr_t curloptNoproxy;

CommandLineOptions()
{
Expand All @@ -49,19 +51,11 @@ struct Credential {
std::wstring password;
};

struct ProxySettings {
bool enable { true };
bool custom { false };
std::wstring url;
std::wstring excludeHosts;
};

void computeFullDesktopFrame();
bool getAppDataFolder(_bstr_t& directory);
CommandLineOptions parseCommandLine();
void createCrashReport(EXCEPTION_POINTERS*);
Optional<Credential> askCredential(HWND, const std::wstring& realm);
bool askProxySettings(HWND, ProxySettings&);

bool askServerTrustEvaluation(HWND, const std::wstring& text);
std::wstring replaceString(std::wstring src, const std::wstring& oldValue, const std::wstring& newValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class WebKitBrowserWindow {
BrowserWindowClient& m_client;
WKRetainPtr<WKViewRef> m_view;
HWND m_hMainWnd { nullptr };
ProxySettings m_proxy { };
std::unordered_map<std::wstring, std::wstring> m_acceptedServerTrustCerts;
WKPageRunJavaScriptAlertResultListenerRef m_alertDialog = { };
WKPageRunJavaScriptConfirmResultListenerRef m_confirmDialog = { };
Expand Down
35 changes: 24 additions & 11 deletions browser_patches/webkit/src/Tools/Playwright/win/WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <WebKit/WKContext.h>
#include <WebKit/WKWebsiteDataStoreConfigurationRef.h>
#include <WebKit/WKWebsiteDataStoreRef.h>
#include <WebKit/WKWebsiteDataStoreRefCurl.h>
#include <wtf/win/SoftLinking.h>
#include "WebKitBrowserWindow.h"
#include <wtf/MainThread.h>
Expand All @@ -44,19 +45,29 @@
SOFT_LINK_LIBRARY(user32);
SOFT_LINK_OPTIONAL(user32, SetProcessDpiAwarenessContext, BOOL, STDAPICALLTYPE, (DPI_AWARENESS_CONTEXT));

WKRetainPtr<WKStringRef> toWK(const std::string& string)
CommandLineOptions g_options;

static WKRetainPtr<WKStringRef> toWK(const std::string& string)
{
return adoptWK(WKStringCreateWithUTF8CString(string.c_str()));
}

std::string toUTF8String(const wchar_t* src, size_t srcLength)
static std::string toUTF8String(const wchar_t* src, size_t srcLength)
{
int length = WideCharToMultiByte(CP_UTF8, 0, src, srcLength, 0, 0, nullptr, nullptr);
std::vector<char> buffer(length);
size_t actualLength = WideCharToMultiByte(CP_UTF8, 0, src, srcLength, buffer.data(), length, nullptr, nullptr);
return { buffer.data(), actualLength };
}

static void configureDataStore(WKWebsiteDataStoreRef dataStore) {
if (g_options.curloptProxy.length()) {
auto curloptProxy = createWKURL(g_options.curloptProxy);
auto curloptNoproxy = createWKString(g_options.curloptNoproxy);
WKWebsiteDataStoreEnableCustomNetworkProxySettings(dataStore, curloptProxy.get(), curloptNoproxy.get());
}
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpstrCmdLine, _In_ int nCmdShow)
{
#ifdef _CRTDBG_MAP_ALLOC
Expand All @@ -73,14 +84,15 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
InitCtrlEx.dwICC = 0x00004000; // ICC_STANDARD_CLASSES;
InitCommonControlsEx(&InitCtrlEx);

auto options = parseCommandLine();
if (options.inspectorPipe) {
g_options = parseCommandLine();
if (g_options.inspectorPipe) {
WKInspectorInitializeRemoteInspectorPipe(
configureDataStore,
WebKitBrowserWindow::createPageCallback,
[]() { PostQuitMessage(0); });
}

if (options.useFullDesktop)
if (g_options.useFullDesktop)
computeFullDesktopFrame();

// Init COM
Expand All @@ -89,12 +101,12 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
if (SetProcessDpiAwarenessContextPtr())
SetProcessDpiAwarenessContextPtr()(DPI_AWARENESS_CONTEXT_UNAWARE);

MainWindow::configure(options.headless, options.noStartupWindow);
MainWindow::configure(g_options.headless, g_options.noStartupWindow);

if (!options.noStartupWindow) {
if (!g_options.noStartupWindow) {
auto configuration = adoptWK(WKWebsiteDataStoreConfigurationCreate());
if (options.userDataDir.length()) {
std::string profileFolder = toUTF8String(options.userDataDir, options.userDataDir.length());
if (g_options.userDataDir.length()) {
std::string profileFolder = toUTF8String(g_options.userDataDir, g_options.userDataDir.length());
WKWebsiteDataStoreConfigurationSetApplicationCacheDirectory(configuration.get(), toWK(profileFolder + "\\ApplicationCache").get());
WKWebsiteDataStoreConfigurationSetNetworkCacheDirectory(configuration.get(), toWK(profileFolder + "\\Cache").get());
WKWebsiteDataStoreConfigurationSetCacheStorageDirectory(configuration.get(), toWK(profileFolder + "\\CacheStorage").get());
Expand All @@ -108,6 +120,7 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
auto context = adoptWK(WKContextCreateWithConfiguration(nullptr));
auto dataStore = adoptWK(WKWebsiteDataStoreCreateWithConfiguration(configuration.get()));
WKContextSetPrimaryDataStore(context.get(), dataStore.get());
configureDataStore(dataStore.get());

auto* mainWindow = new MainWindow();
auto conf = adoptWK(WKPageConfigurationCreate());
Expand All @@ -117,8 +130,8 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
if (FAILED(hr))
goto exit;

if (options.requestedURL.length())
mainWindow->loadURL(options.requestedURL.GetBSTR());
if (g_options.requestedURL.length())
mainWindow->loadURL(g_options.requestedURL.GetBSTR());
else
mainWindow->loadURL(L"about:blank");
}
Expand Down

0 comments on commit c02a862

Please sign in to comment.