buffet: Move global instance of CommandManager to buffet::Manager

Removed the global static instance of CommandManager and
transferred ownership of this global instance to buffet::Manager
class.

BUG=None
TEST=USE=buffet P2_TEST_FILTER="buffet::*" FEATURES=test emerge-link platform2

Change-Id: If1b4875d3c26f146bc4f454bb0aaaaf8b17a0c72
Reviewed-on: https://chromium-review.googlesource.com/209563
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/command_manager.cc b/buffet/commands/command_manager.cc
index 26c0b9f..8c54e51 100644
--- a/buffet/commands/command_manager.cc
+++ b/buffet/commands/command_manager.cc
@@ -4,8 +4,6 @@
 
 #include "buffet/commands/command_manager.h"
 
-#include <base/at_exit.h>
-#include <base/bind.h>
 #include <base/file_util.h>
 #include <base/files/file_enumerator.h>
 #include <base/json/json_reader.h>
@@ -16,8 +14,6 @@
 
 namespace buffet {
 
-CommandManager* CommandManager::instance_ = nullptr;
-
 const CommandDictionary& CommandManager::GetCommandDictionary() const {
   return dictionary_;
 }
@@ -52,20 +48,12 @@
   return LoadCommands(*json, category, error);
 }
 
-CommandManager* CommandManager::GetInstance() {
-  CHECK(instance_) << "CommandManager instance not initialized.";
-  return instance_;
-}
-
 void CommandManager::Startup() {
-  CHECK(!instance_) << "CommandManager instance already initialized.";
   LOG(INFO) << "Initializing CommandManager.";
-  std::unique_ptr<CommandManager> inst(new CommandManager);
-
   // Load global standard GCD command dictionary.
   base::FilePath base_command_file("/etc/buffet/gcd.json");
   LOG(INFO) << "Loading standard commands from " << base_command_file.value();
-  CHECK(inst->LoadBaseCommands(base_command_file, nullptr))
+  CHECK(LoadBaseCommands(base_command_file, nullptr))
       << "Failed to load the standard command definitions.";
 
   // Load static device command definitions.
@@ -76,23 +64,10 @@
   base::FilePath json_file_path = enumerator.Next();
   while (!json_file_path.empty()) {
     LOG(INFO) << "Loading command schema from " << json_file_path.value();
-    CHECK(inst->LoadCommands(json_file_path, nullptr))
+    CHECK(LoadCommands(json_file_path, nullptr))
         << "Failed to load the command definition file.";
     json_file_path = enumerator.Next();
   }
-
-  // Register a cleanup callback to be executed at shut-down.
-  base::AtExitManager::RegisterTask(base::Bind(&CommandManager::Shutdown));
-  // Transfer the object instance pointer from the smart pointer to
-  // the global pointer.
-  instance_ = inst.release();
-}
-
-void CommandManager::Shutdown() {
-  CHECK(instance_) << "CommandManager instance not initialized.";
-  LOG(INFO) << "Shutting down CommandManager.";
-  delete instance_;
-  instance_ = nullptr;
 }
 
 std::unique_ptr<const base::DictionaryValue> CommandManager::LoadJsonDict(
diff --git a/buffet/commands/command_manager.h b/buffet/commands/command_manager.h
index 02d92fd..1199892 100644
--- a/buffet/commands/command_manager.h
+++ b/buffet/commands/command_manager.h
@@ -52,13 +52,11 @@
   bool LoadCommands(const base::FilePath& json_file_path,
                     ErrorPtr* error);
 
-  // Factory static method to get the global singleton instance of the object.
-  static CommandManager* GetInstance();
-  // Global startup method to be called by buffet daemon at startup.
-  // Initializes the global object singleton and loads the standard GCD command
+  // Startup method to be called by buffet daemon at startup.
+  // Initializes the object and loads the standard GCD command
   // dictionary as well as static vendor-provided command definitions for
   // the current device.
-  static void Startup();
+  void Startup();
 
  private:
   // Helper function to load a JSON file that is expected to be
@@ -66,14 +64,10 @@
   // in error details in |error|.
   std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
       const base::FilePath& json_file_path, ErrorPtr* error);
-  // Helper method to be called at buffet shutdown to clean up the global
-  // singleton instance of this class.
-  static void Shutdown();
 
   CommandDictionary base_dictionary_;  // Base/std command definitions/schemas.
   CommandDictionary dictionary_;  // Command definitions/schemas.
 
-  static CommandManager* instance_;  // Global singleton instance of the object.
   DISALLOW_COPY_AND_ASSIGN(CommandManager);
 };
 
diff --git a/buffet/main.cc b/buffet/main.cc
index cc2fea7..358358e 100644
--- a/buffet/main.cc
+++ b/buffet/main.cc
@@ -16,7 +16,6 @@
 #include <sysexits.h>
 
 #include "buffet/async_event_sequencer.h"
-#include "buffet/commands/command_manager.h"
 #include "buffet/dbus_constants.h"
 #include "buffet/exported_object_manager.h"
 #include "buffet/manager.h"
@@ -103,8 +102,6 @@
   // both callbacks return.
   sequencer = nullptr;
 
-  buffet::CommandManager::Startup();
-
   LOG(INFO) << "Entering mainloop.";
   message_loop->Run();
 }
diff --git a/buffet/manager.cc b/buffet/manager.cc
index 3639803..e493329 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -15,6 +15,7 @@
 #include <dbus/values_util.h>
 
 #include "buffet/async_event_sequencer.h"
+#include "buffet/commands/command_manager.h"
 #include "buffet/dbus_constants.h"
 #include "buffet/dbus_utils.h"
 #include "buffet/error.h"
@@ -128,6 +129,8 @@
                  properties_->GetPropertyWriter(
                      dbus_constants::kManagerInterface)));
   sequencer->OnAllTasksCompletedCall({claim_interface_task, cb});
+  command_manager_ = std::make_shared<CommandManager>();
+  command_manager_->Startup();
   device_info_.Load();
 }
 
diff --git a/buffet/manager.h b/buffet/manager.h
index 2041160..c0b346d 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -21,6 +21,8 @@
 
 namespace buffet {
 
+class CommandManager;
+
 namespace dbus_utils {
 class ExportedObjectManager;
 }  // namespace dbus_utils
@@ -73,6 +75,7 @@
   base::WeakPtr<dbus_utils::ExportedObjectManager> object_manager_;
   scoped_ptr<Properties> properties_;
 
+  std::shared_ptr<CommandManager> command_manager_;
   DeviceRegistrationInfo device_info_;
 
   DISALLOW_COPY_AND_ASSIGN(Manager);