BrowserContext::GetRequestContext can only be called on UI thread
This commit is contained in:
parent
e4a7352b62
commit
c295979270
3 changed files with 35 additions and 23 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,
|
||||||
|
@ -346,4 +345,4 @@ mate::Handle<Cookies> Cookies::Create(
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atotContext
|
||||||
|
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 net::CompletionCallback& callback) {
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
||||||
auto request_context =
|
const net::CompletionCallback& callback) {
|
||||||
browser_context->GetRequestContext()->GetURLRequestContext();
|
auto request_context = context_getter->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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue