buffet: Add flag to disable XMPP support

As a temporary work around to major source of flake in tests, allow
disabling XMPP support in tests.

BUG=brillo:769
TEST=FEATURES="test" USE="clang asan" emerge-gizmo buffet && \
     FEATURES="test" emerge-gizmo buffet

Change-Id: I41ca92bb77b77132c1a20c7018b0ce737b69cf60
Reviewed-on: https://chromium-review.googlesource.com/264392
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index b16e626..a87436d 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -126,12 +126,14 @@
     std::unique_ptr<const BuffetConfig> config,
     const std::shared_ptr<chromeos::http::Transport>& transport,
     const std::shared_ptr<StorageInterface>& state_store,
+    bool xmpp_enabled,
     const base::Closure& on_status_changed)
     : transport_{transport},
       storage_{state_store},
       command_manager_{command_manager},
       state_manager_{state_manager},
       config_{std::move(config)},
+      xmpp_enabled_{xmpp_enabled},
       on_status_changed_{on_status_changed} {
 }
 
@@ -340,6 +342,10 @@
 }
 
 void DeviceRegistrationInfo::StartXmpp() {
+  if (!xmpp_enabled_) {
+    LOG(WARNING) << "XMPP support disabled by flag.";
+    return;
+  }
   // If no MessageLoop assume we're in unittests.
   if (!base::MessageLoop::current()) {
     LOG(INFO) << "No MessageLoop, not starting XMPP";
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index 7f5e2c6..52b520a 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -54,6 +54,7 @@
       std::unique_ptr<const BuffetConfig> config,
       const std::shared_ptr<chromeos::http::Transport>& transport,
       const std::shared_ptr<StorageInterface>& state_store,
+      bool xmpp_enabled,
       const base::Closure& on_status_changed);
 
   ~DeviceRegistrationInfo() override;
@@ -209,9 +210,6 @@
   void SetRegistrationStatus(RegistrationStatus new_status);
   void SetDeviceId(const std::string& device_id);
 
-  std::unique_ptr<XmppClient> xmpp_client_;
-  base::MessageLoopForIO::FileDescriptorWatcher fd_watcher_;
-
   // Data that is cached here, persisted in the state store.
   std::string refresh_token_;
   std::string device_id_;
@@ -237,6 +235,10 @@
 
   std::unique_ptr<const BuffetConfig> config_;
 
+  const bool xmpp_enabled_;
+  std::unique_ptr<XmppClient> xmpp_client_;
+  base::MessageLoopForIO::FileDescriptorWatcher fd_watcher_;
+
   // Tracks our current registration status.
   RegistrationStatus registration_status_{RegistrationStatus::kUnconfigured};
   base::Closure on_status_changed_;
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index dfb61cc..91dc8b1 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -211,6 +211,7 @@
         new DeviceRegistrationInfo(command_manager_, state_manager_,
                                    std::move(config),
                                    transport_, storage_,
+                                   true,
                                    mock_callback));
   }
 
diff --git a/buffet/main.cc b/buffet/main.cc
index 58d70d3..461680b 100644
--- a/buffet/main.cc
+++ b/buffet/main.cc
@@ -25,11 +25,13 @@
  public:
   Daemon(const base::FilePath& config_path,
          const base::FilePath& state_path,
-         const base::FilePath& test_definitions_path)
+         const base::FilePath& test_definitions_path,
+         bool enable_xmpp)
       : DBusServiceDaemon(kServiceName, kRootServicePath),
         config_path_{config_path},
         state_path_{state_path},
-        test_definitions_path_{test_definitions_path} {}
+        test_definitions_path_{test_definitions_path},
+        enable_xmpp_{enable_xmpp} {}
 
  protected:
   void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
@@ -38,6 +40,7 @@
         config_path_,
         state_path_,
         test_definitions_path_,
+        enable_xmpp_,
         sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
   }
 
@@ -47,6 +50,7 @@
   const base::FilePath config_path_;
   const base::FilePath state_path_;
   const base::FilePath test_definitions_path_;
+  const bool enable_xmpp_;
 
   DISALLOW_COPY_AND_ASSIGN(Daemon);
 };
@@ -69,6 +73,8 @@
   DEFINE_string(test_definitions_path, "",
                 "Path to directory containing additional command "
                 "and state definitions.  For use in test only.");
+  DEFINE_bool(enable_xmpp, true,
+              "Connect to GCD via a persistent XMPP connection.");
   chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
   if (FLAGS_config_path.empty())
     FLAGS_config_path = kDefaultConfigFilePath;
@@ -81,6 +87,7 @@
 
   buffet::Daemon daemon{base::FilePath{FLAGS_config_path},
                         base::FilePath{FLAGS_state_path},
-                        base::FilePath{FLAGS_test_definitions_path}};
+                        base::FilePath{FLAGS_test_definitions_path},
+                        FLAGS_enable_xmpp};
   return daemon.Run();
 }
diff --git a/buffet/manager.cc b/buffet/manager.cc
index 31115b7..00c5ad2 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -48,6 +48,7 @@
 void Manager::RegisterAsync(const base::FilePath& config_path,
                             const base::FilePath& state_path,
                             const base::FilePath& test_definitions_path,
+                            bool xmpp_enabled,
                             const AsyncEventSequencer::CompletionAction& cb) {
   command_manager_ =
       std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
@@ -71,6 +72,7 @@
           std::move(config),
           chromeos::http::Transport::CreateDefault(),
           std::move(state_store),
+          xmpp_enabled,
           base::Bind(&Manager::OnRegistrationStatusChanged,
                      base::Unretained(this))));
   device_info_->Load();
diff --git a/buffet/manager.h b/buffet/manager.h
index f44e075..d21f062 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -51,6 +51,7 @@
       const base::FilePath& config_path,
       const base::FilePath& state_path,
       const base::FilePath& test_definitions_path,
+      bool xmpp_enabled,
       const chromeos::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
 
  private: