Skip to content

Commit 3eb04f1

Browse files
jonsimantova-maurice
authored andcommitted
Switch InstanceIdDesktopImpl methods to be run via scheduler rather than
synchronously. (also set GenerateAppId test back to 100 entries, from 1000) PiperOrigin-RevId: 248755680
1 parent d83d8ed commit 3eb04f1

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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 {
3637
namespace internal {
3738

3839
using firebase::app::secure::UserSecureManager;
40+
using firebase::callback::NewCallback;
3941

4042
// Response that signals this class when it's complete or canceled.
4143
class 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

209223
Future<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

256296
Future<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

app/instance_id/instance_id_desktop_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class InstanceIdDesktopImpl {
6464
kErrorUnavailable,
6565
// An unknown error occurred.
6666
kErrorUnknownError,
67+
// App shutdown is in progress.
68+
kErrorShutdown,
6769
};
6870

6971
virtual ~InstanceIdDesktopImpl();

instance_id/src/desktop/instance_id.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ Future<std::string> InstanceId::GetId() const {
3434
InstanceIdInternal::kApiFunctionGetId);
3535

3636
const auto internal_future = instance_id_internal_->impl()->GetId();
37-
InstanceIdInternal::InternalRef internal_ref =
37+
InstanceIdInternal::InternalRef& internal_ref =
3838
instance_id_internal_->safe_ref();
3939
internal_future.OnCompletion(
40-
[&internal_ref, &future_handle](const Future<std::string>& result) {
40+
[&internal_ref, future_handle](const Future<std::string>& result) {
4141
InstanceIdInternal::InternalRefLock lock(&internal_ref);
4242
if (lock.GetReference() == nullptr) {
4343
return; // deleted.
@@ -61,10 +61,10 @@ Future<void> InstanceId::DeleteId() {
6161

6262
const auto internal_future = instance_id_internal_->impl()->DeleteId();
6363

64-
InstanceIdInternal::InternalRef internal_ref =
64+
InstanceIdInternal::InternalRef& internal_ref =
6565
instance_id_internal_->safe_ref();
6666
internal_future.OnCompletion([&internal_ref,
67-
&future_handle](const Future<void>& result) {
67+
future_handle](const Future<void>& result) {
6868
InstanceIdInternal::InternalRefLock lock(&internal_ref);
6969
if (lock.GetReference() == nullptr) {
7070
return; // deleted.
@@ -89,10 +89,10 @@ Future<std::string> InstanceId::GetToken(const char* entity,
8989

9090
const auto internal_future = instance_id_internal_->impl()->GetToken(scope);
9191

92-
InstanceIdInternal::InternalRef internal_ref =
92+
InstanceIdInternal::InternalRef& internal_ref =
9393
instance_id_internal_->safe_ref();
9494
internal_future.OnCompletion(
95-
[&internal_ref, &future_handle](const Future<std::string>& result) {
95+
[&internal_ref, future_handle](const Future<std::string>& result) {
9696
InstanceIdInternal::InternalRefLock lock(&internal_ref);
9797
if (lock.GetReference() == nullptr) {
9898
return; // deleted.
@@ -116,11 +116,11 @@ Future<void> InstanceId::DeleteToken(const char* entity, const char* scope) {
116116

117117
const auto internal_future =
118118
instance_id_internal_->impl()->DeleteToken(scope);
119-
InstanceIdInternal::InternalRef internal_ref =
119+
InstanceIdInternal::InternalRef& internal_ref =
120120
instance_id_internal_->safe_ref();
121121

122122
internal_future.OnCompletion([&internal_ref,
123-
&future_handle](const Future<void>& result) {
123+
future_handle](const Future<void>& result) {
124124
InstanceIdInternal::InternalRefLock lock(&internal_ref);
125125
if (lock.GetReference() == nullptr) {
126126
return; // deleted.

0 commit comments

Comments
 (0)