Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef ANDROID_VINTF_UTILS_H |
| 18 | #define ANDROID_VINTF_UTILS_H |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <iostream> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <utils/Errors.h> |
| 26 | |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame^] | 27 | #include "RuntimeInfo.h" |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 28 | #include "parse_xml.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vintf { |
| 32 | namespace details { |
| 33 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 34 | // Return the file from the given location as a string. |
| 35 | // |
| 36 | // This class can be used to create a mock for overriding. |
| 37 | class FileFetcher { |
| 38 | public: |
| 39 | virtual ~FileFetcher() {} |
| 40 | virtual status_t fetch(const std::string& path, std::string& fetched) { |
| 41 | std::ifstream in; |
| 42 | |
| 43 | in.open(path); |
| 44 | if (!in.is_open()) { |
| 45 | LOG(WARNING) << "Cannot open " << path; |
| 46 | return INVALID_OPERATION; |
| 47 | } |
| 48 | |
| 49 | std::stringstream ss; |
| 50 | ss << in.rdbuf(); |
| 51 | fetched = ss.str(); |
| 52 | |
| 53 | return OK; |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 54 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | extern FileFetcher* gFetcher; |
| 58 | |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 59 | class PartitionMounter { |
| 60 | public: |
| 61 | virtual ~PartitionMounter() {} |
| 62 | virtual status_t mountSystem() const { return OK; } |
| 63 | virtual status_t mountVendor() const { return OK; } |
| 64 | virtual status_t umountSystem() const { return OK; } |
| 65 | virtual status_t umountVendor() const { return OK; } |
| 66 | }; |
| 67 | |
| 68 | extern PartitionMounter* gPartitionMounter; |
| 69 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 70 | template <typename T> |
| 71 | status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter, |
| 72 | T* outObject) { |
| 73 | std::string info; |
| 74 | |
| 75 | if (gFetcher == nullptr) { |
| 76 | // Should never happen. |
| 77 | return NO_INIT; |
| 78 | } |
| 79 | |
| 80 | status_t result = gFetcher->fetch(path, info); |
| 81 | |
| 82 | if (result != OK) { |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | bool success = converter(outObject, info); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 87 | if (!success) { |
| 88 | LOG(ERROR) << "Illformed file: " << path << ": " |
| 89 | << converter.lastError(); |
| 90 | return BAD_VALUE; |
| 91 | } |
| 92 | return OK; |
| 93 | } |
| 94 | |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame^] | 95 | template <typename T> |
| 96 | class ObjectFactory { |
| 97 | public: |
| 98 | virtual ~ObjectFactory() = default; |
| 99 | virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); } |
| 100 | }; |
| 101 | extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory; |
| 102 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 103 | } // namespace details |
| 104 | } // namespace vintf |
| 105 | } // namespace android |
| 106 | |
| 107 | |
| 108 | |
| 109 | #endif |