diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index 5a087734ad..bde49e7fa2 100644 --- a/lib/browser/api/session.js +++ b/lib/browser/api/session.js @@ -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 () { diff --git a/shell/browser/api/electron_api_cookies.cc b/shell/browser/api/electron_api_cookies.cc index 88a899f9cd..6c3c66007d 100644 --- a/shell/browser/api/electron_api_cookies.cc +++ b/shell/browser/api/electron_api_cookies.cc @@ -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 Cookies::Get(const gin_helper::Dictionary& filter) { - gin_helper::Promise promise(isolate()); +v8::Local Cookies::Get(v8::Isolate* isolate, + const gin_helper::Dictionary& filter) { + gin_helper::Promise promise(isolate); v8::Local 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 Cookies::Get(const gin_helper::Dictionary& filter) { return handle; } -v8::Local Cookies::Remove(const GURL& url, +v8::Local Cookies::Remove(v8::Isolate* isolate, + const GURL& url, const std::string& name) { - gin_helper::Promise promise(isolate()); + gin_helper::Promise promise(isolate); v8::Local 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 Cookies::Remove(const GURL& url, return handle; } -v8::Local Cookies::Set(base::DictionaryValue details) { - gin_helper::Promise promise(isolate()); +v8::Local Cookies::Set(v8::Isolate* isolate, + const base::DictionaryValue& details) { + gin_helper::Promise promise(isolate); v8::Local handle = promise.GetHandle(); const std::string* url_string = details.FindStringKey("url"); @@ -280,8 +284,8 @@ v8::Local 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 Cookies::Set(base::DictionaryValue details) { return handle; } -v8::Local Cookies::FlushStore() { - gin_helper::Promise promise(isolate()); +v8::Local Cookies::FlushStore(v8::Isolate* isolate) { + gin_helper::Promise promise(isolate); v8::Local 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 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::Create(v8::Isolate* isolate, return gin::CreateHandle(isolate, new Cookies(isolate, browser_context)); } -// static -void Cookies::BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(gin::StringToV8(isolate, "Cookies")); - gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) +gin::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return gin_helper::EventEmitterMixin::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 diff --git a/shell/browser/api/electron_api_cookies.h b/shell/browser/api/electron_api_cookies.h index 6689585a18..b94542b677 100644 --- a/shell/browser/api/electron_api_cookies.h +++ b/shell/browser/api/electron_api_cookies.h @@ -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 { +class Cookies : public gin::Wrappable, + public gin_helper::EventEmitterMixin { public: static gin::Handle Create(v8::Isolate* isolate, ElectronBrowserContext* browser_context); - // gin_helper::TrackableObject: - static void BuildPrototype(v8::Isolate* isolate, - v8::Local 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 Get(const gin_helper::Dictionary& filter); - v8::Local Set(base::DictionaryValue details); - v8::Local Remove(const GURL& url, const std::string& name); - v8::Local FlushStore(); + v8::Local Get(v8::Isolate*, + const gin_helper::Dictionary& filter); + v8::Local Set(v8::Isolate*, + const base::DictionaryValue& details); + v8::Local Remove(v8::Isolate*, + const GURL& url, + const std::string& name); + v8::Local FlushStore(v8::Isolate*); // CookieChangeNotifier subscription: void OnCookieChanged(const net::CookieChangeInfo& change); @@ -58,7 +66,9 @@ class Cookies : public gin_helper::TrackableObject { std::unique_ptr::Subscription> cookie_change_subscription_; - scoped_refptr browser_context_; + + // Weak reference; ElectronBrowserContext is guaranteed to outlive us. + ElectronBrowserContext* browser_context_; DISALLOW_COPY_AND_ASSIGN(Cookies); }; diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 7bd6a42949..1c500cba2f 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -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 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());