Move resolveProxy From app to session

This commit is contained in:
Cheng Zhao 2015-06-24 11:59:11 +08:00
parent 33c2768a77
commit ea69e91e49
4 changed files with 64 additions and 54 deletions

View file

@ -17,7 +17,6 @@
#include "atom/browser/atom_browser_main_parts.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/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
@ -26,10 +25,6 @@
#include "native_mate/callback.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/base/load_flags.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#if defined(OS_WIN)
#include "base/strings/utf_string_conversions.h"
@ -95,43 +90,6 @@ int GetPathConstant(const std::string& name) {
return -1;
}
class ResolveProxyHelper {
public:
ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback)
: callback_(callback) {
auto browser_context = AtomBrowserMainParts::Get()->browser_context();
net::ProxyService* proxy_service = browser_context->
url_request_context_getter()->GetURLRequestContext()->proxy_service();
// Start the request.
int result = proxy_service->ResolveProxy(
url, net::LOAD_NORMAL, &proxy_info_,
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
base::Unretained(this)),
&pac_req_, nullptr, 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() {
@ -200,10 +158,6 @@ void App::SetPath(mate::Arguments* args,
args->ThrowError("Failed to set path");
}
void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(url, callback);
}
void App::SetDesktopName(const std::string& desktop_name) {
#if defined(OS_LINUX)
scoped_ptr<base::Environment> env(base::Environment::Create());
@ -249,7 +203,6 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
#endif
.SetMethod("setPath", &App::SetPath)
.SetMethod("getPath", &App::GetPath)
.SetMethod("resolveProxy", &App::ResolveProxy)
.SetMethod("setDesktopName", &App::SetDesktopName)
.SetMethod("setAppUserModelId", &App::SetAppUserModelId)
.SetProperty("defaultSession", &App::DefaultSession);

View file

@ -9,11 +9,8 @@
#include "atom/browser/api/event_emitter.h"
#include "atom/browser/browser_observer.h"
#include "base/callback.h"
#include "native_mate/handle.h"
class GURL;
namespace base {
class FilePath;
}
@ -29,8 +26,6 @@ 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:
@ -59,7 +54,6 @@ class App : public mate::EventEmitter,
const std::string& name,
const base::FilePath& path);
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
void SetDesktopName(const std::string& desktop_name);
void SetAppUserModelId(const std::string& app_id);
v8::Local<v8::Value> DefaultSession(v8::Isolate* isolate);

View file

@ -4,11 +4,17 @@
#include "atom/browser/api/atom_api_session.h"
#include <string>
#include "atom/browser/api/atom_api_cookies.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "native_mate/callback.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/base/load_flags.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"
@ -16,6 +22,50 @@ namespace atom {
namespace api {
namespace {
class ResolveProxyHelper {
public:
ResolveProxyHelper(AtomBrowserContext* browser_context,
const GURL& url,
Session::ResolveProxyCallback callback)
: callback_(callback) {
net::ProxyService* proxy_service = browser_context->
url_request_context_getter()->GetURLRequestContext()->proxy_service();
// Start the request.
int result = proxy_service->ResolveProxy(
url, net::LOAD_NORMAL, &proxy_info_,
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
base::Unretained(this)),
&pac_req_, nullptr, 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:
Session::ResolveProxyCallback callback_;
net::ProxyInfo proxy_info_;
net::ProxyService::PacRequest* pac_req_;
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
};
} // namespace
Session::Session(AtomBrowserContext* browser_context)
: browser_context_(browser_context) {
}
@ -23,6 +73,10 @@ Session::Session(AtomBrowserContext* browser_context)
Session::~Session() {
}
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(browser_context_, url, callback);
}
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
if (cookies_.IsEmpty()) {
auto handle = atom::api::Cookies::Create(isolate, browser_context_);
@ -34,6 +88,7 @@ v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("resolveProxy", &Session::ResolveProxy)
.SetProperty("cookies", &Session::Cookies);
}

View file

@ -5,9 +5,14 @@
#ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_
#define ATOM_BROWSER_API_ATOM_API_SESSION_H_
#include <string>
#include "base/callback.h"
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
class GURL;
namespace atom {
class AtomBrowserContext;
@ -16,6 +21,8 @@ namespace api {
class Session: public mate::Wrappable {
public:
using ResolveProxyCallback = base::Callback<void(std::string)>;
static mate::Handle<Session> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
@ -28,6 +35,7 @@ class Session: public mate::Wrappable {
v8::Isolate* isolate) override;
private:
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
v8::Global<v8::Value> cookies_;