Skip to content

Commit 324ebf1

Browse files
committed
Fix nwjs#916: Support SetProxyConfig API
1 parent aa802b4 commit 324ebf1

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

src/api/app/app.cc

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@
4444
#include "content/nw/src/nw_shell.h"
4545
#include "content/nw/src/shell_browser_context.h"
4646
#include "content/common/view_messages.h"
47+
#include "content/public/browser/browser_thread.h"
4748
#include "content/public/browser/web_contents.h"
4849
#include "content/public/browser/render_process_host.h"
50+
#include "net/proxy/proxy_config.h"
51+
#include "net/proxy/proxy_config_service_fixed.h"
52+
#include "net/proxy/proxy_service.h"
53+
#include "net/url_request/url_request_context.h"
54+
#include "net/url_request/url_request_context_getter.h"
4955

5056
using base::MessageLoop;
57+
using content::BrowserThread;
5158
using content::Shell;
5259
using content::ShellBrowserContext;
5360
using content::RenderProcessHost;
@@ -81,6 +88,17 @@ void GetRenderProcessHosts(std::set<RenderProcessHost*>& rphs) {
8188
}
8289
}
8390

91+
void SetProxyConfigCallback(
92+
base::WaitableEvent* done,
93+
net::URLRequestContextGetter* url_request_context_getter,
94+
const net::ProxyConfig& proxy_config) {
95+
net::ProxyService* proxy_service =
96+
url_request_context_getter->GetURLRequestContext()->proxy_service();
97+
proxy_service->ResetConfigService(
98+
new net::ProxyConfigServiceFixed(proxy_config));
99+
done->Signal();
100+
}
101+
84102
} // namespace
85103

86104
// static
@@ -133,16 +151,16 @@ void App::Call(Shell* shell,
133151
#if defined(OS_WIN)
134152
base::string16 path;
135153
arguments.GetString(0, &path);
136-
137-
base::win::ShortcutProperties props;
138-
base::string16 appID;
139-
if (content::Shell::GetPackage()->root()->GetString("app-id", &appID) == false)
140-
content::Shell::GetPackage()->root()->GetString(switches::kmName, &appID);
141-
const std::wstring appName = base::UTF8ToWide(content::Shell::GetPackage()->GetName());
142-
props.set_app_id(appID);
143-
144-
base::FilePath processPath;
145-
PathService::Get(base::FILE_EXE, &processPath);
154+
155+
base::win::ShortcutProperties props;
156+
base::string16 appID;
157+
if (content::Shell::GetPackage()->root()->GetString("app-id", &appID) == false)
158+
content::Shell::GetPackage()->root()->GetString(switches::kmName, &appID);
159+
const std::wstring appName = base::UTF8ToWide(content::Shell::GetPackage()->GetName());
160+
props.set_app_id(appID);
161+
162+
base::FilePath processPath;
163+
PathService::Get(base::FILE_EXE, &processPath);
146164
props.set_target(processPath);
147165
result->AppendBoolean(base::win::CreateOrUpdateShortcutLink(base::FilePath(path), props, base::win::SHORTCUT_CREATE_ALWAYS));
148166
#else
@@ -177,6 +195,11 @@ void App::Call(Shell* shell,
177195
GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
178196
shortcut->GetAccelerator(), shortcut);
179197
return;
198+
} else if (method == "SetProxyConfig") {
199+
std::string proxy_config;
200+
arguments.GetString(0, &proxy_config);
201+
SetProxyConfig(GetRenderProcessHost(), proxy_config);
202+
return;
180203
}
181204

182205
NOTREACHED() << "Calling unknown sync method " << method << " of App";
@@ -266,4 +289,20 @@ void App::ClearCache(content::RenderProcessHost* render_process_host) {
266289
render_process_host->GetID());
267290
}
268291

292+
void App::SetProxyConfig(content::RenderProcessHost* render_process_host,
293+
const std::string& proxy_config) {
294+
net::ProxyConfig config;
295+
config.proxy_rules().ParseFromString(proxy_config);
296+
net::URLRequestContextGetter* context_getter =
297+
render_process_host->GetBrowserContext()->
298+
GetRequestContextForRenderProcess(render_process_host->GetID());
299+
300+
base::WaitableEvent done(false, false);
301+
BrowserThread::PostTask(
302+
BrowserThread::IO, FROM_HERE,
303+
base::Bind(&SetProxyConfigCallback, &done,
304+
make_scoped_refptr(context_getter), config));
305+
done.Wait();
306+
307+
}
269308
} // namespace nwapi

src/api/app/app.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class App {
6060
static void EmitReopenEvent();
6161

6262
static void ClearCache(content::RenderProcessHost* render_view_host);
63+
static void SetProxyConfig(content::RenderProcessHost* render_process_host,
64+
const std::string& proxy_config);
6365

6466
private:
6567
App();

src/api/app/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ App.prototype.getProxyForURL = function (url) {
6565
return nw.callStaticMethodSync('App', 'getProxyForURL', [ url ]);
6666
}
6767

68+
App.prototype.setProxyConfig = function (proxy_config) {
69+
return nw.callStaticMethodSync('App', 'SetProxyConfig', [ proxy_config ]);
70+
}
71+
6872
App.prototype.addOriginAccessWhitelistEntry = function(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains) {
6973
return nw.callStaticMethodSync('App', 'AddOriginAccessWhitelistEntry', sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains);
7074
}

0 commit comments

Comments
 (0)