Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 19 | #include <android-base/unique_fd.h> |
| 20 | #include <android/content/pm/DataLoaderParamsParcel.h> |
| 21 | #include <android/content/pm/FileSystemControlParcel.h> |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 22 | #include <android/content/pm/IDataLoader.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 23 | #include <android/content/pm/IDataLoaderStatusListener.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 24 | #include <binder/IAppOpsCallback.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 26 | #include <binder/Status.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 27 | #include <incfs.h> |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 28 | #include <jni.h> |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 29 | #include <utils/Looper.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 30 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 31 | #include <memory> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 32 | #include <span> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <string_view> |
| 35 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 36 | namespace android::incremental { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 37 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame^] | 38 | using Clock = std::chrono::steady_clock; |
| 39 | using TimePoint = std::chrono::time_point<Clock>; |
| 40 | using Milliseconds = std::chrono::milliseconds; |
| 41 | using Job = std::function<void()>; |
| 42 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 43 | // --- Wrapper interfaces --- |
| 44 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 45 | using MountId = int32_t; |
| 46 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 47 | class VoldServiceWrapper { |
| 48 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 49 | virtual ~VoldServiceWrapper() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 50 | virtual binder::Status mountIncFs( |
| 51 | const std::string& backingPath, const std::string& targetDir, int32_t flags, |
| 52 | os::incremental::IncrementalFileSystemControlParcel* result) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 53 | virtual binder::Status unmountIncFs(const std::string& dir) const = 0; |
| 54 | virtual binder::Status bindMount(const std::string& sourceDir, |
| 55 | const std::string& targetDir) const = 0; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 56 | virtual binder::Status setIncFsMountOptions( |
| 57 | const os::incremental::IncrementalFileSystemControlParcel& control, |
| 58 | bool enableReadLogs) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 61 | class DataLoaderManagerWrapper { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 62 | public: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 63 | virtual ~DataLoaderManagerWrapper() = default; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 64 | virtual binder::Status bindToDataLoader( |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 65 | MountId mountId, const content::pm::DataLoaderParamsParcel& params, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 66 | const sp<content::pm::IDataLoaderStatusListener>& listener, bool* result) const = 0; |
| 67 | virtual binder::Status getDataLoader(MountId mountId, |
| 68 | sp<content::pm::IDataLoader>* result) const = 0; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 69 | virtual binder::Status unbindFromDataLoader(MountId mountId) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | class IncFsWrapper { |
| 73 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 74 | using Control = incfs::Control; |
| 75 | using FileId = incfs::FileId; |
| 76 | using ErrorCode = incfs::ErrorCode; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 77 | using WaitResult = incfs::WaitResult; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 78 | |
| 79 | using ExistingMountCallback = |
| 80 | std::function<void(std::string_view root, std::string_view backingDir, |
| 81 | std::span<std::pair<std::string_view, std::string_view>> binds)>; |
| 82 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 83 | virtual ~IncFsWrapper() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 84 | virtual void listExistingMounts(const ExistingMountCallback& cb) const = 0; |
| 85 | virtual Control openMount(std::string_view path) const = 0; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 86 | virtual Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const = 0; |
| 87 | virtual ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 88 | incfs::NewFileParams params) const = 0; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 89 | virtual ErrorCode makeDir(const Control& control, std::string_view path, int mode) const = 0; |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 90 | virtual ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const = 0; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 91 | virtual incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const = 0; |
| 92 | virtual incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const = 0; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 93 | virtual FileId getFileId(const Control& control, std::string_view path) const = 0; |
| 94 | virtual ErrorCode link(const Control& control, std::string_view from, |
| 95 | std::string_view to) const = 0; |
| 96 | virtual ErrorCode unlink(const Control& control, std::string_view path) const = 0; |
Yurii Zubrytskyi | e82cdd7 | 2020-04-01 12:19:26 -0700 | [diff] [blame] | 97 | virtual base::unique_fd openForSpecialOps(const Control& control, FileId id) const = 0; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 98 | virtual ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const = 0; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 99 | virtual WaitResult waitForPendingReads( |
| 100 | const Control& control, std::chrono::milliseconds timeout, |
| 101 | std::vector<incfs::ReadInfo>* pendingReadsBuffer) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 104 | class AppOpsManagerWrapper { |
| 105 | public: |
| 106 | virtual ~AppOpsManagerWrapper() = default; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 107 | virtual binder::Status checkPermission(const char* permission, const char* operation, |
| 108 | const char* package) const = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 109 | virtual void startWatchingMode(int32_t op, const String16& packageName, const sp<IAppOpsCallback>& callback) = 0; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame] | 110 | virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 113 | class JniWrapper { |
| 114 | public: |
| 115 | virtual ~JniWrapper() = default; |
| 116 | virtual void initializeForCurrentThread() const = 0; |
| 117 | }; |
| 118 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 119 | class LooperWrapper { |
| 120 | public: |
| 121 | virtual ~LooperWrapper() = default; |
| 122 | virtual int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback, |
| 123 | void* data) = 0; |
| 124 | virtual int removeFd(int fd) = 0; |
| 125 | virtual void wake() = 0; |
| 126 | virtual int pollAll(int timeoutMillis) = 0; |
| 127 | }; |
| 128 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame^] | 129 | class TimedQueueWrapper { |
| 130 | public: |
| 131 | virtual ~TimedQueueWrapper() = default; |
| 132 | virtual void addJob(MountId id, Milliseconds after, Job what) = 0; |
| 133 | virtual void removeJobs(MountId id) = 0; |
| 134 | virtual void stop() = 0; |
| 135 | }; |
| 136 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 137 | class ServiceManagerWrapper { |
| 138 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 139 | virtual ~ServiceManagerWrapper() = default; |
| 140 | virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 141 | virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 142 | virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 143 | virtual std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() = 0; |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 144 | virtual std::unique_ptr<JniWrapper> getJni() = 0; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 145 | virtual std::unique_ptr<LooperWrapper> getLooper() = 0; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame^] | 146 | virtual std::unique_ptr<TimedQueueWrapper> getTimedQueue() = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | // --- Real stuff --- |
| 150 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 151 | class RealServiceManager : public ServiceManagerWrapper { |
| 152 | public: |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 153 | RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 154 | ~RealServiceManager() = default; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 155 | std::unique_ptr<VoldServiceWrapper> getVoldService() final; |
| 156 | std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final; |
| 157 | std::unique_ptr<IncFsWrapper> getIncFs() final; |
| 158 | std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final; |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 159 | std::unique_ptr<JniWrapper> getJni() final; |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 160 | std::unique_ptr<LooperWrapper> getLooper() final; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame^] | 161 | std::unique_ptr<TimedQueueWrapper> getTimedQueue() final; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | template <class INTERFACE> |
| 165 | sp<INTERFACE> getRealService(std::string_view serviceName) const; |
| 166 | sp<android::IServiceManager> mServiceManager; |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 167 | JavaVM* const mJvm; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 170 | } // namespace android::incremental |