session: using storageparttion to retrieve requestcontext
This commit is contained in:
parent
1418fdbc02
commit
c15a9e7d5c
1 changed files with 40 additions and 14 deletions
|
@ -9,6 +9,9 @@
|
||||||
#include "atom/browser/api/atom_api_cookies.h"
|
#include "atom/browser/api/atom_api_cookies.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
|
#include "base/thread_task_runner_handle.h"
|
||||||
|
#include "content/public/browser/browser_thread.h"
|
||||||
|
#include "content/public/browser/storage_partition.h"
|
||||||
#include "native_mate/callback.h"
|
#include "native_mate/callback.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#include "net/base/load_flags.h"
|
#include "net/base/load_flags.h"
|
||||||
|
@ -29,35 +32,58 @@ class ResolveProxyHelper {
|
||||||
ResolveProxyHelper(AtomBrowserContext* browser_context,
|
ResolveProxyHelper(AtomBrowserContext* browser_context,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
Session::ResolveProxyCallback callback)
|
Session::ResolveProxyCallback callback)
|
||||||
: callback_(callback) {
|
: callback_(callback),
|
||||||
net::ProxyService* proxy_service = browser_context->
|
original_thread_(base::ThreadTaskRunnerHandle::Get()),
|
||||||
url_request_context_getter()->GetURLRequestContext()->proxy_service();
|
weak_ptr_factory_(this) {
|
||||||
|
scoped_refptr<net::URLRequestContextGetter> getter =
|
||||||
|
browser_context->GetDefaultStoragePartition(browser_context)
|
||||||
|
->GetURLRequestContext();
|
||||||
|
|
||||||
// Start the request.
|
getter->GetNetworkTaskRunner()->PostTask(
|
||||||
int result = proxy_service->ResolveProxy(
|
FROM_HERE,
|
||||||
url, net::LOAD_NORMAL, &proxy_info_,
|
base::Bind(&ResolveProxyHelper::ResolveProxy,
|
||||||
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
|
weak_ptr_factory_.GetWeakPtr(),
|
||||||
base::Unretained(this)),
|
getter, url));
|
||||||
&pac_req_, nullptr, net::BoundNetLog());
|
|
||||||
|
|
||||||
// Completed synchronously.
|
|
||||||
if (result != net::ERR_IO_PENDING)
|
|
||||||
OnResolveProxyCompleted(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnResolveProxyCompleted(int result) {
|
void OnResolveProxyCompleted(int result) {
|
||||||
std::string proxy;
|
std::string proxy;
|
||||||
if (result == net::OK)
|
if (result == net::OK)
|
||||||
proxy = proxy_info_.ToPacString();
|
proxy = proxy_info_.ToPacString();
|
||||||
callback_.Run(proxy);
|
original_thread_->PostTask(FROM_HERE,
|
||||||
|
base::Bind(callback_, proxy));
|
||||||
|
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void ResolveProxy(
|
||||||
|
scoped_refptr<net::URLRequestContextGetter> getter,
|
||||||
|
const GURL& url) {
|
||||||
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
||||||
|
|
||||||
|
net::ProxyService* proxy_service =
|
||||||
|
getter->GetURLRequestContext()->proxy_service();
|
||||||
|
|
||||||
|
net::CompletionCallback completion_callback =
|
||||||
|
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
|
||||||
|
weak_ptr_factory_.GetWeakPtr());
|
||||||
|
|
||||||
|
// Start the request.
|
||||||
|
int result = proxy_service->ResolveProxy(
|
||||||
|
url, net::LOAD_NORMAL, &proxy_info_, completion_callback,
|
||||||
|
&pac_req_, nullptr, net::BoundNetLog());
|
||||||
|
|
||||||
|
// Completed synchronously.
|
||||||
|
if (result != net::ERR_IO_PENDING)
|
||||||
|
completion_callback.Run(result);
|
||||||
|
}
|
||||||
|
|
||||||
Session::ResolveProxyCallback callback_;
|
Session::ResolveProxyCallback callback_;
|
||||||
net::ProxyInfo proxy_info_;
|
net::ProxyInfo proxy_info_;
|
||||||
net::ProxyService::PacRequest* pac_req_;
|
net::ProxyService::PacRequest* pac_req_;
|
||||||
|
scoped_refptr<base::SingleThreadTaskRunner> original_thread_;
|
||||||
|
base::WeakPtrFactory<ResolveProxyHelper> weak_ptr_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue