[maxwell_integration] Get EnvironmentController when creating environment in StartAgent

EnvironmentController is being made a required argument for
CreateNestedEnvironment in fxr/241175. This helps ensure that
Environments aren't abandoned unintenionally and never cleaned up,
especially in tests.

This also refactors AgentLauncher::StartAgent out bin/sessionmgr, since it
is not actually used by sessionmgr currently, and into
tests/maxwell_integration's base test class, MaxwellTestBase.

Test: fx run-test maxwell_integration_tests (although it seems like
StartAgent might be dead code even in this test anyway...)

Change-Id: I0588b56b78b7d0464a3b7d15ab17e9c2a4aec5bd
diff --git a/bin/sessionmgr/BUILD.gn b/bin/sessionmgr/BUILD.gn
index b05e909..c846eee 100644
--- a/bin/sessionmgr/BUILD.gn
+++ b/bin/sessionmgr/BUILD.gn
@@ -212,7 +212,6 @@
   ]
 
   deps = [
-    ":agent_launcher",
     ":focus",
     ":intelligence_services",
     "//garnet/public/fidl/fuchsia.bluetooth.le",
@@ -235,18 +234,3 @@
     "//peridot/public/fidl/fuchsia.modular",
   ]
 }
-
-source_set("agent_launcher") {
-  sources = [
-    "agent_launcher.cc",
-    "agent_launcher.h",
-  ]
-
-  public_deps = [
-    "//garnet/public/lib/component/cpp",
-  ]
-
-  deps = [
-    "//peridot/lib/environment_host",
-  ]
-}
diff --git a/bin/sessionmgr/agent_launcher.cc b/bin/sessionmgr/agent_launcher.cc
deleted file mode 100644
index e843039..0000000
--- a/bin/sessionmgr/agent_launcher.cc
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2016 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "peridot/bin/sessionmgr/agent_launcher.h"
-
-#include <lib/fxl/logging.h>
-
-namespace modular {
-namespace {
-
-constexpr char kEnvironmentLabel[] = "agent";
-
-}  // namespace
-
-component::Services AgentLauncher::StartAgent(
-    const std::string& url,
-    std::unique_ptr<maxwell::MaxwellServiceProviderBridge> bridge) {
-  bridge_ = std::move(bridge);
-  fuchsia::sys::ServiceListPtr service_list(new fuchsia::sys::ServiceList);
-  service_list->names = bridge_->service_names();
-  service_list->host_directory = bridge_->OpenAsDirectory();
-  fuchsia::sys::EnvironmentPtr agent_env;
-  environment_->CreateNestedEnvironment(
-      agent_env.NewRequest(), /*controller=*/nullptr, kEnvironmentLabel,
-      std::move(service_list), {});
-
-  fuchsia::sys::LauncherPtr agent_launcher;
-  agent_env->GetLauncher(agent_launcher.NewRequest());
-
-  fuchsia::sys::LaunchInfo launch_info;
-  launch_info.url = url;
-  component::Services services;
-  launch_info.directory_request = services.NewRequest();
-  FXL_LOG(INFO) << "Starting modular agent " << url;
-  agent_launcher->CreateComponent(std::move(launch_info), nullptr);
-  return services;
-}
-
-}  // namespace modular
diff --git a/bin/sessionmgr/agent_launcher.h b/bin/sessionmgr/agent_launcher.h
deleted file mode 100644
index b4f6a2f..0000000
--- a/bin/sessionmgr/agent_launcher.h
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright 2016 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef PERIDOT_BIN_SESSIONMGR_AGENT_LAUNCHER_H_
-#define PERIDOT_BIN_SESSIONMGR_AGENT_LAUNCHER_H_
-
-#include <fuchsia/sys/cpp/fidl.h>
-#include <lib/fidl/cpp/binding_set.h>
-#include <lib/svc/cpp/services.h>
-
-#include "peridot/lib/environment_host/maxwell_service_provider_bridge.h"
-
-namespace modular {
-
-class AgentLauncher {
- public:
-  AgentLauncher(fuchsia::sys::Environment* environment)
-      : environment_(environment) {}
-  component::Services StartAgent(
-      const std::string& url,
-      std::unique_ptr<maxwell::MaxwellServiceProviderBridge> bridge);
-
- private:
-  fuchsia::sys::Environment* environment_;
-
-  std::unique_ptr<maxwell::MaxwellServiceProviderBridge> bridge_;
-};
-
-}  // namespace modular
-
-#endif  // PERIDOT_BIN_SESSIONMGR_AGENT_LAUNCHER_H_
diff --git a/bin/sessionmgr/user_intelligence_provider_impl.h b/bin/sessionmgr/user_intelligence_provider_impl.h
index 8b31804..cbf6779 100644
--- a/bin/sessionmgr/user_intelligence_provider_impl.h
+++ b/bin/sessionmgr/user_intelligence_provider_impl.h
@@ -14,7 +14,6 @@
 #include <lib/svc/cpp/services.h>
 #include <lib/zx/channel.h>
 
