1919
2020#include " app/rest/transport_builder.h"
2121#include " app/rest/util.h"
22+ #include " app/src/callback.h"
2223#include " app/src/include/firebase/future.h"
24+ #include " app/src/scheduler.h"
2325#include " app/src/secure/user_secure_manager.h"
24- #include " app/src/thread.h"
2526#include " auth/src/common.h"
2627#include " auth/src/data.h"
2728#include " auth/src/desktop/auth_data_handle.h"
@@ -54,6 +55,7 @@ namespace firebase {
5455namespace auth {
5556
5657using firebase::app::secure::UserSecureManager;
58+ using firebase::callback::NewCallback;
5759
5860namespace {
5961
@@ -112,7 +114,8 @@ GetTokenResult GetTokenIfFresh(const UserView::Reader& user,
112114// Note: this is a blocking call! The caller is supposed to call this function
113115// on the appropriate thread.
114116GetTokenResult EnsureFreshToken (AuthData* const auth_data,
115- const bool force_refresh) {
117+ const bool force_refresh,
118+ const bool notify_listener) {
116119 FIREBASE_ASSERT_RETURN (GetTokenResult (kAuthErrorFailure ), auth_data);
117120
118121 GetTokenResult old_token (kAuthErrorFailure );
@@ -149,13 +152,18 @@ GetTokenResult EnsureFreshToken(AuthData* const auth_data,
149152 return GetTokenResult (kAuthErrorNoSignedInUser );
150153 }
151154 }
152- if (has_token_changed) {
155+ if (has_token_changed && notify_listener ) {
153156 NotifyIdTokenListeners (auth_data);
154157 }
155158
156159 return GetTokenResult (response.id_token ());
157160}
158161
162+ GetTokenResult EnsureFreshToken (AuthData* const auth_data,
163+ const bool force_refresh) {
164+ return EnsureFreshToken (auth_data, force_refresh, true );
165+ }
166+
159167// Checks whether there is a currently logged in user. If no user is signed in,
160168// fails the given promise and returns false. Otherwise, doesn't touch the
161169// promise and returns true.
@@ -181,7 +189,8 @@ Future<ResultT> CallAsyncWithFreshToken(
181189 StartAsyncFunction (auth_data->auth_impl );
182190
183191 typedef AuthDataHandle<ResultT, RequestT> HandleT;
184- firebase::Thread (
192+
193+ auto scheduler_callback = NewCallback (
185194 [](HandleT* const raw_auth_data_handle) {
186195 std::unique_ptr<HandleT> handle (raw_auth_data_handle);
187196
@@ -197,8 +206,10 @@ Future<ResultT> CallAsyncWithFreshToken(
197206 handle->callback (handle.get ());
198207 EndAsyncFunction (handle->auth_data ->auth_impl );
199208 },
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+
202213 return promise.LastResult ();
203214}
204215
@@ -472,14 +483,37 @@ void AssignLoadedData(const Future<std::string>& future, AuthData* auth_data) {
472483void HandleLoadedData (const Future<std::string>& future, void * auth_data) {
473484 auto cast_auth_data = static_cast <AuthData*>(auth_data);
474485 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 );
476509}
477510
478511Future<std::string> UserDataPersist::LoadUserData (AuthData* auth_data) {
479512 if (auth_data == nullptr ) {
480513 return Future<std::string>();
481514 }
482515
516+ StartAsyncFunction (auth_data->auth_impl );
483517 Future<std::string> future =
484518 user_secure_manager_->LoadUserData (auth_data->app ->name ());
485519 future.OnCompletion (HandleLoadedData, auth_data);
@@ -531,11 +565,11 @@ Future<void> UserDataPersist::SaveUserData(AuthData* auth_data) {
531565}
532566
533567Future<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 ;
539573}
540574
541575User::~User () {
0 commit comments