blob: 973c52499475abcf741b9a16359f00be15acd275 [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>
26
Yifan Hong29bb2d42017-09-27 13:28:00 -070027#include "RuntimeInfo.h"
Yifan Hong2272bf82017-04-28 14:37:56 -070028#include "parse_xml.h"
29
30namespace android {
31namespace vintf {
32namespace details {
33
Michael Schwartz97dc0f92017-05-08 14:07:14 -070034// Return the file from the given location as a string.
35//
36// This class can be used to create a mock for overriding.
37class 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 Hong2272bf82017-04-28 14:37:56 -070054 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070055};
56
57extern FileFetcher* gFetcher;
58
Yifan Hong8640cd12017-05-17 12:02:28 -070059class 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
68extern PartitionMounter* gPartitionMounter;
69
Michael Schwartz97dc0f92017-05-08 14:07:14 -070070template <typename T>
71status_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 Hong2272bf82017-04-28 14:37:56 -070087 if (!success) {
88 LOG(ERROR) << "Illformed file: " << path << ": "
89 << converter.lastError();
90 return BAD_VALUE;
91 }
92 return OK;
93}
94
Yifan Hong29bb2d42017-09-27 13:28:00 -070095template <typename T>
96class ObjectFactory {
97 public:
98 virtual ~ObjectFactory() = default;
99 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
100};
101extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
102
Yifan Hong2272bf82017-04-28 14:37:56 -0700103} // namespace details
104} // namespace vintf
105} // namespace android
106
107
108
109#endif