24
24
#include " app/src/app_common.h"
25
25
#include " app/src/app_identifier.h"
26
26
#include " app/src/base64.h"
27
+ #include " app/src/callback.h"
27
28
#include " app/src/cleanup_notifier.h"
28
29
#include " app/src/locale.h"
29
30
#include " app/src/time.h"
@@ -36,12 +37,12 @@ namespace instance_id {
36
37
namespace internal {
37
38
38
39
using firebase::app::secure::UserSecureManager;
40
+ using firebase::callback::NewCallback;
39
41
40
42
// Response that signals this class when it's complete or canceled.
41
43
class SignalSemaphoreResponse : public rest ::Response {
42
44
public:
43
- explicit SignalSemaphoreResponse (Semaphore* complete)
44
- : complete_(complete) {}
45
+ explicit SignalSemaphoreResponse (Semaphore* complete) : complete_(complete) {}
45
46
46
47
void MarkCompleted () override {
47
48
rest::Response::MarkCompleted ();
@@ -196,14 +197,27 @@ Future<std::string> InstanceIdDesktopImpl::GetId() {
196
197
SafeFutureHandle<std::string> handle =
197
198
ref_future ()->SafeAlloc <std::string>(kInstanceIdFnGetId );
198
199
199
- Future<std::string> future = MakeFuture (ref_future (), handle);
200
- if (!InitialOrRefreshCheckin ()) {
201
- ref_future ()->Complete (handle, kErrorUnavailable , " Error in checkin" );
202
- return future;
200
+ if (terminating_) {
201
+ ref_future ()->Complete (handle, kErrorShutdown ,
202
+ " Failed due to App shutdown in progress" );
203
+ } else {
204
+ auto callback = NewCallback (
205
+ [](InstanceIdDesktopImpl* _this,
206
+ SafeFutureHandle<std::string> _handle) {
207
+ if (_this->InitialOrRefreshCheckin ()) {
208
+ _this->ref_future ()->CompleteWithResult (_handle, kErrorNone , " " ,
209
+ _this->instance_id_ );
210
+ } else {
211
+ _this->ref_future ()->Complete (_handle, kErrorUnavailable ,
212
+ " Error in checkin" );
213
+ }
214
+ },
215
+ this , handle);
216
+
217
+ scheduler_.Schedule (callback);
203
218
}
204
- ref_future ()->CompleteWithResult (handle, 0 , " " , instance_id_);
205
219
206
- return future ;
220
+ return MakeFuture ( ref_future (), handle) ;
207
221
}
208
222
209
223
Future<std::string> InstanceIdDesktopImpl::GetIdLastResult () {
@@ -217,10 +231,22 @@ Future<void> InstanceIdDesktopImpl::DeleteId() {
217
231
SafeFutureHandle<void > handle =
218
232
ref_future ()->SafeAlloc <void >(kInstanceIdFnRemoveId );
219
233
220
- if (DeleteServerToken (nullptr , true )) {
221
- ref_future ()->Complete (handle, 0 , " " );
234
+ if (terminating_) {
235
+ ref_future ()->Complete (handle, kErrorShutdown ,
236
+ " Failed due to App shutdown in progress" );
222
237
} else {
223
- ref_future ()->Complete (handle, kErrorUnknownError , " DeleteId failed" );
238
+ auto callback = NewCallback (
239
+ [](InstanceIdDesktopImpl* _this, SafeFutureHandle<void > _handle) {
240
+ if (_this->DeleteServerToken (nullptr , true )) {
241
+ _this->ref_future ()->Complete (_handle, kErrorNone , " " );
242
+ } else {
243
+ _this->ref_future ()->Complete (_handle, kErrorUnknownError ,
244
+ " DeleteId failed" );
245
+ }
246
+ },
247
+ this , handle);
248
+
249
+ scheduler_.Schedule (callback);
224
250
}
225
251
return MakeFuture (ref_future (), handle);
226
252
}
@@ -237,12 +263,26 @@ Future<std::string> InstanceIdDesktopImpl::GetToken(const char* scope) {
237
263
SafeFutureHandle<std::string> handle =
238
264
ref_future ()->SafeAlloc <std::string>(kInstanceIdFnGetToken );
239
265
240
- std::string scope_str (scope);
241
- if (FetchServerToken (scope_str.c_str ())) {
242
- ref_future ()->CompleteWithResult (handle, 0 , " " ,
243
- FindCachedToken (scope_str.c_str ()));
266
+ if (terminating_) {
267
+ ref_future ()->Complete (handle, kErrorShutdown ,
268
+ " Failed due to App shutdown in progress" );
244
269
} else {
245
- ref_future ()->Complete (handle, kErrorUnknownError , " FetchToken failed" );
270
+ std::string scope_str (scope);
271
+ auto callback = NewCallback (
272
+ [](InstanceIdDesktopImpl* _this, std::string _scope_str,
273
+ SafeFutureHandle<std::string> _handle) {
274
+ if (_this->FetchServerToken (_scope_str.c_str ())) {
275
+ _this->ref_future ()->CompleteWithResult (
276
+ _handle, kErrorNone , " " ,
277
+ _this->FindCachedToken (_scope_str.c_str ()));
278
+ } else {
279
+ _this->ref_future ()->Complete (_handle, kErrorUnknownError ,
280
+ " FetchToken failed" );
281
+ }
282
+ },
283
+ this , scope_str, handle);
284
+
285
+ scheduler_.Schedule (callback);
246
286
}
247
287
248
288
return MakeFuture (ref_future (), handle);
@@ -255,16 +295,30 @@ Future<std::string> InstanceIdDesktopImpl::GetTokenLastResult() {
255
295
256
296
Future<void > InstanceIdDesktopImpl::DeleteToken (const char * scope) {
257
297
// DeleteToken() --> delete token request and remove from the cache
258
-
259
298
SafeFutureHandle<void > handle =
260
299
ref_future ()->SafeAlloc <void >(kInstanceIdFnRemoveToken );
261
300
262
- std::string scope_str (scope);
263
- if ( DeleteServerToken (scope_str. c_str (), false )) {
264
- ref_future ()-> Complete (handle, 0 , " " );
301
+ if (terminating_) {
302
+ ref_future ()-> Complete (handle, kErrorShutdown ,
303
+ " Failed due to App shutdown in progress " );
265
304
} else {
266
- ref_future ()->Complete (handle, kErrorUnknownError , " DeleteToken failed" );
305
+ std::string scope_str (scope);
306
+
307
+ auto callback = NewCallback (
308
+ [](InstanceIdDesktopImpl* _this, std::string _scope_str,
309
+ SafeFutureHandle<void > _handle) {
310
+ if (_this->DeleteServerToken (_scope_str.c_str (), false )) {
311
+ _this->ref_future ()->Complete (_handle, kErrorNone , " " );
312
+ } else {
313
+ _this->ref_future ()->Complete (_handle, kErrorUnknownError ,
314
+ " DeleteToken failed" );
315
+ }
316
+ },
317
+ this , scope_str, handle);
318
+
319
+ scheduler_.Schedule (callback);
267
320
}
321
+
268
322
return MakeFuture (ref_future (), handle);
269
323
}
270
324
0 commit comments