Merge pull request #2465 from atom/fix-incept-scheme
Force request context to initialize beforing incepting protocol
This commit is contained in:
commit
a296b4ef33
10 changed files with 69 additions and 55 deletions
|
@ -9,6 +9,7 @@
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/time/time.h"
|
#include "base/time/time.h"
|
||||||
|
#include "base/values.h"
|
||||||
#include "content/public/browser/browser_context.h"
|
#include "content/public/browser/browser_context.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
@ -179,8 +180,8 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
Cookies::Cookies(content::BrowserContext* browser_context) :
|
Cookies::Cookies(content::BrowserContext* browser_context)
|
||||||
browser_context_(browser_context) {
|
: request_context_getter_(browser_context->GetRequestContext()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Cookies::~Cookies() {
|
Cookies::~Cookies() {
|
||||||
|
@ -198,11 +199,9 @@ void Cookies::Get(const base::DictionaryValue& options,
|
||||||
|
|
||||||
void Cookies::GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
|
void Cookies::GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
|
||||||
const CookiesCallback& callback) {
|
const CookiesCallback& callback) {
|
||||||
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
|
|
||||||
->GetURLRequestContext()->cookie_store();
|
|
||||||
std::string url;
|
std::string url;
|
||||||
filter->GetString("url", &url);
|
filter->GetString("url", &url);
|
||||||
if (!GetCookieListFromStore(cookie_store, url,
|
if (!GetCookieListFromStore(GetCookieStore(), url,
|
||||||
base::Bind(&Cookies::OnGetCookies, base::Unretained(this),
|
base::Bind(&Cookies::OnGetCookies, base::Unretained(this),
|
||||||
Passed(&filter), callback))) {
|
Passed(&filter), callback))) {
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
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,
|
void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name,
|
||||||
const CookiesCallback& callback) {
|
const CookiesCallback& callback) {
|
||||||
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
|
GetCookieStore()->DeleteCookieAsync(url, name,
|
||||||
->GetURLRequestContext()->cookie_store();
|
|
||||||
cookie_store->DeleteCookieAsync(url, name,
|
|
||||||
base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback));
|
base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,8 +283,6 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
const CookiesCallback& callback) {
|
const CookiesCallback& callback) {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||||
net::CookieStore* cookie_store = browser_context_->GetRequestContext()
|
|
||||||
->GetURLRequestContext()->cookie_store();
|
|
||||||
|
|
||||||
std::string name, value, domain, path;
|
std::string name, value, domain, path;
|
||||||
bool secure = false;
|
bool secure = false;
|
||||||
|
@ -308,7 +303,7 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
|
||||||
base::Time::FromDoubleT(expiration_date);
|
base::Time::FromDoubleT(expiration_date);
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie_store->GetCookieMonster()->SetCookieWithDetailsAsync(
|
GetCookieStore()->GetCookieMonster()->SetCookieWithDetailsAsync(
|
||||||
url,
|
url,
|
||||||
name,
|
name,
|
||||||
value,
|
value,
|
||||||
|
@ -337,6 +332,10 @@ mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
|
||||||
.SetMethod("set", &Cookies::Set);
|
.SetMethod("set", &Cookies::Set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
net::CookieStore* Cookies::GetCookieStore() {
|
||||||
|
return request_context_getter_->GetURLRequestContext()->cookie_store();
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<Cookies> Cookies::Create(
|
mate::Handle<Cookies> Cookies::Create(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
|
|
|
@ -8,17 +8,27 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/callback.h"
|
#include "base/callback.h"
|
||||||
#include "base/values.h"
|
|
||||||
#include "native_mate/wrappable.h"
|
#include "native_mate/wrappable.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "native_mate/dictionary.h"
|
|
||||||
|
|
||||||
#include "net/cookies/canonical_cookie.h"
|
#include "net/cookies/canonical_cookie.h"
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
class DictionaryValue;
|
||||||
|
}
|
||||||
|
|
||||||
namespace content {
|
namespace content {
|
||||||
class BrowserContext;
|
class BrowserContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
class Dictionary;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace net {
|
||||||
|
class CookieStore;
|
||||||
|
class URLRequestContextGetter;
|
||||||
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
@ -60,13 +70,15 @@ class Cookies : public mate::Wrappable {
|
||||||
void OnSetCookies(const CookiesCallback& callback,
|
void OnSetCookies(const CookiesCallback& callback,
|
||||||
bool set_success);
|
bool set_success);
|
||||||
|
|
||||||
|
// mate::Wrappable:
|
||||||
// mate::Wrappable implementations:
|
|
||||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||||
v8::Isolate* isolate) override;
|
v8::Isolate* isolate) override;
|
||||||
|
|
||||||
private:
|
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);
|
DISALLOW_COPY_AND_ASSIGN(Cookies);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 mate
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -141,16 +124,27 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
|
||||||
} else if (name == "RequestHttpJob") {
|
} else if (name == "RequestHttpJob") {
|
||||||
GURL url;
|
GURL url;
|
||||||
std::string method, referrer;
|
std::string method, referrer;
|
||||||
net::URLRequestContextGetter* getter =
|
|
||||||
registry_->browser_context()->GetRequestContext();
|
|
||||||
dict.Get("url", &url);
|
dict.Get("url", &url);
|
||||||
dict.Get("method", &method);
|
dict.Get("method", &method);
|
||||||
dict.Get("referrer", &referrer);
|
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,
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
|
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
|
||||||
base::Unretained(getter), url, method, referrer));
|
request_context_getter, url, method, referrer));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +231,7 @@ std::string ConvertErrorCode(int error_code) {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Protocol::Protocol(AtomBrowserContext* browser_context)
|
Protocol::Protocol(AtomBrowserContext* browser_context)
|
||||||
: browser_context_(browser_context),
|
: request_context_getter_(browser_context->GetRequestContext()),
|
||||||
job_factory_(browser_context->job_factory()) {
|
job_factory_(browser_context->job_factory()) {
|
||||||
CHECK(job_factory_);
|
CHECK(job_factory_);
|
||||||
}
|
}
|
||||||
|
@ -361,6 +355,10 @@ int Protocol::InterceptProtocolInIO(const std::string& scheme,
|
||||||
const JsProtocolHandler& handler) {
|
const JsProtocolHandler& handler) {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
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))
|
if (!job_factory_->HasProtocolHandler(scheme))
|
||||||
return ERR_NO_SCHEME;
|
return ERR_NO_SCHEME;
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
namespace net {
|
namespace net {
|
||||||
class URLRequest;
|
class URLRequest;
|
||||||
|
class URLRequestContextGetter;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -46,7 +47,9 @@ class Protocol : public mate::EventEmitter {
|
||||||
|
|
||||||
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
|
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
|
||||||
|
|
||||||
AtomBrowserContext* browser_context() const { return browser_context_; }
|
net::URLRequestContextGetter* request_context_getter() {
|
||||||
|
return request_context_getter_.get();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Protocol(AtomBrowserContext* browser_context);
|
explicit Protocol(AtomBrowserContext* browser_context);
|
||||||
|
@ -94,7 +97,8 @@ class Protocol : public mate::EventEmitter {
|
||||||
const JsProtocolHandler& handler);
|
const JsProtocolHandler& handler);
|
||||||
int UninterceptProtocolInIO(const std::string& scheme);
|
int UninterceptProtocolInIO(const std::string& scheme);
|
||||||
|
|
||||||
AtomBrowserContext* browser_context_;
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
|
||||||
|
|
||||||
AtomURLRequestJobFactory* job_factory_;
|
AtomURLRequestJobFactory* job_factory_;
|
||||||
ProtocolHandlersMap protocol_handlers_;
|
ProtocolHandlersMap protocol_handlers_;
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "chrome/common/pref_names.h"
|
#include "chrome/common/pref_names.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/storage_partition.h"
|
#include "content/public/browser/storage_partition.h"
|
||||||
|
#include "native_mate/dictionary.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"
|
||||||
#include "net/disk_cache/disk_cache.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,
|
void ClearHttpCacheInIO(
|
||||||
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
||||||
const net::CompletionCallback& callback) {
|
const net::CompletionCallback& callback) {
|
||||||
auto request_context =
|
auto request_context = context_getter->GetURLRequestContext();
|
||||||
browser_context->GetRequestContext()->GetURLRequestContext();
|
|
||||||
auto http_cache = request_context->http_transaction_factory()->GetCache();
|
auto http_cache = request_context->http_transaction_factory()->GetCache();
|
||||||
if (!http_cache)
|
if (!http_cache)
|
||||||
RunCallbackInUI<int>(callback, net::ERR_FAILED);
|
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) {
|
void Session::ClearCache(const net::CompletionCallback& callback) {
|
||||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(&ClearHttpCacheInIO,
|
base::Bind(&ClearHttpCacheInIO,
|
||||||
base::Unretained(browser_context_),
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
||||||
callback));
|
callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,14 +13,14 @@
|
||||||
|
|
||||||
class GURL;
|
class GURL;
|
||||||
|
|
||||||
namespace mate {
|
|
||||||
class Arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class FilePath;
|
class FilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
class Arguments;
|
||||||
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class AtomBrowserContext;
|
class AtomBrowserContext;
|
||||||
|
|
|
@ -119,7 +119,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdapterRequestJob::CreateHttpJobAndStart(
|
void AdapterRequestJob::CreateHttpJobAndStart(
|
||||||
net::URLRequestContextGetter* request_context_getter,
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
const std::string& method,
|
const std::string& method,
|
||||||
const std::string& referrer) {
|
const std::string& referrer) {
|
||||||
|
|
|
@ -65,7 +65,7 @@ class AdapterRequestJob : public net::URLRequestJob {
|
||||||
scoped_refptr<base::RefCountedBytes> data);
|
scoped_refptr<base::RefCountedBytes> data);
|
||||||
void CreateFileJobAndStart(const base::FilePath& path);
|
void CreateFileJobAndStart(const base::FilePath& path);
|
||||||
void CreateHttpJobAndStart(
|
void CreateHttpJobAndStart(
|
||||||
net::URLRequestContextGetter* request_context_getter,
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
const std::string& method,
|
const std::string& method,
|
||||||
const std::string& referrer);
|
const std::string& referrer);
|
||||||
|
|
|
@ -75,7 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
URLRequestFetchJob::URLRequestFetchJob(
|
URLRequestFetchJob::URLRequestFetchJob(
|
||||||
net::URLRequestContextGetter* request_context_getter,
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
net::NetworkDelegate* network_delegate,
|
net::NetworkDelegate* network_delegate,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
|
@ -93,7 +93,7 @@ URLRequestFetchJob::URLRequestFetchJob(
|
||||||
fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
|
fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
|
||||||
// Use request context if provided else create one.
|
// Use request context if provided else create one.
|
||||||
if (request_context_getter)
|
if (request_context_getter)
|
||||||
fetcher_->SetRequestContext(request_context_getter);
|
fetcher_->SetRequestContext(request_context_getter.get());
|
||||||
else
|
else
|
||||||
fetcher_->SetRequestContext(GetRequestContext());
|
fetcher_->SetRequestContext(GetRequestContext());
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ class AtomBrowserContext;
|
||||||
class URLRequestFetchJob : public net::URLRequestJob,
|
class URLRequestFetchJob : public net::URLRequestJob,
|
||||||
public net::URLFetcherDelegate {
|
public net::URLFetcherDelegate {
|
||||||
public:
|
public:
|
||||||
URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter,
|
URLRequestFetchJob(scoped_refptr<net::URLRequestContextGetter> context_getter,
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
net::NetworkDelegate* network_delegate,
|
net::NetworkDelegate* network_delegate,
|
||||||
const GURL& url,
|
const GURL& url,
|
||||||
|
|
Loading…
Reference in a new issue