shill: Allow DBus name watchers to be deleted.

This CL refactors the code for watching DBus names as follows:
1. Adds a new DBusNameWatcher class.
2. Replaces DBusManager::WatchName() with
   DBusManager::CreateNameWatcher() returning a DBusNameWatcher, which
   can be deleted by the caller to unwatch the DBus name.
3. Removes MockDBusManager and changes unit tests to use DBusManager
   with a MockDBusServiceProxy.
4. Make related log messages more readable and consistent in format.

BUG=chromium:214476
TEST=Tested the following:
1. Build and run unit tests with USE=wimax.
2. Manually verify, via /var/log/net.log, that shill can observe and
   handle the following scenarios when WPA supplicant and WiMAX manager
   appear on and vanish from DBus:
   - shill starts before WPA supplicant and WiMAX manager
   - shill starts after WPA supplicant and WiMAX manager

Change-Id: I719806220c39de1296b62eb457c4610506a5b815
Reviewed-on: https://chromium-review.googlesource.com/181890
Commit-Queue: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
diff --git a/dbus_manager.h b/dbus_manager.h
index adab5c2..a979323 100644
--- a/dbus_manager.h
+++ b/dbus_manager.h
@@ -13,66 +13,65 @@
 #include <base/callback.h>
 #include <base/cancelable_callback.h>
 #include <base/memory/scoped_ptr.h>
+#include <base/memory/weak_ptr.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
+#include "shill/dbus_name_watcher.h"
+
 namespace shill {
 
 class DBusServiceProxyInterface;
 class Error;
 class ProxyFactory;
 
-class DBusManager {
+class DBusManager : public base::SupportsWeakPtr<DBusManager> {
  public:
-  typedef base::Callback<void(const std::string &owner)> AppearedCallback;
-  typedef base::Closure VanishedCallback;
-  typedef base::CancelableCallback<void(
-      const std::string &owner)> CancelableAppearedCallback;
-  typedef base::CancelableClosure CancelableVanishedCallback;
-
   DBusManager();
   virtual ~DBusManager();
 
   void Start();
   void Stop();
 
-  // Registers a watcher for DBus service |name|. When the service appears,
-  // |on_appear| is invoked if non-null. When the service vanishes, |on_vanish|
-  // is invoked if non-null. |on_appear| or |on_vanish| will be notified once
+  // Creates and registers a watcher for DBus service |name|. When the service
+  // appears, |name_appeared_callback| is invoked if non-null. When the service
+  // vanishes, |name_vanished_callback| is invoked if non-null.
+  // |name_appeared_callback| or |name_vanished_callback| will be notified once
   // asynchronously if the service has or doesn't have an owner, respectively,
-  // when WatchName is invoked.
-  virtual void WatchName(const std::string &name,
-                         const AppearedCallback &on_appear,
-                         const VanishedCallback &on_vanish);
+  // when this method is invoked. The returned watcher should be managed by the
+  // caller and may outlive this DBus manager. The watcher holds a weak pointer
+  // to this DBus manager.  When it is destructed, it automatically calls
+  // RemoveNameWatcher() to deregister and remove itself from this DBus
+  // manager.
+  virtual DBusNameWatcher *CreateNameWatcher(
+      const std::string &name,
+      const DBusNameWatcher::NameAppearedCallback &name_appeared_callback,
+      const DBusNameWatcher::NameVanishedCallback &name_vanished_callback);
+
+  // Deregisters and removes the watcher such that it stops monitoring the
+  // associated DBus service name.
+  virtual void RemoveNameWatcher(DBusNameWatcher *name_watcher);
 
  private:
   friend class DBusManagerTest;
+  friend class WiFiObjectTest;
+  friend class WiMaxProviderTest;
   FRIEND_TEST(DBusManagerTest, NameWatchers);
-
-  struct NameWatcher {
-    NameWatcher(const AppearedCallback &on_appear_in,
-                const VanishedCallback &on_vanish_in)
-        : on_appear(on_appear_in), on_vanish(on_vanish_in) {}
-
-    AppearedCallback on_appear;
-    VanishedCallback on_vanish;
-  };
+  FRIEND_TEST(WiMaxProviderTest, StartStop);
 
   void OnNameOwnerChanged(const std::string &name,
                           const std::string &old_owner,
                           const std::string &new_owner);
 
-  void OnGetNameOwnerComplete(const NameWatcher &watcher,
-                              const std::string &unique_name,
-                              const Error &error);
-
-  static void NotifyNameWatcher(const NameWatcher &watcher,
-                                const std::string &owner);
+  void OnGetNameOwnerComplete(
+      const base::WeakPtr<DBusNameWatcher> &name_watcher,
+      const std::string &unique_name,
+      const Error &error);
 
   ProxyFactory *proxy_factory_;
 
   scoped_ptr<DBusServiceProxyInterface> proxy_;
 
-  std::map<std::string, std::list<NameWatcher> > name_watchers_;
+  std::map<std::string, std::list<DBusNameWatcher *>> name_watchers_;
 
   DISALLOW_COPY_AND_ASSIGN(DBusManager);
 };