Merge pull request #2465 from atom/fix-incept-scheme

Force request context to initialize beforing incepting protocol
This commit is contained in:
Cheng Zhao 2015-08-10 15:47:12 +08:00
commit a296b4ef33
10 changed files with 69 additions and 55 deletions

View file

@ -9,6 +9,7 @@
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/bind.h"
#include "base/time/time.h"
#include "base/values.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/dictionary.h"
@ -179,8 +180,8 @@ namespace atom {
namespace api {
Cookies::Cookies(content::BrowserContext* browser_context) :
browser_context_(browser_context) {
Cookies::Cookies(content::BrowserContext* browser_context)
: request_context_getter_(browser_context->GetRequestContext()) {
}
Cookies::~Cookies() {
@ -198,11 +199,9 @@ void Cookies::Get(const base::DictionaryValue& options,
void Cookies::GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
const CookiesCallback& callback) {
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
->GetURLRequestContext()->cookie_store();
std::string url;
filter->GetString("url", &url);
if (!GetCookieListFromStore(cookie_store, url,
if (!GetCookieListFromStore(GetCookieStore(), url,
base::Bind(&Cookies::OnGetCookies, base::Unretained(this),
Passed(&filter), callback))) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
@ -245,9 +244,7 @@ void Cookies::Remove(const mate::Dictionary& details,
void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name,
const CookiesCallback& callback) {
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
->GetURLRequestContext()->cookie_store();
cookie_store->DeleteCookieAsync(url, name,
GetCookieStore()->DeleteCookieAsync(url, name,
base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback));
}
@ -286,8 +283,6 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
const GURL& url,
const CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
->GetURLRequestContext()->cookie_store();
std::string name, value, domain, path;
bool secure = false;
@ -308,7 +303,7 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
base::Time::FromDoubleT(expiration_date);
}
cookie_store->GetCookieMonster()->SetCookieWithDetailsAsync(
GetCookieStore()->GetCookieMonster()->SetCookieWithDetailsAsync(
url,
name,
value,
@ -337,6 +332,10 @@ mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
.SetMethod("set", &Cookies::Set);
}
net::CookieStore* Cookies::GetCookieStore() {
return request_context_getter_->GetURLRequestContext()->cookie_store();
}
// static
mate::Handle<Cookies> Cookies::Create(
v8::Isolate* isolate,

View file

@ -8,17 +8,27 @@
#include <string>
#include "base/callback.h"
#include "base/values.h"
#include "native_mate/wrappable.h"
#include "native_mate/handle.h"
#include "native_mate/dictionary.h"
#include "net/cookies/canonical_cookie.h"
namespace base {
class DictionaryValue;
}
namespace content {
class BrowserContext;
}
namespace mate {
class Dictionary;
}
namespace net {
class CookieStore;
class URLRequestContextGetter;
}
namespace atom {
namespace api {
@ -60,13 +70,15 @@ class Cookies : public mate::Wrappable {
void OnSetCookies(const CookiesCallback& callback,
bool set_success);
// mate::Wrappable implementations:
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
private:
content::BrowserContext* browser_context_;
// Must be called on IO thread.
net::CookieStore* GetCookieStore();
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(Cookies);
};

View file

@ -35,23 +35,6 @@ struct Converter<const net::URLRequest*> {
}
};
template<>
struct Converter<net::URLRequestContextGetter*> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
net::URLRequestContextGetter** out) {
if (val->IsNull()) {
*out = nullptr;
return true;
}
atom::api::Session* session;
if (!Converter<atom::api::Session*>::FromV8(isolate, val, &session))
return false;
*out = session->browser_context()->GetRequestContext();
return true;
}
};
} // namespace mate
namespace atom {
@ -141,16 +124,27 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
} else if (name == "RequestHttpJob") {
GURL url;
std::string method, referrer;
net::URLRequestContextGetter* getter =
registry_->browser_context()->GetRequestContext();
dict.Get("url", &url);
dict.Get("method", &method);
dict.Get("referrer", &referrer);
dict.Get("session", &getter);
v8::Local<v8::Value> value;
mate::Handle<Session> session;
scoped_refptr<net::URLRequestContextGetter> request_context_getter;
// "session" null -> pass nullptr;
// "session" a Session object -> use passed session.
// "session" undefined -> use current session;
if (dict.Get("session", &session))
request_context_getter =
session->browser_context()->GetRequestContext();
else if (dict.Get("session", &value) && value->IsNull())
request_context_getter = nullptr;
else
request_context_getter = registry_->request_context_getter();
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
base::Unretained(getter), url, method, referrer));
request_context_getter, url, method, referrer));
return;
}
}
@ -237,7 +231,7 @@ std::string ConvertErrorCode(int error_code) {
} // namespace
Protocol::Protocol(AtomBrowserContext* browser_context)
: browser_context_(browser_context),
: request_context_getter_(browser_context->GetRequestContext()),
job_factory_(browser_context->job_factory()) {
CHECK(job_factory_);
}
@ -361,6 +355,10 @@ int Protocol::InterceptProtocolInIO(const std::string& scheme,
const JsProtocolHandler& handler) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Force the request context to initialize, otherwise we might have nothing
// to intercept.
request_context_getter_->GetURLRequestContext();
if (!job_factory_->HasProtocolHandler(scheme))
return ERR_NO_SCHEME;

View file

@ -16,6 +16,7 @@
namespace net {
class URLRequest;
class URLRequestContextGetter;
}
namespace atom {
@ -46,7 +47,9 @@ class Protocol : public mate::EventEmitter {
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
AtomBrowserContext* browser_context() const { return browser_context_; }
net::URLRequestContextGetter* request_context_getter() {
return request_context_getter_.get();
}
protected:
explicit Protocol(AtomBrowserContext* browser_context);
@ -94,7 +97,8 @@ class Protocol : public mate::EventEmitter {
const JsProtocolHandler& handler);
int UninterceptProtocolInIO(const std::string& scheme);
AtomBrowserContext* browser_context_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
AtomURLRequestJobFactory* job_factory_;
ProtocolHandlersMap protocol_handlers_;

View file

@ -19,6 +19,7 @@
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/base/load_flags.h"
#include "net/disk_cache/disk_cache.h"
@ -181,10 +182,10 @@ void OnGetBackend(disk_cache::Backend** backend_ptr,
}
}
void ClearHttpCacheInIO(content::BrowserContext* browser_context,
const net::CompletionCallback& callback) {
auto request_context =
browser_context->GetRequestContext()->GetURLRequestContext();
void ClearHttpCacheInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
const net::CompletionCallback& callback) {
auto request_context = context_getter->GetURLRequestContext();
auto http_cache = request_context->http_transaction_factory()->GetCache();
if (!http_cache)
RunCallbackInUI<int>(callback, net::ERR_FAILED);
@ -226,7 +227,7 @@ void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
void Session::ClearCache(const net::CompletionCallback& callback) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&ClearHttpCacheInIO,
base::Unretained(browser_context_),
make_scoped_refptr(browser_context_->GetRequestContext()),
callback));
}

