19
19
20
20
#include " app/rest/transport_builder.h"
21
21
#include " app/rest/util.h"
22
+ #include " app/src/callback.h"
22
23
#include " app/src/include/firebase/future.h"
24
+ #include " app/src/scheduler.h"
23
25
#include " app/src/secure/user_secure_manager.h"
24
- #include " app/src/thread.h"
25
26
#include " auth/src/common.h"
26
27
#include " auth/src/data.h"
27
28
#include " auth/src/desktop/auth_data_handle.h"
@@ -54,6 +55,7 @@ namespace firebase {
54
55
namespace auth {
55
56
56
57
using firebase::app::secure::UserSecureManager;
58
+ using firebase::callback::NewCallback;
57
59
58
60
namespace {
59
61
@@ -112,7 +114,8 @@ GetTokenResult GetTokenIfFresh(const UserView::Reader& user,
112
114
// Note: this is a blocking call! The caller is supposed to call this function
113
115
// on the appropriate thread.
114
116
GetTokenResult EnsureFreshToken (AuthData* const auth_data,
115
- const bool force_refresh) {
117
+ const bool force_refresh,
118
+ const bool notify_listener) {
116
119
FIREBASE_ASSERT_RETURN (GetTokenResult (kAuthErrorFailure ), auth_data);
117
120
118
121
GetTokenResult old_token (kAuthErrorFailure );
@@ -149,13 +152,18 @@ GetTokenResult EnsureFreshToken(AuthData* const auth_data,
149
152
return GetTokenResult (kAuthErrorNoSignedInUser );
150
153
}
151
154
}
152
- if (has_token_changed) {
155
+ if (has_token_changed && notify_listener ) {
153
156
NotifyIdTokenListeners (auth_data);
154
157
}
155
158
156
159
return GetTokenResult (response.id_token ());
157
160
}
158
161
162
+ GetTokenResult EnsureFreshToken (AuthData* const auth_data,
163
+ const bool force_refresh) {
164
+ return EnsureFreshToken (auth_data, force_refresh, true );
165
+ }
166
+
159
167
// Checks whether there is a currently logged in user. If no user is signed in,
160
168
// fails the given promise and returns false. Otherwise, doesn't touch the
161
169
// promise and returns true.
@@ -181,7 +189,8 @@ Future<ResultT> CallAsyncWithFreshToken(
181
189
StartAsyncFunction (auth_data->auth_impl );
182
190
183
191
typedef AuthDataHandle<ResultT, RequestT> HandleT;
184
- firebase::Thread (
192
+
193
+ auto scheduler_callback = NewCallback (
185
194
[](HandleT* const raw_auth_data_handle) {
186
195
std::unique_ptr<HandleT> handle (raw_auth_data_handle);
187
196
@@ -197,8 +206,10 @@ Future<ResultT> CallAsyncWithFreshToken(
197
206
handle->callback (handle.get ());
198
207
EndAsyncFunction (handle->auth_data ->auth_impl );
199
208
},
200
- new HandleT (auth_data, promise, std::move (request), callback))
201
- .Detach ();
209
+ new HandleT (auth_data, promise, std::move (request), callback));
210
+ auto auth_impl = static_cast <AuthImpl*>(auth_data->auth_impl );
211
+ auth_impl->scheduler_ .Schedule (scheduler_callback);
212
+
202
213
return promise.LastResult ();
203
214
}
204
215
@@ -472,14 +483,37 @@ void AssignLoadedData(const Future<std::string>& future, AuthData* auth_data) {
472
483
void HandleLoadedData (const Future<std::string>& future, void * auth_data) {
473
484
auto cast_auth_data = static_cast <AuthData*>(auth_data);
474
485
AssignLoadedData (future, cast_auth_data);
475
- LoadFinishTriggerListeners (cast_auth_data);
486
+
487
+ // End async call for LoadUserData
488
+ EndAsyncFunction (cast_auth_data->auth_impl );
489
+
490
+ auto scheduler_callback = NewCallback (
491
+ [](AuthData* callback_auth_data) {
492
+ // Don't trigger token listeners if token get refreshed, since
493
+ // it will get triggered inside LoadFinishTriggerListeners anyways.
494
+ const GetTokenResult get_token_result =
495
+ EnsureFreshToken (callback_auth_data, false , false );
496
+ LoadFinishTriggerListeners (callback_auth_data);
497
+ EndAsyncFunction (callback_auth_data->auth_impl );
498
+ },
499
+ cast_auth_data);
500
+ // Start Async call for EnsureFreshToken
501
+ StartAsyncFunction (cast_auth_data->auth_impl );
502
+ auto auth_impl = static_cast <AuthImpl*>(cast_auth_data->auth_impl );
503
+ auth_impl->scheduler_ .Schedule (scheduler_callback);
504
+ }
505
+
506
+ void HandlePersistenceComplete (const Future<void >& future, void * auth_data) {
507
+ auto cast_auth_data = static_cast <AuthData*>(auth_data);
508
+ EndAsyncFunction (cast_auth_data->auth_impl );
476
509
}
477
510
478
511
Future<std::string> UserDataPersist::LoadUserData (AuthData* auth_data) {
479
512
if (auth_data == nullptr ) {
480
513
return Future<std::string>();
481
514
}
482
515
516
+ StartAsyncFunction (auth_data->auth_impl );
483
517
Future<std::string> future =
484
518
user_secure_manager_->LoadUserData (auth_data->app ->name ());
485
519
future.OnCompletion (HandleLoadedData, auth_data);
@@ -531,11 +565,11 @@ Future<void> UserDataPersist::SaveUserData(AuthData* auth_data) {
531
565
}
532
566
533
567
Future<void > UserDataPersist::DeleteUserData (AuthData* auth_data) {
534
- return user_secure_manager_-> DeleteUserData (auth_data->app -> name () );
535
- }
536
-
537
- Future< void > UserDataPersist::DeleteAllData () {
538
- return user_secure_manager_-> DeleteAllData () ;
568
+ StartAsyncFunction (auth_data->auth_impl );
569
+ Future< void > future =
570
+ user_secure_manager_-> DeleteUserData (auth_data-> app -> name ());
571
+ future. OnCompletion (HandlePersistenceComplete, auth_data);
572
+ return future ;
539
573
}
540
574
541
575
User::~User () {
0 commit comments