PolicyManager: Don't schedule a callback when there are no observers.
BaseVariable::NotifyValueChanged() will schedule a callback to call
all the observers on that variable from the same call. This prevents
a corner case where the observer removes itself and is detroyed
between the NotifyValueChanged() and the callback while it doesn't
fire all observers from the same context that called
NotifyValueChanged(). Nevertheless, this callback interferes with
the unittest code where Variables are created and removed all the
time, and the scheduled callback could remain on the main loop until
other unittest runs it.
This patch only schedules the callback to the BaseVariable if there
is any observer waiting for it, which helps to reduce this
uninteresting callbacks during testing.
BUG=chromium:358326
TEST=Unit test still pass.
Change-Id: I4f86d7eaf956afeebee91d27a80b9e3061bc56fb
Reviewed-on: https://chromium-review.googlesource.com/195447
Tested-by: Alex Deymo <[email protected]>
Reviewed-by: Gilad Arnold <[email protected]>
Commit-Queue: Alex Deymo <[email protected]>
diff --git a/policy_manager/variable.h b/policy_manager/variable.h
index 4507280..bf610c1 100644
--- a/policy_manager/variable.h
+++ b/policy_manager/variable.h
@@ -102,8 +102,13 @@
// Calls ValueChanged on all the observers.
void NotifyValueChanged() {
- RunFromMainLoop(base::Bind(&BaseVariable::OnValueChangedNotification,
- base::Unretained(this)));
+ // Fire all the observer methods from the main loop as single call. In order
+ // to avoid scheduling these callbacks when it is not needed, we check
+ // first the list of observers.
+ if (!observer_list_.empty()) {
+ RunFromMainLoop(base::Bind(&BaseVariable::OnValueChangedNotification,
+ base::Unretained(this)));
+ }
}
private: