Skip to content
Permalink
Browse files

refactor: ginify Cookies (#22823)

  • Loading branch information
nornagon committed Mar 25, 2020
1 parent b327478 commit 222022556f223b3f059238a04fef4828af19f852
@@ -2,7 +2,7 @@

const { EventEmitter } = require('events');
const { app, deprecate } = require('electron');
const { fromPartition, Session, Cookies, Protocol } = process.electronBinding('session');
const { fromPartition, Session, Protocol } = process.electronBinding('session');

// Public API.
Object.defineProperties(exports, {
@@ -16,7 +16,6 @@ Object.defineProperties(exports, {
}
});

Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype);
Object.setPrototypeOf(Session.prototype, EventEmitter.prototype);

Session.prototype._init = function () {
@@ -173,9 +173,10 @@ std::string InclusionStatusToString(

} // namespace

gin::WrapperInfo Cookies::kWrapperInfo = {gin::kEmbedderNativeGin};

Cookies::Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
: browser_context_(browser_context) {
Init(isolate);
cookie_change_subscription_ =
browser_context_->cookie_change_notifier()->RegisterCookieChangeCallback(
base::BindRepeating(&Cookies::OnCookieChanged,
@@ -184,16 +185,17 @@ Cookies::Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context)

Cookies::~Cookies() = default;

v8::Local<v8::Promise> Cookies::Get(const gin_helper::Dictionary& filter) {
gin_helper::Promise<net::CookieList> promise(isolate());
v8::Local<v8::Promise> Cookies::Get(v8::Isolate* isolate,
const gin_helper::Dictionary& filter) {
gin_helper::Promise<net::CookieList> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition(
browser_context_.get());
auto* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess();

base::DictionaryValue dict;
gin::ConvertFromV8(isolate(), filter.GetHandle(), &dict);
gin::ConvertFromV8(isolate, filter.GetHandle(), &dict);

std::string url;
filter.Get("url", &url);
@@ -215,17 +217,18 @@ v8::Local<v8::Promise> Cookies::Get(const gin_helper::Dictionary& filter) {
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) {
gin_helper::Promise<void> promise(isolate());
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

auto cookie_deletion_filter = network::mojom::CookieDeletionFilter::New();
cookie_deletion_filter->url = url;
cookie_deletion_filter->cookie_name = name;

auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition(
browser_context_.get());
auto* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess();

manager->DeleteCookies(
@@ -239,8 +242,9 @@ v8::Local<v8::Promise> Cookies::Remove(const GURL& url,
return handle;
}

v8::Local<v8::Promise> Cookies::Set(base::DictionaryValue details) {
gin_helper::Promise<void> promise(isolate());
v8::Local<v8::Promise> Cookies::Set(v8::Isolate* isolate,
const base::DictionaryValue& details) {
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

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();
}

auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition(
browser_context_.get());
auto* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess();
manager->SetCanonicalCookie(
*canonical_cookie, url.scheme(), options,
@@ -299,12 +303,12 @@ v8::Local<v8::Promise> Cookies::Set(base::DictionaryValue details) {
return handle;
}

v8::Local<v8::Promise> Cookies::FlushStore() {
gin_helper::Promise<void> promise(isolate());
v8::Local<v8::Promise> Cookies::FlushStore(v8::Isolate* isolate) {
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

auto* storage_partition = content::BrowserContext::GetDefaultStoragePartition(
browser_context_.get());
auto* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context_);
auto* manager = storage_partition->GetCookieManagerForBrowserProcess();

manager->FlushCookieStore(base::BindOnce(
@@ -314,10 +318,11 @@ v8::Local<v8::Promise> Cookies::FlushStore() {
}

void Cookies::OnCookieChanged(const net::CookieChangeInfo& change) {
v8::HandleScope scope(isolate());
Emit("changed", gin::ConvertToV8(isolate(), change.cookie),
gin::ConvertToV8(isolate(), change.cause),
gin::ConvertToV8(isolate(),
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
Emit("changed", gin::ConvertToV8(isolate, change.cookie),
gin::ConvertToV8(isolate, change.cause),
gin::ConvertToV8(isolate,
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));
}

// static
void Cookies::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "Cookies"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<Cookies>::GetObjectTemplateBuilder(
isolate)
.SetMethod("get", &Cookies::Get)
.SetMethod("remove", &Cookies::Remove)
.SetMethod("set", &Cookies::Set)
.SetMethod("flushStore", &Cookies::FlushStore);
}

const char* Cookies::GetTypeName() {
return "Cookies";
}

} // namespace api

} // namespace electron
@@ -12,6 +12,7 @@
#include "gin/handle.h"
#include "net/cookies/canonical_cookie.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/trackable_object.h"

@@ -33,23 +34,30 @@ class ElectronBrowserContext;

namespace api {

class Cookies : public gin_helper::TrackableObject<Cookies> {
class Cookies : public gin::Wrappable<Cookies>,
public gin_helper::EventEmitterMixin<Cookies> {
public:
static gin::Handle<Cookies> Create(v8::Isolate* isolate,
ElectronBrowserContext* browser_context);

// gin_helper::TrackableObject:
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;

protected:
Cookies(v8::Isolate* isolate, ElectronBrowserContext* browser_context);
~Cookies() override;

v8::Local<v8::Promise> Get(const gin_helper::Dictionary& filter);
v8::Local<v8::Promise> Set(base::DictionaryValue details);
v8::Local<v8::Promise> Remove(const GURL& url, const std::string& name);
v8::Local<v8::Promise> FlushStore();
v8::Local<v8::Promise> Get(v8::Isolate*,
const gin_helper::Dictionary& filter);
v8::Local<v8::Promise> Set(v8::Isolate*,
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:
void OnCookieChanged(const net::CookieChangeInfo& change);
@@ -58,7 +66,9 @@ class Cookies : public gin_helper::TrackableObject<Cookies> {
std::unique_ptr<base::CallbackList<void(
const net::CookieChangeInfo& 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);
};
@@ -305,7 +305,6 @@ Session::~Session() {
// TODO(zcbenz): Now since URLRequestContextGetter is gone, is this still
// needed?
// Refs https://github.com/electron/electron/pull/12305.
DestroyGlobalHandle(isolate(), cookies_);
DestroyGlobalHandle(isolate(), protocol_);
g_sessions.erase(weak_map_id());
}
@@ -1053,9 +1052,6 @@ void Initialize(v8::Local<v8::Object> exports,
dict.Set(
"Session",
Session::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set(
"Cookies",
Cookies::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set(
"Protocol",
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());

0 comments on commit 2220225

Please sign in to comment.
You can’t perform that action at this time.