From 22c17bcc5b73c915267d163c09566294c21baa7b Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Fri, 20 Mar 2020 14:15:14 -0700 Subject: [PATCH] refactor: ginify ServiceWorkerContext (#22756) --- lib/browser/api/session.js | 3 +-- .../electron_api_service_worker_context.cc | 19 ++++++++++++------- .../api/electron_api_service_worker_context.h | 15 ++++++++++----- shell/browser/api/electron_api_session.cc | 3 --- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index 3babd2a2cb0e..5a087734ad43 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, ServiceWorkerContext } = process.electronBinding('session'); +const { fromPartition, Session, Cookies, Protocol } = process.electronBinding('session'); // Public API. Object.defineProperties(exports, { @@ -17,7 +17,6 @@ Object.defineProperties(exports, { }); Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype); -Object.setPrototypeOf(ServiceWorkerContext.prototype, EventEmitter.prototype); Object.setPrototypeOf(Session.prototype, EventEmitter.prototype); Session.prototype._init = function () { diff --git a/shell/browser/api/electron_api_service_worker_context.cc b/shell/browser/api/electron_api_service_worker_context.cc index 4dc1d2cdf29e..6f646cf9cabe 100644 --- a/shell/browser/api/electron_api_service_worker_context.cc +++ b/shell/browser/api/electron_api_service_worker_context.cc @@ -12,10 +12,11 @@ #include "content/public/browser/storage_partition.h" #include "gin/data_object_builder.h" #include "gin/handle.h" +#include "gin/object_template_builder.h" #include "shell/browser/electron_browser_context.h" #include "shell/common/gin_converters/value_converter.h" #include "shell/common/gin_helper/dictionary.h" -#include "shell/common/gin_helper/object_template_builder.h" +#include "shell/common/gin_helper/function_template_extensions.h" #include "shell/common/node_includes.h" namespace electron { @@ -67,11 +68,12 @@ v8::Local ServiceWorkerRunningInfoToDict( } // namespace +gin::WrapperInfo ServiceWorkerContext::kWrapperInfo = {gin::kEmbedderNativeGin}; + ServiceWorkerContext::ServiceWorkerContext( v8::Isolate* isolate, ElectronBrowserContext* browser_context) : browser_context_(browser_context), weak_ptr_factory_(this) { - Init(isolate); service_worker_context_ = content::BrowserContext::GetDefaultStoragePartition(browser_context_) ->GetServiceWorkerContext(); @@ -140,17 +142,20 @@ gin::Handle ServiceWorkerContext::Create( } // static -void ServiceWorkerContext::BuildPrototype( - v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(gin::StringToV8(isolate, "ServiceWorkerContext")); - gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) +gin::ObjectTemplateBuilder ServiceWorkerContext::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return gin_helper::EventEmitterMixin< + ServiceWorkerContext>::GetObjectTemplateBuilder(isolate) .SetMethod("getAllRunning", &ServiceWorkerContext::GetAllRunningWorkerInfo) .SetMethod("getFromVersionID", &ServiceWorkerContext::GetWorkerInfoFromID); } +const char* ServiceWorkerContext::GetTypeName() { + return "ServiceWorkerContext"; +} + } // namespace api } // namespace electron diff --git a/shell/browser/api/electron_api_service_worker_context.h b/shell/browser/api/electron_api_service_worker_context.h index c0586259c34d..c4d4ba8b4c13 100644 --- a/shell/browser/api/electron_api_service_worker_context.h +++ b/shell/browser/api/electron_api_service_worker_context.h @@ -8,7 +8,8 @@ #include "content/public/browser/service_worker_context.h" #include "content/public/browser/service_worker_context_observer.h" #include "gin/handle.h" -#include "shell/common/gin_helper/trackable_object.h" +#include "gin/wrappable.h" +#include "shell/browser/event_emitter_mixin.h" namespace electron { @@ -17,16 +18,14 @@ class ElectronBrowserContext; namespace api { class ServiceWorkerContext - : public gin_helper::TrackableObject, + : public gin::Wrappable, + public gin_helper::EventEmitterMixin, public content::ServiceWorkerContextObserver { public: static gin::Handle Create( v8::Isolate* isolate, ElectronBrowserContext* browser_context); - static void BuildPrototype(v8::Isolate* isolate, - v8::Local prototype); - v8::Local GetAllRunningWorkerInfo(v8::Isolate* isolate); v8::Local GetWorkerInfoFromID(gin_helper::ErrorThrower thrower, int64_t version_id); @@ -36,6 +35,12 @@ class ServiceWorkerContext const content::ConsoleMessage& message) override; void OnDestruct(content::ServiceWorkerContext* context) override; + // gin::Wrappable + static gin::WrapperInfo kWrapperInfo; + gin::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + const char* GetTypeName() override; + protected: explicit ServiceWorkerContext(v8::Isolate* isolate, ElectronBrowserContext* browser_context); diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 80a68d06f92d..7bd6a42949e8 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -1059,9 +1059,6 @@ void Initialize(v8::Local exports, dict.Set( "Protocol", Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked()); - dict.Set("ServiceWorkerContext", ServiceWorkerContext::GetConstructor(isolate) - ->GetFunction(context) - .ToLocalChecked()); dict.SetMethod("fromPartition", &FromPartition); }