blob: c401995b744a1bcbc78de8f6e8c79c4995f24ec4 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huber5345ec22016-07-29 13:33:27 -070017#ifndef COORDINATOR_H_
18
19#define COORDINATOR_H_
20
21#include <android-base/macros.h>
Andreas Hubere61e3f72016-08-03 10:22:03 -070022#include <functional>
Steven Morelandd537ab02016-09-12 10:32:01 -070023#include <map>
Andreas Huber39fa7182016-08-19 14:27:33 -070024#include <set>
Andreas Huber5345ec22016-07-29 13:33:27 -070025#include <string>
Steven Morelandd537ab02016-09-12 10:32:01 -070026#include <utils/Errors.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <vector>
Andreas Huber5345ec22016-07-29 13:33:27 -070028
29namespace android {
30
31struct AST;
32struct FQName;
Andreas Huberfd4afab2016-08-03 13:02:57 -070033struct Type;
Andreas Huber5345ec22016-07-29 13:33:27 -070034
35struct Coordinator {
Andreas Huberdca261f2016-08-04 13:47:51 -070036 Coordinator(
37 const std::vector<std::string> &packageRootPaths,
Steven Morelandf7fa0682017-05-11 16:14:55 -070038 const std::vector<std::string> &packageRoots,
39 const std::string &rootPath);
Andreas Huberdca261f2016-08-04 13:47:51 -070040
Andreas Huber5345ec22016-07-29 13:33:27 -070041 ~Coordinator();
42
Steven Moreland25c81662017-05-12 14:57:36 -070043 // adds path only if it doesn't exist
44 void addDefaultPackagePath(const std::string& root, const std::string& path);
45
Andreas Huber39fa7182016-08-19 14:27:33 -070046 // Attempts to parse the interface/types referred to by fqName.
47 // Parsing an interface also parses the associated package's types.hal
48 // file if it exists.
49 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
50 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070051 // If !enforce, enforceRestrictionsOnPackage won't be run.
52 AST *parse(const FQName &fqName, std::set<AST *> *parsedASTs = nullptr,
Steven Moreland28b9b532017-05-12 17:02:58 -070053 bool enforce = true) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070054
Andreas Huberdca261f2016-08-04 13:47:51 -070055 // Given package-root paths of ["hardware/interfaces",
56 // "vendor/<something>/interfaces"], package roots of
57 // ["android.hardware", "vendor.<something>.hardware"], and a
58 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -080059 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
60 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Andreas Huber881227d2016-08-02 14:20:21 -070061 std::string getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -080062 const FQName &fqName, bool relative = false,
63 bool sanitized = false) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070064
Andreas Huberdca261f2016-08-04 13:47:51 -070065 // Given package roots of ["android.hardware",
66 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -070067 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -070068 // return "android.hardware".
Andreas Huberdca261f2016-08-04 13:47:51 -070069 std::string getPackageRoot(const FQName &fqName) const;
70
Iliyan Malchev5bb14022016-08-09 15:04:39 -070071 // Given package-root paths of ["hardware/interfaces",
72 // "vendor/<something>/interfaces"], package roots of
73 // ["android.hardware", "vendor.<something>.hardware"], and a
74 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
75 // will return "hardware/interfaces".
Iliyan Malchev5bb14022016-08-09 15:04:39 -070076 std::string getPackageRootPath(const FQName &fqName) const;
77
Yifan Hongc8934042016-11-17 17:10:52 -080078 // return getPackageRoot + ":" + getPackageRootPath
79 std::string getPackageRootOption(const FQName &fqName) const;
80
Andreas Huberd2943e12016-08-05 11:59:31 -070081 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
82 // "android/hardware/".
83 std::string convertPackageRootToPath(const FQName &fqName) const;
84
85 status_t getPackageInterfaceFiles(
86 const FQName &package,
87 std::vector<std::string> *fileNames) const;
88
Steven Morelandaa186832016-09-26 13:51:43 -070089 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -070090 const FQName &package,
91 std::vector<FQName> *packageInterfaces) const;
92
Yifan Hong78b38d12017-02-13 18:14:46 +000093 // Enforce a set of restrictions on a set of packages. These include:
94 // - minor version upgrades
95 // "packages" contains names like "android.hardware.nfc@1.1".
Steven Moreland218625a2017-04-18 22:31:50 -070096 // - hashing restrictions
Steven Moreland28b9b532017-05-12 17:02:58 -070097 status_t enforceRestrictionsOnPackage(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +000098
Andreas Huberd2943e12016-08-05 11:59:31 -070099 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700100
Andreas Huber5345ec22016-07-29 13:33:27 -0700101private:
Andreas Huberdca261f2016-08-04 13:47:51 -0700102 // A list of top-level directories (mPackageRootPaths)
103 // corresponding to a list of package roots (mPackageRoots). For
104 // example, if mPackageRootPaths[0] == "hardware/interfaces" and
105 // mPackageRoots[0] == "android.hardware" this means that all
106 // packages starting with "android.hardware" will be looked up in
107 // "hardware/interfaces".
108 std::vector<std::string> mPackageRootPaths;
109 std::vector<std::string> mPackageRoots;
Yifan Hong78b38d12017-02-13 18:14:46 +0000110
Steven Morelandf7fa0682017-05-11 16:14:55 -0700111 std::string mRootPath;
112
Yifan Hong78b38d12017-02-13 18:14:46 +0000113 // cache to parse().
Steven Moreland28b9b532017-05-12 17:02:58 -0700114 mutable std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700115
Yifan Hong78b38d12017-02-13 18:14:46 +0000116 // cache to enforceRestrictionsOnPackage().
Steven Moreland28b9b532017-05-12 17:02:58 -0700117 mutable std::set<FQName> mPackagesEnforced;
Yifan Hong78b38d12017-02-13 18:14:46 +0000118
Andreas Huberdca261f2016-08-04 13:47:51 -0700119 std::vector<std::string>::const_iterator findPackageRoot(
120 const FQName &fqName) const;
121
Steven Morelandb90d3272017-05-26 10:02:39 -0700122 // Returns abs package path by prepending the root path if a package
123 // path is non-absolute.
124 // If root is '/android/master' and getPackagePath returns 'h/i/nfc/V1_0'
125 // this will return '/android/master/h/i/nfc/V1_0'.
126 // If root is '/android/master' and getPackagePath returns '/abs/path/to/nfc/V1_0'
127 // this will return '/abs/path/to/nfc/V1_0'
128 std::string getAbsolutePackagePath(const FQName& fqName) const;
129
Yifan Hong78b38d12017-02-13 18:14:46 +0000130 // Rules of enforceRestrictionsOnPackage are listed below.
Steven Moreland28b9b532017-05-12 17:02:58 -0700131 status_t enforceMinorVersionUprevs(const FQName &fqName) const;
132 status_t enforceHashes(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000133
Andreas Huber5345ec22016-07-29 13:33:27 -0700134 DISALLOW_COPY_AND_ASSIGN(Coordinator);
135};
136
137} // namespace android
138
139#endif // COORDINATOR_H_