blob: c08a22efd3cc1590c01a8ac06a9bd04a97cf8e38 [file] [log] [blame]
Yifan Hong2272bf82017-04-28 14:37:56 -07001/*
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>
Yifan Honga72bde72017-09-28 13:36:48 -070026#include <vintf/RuntimeInfo.h>
27#include <vintf/parse_xml.h>
Yifan Hong2272bf82017-04-28 14:37:56 -070028
29namespace android {
30namespace vintf {
31namespace details {
32
Michael Schwartz97dc0f92017-05-08 14:07:14 -070033// Return the file from the given location as a string.
34//
35// This class can be used to create a mock for overriding.
36class FileFetcher {
37 public:
38 virtual ~FileFetcher() {}
39 virtual status_t fetch(const std::string& path, std::string& fetched) {
40 std::ifstream in;
41
42 in.open(path);
43 if (!in.is_open()) {
44 LOG(WARNING) << "Cannot open " << path;
45 return INVALID_OPERATION;
46 }
47
48 std::stringstream ss;
49 ss << in.rdbuf();
50 fetched = ss.str();
51
52 return OK;
Yifan Hong2272bf82017-04-28 14:37:56 -070053 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070054};
55
56extern FileFetcher* gFetcher;
57
Yifan Hong8640cd12017-05-17 12:02:28 -070058class PartitionMounter {
59 public:
60 virtual ~PartitionMounter() {}
61 virtual status_t mountSystem() const { return OK; }
62 virtual status_t mountVendor() const { return OK; }
63 virtual status_t umountSystem() const { return OK; }
64 virtual status_t umountVendor() const { return OK; }
65};
66
67extern PartitionMounter* gPartitionMounter;
68
Michael Schwartz97dc0f92017-05-08 14:07:14 -070069template <typename T>
70status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter,
71 T* outObject) {
72 std::string info;
73
74 if (gFetcher == nullptr) {
75 // Should never happen.
76 return NO_INIT;
77 }
78
79 status_t result = gFetcher->fetch(path, info);
80
81 if (result != OK) {
82 return result;
83 }
84
85 bool success = converter(outObject, info);
Yifan Hong2272bf82017-04-28 14:37:56 -070086 if (!success) {
87 LOG(ERROR) << "Illformed file: " << path << ": "
88 << converter.lastError();
89 return BAD_VALUE;
90 }
91 return OK;
92}
93
Yifan Hong29bb2d42017-09-27 13:28:00 -070094template <typename T>
95class ObjectFactory {
96 public:
97 virtual ~ObjectFactory() = default;
98 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
99};
100extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
101
Yifan Hong2272bf82017-04-28 14:37:56 -0700102} // namespace details
103} // namespace vintf
104} // namespace android
105
106
107
108#endif