This commit is contained in:
Jeremy Apthorp 2020-03-25 15:34:53 -07:00 коммит произвёл GitHub
Родитель b327478cf0
Коммит 222022556f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 55 добавлений и 42 удалений

Просмотреть файл

@ -2,7 +2,7 @@
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
const { app, deprecate } = require('electron'); const { app, deprecate } = require('electron');
const { fromPartition, Session, Cookies, Protocol } = process.electronBinding('session'); const { fromPartition, Session, Protocol } = process.electronBinding('session');
// Public API. // Public API.
Object.defineProperties(exports, { Object.defineProperties(exports, {
@ -16,7 +16,6 @@ Object.defineProperties(exports, {
} }
}); });
Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype);
Object.setPrototypeOf(Session.prototype, EventEmitter.prototype); Object.setPrototypeOf(Session.prototype, EventEmitter.prototype);
Session.prototype._init = function () { Session.prototype._init = function () {

Просмотреть файл

@ -173,9 +173,10 @@ std::string InclusionStatusToString(
} // namespace } // namespace
gin::WrapperInfo Cookies::kWrapperInfo = {gin::kEmbedderNativeGin};
Cookies::Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context) Cookies::Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
: browser_context_(browser_context) { : browser_context_(browser_context) {
Init(isolate);
cookie_change_subscription_ = cookie_change_subscription_ =
browser_context_->cookie_change_notifier()->RegisterCookieChangeCallback( browser_context_->cookie_change_notifier()->RegisterCookieChangeCallback(
base::BindRepeating(&Cookies::OnCookieChanged, base::BindRepeating(&Cookies::OnCookieChanged,
@ -184,16 +185,17 @@ Cookies::Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
Cookies::~Cookies() = default; Cookies::~Cookies() = default;
v8::Local<v8::Promise> Cookies::Get(const gin_helper::Dictionary& filter) { v8::Local<v8::Promise> Cookies::Get(v8::Isolate* isolate,
gin_helper::Promise<net::CookieList> promise(isolate()); const gin_helper::Dictionary& filter) {
gin_helper::Promise<net::CookieList> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition( auto* storage_partition =
browser_context_.get()); content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess(); auto* manager = storage_partition->GetCookieManagerForBrowserProcess();
base::DictionaryValue dict; base::DictionaryValue dict;
gin::ConvertFromV8(isolate(), filter.GetHandle(), &dict); gin::ConvertFromV8(isolate, filter.GetHandle(), &dict);
std::string url; std::string url;
filter.Get("url", &url); filter.Get("url", &url);
@ -215,17 +217,18 @@ v8::Local<v8::Promise> Cookies::Get(const gin_helper::Dictionary& filter) {
return handle; return handle;
} }
v8::Local<v8::Promise> Cookies::Remove(const GURL& url, v8::Local<v8::Promise> Cookies::Remove(v8::Isolate* isolate,
const GURL& url,
const std::string& name) { const std::string& name) {
gin_helper::Promise<void> promise(isolate()); gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
auto cookie_deletion_filter = network::mojom::CookieDeletionFilter::New(); auto cookie_deletion_filter = network::mojom::CookieDeletionFilter::New();
cookie_deletion_filter->url = url; cookie_deletion_filter->url = url;
cookie_deletion_filter->cookie_name = name; cookie_deletion_filter->cookie_name = name;
auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition( auto* storage_partition =
browser_context_.get()); content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess(); auto* manager = storage_partition->GetCookieManagerForBrowserProcess();
manager->DeleteCookies( manager->DeleteCookies(
@ -239,8 +242,9 @@ v8::Local<v8::Promise> Cookies::Remove(const GURL& url,
return handle; return handle;
} }
v8::Local<v8::Promise> Cookies::Set(base::DictionaryValue details) { v8::Local<v8::Promise> Cookies::Set(v8::Isolate* isolate,
gin_helper::Promise<void> promise(isolate()); const base::DictionaryValue& details) {
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
const std::string* url_string = details.FindStringKey("url"); const std::string* url_string = details.FindStringKey("url");
@ -280,8 +284,8 @@ v8::Local<v8::Promise> Cookies::Set(base::DictionaryValue details) {
options.set_include_httponly(); options.set_include_httponly();
} }
auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition( auto* storage_partition =
browser_context_.get()); content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess(); auto* manager = storage_partition->GetCookieManagerForBrowserProcess();
manager->SetCanonicalCookie( manager->SetCanonicalCookie(
*canonical_cookie, url.scheme(), options, *canonical_cookie, url.scheme(), options,
@ -299,12 +303,12 @@ v8::Local<v8::Promise> Cookies::Set(base::DictionaryValue details) {
return handle; return handle;
} }
v8::Local<v8::Promise> Cookies::FlushStore() { v8::Local<v8::Promise> Cookies::FlushStore(v8::Isolate* isolate) {
gin_helper::Promise<void> promise(isolate()); gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition( auto* storage_partition =
browser_context_.get()); content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess(); auto* manager = storage_partition->GetCookieManagerForBrowserProcess();
manager->FlushCookieStore(base::BindOnce( manager->FlushCookieStore(base::BindOnce(
@ -314,10 +318,11 @@ v8::Local<v8::Promise> Cookies::FlushStore() {
} }
void Cookies::OnCookieChanged(const net::CookieChangeInfo& change) { void Cookies::OnCookieChanged(const net::CookieChangeInfo& change) {
v8::HandleScope scope(isolate()); v8::Isolate* isolate = v8::Isolate::GetCurrent();
Emit("changed", gin::ConvertToV8(isolate(), change.cookie), v8::HandleScope scope(isolate);
gin::ConvertToV8(isolate(), change.cause), Emit("changed", gin::ConvertToV8(isolate, change.cookie),
gin::ConvertToV8(isolate(), gin::ConvertToV8(isolate, change.cause),
gin::ConvertToV8(isolate,
change.cause != net::CookieChangeCause::INSERTED)); change.cause != net::CookieChangeCause::INSERTED));
} }
@ -327,17 +332,20 @@ gin::Handle<Cookies> Cookies::Create(v8::Isolate* isolate,
return gin::CreateHandle(isolate, new Cookies(isolate, browser_context)); return gin::CreateHandle(isolate, new Cookies(isolate, browser_context));
} }
// static gin::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
void Cookies::BuildPrototype(v8::Isolate* isolate, v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> prototype) { return gin_helper::EventEmitterMixin<Cookies>::GetObjectTemplateBuilder(
prototype->SetClassName(gin::StringToV8(isolate, "Cookies")); isolate)
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("get", &Cookies::Get) .SetMethod("get", &Cookies::Get)
.SetMethod("remove", &Cookies::Remove) .SetMethod("remove", &Cookies::Remove)
.SetMethod("set", &Cookies::Set) .SetMethod("set", &Cookies::Set)
.SetMethod("flushStore", &Cookies::FlushStore); .SetMethod("flushStore", &Cookies::FlushStore);
} }
const char* Cookies::GetTypeName() {
return "Cookies";
}
} // namespace api } // namespace api
} // namespace electron } // namespace electron

Просмотреть файл

@ -12,6 +12,7 @@
#include "gin/handle.h" #include "gin/handle.h"
#include "net/cookies/canonical_cookie.h" #include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_change_dispatcher.h" #include "net/cookies/cookie_change_dispatcher.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/promise.h" #include "shell/common/gin_helper/promise.h"
#include "shell/common/gin_helper/trackable_object.h" #include "shell/common/gin_helper/trackable_object.h"
@ -33,23 +34,30 @@ class ElectronBrowserContext;
namespace api { namespace api {
class Cookies : public gin_helper::TrackableObject<Cookies> { class Cookies : public gin::Wrappable<Cookies>,
public gin_helper::EventEmitterMixin<Cookies> {
public: public:
static gin::Handle<Cookies> Create(v8::Isolate* isolate, static gin::Handle<Cookies> Create(v8::Isolate* isolate,
ElectronBrowserContext* browser_context); ElectronBrowserContext* browser_context);
// gin_helper::TrackableObject: // gin::Wrappable
static void BuildPrototype(v8::Isolate* isolate, static gin::WrapperInfo kWrapperInfo;
v8::Local<v8::FunctionTemplate> prototype); gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
protected: protected:
Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context); Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context);
~Cookies() override; ~Cookies() override;
v8::Local<v8::Promise> Get(const gin_helper::Dictionary& filter); v8::Local<v8::Promise> Get(v8::Isolate*,
v8::Local<v8::Promise> Set(base::DictionaryValue details); const gin_helper::Dictionary& filter);
v8::Local<v8::Promise> Remove(const GURL& url, const std::string& name); v8::Local<v8::Promise> Set(v8::Isolate*,
v8::Local<v8::Promise> FlushStore(); const base::DictionaryValue& details);
v8::Local<v8::Promise> Remove(v8::Isolate*,
const GURL& url,
const std::string& name);
v8::Local<v8::Promise> FlushStore(v8::Isolate*);
// CookieChangeNotifier subscription: // CookieChangeNotifier subscription:
void OnCookieChanged(const net::CookieChangeInfo& change); void OnCookieChanged(const net::CookieChangeInfo& change);
@ -58,7 +66,9 @@ class Cookies : public gin_helper::TrackableObject<Cookies> {
std::unique_ptr<base::CallbackList<void( std::unique_ptr<base::CallbackList<void(
const net::CookieChangeInfo& change)>::Subscription> const net::CookieChangeInfo& change)>::Subscription>
cookie_change_subscription_; cookie_change_subscription_;
scoped_refptr<ElectronBrowserContext> browser_context_;
// Weak reference; ElectronBrowserContext is guaranteed to outlive us.
ElectronBrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(Cookies); DISALLOW_COPY_AND_ASSIGN(Cookies);
}; };

Просмотреть файл

@ -305,7 +305,6 @@ Session::~Session() {
// TODO(zcbenz): Now since URLRequestContextGetter is gone, is this still // TODO(zcbenz): Now since URLRequestContextGetter is gone, is this still
// needed? // needed?
// Refs https://github.com/electron/electron/pull/12305. // Refs https://github.com/electron/electron/pull/12305.
DestroyGlobalHandle(isolate(), cookies_);
DestroyGlobalHandle(isolate(), protocol_); DestroyGlobalHandle(isolate(), protocol_);
g_sessions.erase(weak_map_id()); g_sessions.erase(weak_map_id());
} }
@ -1053,9 +1052,6 @@ void Initialize(v8::Local<v8::Object> exports,
dict.Set( dict.Set(
"Session", "Session",
Session::GetConstructor(isolate)->GetFunction(context).ToLocalChecked()); Session::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set(
"Cookies",
Cookies::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set( dict.Set(
"Protocol", "Protocol",
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked()); Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());