2424#include " app/src/app_common.h"
2525#include " app/src/app_identifier.h"
2626#include " app/src/base64.h"
27+ #include " app/src/callback.h"
2728#include " app/src/cleanup_notifier.h"
2829#include " app/src/locale.h"
2930#include " app/src/time.h"
@@ -36,12 +37,12 @@ namespace instance_id {
3637namespace internal {
3738
3839using firebase::app::secure::UserSecureManager;
40+ using firebase::callback::NewCallback;
3941
4042// Response that signals this class when it's complete or canceled.
4143class SignalSemaphoreResponse : public rest ::Response {
4244 public:
43- explicit SignalSemaphoreResponse (Semaphore* complete)
44- : complete_(complete) {}
45+ explicit SignalSemaphoreResponse (Semaphore* complete) : complete_(complete) {}
4546
4647 void MarkCompleted () override {
4748 rest::Response::MarkCompleted ();
@@ -196,14 +197,27 @@ Future<std::string> InstanceIdDesktopImpl::GetId() {
196197 SafeFutureHandle<std::string> handle =
197198 ref_future ()->SafeAlloc <std::string>(kInstanceIdFnGetId );
198199
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);
203218 }
204- ref_future ()->CompleteWithResult (handle, 0 , " " , instance_id_);
205219
206- return future ;
220+ return MakeFuture ( ref_future (), handle) ;
207221}
208222
209223Future<std::string> InstanceIdDesktopImpl::GetIdLastResult () {
@@ -217,10 +231,22 @@ Future<void> InstanceIdDesktopImpl::DeleteId() {
217231 SafeFutureHandle<void > handle =
218232 ref_future ()->SafeAlloc <void >(kInstanceIdFnRemoveId );
219233
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" );
222237 } 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);
224250 }
225251 return MakeFuture (ref_future (), handle);
226252}
@@ -237,12 +263,26 @@ Future<std::string> InstanceIdDesktopImpl::GetToken(const char* scope) {
237263 SafeFutureHandle<std::string> handle =
238264 ref_future ()->SafeAlloc <std::string>(kInstanceIdFnGetToken );
239265
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" );
244269 } 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);
246286 }
247287
248288 return MakeFuture (ref_future (), handle);
@@ -255,16 +295,30 @@ Future<std::string> InstanceIdDesktopImpl::GetTokenLastResult() {
255295
256296Future<void > InstanceIdDesktopImpl::DeleteToken (const char * scope) {
257297 // DeleteToken() --> delete token request and remove from the cache
258-
259298 SafeFutureHandle<void > handle =
260299 ref_future ()->SafeAlloc <void >(kInstanceIdFnRemoveToken );
261300
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 " );
265304 } 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);
267320 }
321+
268322 return MakeFuture (ref_future (), handle);
269323}
270324
0 commit comments