|
| 1 | +#include "qwebchannel_wrap.h" |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | +#include <QObject> |
| 5 | +#include <QWebChannel> |
| 6 | + |
| 7 | +#include "nodegui/Extras/Utils/nutils.h" |
| 8 | +#include "nodegui/QtCore/QObject/qobject_wrap.h" |
| 9 | + |
| 10 | +Napi::FunctionReference QWebChannelWrap::constructor; |
| 11 | + |
| 12 | +Napi::Object QWebChannelWrap::init(Napi::Env env, Napi::Object exports) { |
| 13 | + Napi::HandleScope scope(env); |
| 14 | + char CLASSNAME[] = "QWebChannel"; |
| 15 | + Napi::Function func = DefineClass( |
| 16 | + env, CLASSNAME, |
| 17 | + {InstanceMethod("registerObject", &QWebChannelWrap::registerObject), |
| 18 | + InstanceMethod("deregisterObject", &QWebChannelWrap::deregisterObject), |
| 19 | + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebChannelWrap)}); |
| 20 | + constructor = Napi::Persistent(func); |
| 21 | + exports.Set(CLASSNAME, func); |
| 22 | + return exports; |
| 23 | +} |
| 24 | + |
| 25 | +QWebChannelWrap::QWebChannelWrap(const Napi::CallbackInfo& info) |
| 26 | + : Napi::ObjectWrap<QWebChannelWrap>(info) { |
| 27 | + Napi::Env env = info.Env(); |
| 28 | + Napi::HandleScope scope(env); |
| 29 | + |
| 30 | + if (info[0].IsExternal()) { |
| 31 | + this->instance = info[0].As<Napi::External<NWebChannel>>().Data(); |
| 32 | + } else if (info.Length() == 1) { |
| 33 | + Napi::Object parentObject = info[0].As<Napi::Object>(); |
| 34 | + QObjectWrap* parentWrap = |
| 35 | + Napi::ObjectWrap<QObjectWrap>::Unwrap(parentObject); |
| 36 | + this->instance = new NWebChannel(parentWrap->getInternalInstance()); |
| 37 | + } else if (info.Length() == 0) { |
| 38 | + this->instance = new NWebChannel(); |
| 39 | + } else { |
| 40 | + Napi::TypeError::New(env, "Incorrect initialization of QWebChannelWrap") |
| 41 | + .ThrowAsJavaScriptException(); |
| 42 | + } |
| 43 | + this->rawData = extrautils::configureComponent(this->getInternalInstance()); |
| 44 | +} |
| 45 | + |
| 46 | +QWebChannelWrap::~QWebChannelWrap() { extrautils::safeDelete(this->instance); } |
| 47 | + |
| 48 | +NWebChannel* QWebChannelWrap::getInternalInstance() { return this->instance; } |
| 49 | + |
| 50 | +Napi::Value QWebChannelWrap::registerObject(const Napi::CallbackInfo& info) { |
| 51 | + Napi::Env env = info.Env(); |
| 52 | + Napi::HandleScope scope(env); |
| 53 | + |
| 54 | + Napi::String napiName = info[0].As<Napi::String>(); |
| 55 | + std::string name = napiName.Utf8Value(); |
| 56 | + |
| 57 | + Napi::Object napiObject = info[1].As<Napi::Object>(); |
| 58 | + QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject); |
| 59 | + |
| 60 | + this->instance->registerObject(QString::fromUtf8(name.c_str()), |
| 61 | + objectWrap->getInternalInstance()); |
| 62 | + return env.Null(); |
| 63 | +} |
| 64 | + |
| 65 | +Napi::Value QWebChannelWrap::deregisterObject(const Napi::CallbackInfo& info) { |
| 66 | + Napi::Env env = info.Env(); |
| 67 | + Napi::HandleScope scope(env); |
| 68 | + |
| 69 | + Napi::Object napiObject = info[0].As<Napi::Object>(); |
| 70 | + QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject); |
| 71 | + |
| 72 | + this->instance->deregisterObject(objectWrap->getInternalInstance()); |
| 73 | + return env.Null(); |
| 74 | +} |
0 commit comments