@@ -195,8 +195,7 @@ static bool PushBackIfMissing(const T& entry, std::vector<T>* v) {
195195// Return whether the listener is added.
196196template <typename T>
197197static bool AddListener (T listener, std::vector<T>* listener_vector, Auth* auth,
198- std::vector<Auth*>* auth_vector, Mutex* mutex) {
199- MutexLock lock (*mutex);
198+ std::vector<Auth*>* auth_vector) {
200199 // Add to array of listeners if not already there.
201200 const bool listener_added = PushBackIfMissing (listener, listener_vector);
202201 // Add auth to array of Auths if not already there.
@@ -212,28 +211,35 @@ static bool AddListener(T listener, std::vector<T>* listener_vector, Auth* auth,
212211
213212void Auth::AddAuthStateListener (AuthStateListener* listener) {
214213 if (!auth_data_) return ;
215- bool added = AddListener (listener, &auth_data_->listeners , this ,
216- &listener->auths_ , &auth_data_->listeners_mutex );
214+ // Would have to lock mutex till the method ends to protect on race
215+ // conditions.
216+ MutexLock lock (auth_data_->listeners_mutex );
217+ bool added =
218+ AddListener (listener, &auth_data_->listeners , this , &listener->auths_ );
219+
217220 // If the listener is registered successfully and persistent cache has been
218221 // loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
219222 // the cache is loaded, through AuthStateListener event
220- if (added && auth_data_->persistent_cache_loaded ) {
223+ if (added && ! auth_data_->persistent_cache_load_pending ) {
221224 listener->OnAuthStateChanged (this );
222225 }
223226}
224227
225228void Auth::AddIdTokenListener (IdTokenListener* listener) {
226229 if (!auth_data_) return ;
230+ // Would have to lock mutex till the method ends to protect on race
231+ // conditions.
232+ MutexLock lock (auth_data_->listeners_mutex );
227233 bool added = AddListener (listener, &auth_data_->id_token_listeners , this ,
228- &listener->auths_ , &auth_data_-> listeners_mutex );
234+ &listener->auths_ );
229235 // AddListener is valid even if the listener is already registered.
230236 // This makes sure that we only increase the reference count if a listener
231237 // was actually added.
232238 if (added) {
233239 // If the listener is registered successfully and persistent cache has been
234240 // loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
235241 // the cache is loaded, through AuthStateListener event
236- if (auth_data_->persistent_cache_loaded ) {
242+ if (! auth_data_->persistent_cache_load_pending ) {
237243 listener->OnIdTokenChanged (this );
238244 }
239245 EnableTokenAutoRefresh (auth_data_);
@@ -337,7 +343,7 @@ static inline bool VectorContains(const T& entry, const std::vector<T>& v) {
337343 \
338344 /* Auth should have loaded persistent cache if exists when the listener */ \
339345 /* event is triggered for the first time. */ \
340- auth_data->persistent_cache_loaded = true ; \
346+ auth_data->persistent_cache_load_pending = false ; \
341347 }
342348
343349AUTH_NOTIFY_LISTENERS (NotifyAuthStateListeners, " Auth state" , listeners,
0 commit comments