• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "host/commands/cvd/server_command/try_acloud.h"
18 
19 #include <mutex>
20 
21 #include <fruit/fruit.h>
22 
23 #include "common/libs/utils/result.h"
24 #include "cvd_server.pb.h"
25 #include "host/commands/cvd/server_command/server_handler.h"
26 #include "host/commands/cvd/server_command/utils.h"
27 #include "host/commands/cvd/types.h"
28 
29 namespace cuttlefish {
30 
31 class TryAcloudCommand : public CvdServerHandler {
32  public:
INJECT(TryAcloudCommand (ConvertAcloudCreateCommand & converter,ANNOTATED (AcloudTranslatorOptOut,const std::atomic<bool> &)optout))33   INJECT(TryAcloudCommand(ConvertAcloudCreateCommand& converter,
34                           ANNOTATED(AcloudTranslatorOptOut,
35                                     const std::atomic<bool>&) optout))
36       : converter_(converter), optout_(optout) {}
37   ~TryAcloudCommand() = default;
38 
CanHandle(const RequestWithStdio & request) const39   Result<bool> CanHandle(const RequestWithStdio& request) const override {
40     auto invocation = ParseInvocation(request.Message());
41     return invocation.command == "try-acloud";
42   }
43 
CmdList() const44   cvd_common::Args CmdList() const override { return {"try-acloud"}; }
45 
Handle(const RequestWithStdio & request)46   Result<cvd::Response> Handle(const RequestWithStdio& request) override {
47     CF_EXPECT(CanHandle(request));
48     CF_EXPECT(IsSubOperationSupported(request));
49     CF_EXPECT(converter_.Convert(request));
50     // currently, optout/optin feature only works in local instance
51     // remote instance still uses legacy python acloud
52     CF_EXPECT(!optout_);
53     cvd::Response response;
54     response.mutable_command_response();
55     return response;
56   }
Interrupt()57   Result<void> Interrupt() override { return CF_ERR("Can't be interrupted."); }
58 
59  private:
60   ConvertAcloudCreateCommand& converter_;
61   const std::atomic<bool>& optout_;
62 };
63 
64 fruit::Component<fruit::Required<
65     ConvertAcloudCreateCommand,
66     fruit::Annotated<AcloudTranslatorOptOut, std::atomic<bool>>>>
TryAcloudCommandComponent()67 TryAcloudCommandComponent() {
68   return fruit::createComponent()
69       .addMultibinding<CvdServerHandler, TryAcloudCommand>();
70 }
71 
72 }  // namespace cuttlefish
73