diff --git a/docs/References/App.md b/docs/References/App.md index e21e65952e..cc1ded17c0 100644 --- a/docs/References/App.md +++ b/docs/References/App.md @@ -62,11 +62,12 @@ These 2 functions crashes the browser process and the renderer process respectiv Query the proxy to be used for loading `url` in DOM. The return value is in the same format used in [PAC](http://en.wikipedia.org/wiki/Proxy_auto-config) (e.g. "DIRECT", "PROXY localhost:8080"). -## App.setProxyConfig(config) +## App.setProxyConfig(config, pac_url) * `config` `{String}` Proxy rules +* `pac_url` `{String}` PAC url -Set the proxy config which the web engine will be used to request network resources. +Set the proxy config which the web engine will be used to request network resources or PAC url to detect proxy automatically. Rule (copied from [`net/proxy/proxy_config.h`](https://github.com/nwjs/chromium.src/blob/nw13/net/proxy/proxy_config.h)) diff --git a/src/api/nw_app.idl b/src/api/nw_app.idl index be195a1aec..482cfe9dfd 100644 --- a/src/api/nw_app.idl +++ b/src/api/nw_app.idl @@ -19,7 +19,7 @@ namespace nw.App { static void closeAllWindows(); static void clearCache(); static void clearAppCache(DOMString manifest_url); - static void setProxyConfig(DOMString config); + static void setProxyConfig(DOMString config, optional DOMString pac_url); [nocompile] static DOMString getProxyForURL(DOMString url); [nocompile] static void addOriginAccessWhitelistEntry(DOMString sourceOrigin, DOMString destinationProtocol, DOMString destinationHost, boolean allowDestinationSubdomains); [nocompile] static void removeOriginAccessWhitelistEntry(DOMString sourceOrigin, DOMString destinationProtocol, DOMString destinationHost, boolean allowDestinationSubdomains); diff --git a/src/api/nw_app_api.cc b/src/api/nw_app_api.cc index 8be279b0cd..0f7ebace18 100644 --- a/src/api/nw_app_api.cc +++ b/src/api/nw_app_api.cc @@ -8,6 +8,7 @@ #include "chrome/browser/devtools/devtools_window.h" #include "chrome/browser/extensions/devtools_util.h" #include "chrome/browser/extensions/extension_service.h" +#include "content/nw/src/api/nw_app.h" #include "content/nw/src/nw_base.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -24,6 +25,8 @@ #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context_getter.h" +using namespace extensions::nwapi::nw__app; + namespace { void SetProxyConfigCallback( base::WaitableEvent* done, @@ -150,13 +153,27 @@ NwAppSetProxyConfigFunction::~NwAppSetProxyConfigFunction() { } bool NwAppSetProxyConfigFunction::RunNWSync(base::ListValue* response, std::string* error) { - std::string proxy_config; - EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &proxy_config)); + net::ProxyConfig config; + std::unique_ptr params( + nwapi::nw__app::SetProxyConfig::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); + + std::string pac_url = params->pac_url.get() ? *params->pac_url : ""; + if (!pac_url.empty()) { + if (pac_url == "") + config = net::ProxyConfig::CreateDirect(); + else if (pac_url == "") + config = net::ProxyConfig::CreateAutoDetect(); + else + config = net::ProxyConfig::CreateFromCustomPacURL(GURL(pac_url)); + } else { + std::string proxy_config; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &proxy_config)); + config.proxy_rules().ParseFromString(proxy_config); + } base::ThreadRestrictions::ScopedAllowWait allow_wait; - net::ProxyConfig config; - config.proxy_rules().ParseFromString(proxy_config); content::RenderProcessHost* render_process_host = GetSenderWebContents()->GetRenderProcessHost(); net::URLRequestContextGetter* context_getter = render_process_host->GetStoragePartition()->GetURLRequestContext(); diff --git a/test/sanity/app-getproxyforurl/bg.js b/test/sanity/app-getproxyforurl/bg.js new file mode 100644 index 0000000000..5e218249d3 --- /dev/null +++ b/test/sanity/app-getproxyforurl/bg.js @@ -0,0 +1,19 @@ +(function(){ +const http = require('http'); +let id = 1; + +// Create an HTTP server +var srv = http.createServer( (req, res) => { + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write('function FindProxyForURL(url, host) {\n'); + res.write(' if (host == \'www.port3128.com\')\n'); + res.write(' return \'PROXY localhost:3128\';\n'); + res.write(' else if (host == \'www.port4040.com\')\n'); + res.write(' return \'PROXY localhost:4040\';\n'); + res.write(' return "DIRECT";\n'); + res.write('}'); + res.end(); +}); + +srv.listen(nw.App.manifest.port, '127.0.0.1'); +})() diff --git a/test/sanity/app-getproxyforurl/index.html b/test/sanity/app-getproxyforurl/index.html index 59b1cf661c..fd8c2116a1 100644 --- a/test/sanity/app-getproxyforurl/index.html +++ b/test/sanity/app-getproxyforurl/index.html @@ -1,20 +1,54 @@ - - - App.getProxyForURL + + + App.getProxyForURL - + + + - \ No newline at end of file + diff --git a/test/sanity/app-getproxyforurl/package.json b/test/sanity/app-getproxyforurl/package.json deleted file mode 100644 index b016f89b77..0000000000 --- a/test/sanity/app-getproxyforurl/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "app-getproxyforurl", - "main": "index.html" -} \ No newline at end of file diff --git a/test/sanity/app-getproxyforurl/test.py b/test/sanity/app-getproxyforurl/test.py index 596e8a00e7..296a172d72 100644 --- a/test/sanity/app-getproxyforurl/test.py +++ b/test/sanity/app-getproxyforurl/test.py @@ -1,17 +1,50 @@ import time import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from nw_util import * from selenium import webdriver from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common import utils + +test_dir = os.path.dirname(os.path.abspath(__file__)) chrome_options = Options() -chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__))) +chrome_options.add_argument("nwapp=" + test_dir) + +port = str(utils.free_port()) + +pkgjson = ''' +{ + "name": "app-getproxyforurl", + "main": "index.html", + "bg-script": "bg.js", + "port": "%s" +} +''' % port + +with open(os.path.join(test_dir, 'package.json'), 'w') as bg: + bg.write(pkgjson) driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options) +driver.implicitly_wait(2) +time.sleep(1) try: print driver.current_url + time.sleep(1) result = driver.find_element_by_id('result') print result.get_attribute('innerHTML') assert("success" in result.get_attribute('innerHTML')) + + wait_window_handles(driver, 1) + switch_to_app(driver) + driver.find_element_by_id('pac').click() + wait_window_handles(driver, 1) + result2 = driver.find_element_by_id('result2') + assert("success" in result2.get_attribute('innerHTML')) + wait_window_handles(driver, 1) + result3 = driver.find_element_by_id('result3') + assert("success" in result3.get_attribute('innerHTML')) finally: driver.quit()