-#include "peridot/bin/sessionmgr/agent_launcher.h"
 #include "peridot/lib/util/rate_limited_retry.h"
 
 namespace modular {
diff --git a/tests/maxwell_integration/BUILD.gn b/tests/maxwell_integration/BUILD.gn
index b8da28c..c72a6a9 100644
--- a/tests/maxwell_integration/BUILD.gn
+++ b/tests/maxwell_integration/BUILD.gn
@@ -83,7 +83,6 @@
   ]
 
   deps = [
-    "//peridot/bin/sessionmgr:agent_launcher",
     "//peridot/lib/environment_host",
     "//peridot/lib/testing:component_context_fake",
     "//peridot/lib/testing:entity_resolver_fake",
diff --git a/tests/maxwell_integration/test.cc b/tests/maxwell_integration/test.cc
index 8ce395b..f2f2874 100644
--- a/tests/maxwell_integration/test.cc
+++ b/tests/maxwell_integration/test.cc
@@ -7,13 +7,14 @@
 #include <lib/fxl/logging.h>
 
 namespace maxwell {
+namespace {
+
+constexpr char kEnvironmentLabel[] = "maxwell-test-env";
+
+}
 
 MaxwellTestBase::MaxwellTestBase() : loop_(&kAsyncLoopConfigAttachToThread) {
   startup_context_ = component::StartupContext::CreateFromStartupInfo();
-  auto root_environment = startup_context_->environment().get();
-  FXL_CHECK(root_environment != nullptr);
-
-  agent_launcher_ = std::make_unique<modular::AgentLauncher>(root_environment);
 
   child_app_services_.AddService<fuchsia::modular::ComponentContext>(
       [this](
@@ -22,6 +23,30 @@
       });
 }
 
+void MaxwellTestBase::StartAgent(
+    const std::string& url,
+    std::unique_ptr<MaxwellServiceProviderBridge> bridge) {
+  bridge_ = std::move(bridge);
+
+  fuchsia::sys::ServiceListPtr service_list(new fuchsia::sys::ServiceList);
+  service_list->names = bridge_->service_names();
+  service_list->host_directory = bridge_->OpenAsDirectory();
+  fuchsia::sys::EnvironmentPtr agent_env;
+  startup_context_->environment()->CreateNestedEnvironment(
+      agent_env.NewRequest(), environment_controller_.NewRequest(),
+      kEnvironmentLabel, std::move(service_list), {});
+
+  fuchsia::sys::LauncherPtr launcher;
+  agent_env->GetLauncher(launcher.NewRequest());
+
+  fuchsia::sys::LaunchInfo launch_info;
+  launch_info.url = url;
+  component::Services services;
+  launch_info.directory_request = services.NewRequest();
+  FXL_LOG(INFO) << "Starting modular agent " << url;
+  launcher->CreateComponent(std::move(launch_info), nullptr);
+}
+
 component::Services MaxwellTestBase::StartServices(const std::string& url) {
   component::Services services;
   fuchsia::sys::LaunchInfo launch_info;
diff --git a/tests/maxwell_integration/test.h b/tests/maxwell_integration/test.h
index 1516e80..d30c743 100644
--- a/tests/maxwell_integration/test.h
+++ b/tests/maxwell_integration/test.h
@@ -5,13 +5,15 @@
 #ifndef PERIDOT_TESTS_MAXWELL_INTEGRATION_TEST_H_
 #define PERIDOT_TESTS_MAXWELL_INTEGRATION_TEST_H_
 
+#include <fuchsia/sys/cpp/fidl.h>
 #include <lib/async-loop/cpp/loop.h>
 #include <lib/component/cpp/connect.h>
 #include <lib/component/cpp/service_provider_impl.h>
 #include <lib/component/cpp/startup_context.h>
+#include <lib/svc/cpp/services.h>
 
 #include "gtest/gtest.h"
-#include "peridot/bin/sessionmgr/agent_launcher.h"
+#include "peridot/lib/environment_host/maxwell_service_provider_bridge.h"
 #include "peridot/lib/testing/component_context_fake.h"
 #include "peridot/lib/testing/entity_resolver_fake.h"
 
@@ -23,9 +25,7 @@
   ~MaxwellTestBase() override = default;
 
   void StartAgent(const std::string& url,
-                  std::unique_ptr<MaxwellServiceProviderBridge> bridge) {
-    agent_launcher_->StartAgent(url, std::move(bridge));
-  }
+                  std::unique_ptr<MaxwellServiceProviderBridge> bridge);
 
   component::Services StartServices(const std::string& url);
 
@@ -45,9 +45,11 @@
 
  private:
   std::unique_ptr<component::StartupContext> startup_context_;
-  std::unique_ptr<modular::AgentLauncher> agent_launcher_;
+  fuchsia::sys::EnvironmentControllerPtr environment_controller_;
   std::vector<fuchsia::sys::ComponentControllerPtr> component_ptrs_;
 
+  std::unique_ptr<maxwell::MaxwellServiceProviderBridge> bridge_;
+
   component::ServiceProviderImpl child_app_services_;
   modular::ComponentContextFake child_component_context_;
 };