diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index ca8166136f15..942485397081 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -7,14 +7,21 @@ #include #include +#include "atom/browser/atom_browser_context.h" +#include "atom/browser/browser.h" +#include "atom/common/native_mate_converters/file_path_converter.h" +#include "atom/common/native_mate_converters/gurl_converter.h" #include "base/values.h" #include "base/command_line.h" #include "base/environment.h" #include "base/files/file_path.h" #include "base/path_service.h" -#include "atom/browser/browser.h" +#include "native_mate/callback.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" +#include "net/proxy/proxy_service.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" #include "atom/common/node_includes.h" @@ -28,6 +35,46 @@ namespace atom { namespace api { +namespace { + +class ResolveProxyHelper { + public: + ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback) + : callback_(callback) { + net::ProxyService* proxy_service = AtomBrowserContext::Get()-> + url_request_context_getter()->GetURLRequestContext()->proxy_service(); + + // Start the request. + int result = proxy_service->ResolveProxy( + url, &proxy_info_, + base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted, + base::Unretained(this)), + &pac_req_, net::BoundNetLog()); + + // Completed synchronously. + if (result != net::ERR_IO_PENDING) + OnResolveProxyCompleted(result); + } + + void OnResolveProxyCompleted(int result) { + std::string proxy; + if (result == net::OK) + proxy = proxy_info_.ToPacString(); + callback_.Run(proxy); + + delete this; + } + + private: + App::ResolveProxyCallback callback_; + net::ProxyInfo proxy_info_; + net::ProxyService::PacRequest* pac_req_; + + DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper); +}; + +} // namespace + App::App() { Browser::Get()->AddObserver(this); } @@ -76,13 +123,17 @@ base::FilePath App::GetDataPath() { base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir); #else - CHECK(PathService::Get(base::DIR_APP_DATA, &path)); + PathService::Get(base::DIR_APP_DATA, &path); #endif return path.Append(base::FilePath::FromUTF8Unsafe( Browser::Get()->GetName())); } +void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { + new ResolveProxyHelper(url, callback); +} + mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( v8::Isolate* isolate) { Browser* browser = Browser::Get(); @@ -99,7 +150,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( base::Unretained(browser))) .SetMethod("setName", base::Bind(&Browser::SetName, base::Unretained(browser))) - .SetMethod("getDataPath", &App::GetDataPath); + .SetMethod("getDataPath", &App::GetDataPath) + .SetMethod("resolveProxy", &App::ResolveProxy); } // static diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index d0cbddaadff5..91aa16b8207a 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -7,12 +7,17 @@ #include -#include "base/compiler_specific.h" #include "atom/browser/api/event_emitter.h" #include "atom/browser/browser_observer.h" -#include "atom/common/native_mate_converters/file_path_converter.h" +#include "base/callback.h" #include "native_mate/handle.h" +class GURL; + +namespace base { +class FilePath; +} + namespace atom { namespace api { @@ -20,6 +25,8 @@ namespace api { class App : public mate::EventEmitter, public BrowserObserver { public: + typedef base::Callback ResolveProxyCallback; + static mate::Handle Create(v8::Isolate* isolate); protected: @@ -35,13 +42,15 @@ class App : public mate::EventEmitter, virtual void OnActivateWithNoOpenWindows() OVERRIDE; virtual void OnWillFinishLaunching() OVERRIDE; virtual void OnFinishLaunching() OVERRIDE; - virtual base::FilePath GetDataPath(); // mate::Wrappable implementations: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate); private: + base::FilePath GetDataPath(); + void ResolveProxy(const GURL& url, ResolveProxyCallback callback); + DISALLOW_COPY_AND_ASSIGN(App); }; diff --git a/docs/api/app.md b/docs/api/app.md index f2f19f5268da..0f585f5e521f 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -111,6 +111,14 @@ to the spec of npm modules. So usually you should also specify a `productName` field, which is your application's full capitalized name, and it will be preferred over `name` by atom-shell. +## app.resolveProxy(url, callback) + +* `url` URL +* `callback` Function + +Resolves the proxy information for `url`, the `callback` would be called with +`callback(proxy)` when the request is done. + ## app.commandLine.appendSwitch(switch, [value]) Append a switch [with optional value] to Chromium's command line.