1 /* 2 * Copyright (C) 2018 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 #pragma once 18 19 #include <memory> 20 21 #include "SchemaType.h" 22 #include "VintfObject.h" 23 24 namespace android { 25 namespace vintf { 26 namespace details { 27 /** 28 * Simulate the state of VintfObject after an update. 29 * 30 * - Old metadata is stored in parent VintfObject. 31 * - New (updated) metadata is stored in this VintfObjectAfterUpdate 32 * - Dependencies are from the given VintfObject (dep) before construction. 33 */ 34 class VintfObjectAfterUpdate : public VintfObject { 35 public: 36 /* Use dependencies from the object dep. */ VintfObjectAfterUpdate(VintfObject * dep)37 VintfObjectAfterUpdate(VintfObject* dep) : mDependency(dep) {} 38 39 std::shared_ptr<const HalManifest> getDeviceHalManifest(bool skipCache = false) override { 40 if (mDeviceManifest != nullptr) return mDeviceManifest; 41 return VintfObject::getDeviceHalManifest(skipCache); 42 } 43 44 std::shared_ptr<const HalManifest> getFrameworkHalManifest(bool skipCache = false) override { 45 if (mFrameworkManifest != nullptr) return mFrameworkManifest; 46 return VintfObject::getFrameworkHalManifest(skipCache); 47 } 48 49 std::shared_ptr<const CompatibilityMatrix> getDeviceCompatibilityMatrix( 50 bool skipCache = false) override { 51 if (mDeviceMatrix != nullptr) return mDeviceMatrix; 52 return VintfObject::getDeviceCompatibilityMatrix(skipCache); 53 } 54 55 std::shared_ptr<const CompatibilityMatrix> getFrameworkCompatibilityMatrix( 56 bool skipCache = false) override { 57 if (mFrameworkMatrix != nullptr) return mFrameworkMatrix; 58 return VintfObject::getFrameworkCompatibilityMatrix(skipCache); 59 } 60 getFileSystem()61 const std::unique_ptr<FileSystem>& getFileSystem() override { 62 return mDependency->getFileSystem(); 63 } 64 getPropertyFetcher()65 const std::unique_ptr<PropertyFetcher>& getPropertyFetcher() override { 66 return mDependency->getPropertyFetcher(); 67 } 68 getRuntimeInfoFactory()69 const std::unique_ptr<ObjectFactory<RuntimeInfo>>& getRuntimeInfoFactory() override { 70 return mDependency->getRuntimeInfoFactory(); 71 } 72 set(const std::shared_ptr<HalManifest> & o)73 bool set(const std::shared_ptr<HalManifest>& o) { 74 return set(o, &mDeviceManifest, &mFrameworkManifest); 75 } 76 set(const std::shared_ptr<CompatibilityMatrix> & o)77 bool set(const std::shared_ptr<CompatibilityMatrix>& o) { 78 return set(o, &mDeviceMatrix, &mFrameworkMatrix); 79 } 80 81 private: 82 VintfObject* mDependency = nullptr; 83 std::shared_ptr<HalManifest> mDeviceManifest; 84 std::shared_ptr<HalManifest> mFrameworkManifest; 85 std::shared_ptr<CompatibilityMatrix> mDeviceMatrix; 86 std::shared_ptr<CompatibilityMatrix> mFrameworkMatrix; 87 88 template <typename T> set(const std::shared_ptr<T> & o,std::shared_ptr<T> * dev,std::shared_ptr<T> * fwk)89 bool set(const std::shared_ptr<T>& o, std::shared_ptr<T>* dev, std::shared_ptr<T>* fwk) { 90 if (o->type() == SchemaType::DEVICE) { 91 if (*dev != nullptr) return false; 92 *dev = o; 93 return true; 94 } else if (o->type() == SchemaType::FRAMEWORK) { 95 if (*fwk != nullptr) return false; 96 *fwk = o; 97 return true; 98 } 99 return false; 100 } 101 }; 102 103 } // namespace details 104 } // namespace vintf 105 } // namespace android 106