Add app.resolveProxy API, fixes #545.

This commit is contained in:
Cheng Zhao 2014-08-19 21:26:45 +08:00
parent a840799b46
commit cfae3cd3af
3 changed files with 75 additions and 6 deletions

View file

@ -7,14 +7,21 @@
#include <string>
#include <vector>
#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

View file

@ -7,12 +7,17 @@
#include <string>
#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<void(std::string)> ResolveProxyCallback;
static mate::Handle<App> 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);
};

View file

@ -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.