@@ -195,8 +195,7 @@ static bool PushBackIfMissing(const T& entry, std::vector<T>* v) {
195
195
// Return whether the listener is added.
196
196
template <typename T>
197
197
static 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) {
200
199
// Add to array of listeners if not already there.
201
200
const bool listener_added = PushBackIfMissing (listener, listener_vector);
202
201
// 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,
212
211
213
212
void Auth::AddAuthStateListener (AuthStateListener* listener) {
214
213
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
+
217
220
// If the listener is registered successfully and persistent cache has been
218
221
// loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
219
222
// the cache is loaded, through AuthStateListener event
220
- if (added && auth_data_->persistent_cache_loaded ) {
223
+ if (added && ! auth_data_->persistent_cache_load_pending ) {
221
224
listener->OnAuthStateChanged (this );
222
225
}
223
226
}
224
227
225
228
void Auth::AddIdTokenListener (IdTokenListener* listener) {
226
229
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 );
227
233
bool added = AddListener (listener, &auth_data_->id_token_listeners , this ,
228
- &listener->auths_ , &auth_data_-> listeners_mutex );
234
+ &listener->auths_ );
229
235
// AddListener is valid even if the listener is already registered.
230
236
// This makes sure that we only increase the reference count if a listener
231
237
// was actually added.
232
238
if (added) {
233
239
// If the listener is registered successfully and persistent cache has been
234
240
// loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
235
241
// the cache is loaded, through AuthStateListener event
236
- if (auth_data_->persistent_cache_loaded ) {
242
+ if (! auth_data_->persistent_cache_load_pending ) {
237
243
listener->OnIdTokenChanged (this );
238
244
}
239
245
EnableTokenAutoRefresh (auth_data_);
@@ -337,7 +343,7 @@ static inline bool VectorContains(const T& entry, const std::vector<T>& v) {
337
343
\
338
344
/* Auth should have loaded persistent cache if exists when the listener */ \
339
345
/* event is triggered for the first time. */ \
340
- auth_data->persistent_cache_loaded = true ; \
346
+ auth_data->persistent_cache_load_pending = false ; \
341
347
}
342
348
343
349
AUTH_NOTIFY_LISTENERS (NotifyAuthStateListeners, " Auth state" , listeners,
0 commit comments