From c15a9e7d5c220010ab42c2bfac58c2efb3dccbb9 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 25 Jun 2015 14:13:01 +0530 Subject: [PATCH] session: using storageparttion to retrieve requestcontext --- atom/browser/api/atom_api_session.cc | 54 ++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 8a718b5630..d1f21e48c4 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -9,6 +9,9 @@ #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 "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/object_template_builder.h" #include "net/base/load_flags.h" @@ -29,35 +32,58 @@ class ResolveProxyHelper { 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(); + : callback_(callback), + original_thread_(base::ThreadTaskRunnerHandle::Get()), + weak_ptr_factory_(this) { + scoped_refptr getter = + browser_context->GetDefaultStoragePartition(browser_context) + ->GetURLRequestContext(); - // 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); + getter->GetNetworkTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&ResolveProxyHelper::ResolveProxy, + weak_ptr_factory_.GetWeakPtr(), + getter, url)); } void OnResolveProxyCompleted(int result) { std::string proxy; if (result == net::OK) proxy = proxy_info_.ToPacString(); - callback_.Run(proxy); + original_thread_->PostTask(FROM_HERE, + base::Bind(callback_, proxy)); delete this; } private: + void ResolveProxy( + scoped_refptr 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_; net::ProxyInfo proxy_info_; net::ProxyService::PacRequest* pac_req_; + scoped_refptr original_thread_; + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper); };