blob: dce03b38ea75a9eb259d1acbfd242882c37f9682 [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
Yifan Hong2272bf82017-04-28 14:37:56 -070024#include <utils/Errors.h>
Yifan Honga72bde72017-09-28 13:36:48 -070025#include <vintf/RuntimeInfo.h>
26#include <vintf/parse_xml.h>
Yifan Hong2272bf82017-04-28 14:37:56 -070027
28namespace android {
29namespace vintf {
30namespace details {
31
Michael Schwartz97dc0f92017-05-08 14:07:14 -070032// Return the file from the given location as a string.
33//
34// This class can be used to create a mock for overriding.
35class FileFetcher {
36 public:
37 virtual ~FileFetcher() {}
Yifan Hong60217032018-01-08 16:19:42 -080038 status_t fetchInternal(const std::string& path, std::string& fetched, std::string* error) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070039 std::ifstream in;
40
41 in.open(path);
42 if (!in.is_open()) {
Yifan Hong60217032018-01-08 16:19:42 -080043 if (error) {
44 *error = "Cannot open " + path;
45 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070046 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 }
Yifan Hong60217032018-01-08 16:19:42 -080055 virtual status_t fetch(const std::string& path, std::string& fetched, std::string* error) {
56 return fetchInternal(path, fetched, error);
57 }
58 virtual status_t fetch(const std::string& path, std::string& fetched) {
59 return fetchInternal(path, fetched, nullptr);
60 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070061};
62
63extern FileFetcher* gFetcher;
64
Yifan Hong8640cd12017-05-17 12:02:28 -070065class PartitionMounter {
66 public:
67 virtual ~PartitionMounter() {}
68 virtual status_t mountSystem() const { return OK; }
69 virtual status_t mountVendor() const { return OK; }
70 virtual status_t umountSystem() const { return OK; }
71 virtual status_t umountVendor() const { return OK; }
72};
73
74extern PartitionMounter* gPartitionMounter;
75
Michael Schwartz97dc0f92017-05-08 14:07:14 -070076template <typename T>
77status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter,
Yifan Hong60217032018-01-08 16:19:42 -080078 T* outObject, std::string* error = nullptr) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070079 std::string info;
80
81 if (gFetcher == nullptr) {
82 // Should never happen.
83 return NO_INIT;
84 }
85
Yifan Hong60217032018-01-08 16:19:42 -080086 status_t result = gFetcher->fetch(path, info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -070087
88 if (result != OK) {
89 return result;
90 }
91
92 bool success = converter(outObject, info);
Yifan Hong2272bf82017-04-28 14:37:56 -070093 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -080094 if (error) {
95 *error = "Illformed file: " + path + ": " + converter.lastError();
96 }
Yifan Hong2272bf82017-04-28 14:37:56 -070097 return BAD_VALUE;
98 }
99 return OK;
100}
101
Yifan Hong29bb2d42017-09-27 13:28:00 -0700102template <typename T>
103class ObjectFactory {
104 public:
105 virtual ~ObjectFactory() = default;
106 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
107};
108extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
109
Yifan Hong2272bf82017-04-28 14:37:56 -0700110} // namespace details
111} // namespace vintf
112} // namespace android
113
114
115
116#endif