View file

@ -13,14 +13,14 @@
class GURL;
namespace mate {
class Arguments;
}
namespace base {
class FilePath;
}
namespace mate {
class Arguments;
}
namespace atom {
class AtomBrowserContext;

View file

@ -119,7 +119,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
}
void AdapterRequestJob::CreateHttpJobAndStart(
net::URLRequestContextGetter* request_context_getter,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer) {

View file

@ -65,7 +65,7 @@ class AdapterRequestJob : public net::URLRequestJob {
scoped_refptr<base::RefCountedBytes> data);
void CreateFileJobAndStart(const base::FilePath& path);
void CreateHttpJobAndStart(
net::URLRequestContextGetter* request_context_getter,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer);

View file

@ -75,7 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace
URLRequestFetchJob::URLRequestFetchJob(
net::URLRequestContextGetter* request_context_getter,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
@ -93,7 +93,7 @@ URLRequestFetchJob::URLRequestFetchJob(
fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
// Use request context if provided else create one.
if (request_context_getter)
fetcher_->SetRequestContext(request_context_getter);
fetcher_->SetRequestContext(request_context_getter.get());
else
fetcher_->SetRequestContext(GetRequestContext());

View file

@ -18,7 +18,7 @@ class AtomBrowserContext;
class URLRequestFetchJob : public net::URLRequestJob,
public net::URLFetcherDelegate {
public:
URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter,
URLRequestFetchJob(scoped_refptr<net::URLRequestContextGetter> context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,