update_engine: UM: UpdateManager removes EvaluationContext objects.

This fixes a situation where the destruction of an UpdateManager object
may leave a bunch of dangling main loop events due to delayed
evaluation, whose presence prevents the destruction of their
corresponding EvaluationContext objects. We solve this by registering
each EvaluationContext that's created for an async evaluation with the
UpdateManager, and storing a (weak) reverse callback in each
EvaluationContext for unregistering itself upon destruction. The
UpdateManager itself cares to unregister (i.e. remove any pending
events) for any outstanding EvaluationContexts that are still present
during its destruction. This also ensures that these objects are
properly destructed right after the destruction of the UpdateManager.

This CL also fixes a bug whereas removal of pending events might have
left a "live" callback handle inside the EvaluationContext, thus
creating a reference-count cycle and preventing the object from being
deallocated.

BUG=None
TEST=Unit tests.

Change-Id: I5b7f4b740241ed3a5f1831ae41fead0fc1481071
Reviewed-on: https://chromium-review.googlesource.com/211720
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/update_manager/evaluation_context.h b/update_manager/evaluation_context.h
index 81574d4..e743669 100644
--- a/update_manager/evaluation_context.h
+++ b/update_manager/evaluation_context.h
@@ -8,6 +8,7 @@
 #include <map>
 #include <string>
 
+#include <base/bind.h>
 #include <base/callback.h>
 #include <base/memory/ref_counted.h>
 #include <base/memory/scoped_ptr.h>
@@ -52,12 +53,16 @@
 class EvaluationContext : public base::RefCounted<EvaluationContext>,
                           private BaseVariable::ObserverInterface {
  public:
-  EvaluationContext(chromeos_update_engine::ClockInterface* clock,
-                    base::TimeDelta evaluation_timeout,
-                    base::TimeDelta expiration_timeout);
+  EvaluationContext(
+      chromeos_update_engine::ClockInterface* clock,
+      base::TimeDelta evaluation_timeout,
+      base::TimeDelta expiration_timeout,
+      scoped_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb);
   EvaluationContext(chromeos_update_engine::ClockInterface* clock,
                     base::TimeDelta evaluation_timeout)
-      : EvaluationContext(clock, evaluation_timeout, base::TimeDelta::Max()) {}
+      : EvaluationContext(
+          clock, evaluation_timeout, base::TimeDelta::Max(),
+          scoped_ptr<base::Callback<void(EvaluationContext*)>>()) {}
   ~EvaluationContext();
 
   // Returns a pointer to the value returned by the passed variable |var|. The
@@ -105,13 +110,14 @@
   // to help with debugging and the format may change in the future.
   std::string DumpContext() const;
 
+  // Removes all the Observers callbacks and timeout events scheduled by
+  // RunOnValueChangeOrTimeout(). Also releases and returns the closure
+  // associated with these events. This method is idempotent.
+  scoped_ptr<base::Closure> RemoveObserversAndTimeout();
+
  private:
   friend class UmEvaluationContextTest;
 
-  // Removes all the Observers and timeout callbacks scheduled by
-  // RunOnValueChangeOrTimeout(). This method is idempotent.
-  void RemoveObserversAndTimeout();
-
   // BaseVariable::ObserverInterface override.
   void ValueChanged(BaseVariable* var);
 
@@ -186,6 +192,9 @@
   // The monotonic clock deadline at which expiration occurs.
   base::Time expiration_monotonic_deadline_;
 
+  // A callback for unregistering the context upon destruction.
+  scoped_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb_;
+
   base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(EvaluationContext);