1 /*
2  * Copyright (C) 2022 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/libs/config/fastboot/fastboot.h"
17 
18 #include <android-base/logging.h>
19 #include <json/json.h>
20 
21 #include "host/libs/config/config_fragment.h"
22 
23 namespace cuttlefish {
24 namespace {
25 
26 class FastbootConfigFragmentImpl : public FastbootConfigFragment {
27  public:
INJECT(FastbootConfigFragmentImpl (FastbootConfig & config))28   INJECT(FastbootConfigFragmentImpl(FastbootConfig& config)) : config_(config) {}
29 
Name() const30   std::string Name() const override { return "FastbootConfigFragmentImpl"; }
31 
Serialize() const32   Json::Value Serialize() const override {
33     Json::Value json;
34     json[kProxyFastboot] = config_.ProxyFastboot();
35     return json;
36   }
37 
Deserialize(const Json::Value & json)38   bool Deserialize(const Json::Value& json) override {
39     if (!json.isMember(kProxyFastboot) ||
40         json[kProxyFastboot].type() != Json::booleanValue) {
41       LOG(ERROR) << "Invalid value for " << kProxyFastboot;
42       return false;
43     }
44     if (!config_.SetProxyFastboot(json[kProxyFastboot].asBool())) {
45       LOG(ERROR) << "Failed to set whether to run the fastboot proxy";
46     }
47     return true;
48   }
49 
50  private:
51   static constexpr char kProxyFastboot[] = "proxy_fastboot";
52   FastbootConfig& config_;
53 };
54 
55 }  // namespace
56 
57 fruit::Component<fruit::Required<FastbootConfig>, FastbootConfigFragment>
FastbootConfigFragmentComponent()58 FastbootConfigFragmentComponent() {
59   return fruit::createComponent()
60       .bind<FastbootConfigFragment, FastbootConfigFragmentImpl>()
61       .addMultibinding<ConfigFragment, FastbootConfigFragment>();
62 }
63 
64 }  // namespace cuttlefish
65