Skip to content

Commit

Permalink
refactor: ginify Notification (#22821)
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Mar 31, 2020
1 parent 629465a commit 765c08c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
3 changes: 0 additions & 3 deletions lib/browser/api/notification.js
@@ -1,10 +1,7 @@
'use strict';

const { EventEmitter } = require('events');
const { Notification, isSupported } = process.electronBinding('notification');

Object.setPrototypeOf(Notification.prototype, EventEmitter.prototype);

Notification.isSupported = isSupported;

module.exports = Notification;
34 changes: 14 additions & 20 deletions shell/browser/api/electron_api_notification.cc
Expand Up @@ -6,6 +6,7 @@

#include "base/guid.h"
#include "base/strings/utf_string_conversions.h"
#include "gin/handle.h"
#include "shell/browser/api/electron_api_menu.h"
#include "shell/browser/browser.h"
#include "shell/browser/electron_browser_client.h"
Expand Down Expand Up @@ -48,9 +49,9 @@ namespace electron {

namespace api {

Notification::Notification(gin::Arguments* args) {
InitWithArgs(args);
gin::WrapperInfo Notification::kWrapperInfo = {gin::kEmbedderNativeGin};

Notification::Notification(gin::Arguments* args) {
presenter_ = static_cast<ElectronBrowserClient*>(ElectronBrowserClient::Get())
->GetNotificationPresenter();

Expand Down Expand Up @@ -80,13 +81,13 @@ Notification::~Notification() {
}

// static
gin_helper::WrappableBase* Notification::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
gin::Handle<Notification> Notification::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create Notification before app is ready");
return nullptr;
return gin::Handle<Notification>();
}
return new Notification(args);
return gin::CreateHandle(thrower.isolate(), new Notification(args));
}

// Getters
Expand Down Expand Up @@ -240,12 +241,10 @@ bool Notification::IsSupported() {
->GetNotificationPresenter();
}

// static
void Notification::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "Notification"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
v8::Local<v8::ObjectTemplate> Notification::FillObjectTemplate(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ) {
return gin::ObjectTemplateBuilder(isolate, "Notification", templ)
.SetMethod("show", &Notification::Show)
.SetMethod("close", &Notification::Close)
.SetProperty("title", &Notification::GetTitle, &Notification::SetTitle)
Expand All @@ -265,7 +264,8 @@ void Notification::BuildPrototype(v8::Isolate* isolate,
.SetProperty("actions", &Notification::GetActions,
&Notification::SetActions)
.SetProperty("closeButtonText", &Notification::GetCloseButtonText,
&Notification::SetCloseButtonText);
&Notification::SetCloseButtonText)
.Build();
}

} // namespace api
Expand All @@ -281,14 +281,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
Notification::SetConstructor(isolate,
base::BindRepeating(&Notification::New));

gin_helper::Dictionary dict(isolate, exports);
dict.Set("Notification", Notification::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());

dict.Set("Notification", Notification::GetConstructor(context));
dict.SetMethod("isSupported", &Notification::IsSupported);
}

Expand Down
27 changes: 20 additions & 7 deletions shell/browser/api/electron_api_notification.h
Expand Up @@ -10,30 +10,40 @@
#include <vector>

#include "base/strings/utf_string_conversions.h"
#include "gin/wrappable.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/browser/notifications/notification.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/notifications/notification_presenter.h"
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
#include "shell/common/gin_helper/constructible.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/trackable_object.h"
#include "ui/gfx/image/image.h"

namespace gin {
class Arguments;
}
template <typename T>
class Handle;
} // namespace gin

namespace electron {

namespace api {

class Notification : public gin_helper::TrackableObject<Notification>,
class Notification : public gin::Wrappable<Notification>,
public gin_helper::EventEmitterMixin<Notification>,
public gin_helper::Constructible<Notification>,
public gin_helper::CleanedUpAtExit,
public NotificationDelegate {
public:
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static bool IsSupported();

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// gin_helper::Constructible
static gin::Handle<Notification> New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
v8::Isolate*,
v8::Local<v8::ObjectTemplate>);

// NotificationDelegate:
void NotificationAction(int index) override;
Expand All @@ -43,6 +53,9 @@ class Notification : public gin_helper::TrackableObject<Notification>,
void NotificationDestroyed() override;
void NotificationClosed() override;

// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;

protected:
explicit Notification(gin::Arguments* args);
~Notification() override;
Expand Down
34 changes: 34 additions & 0 deletions spec-main/api-notification-spec.ts
@@ -1,7 +1,13 @@
import { expect } from 'chai';
import { Notification } from 'electron';
import { emittedOnce } from './events-helpers';
import { ifit } from './spec-helpers';

describe('Notification module', () => {
it('is supported', () => {
expect(Notification.isSupported()).to.be.a('boolean');
});

it('inits, gets and sets basic string properties correctly', () => {
const n = new Notification({
title: 'title',
Expand Down Expand Up @@ -92,5 +98,33 @@ describe('Notification module', () => {
expect(n.actions[1].text).to.equal('4');
});

it('can be shown and closed', () => {
const n = new Notification({
title: 'test notification',
body: 'test body',
silent: true
});
n.show();
n.close();
});

ifit(process.platform === 'darwin')('emits show and close events', async () => {
const n = new Notification({
title: 'test notification',
body: 'test body',
silent: true
});
{
const e = emittedOnce(n, 'show');
n.show();
await e;
}
{
const e = emittedOnce(n, 'close');
n.close();
await e;
}
});

// TODO(sethlu): Find way to test init with notification icon?
});

0 comments on commit 765c08c

Please sign in to comment.