1 //
2 // Copyright (C) 2023 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "host/commands/run_cvd/launch/launch.h"
17 
18 #include <string>
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22 
23 #include <fruit/fruit.h>
24 
25 #include "common/libs/utils/result.h"
26 #include "host/commands/run_cvd/launch/grpc_socket_creator.h"
27 #include "host/libs/config/command_source.h"
28 #include "host/libs/config/known_paths.h"
29 
30 namespace cuttlefish {
31 namespace {
32 
33 class ControlEnvProxyServer : public CommandSource {
34  public:
INJECT(ControlEnvProxyServer (const CuttlefishConfig::InstanceSpecific & instance,GrpcSocketCreator & grpc_socket))35   INJECT(
36       ControlEnvProxyServer(const CuttlefishConfig::InstanceSpecific& instance,
37                             GrpcSocketCreator& grpc_socket))
38       : instance_(instance), grpc_socket_(grpc_socket) {}
39 
40   // CommandSource
Commands()41   Result<std::vector<MonitorCommand>> Commands() override {
42     Command command(ControlEnvProxyServerBinary());
43     command.AddParameter("--grpc_uds_path=",
44                          grpc_socket_.CreateGrpcSocket(Name()));
45     command.AddParameter("--grpc_socket_path=", instance_.grpc_socket_path());
46     std::vector<MonitorCommand> commands;
47     commands.emplace_back(std::move(command));
48     return commands;
49   }
50 
51   // SetupFeature
Name() const52   std::string Name() const override { return "ControlEnvProxyServer"; }
Enabled() const53   bool Enabled() const override { return true; }
54 
55  private:
Dependencies() const56   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()57   Result<void> ResultSetup() override { return {}; }
58 
59   const CuttlefishConfig::InstanceSpecific& instance_;
60   GrpcSocketCreator& grpc_socket_;
61 };
62 
63 }  // namespace
64 
65 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific,
66                                  GrpcSocketCreator>>
ControlEnvProxyServerComponent()67 ControlEnvProxyServerComponent() {
68   return fruit::createComponent()
69       .addMultibinding<CommandSource, ControlEnvProxyServer>()
70       .addMultibinding<SetupFeature, ControlEnvProxyServer>();
71 }
72 
73 }  // namespace cuttlefish
74