This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathasync.cc
242 lines (203 loc) · 6.13 KB
/
async.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <string>
#include <vector>
#include "napi.h"
#include "keytar.h"
#include "async.h"
using keytar::KEYTAR_OP_RESULT;
SetPasswordWorker::SetPasswordWorker(
const std::string& service,
const std::string& account,
const std::string& password,
const Napi::Env &env
) : AsyncWorker(env),
service(service),
account(account),
password(password),
deferred(Napi::Promise::Deferred::New(env)) {}
SetPasswordWorker::~SetPasswordWorker() {}
Napi::Promise SetPasswordWorker::Promise() {
return deferred.Promise();
}
void SetPasswordWorker::Execute() {
std::string error;
KEYTAR_OP_RESULT result = keytar::SetPassword(service,
account,
password,
&error);
if (result == keytar::FAIL_ERROR) {
SetError(error.c_str());
}
}
void SetPasswordWorker::OnOK() {
Napi::HandleScope scope(Env());
deferred.Resolve(Env().Undefined());
}
void SetPasswordWorker::OnError(Napi::Error const &error) {
Napi::HandleScope scope(Env());
deferred.Reject(error.Value());
}
GetPasswordWorker::GetPasswordWorker(
const std::string& service,
const std::string& account,
const Napi::Env &env
) : AsyncWorker(env),
service(service),
account(account),
deferred(Napi::Promise::Deferred::New(env)) {}
GetPasswordWorker::~GetPasswordWorker() {}
Napi::Promise GetPasswordWorker::Promise() {
return deferred.Promise();
}
void GetPasswordWorker::Execute() {
std::string error;
KEYTAR_OP_RESULT result = keytar::GetPassword(service,
account,
&password,
&error);
if (result == keytar::FAIL_ERROR) {
SetError(error.c_str());
} else if (result == keytar::FAIL_NONFATAL) {
success = false;
} else {
success = true;
}
}
void GetPasswordWorker::OnOK() {
Napi::HandleScope scope(Env());
Napi::Value val = Env().Null();
if (success) {
val = Napi::String::New(Env(), password.data(),
password.length());
}
deferred.Resolve(val);
}
void GetPasswordWorker::OnError(Napi::Error const &error) {
Napi::HandleScope scope(Env());
deferred.Reject(error.Value());
}
DeletePasswordWorker::DeletePasswordWorker(
const std::string& service,
const std::string& account,
const Napi::Env &env
) : AsyncWorker(env),
service(service),
account(account),
deferred(Napi::Promise::Deferred::New(env)) {}
DeletePasswordWorker::~DeletePasswordWorker() {}
Napi::Promise DeletePasswordWorker::Promise() {
return deferred.Promise();
}
void DeletePasswordWorker::Execute() {
std::string error;
KEYTAR_OP_RESULT result = keytar::DeletePassword(service, account, &error);
if (result == keytar::FAIL_ERROR) {
SetError(error.c_str());
} else if (result == keytar::FAIL_NONFATAL) {
success = false;
} else {
success = true;
}
}
void DeletePasswordWorker::OnOK() {
Napi::HandleScope scope(Env());
deferred.Resolve(Napi::Boolean::New(Env(), success));
}
void DeletePasswordWorker::OnError(Napi::Error const &error) {
Napi::HandleScope scope(Env());
deferred.Reject(error.Value());
}
FindPasswordWorker::FindPasswordWorker(
const std::string& service,
const Napi::Env &env
) : AsyncWorker(env),
service(service),
deferred(Napi::Promise::Deferred::New(env)) {}
FindPasswordWorker::~FindPasswordWorker() {}
Napi::Promise FindPasswordWorker::Promise() {
return deferred.Promise();
}
void FindPasswordWorker::Execute() {
std::string error;
KEYTAR_OP_RESULT result = keytar::FindPassword(service,
&password,
&error);
if (result == keytar::FAIL_ERROR) {
SetError(error.c_str());
} else if (result == keytar::FAIL_NONFATAL) {
success = false;
} else {
success = true;
}
}
void FindPasswordWorker::OnOK() {
Napi::HandleScope scope(Env());
Napi::Value val = Env().Null();
if (success) {
val = Napi::String::New(Env(), password.data(),
password.length());
}
deferred.Resolve(val);
}
void FindPasswordWorker::OnError(Napi::Error const &error) {
Napi::HandleScope scope(Env());
deferred.Reject(error.Value());
}
FindCredentialsWorker::FindCredentialsWorker(
const std::string& service,
const Napi::Env &env
) : AsyncWorker(env),
service(service),
deferred(Napi::Promise::Deferred::New(env)) {}
FindCredentialsWorker::~FindCredentialsWorker() {}
Napi::Promise FindCredentialsWorker::Promise() {
return deferred.Promise();
}
void FindCredentialsWorker::Execute() {
std::string error;
KEYTAR_OP_RESULT result = keytar::FindCredentials(service,
&credentials,
&error);
if (result == keytar::FAIL_ERROR) {
SetError(error.c_str());
} else if (result == keytar::FAIL_NONFATAL) {
success = false;
} else {
success = true;
}
}
void FindCredentialsWorker::OnOK() {
Napi::HandleScope scope(Env());
Napi::Env env = Env();
if (success) {
Napi::Array val = Napi::Array::New(env, credentials.size());
unsigned int idx = 0;
std::vector<keytar::Credentials>::iterator it;
for (it = credentials.begin(); it != credentials.end(); it++) {
keytar::Credentials cred = *it;
Napi::Object obj = Napi::Object::New(env);
Napi::String account = Napi::String::New(env,
cred.first.data(),
cred.first.length());
Napi::String password = Napi::String::New(env,
cred.second.data(),
cred.second.length());
#ifndef _WIN32
#pragma GCC diagnostic ignored "-Wunused-result"
#endif
obj.Set("account", account);
#ifndef _WIN32
#pragma GCC diagnostic ignored "-Wunused-result"
#endif
obj.Set("password", password);
(val).Set(idx, obj);
++idx;
}
deferred.Resolve(val);
} else {
deferred.Resolve(Napi::Array::New(env, 0));
}
}
void FindCredentialsWorker::OnError(Napi::Error const &error) {
Napi::HandleScope scope(Env());
deferred.Reject(error.Value());
}