Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ginify ServiceWorkerContext #22756

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/browser/api/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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 () {
Expand Down
19 changes: 12 additions & 7 deletions shell/browser/api/electron_api_service_worker_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,11 +68,12 @@ v8::Local<v8::Value> 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();
Expand Down Expand Up @@ -140,17 +142,20 @@ gin::Handle<ServiceWorkerContext> ServiceWorkerContext::Create(
}

// static
void ServiceWorkerContext::BuildPrototype(
v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> 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
15 changes: 10 additions & 5 deletions shell/browser/api/electron_api_service_worker_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -17,16 +18,14 @@ class ElectronBrowserContext;
namespace api {

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

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

v8::Local<v8::Value> GetAllRunningWorkerInfo(v8::Isolate* isolate);
v8::Local<v8::Value> GetWorkerInfoFromID(gin_helper::ErrorThrower thrower,
int64_t version_id);
Expand All @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions shell/browser/api/electron_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,6 @@ void Initialize(v8::Local<v8::Object> exports,
dict.Set(
"Protocol",
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set("ServiceWorkerContext", ServiceWorkerContext::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
dict.SetMethod("fromPartition", &FromPartition);
}

Expand Down