Select Git revision
Asad Salman authored and
Adrien Beraud
committed
Also made the registration of callbacks and init() an atomic function call. Change-Id: Ifdc8e0dc20c8d617e9c61320634e257e7f264277
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
callback.h 7.99 KiB
#pragma once
#define V8_STRING_NEW(str) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str.data(), v8::String::kNormalString, str.size())
using namespace v8;
Persistent<Function> accountsChangedCb;
Persistent<Function> registrationStateChangedCb;
Persistent<Function> volatileDetailsChangedCb;
Persistent<Function> incomingAccountMessageCb;
Persistent<Function> accountMessageStatusChangedCb;
Persistent<Function> incomingTrustRequestCb;
Persistent<Function> contactAddedCb;
Persistent<Function> contactRemovedCb;
Persistent<Function> exportOnRingEndedCb;
Persistent<Function> nameRegistrationEndedCb;
Persistent<Function> knownDevicesChangedCb;
Persistent<Function> registeredNameFoundCb;
Persistent<Function>* getPresistentCb(const std::string &signal){
if (signal == "AccountsChanged")
return &accountsChangedCb;
else if (signal == "RegistrationStateChanged")
return ®istrationStateChangedCb;
else if (signal == "VolatileDetailsChanged")
return &volatileDetailsChangedCb;
else if (signal == "IncomingAccountMessage")
return &incomingAccountMessageCb;
else if (signal == "AccountMessageStatusChanged")
return &accountMessageStatusChangedCb;
else if (signal == "IncomingTrustRequest")
return &incomingTrustRequestCb;
else if (signal == "ContactAdded")
return &contactAddedCb;
else if (signal == "ContactRemoved")
return &contactRemovedCb;
else if (signal == "ExportOnRingEnded")
return &exportOnRingEndedCb;
else if (signal == "NameRegistrationEnded")
return &nameRegistrationEndedCb;
else if (signal == "KnownDevicesChanged")
return &knownDevicesChangedCb;
else if (signal == "RegisteredNameFound")
return ®isteredNameFoundCb;
else return nullptr;
}
void setCallback(const std::string& signal, Local<Function>& func){
if (auto* presistentCb = getPresistentCb(signal)) {
if (func->IsObject() && func->IsFunction()) {
presistentCb->Reset(Isolate::GetCurrent(), func);
} else {
presistentCb->Reset();
}
}
else {
printf("No Signal Associated with Event \'%s\'\n", signal.c_str() );
}
}
void parseCbMap(const Local<Value>& arg){
Local<Object> array = arg->ToObject();
Local<Array> props = array->GetOwnPropertyNames();
for (uint32_t i = 0; i < props->Length(); ++i) {
const Local<Value> key_local = props->Get(i);
std::string key = *String::Utf8Value(key_local);
Handle<Object> buffer = array->Get(V8_STRING_NEW(key))->ToObject();
Local<Function> func = Local<Function>::Cast(buffer);
setCallback(key,func);
}
}
void registrationStateChanged(const std::string& account_id,const std::string& state,int code,const std::string& detail_str){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), registrationStateChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), V8_STRING_NEW(state),
SWIGV8_INTEGER_NEW(code),V8_STRING_NEW(detail_str) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void accountsChanged(){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), accountsChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 0, callback_args);
}
}
void contactAdded(const std::string& account_id, const std::string& uri, bool confirmed){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), contactAddedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),V8_STRING_NEW(uri),
SWIGV8_BOOLEAN_NEW(confirmed)};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void contactRemoved(const std::string& account_id, const std::string& uri, bool banned){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), contactRemovedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),V8_STRING_NEW(uri),
SWIGV8_BOOLEAN_NEW(banned) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void exportOnRingEnded(const std::string& account_id, int state, const std::string& pin){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), exportOnRingEndedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id), SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(pin) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void nameRegistrationEnded(const std::string& account_id, int state, const std::string& name){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), nameRegistrationEndedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(name)};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void registeredNameFound(const std::string& account_id, int state, const std::string& address, const std::string& name){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), registeredNameFoundCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(address),V8_STRING_NEW(name) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
/*
void accountMessageStatusChanged(const std::string& account_id, uint64_t message_id, const std::string& to, int state){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), accountMessageStatusChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { SWIGV8_STRING_NEW(account_id.c_str()), SWIGV8_INTEGER_NEW(message_id)
SWIGV8_STRING_NEW(to.c_str()),SWIGV8_INTEGER_NEW(state) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void incomingTrustRequest(const std::string& account_id, const std::string& from, const std::vector<uint8_t>& payload, time_t received){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingTrustRequestCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void knownDevicesChanged(const std::string& account_id, const std::map<std::string, std::string>& devices){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), knownDevicesChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}*/
/*void volatileDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details ){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), volatileDetailsChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void incomingAccountMessage(const std::string& account_id, const std::string& from, const std::map<std::string, std::string>& payloads){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingAccountMessageCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}*/