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 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 17 | #define LOG_TAG "IncrementalService" |
| 18 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 19 | #include "ServiceWrappers.h" |
| 20 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 21 | #include <MountRegistry.h> |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 23 | #include <android/content/pm/IDataLoaderManager.h> |
| 24 | #include <android/os/IVold.h> |
| 25 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 26 | #include <utils/String16.h> |
| 27 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 28 | #include <filesystem> |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 29 | #include <thread> |
| 30 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 31 | #include "IncrementalServiceValidation.h" |
| 32 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | using namespace std::literals; |
| 34 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 35 | namespace android::incremental { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 36 | |
| 37 | static constexpr auto kVoldServiceName = "vold"sv; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 38 | static constexpr auto kDataLoaderManagerName = "dataloader_manager"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 39 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 40 | class RealVoldService : public VoldServiceWrapper { |
| 41 | public: |
Yurii Zubrytskyi | 510037b | 2020-04-22 15:46:21 -0700 | [diff] [blame] | 42 | RealVoldService(sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 43 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 44 | binder::Status mountIncFs( |
| 45 | const std::string& backingPath, const std::string& targetDir, int32_t flags, |
| 46 | os::incremental::IncrementalFileSystemControlParcel* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 47 | return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return); |
| 48 | } |
| 49 | binder::Status unmountIncFs(const std::string& dir) const final { |
| 50 | return mInterface->unmountIncFs(dir); |
| 51 | } |
| 52 | binder::Status bindMount(const std::string& sourceDir, |
| 53 | const std::string& targetDir) const final { |
| 54 | return mInterface->bindMount(sourceDir, targetDir); |
| 55 | } |
| 56 | binder::Status setIncFsMountOptions( |
| 57 | const ::android::os::incremental::IncrementalFileSystemControlParcel& control, |
| 58 | bool enableReadLogs) const final { |
| 59 | return mInterface->setIncFsMountOptions(control, enableReadLogs); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | sp<os::IVold> mInterface; |
| 64 | }; |
| 65 | |
| 66 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
| 67 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 68 | RealDataLoaderManager(sp<content::pm::IDataLoaderManager> manager) |
| 69 | : mInterface(std::move(manager)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 70 | ~RealDataLoaderManager() = default; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 71 | binder::Status bindToDataLoader(MountId mountId, |
| 72 | const content::pm::DataLoaderParamsParcel& params, |
| 73 | const sp<content::pm::IDataLoaderStatusListener>& listener, |
| 74 | bool* _aidl_return) const final { |
| 75 | return mInterface->bindToDataLoader(mountId, params, listener, _aidl_return); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 76 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 77 | binder::Status getDataLoader(MountId mountId, |
| 78 | sp<content::pm::IDataLoader>* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 79 | return mInterface->getDataLoader(mountId, _aidl_return); |
| 80 | } |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 81 | binder::Status unbindFromDataLoader(MountId mountId) const final { |
| 82 | return mInterface->unbindFromDataLoader(mountId); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
| 86 | sp<content::pm::IDataLoaderManager> mInterface; |
| 87 | }; |
| 88 | |
| 89 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 90 | public: |
| 91 | ~RealAppOpsManager() = default; |
| 92 | binder::Status checkPermission(const char* permission, const char* operation, |
| 93 | const char* package) const final { |
| 94 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 95 | } |
| 96 | void startWatchingMode(int32_t op, const String16& packageName, |
| 97 | const sp<IAppOpsCallback>& callback) final { |
| 98 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 99 | } |
| 100 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 101 | mAppOpsManager.stopWatchingMode(callback); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | android::AppOpsManager mAppOpsManager; |
| 106 | }; |
| 107 | |
| 108 | class RealJniWrapper final : public JniWrapper { |
| 109 | public: |
| 110 | RealJniWrapper(JavaVM* jvm); |
| 111 | void initializeForCurrentThread() const final; |
| 112 | |
| 113 | static JavaVM* getJvm(JNIEnv* env); |
| 114 | |
| 115 | private: |
| 116 | JavaVM* const mJvm; |
| 117 | }; |
| 118 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 119 | class RealLooperWrapper final : public LooperWrapper { |
| 120 | public: |
| 121 | int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback, |
| 122 | void* data) final { |
| 123 | return mLooper.addFd(fd, ident, events, callback, data); |
| 124 | } |
| 125 | int removeFd(int fd) final { return mLooper.removeFd(fd); } |
| 126 | void wake() final { return mLooper.wake(); } |
| 127 | int pollAll(int timeoutMillis) final { return mLooper.pollAll(timeoutMillis); } |
| 128 | |
| 129 | private: |
| 130 | struct Looper : public android::Looper { |
| 131 | Looper() : android::Looper(/*allowNonCallbacks=*/false) {} |
| 132 | ~Looper() {} |
| 133 | } mLooper; |
| 134 | }; |
| 135 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 136 | class RealIncFs : public IncFsWrapper { |
| 137 | public: |
| 138 | RealIncFs() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 139 | ~RealIncFs() final = default; |
| 140 | void listExistingMounts(const ExistingMountCallback& cb) const final { |
| 141 | for (auto mount : incfs::defaultMountRegistry().copyMounts()) { |
| 142 | auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it. |
| 143 | cb(mount.root(), mount.backingDir(), binds); |
| 144 | } |
| 145 | } |
| 146 | Control openMount(std::string_view path) const final { return incfs::open(path); } |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame^] | 147 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs, |
| 148 | IncFsFd blocksWritten) const final { |
| 149 | return incfs::createControl(cmd, pendingReads, logs, blocksWritten); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 150 | } |
| 151 | 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] | 152 | incfs::NewFileParams params) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 153 | return incfs::makeFile(control, path, mode, id, params); |
| 154 | } |
| 155 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
| 156 | return incfs::makeDir(control, path, mode); |
| 157 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 158 | ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final { |
| 159 | return incfs::makeDirs(control, path, mode); |
| 160 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 161 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 162 | return incfs::getMetadata(control, fileid); |
| 163 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 164 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 165 | return incfs::getMetadata(control, path); |
| 166 | } |
| 167 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 168 | return incfs::getFileId(control, path); |
| 169 | } |
Songchun Fan | 6944f1e | 2020-11-06 15:24:24 -0800 | [diff] [blame] | 170 | std::string toString(FileId fileId) const final { return incfs::toString(fileId); } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 171 | std::pair<IncFsBlockIndex, IncFsBlockIndex> countFilledBlocks( |
| 172 | const Control& control, std::string_view path) const final { |
| 173 | const auto fileId = incfs::getFileId(control, path); |
| 174 | const auto fd = incfs::openForSpecialOps(control, fileId); |
| 175 | int res = fd.get(); |
| 176 | if (!fd.ok()) { |
| 177 | return {res, res}; |
| 178 | } |
| 179 | const auto ranges = incfs::getFilledRanges(res); |
| 180 | res = ranges.first; |
| 181 | if (res) { |
| 182 | return {res, res}; |
| 183 | } |
| 184 | const auto totalBlocksCount = ranges.second.internalRawRanges().endIndex; |
| 185 | int filledBlockCount = 0; |
| 186 | for (const auto& dataRange : ranges.second.dataRanges()) { |
| 187 | filledBlockCount += dataRange.size(); |
| 188 | } |
| 189 | for (const auto& hashRange : ranges.second.hashRanges()) { |
| 190 | filledBlockCount += hashRange.size(); |
| 191 | } |
| 192 | return {filledBlockCount, totalBlocksCount}; |
| 193 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 194 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 195 | return incfs::link(control, from, to); |
| 196 | } |
| 197 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 198 | return incfs::unlink(control, path); |
| 199 | } |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 200 | incfs::UniqueFd openForSpecialOps(const Control& control, FileId id) const final { |
| 201 | return incfs::openForSpecialOps(control, id); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 202 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 203 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 204 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 205 | } |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 206 | WaitResult waitForPendingReads(const Control& control, std::chrono::milliseconds timeout, |
| 207 | std::vector<incfs::ReadInfo>* pendingReadsBuffer) const final { |
| 208 | return incfs::waitForPendingReads(control, timeout, pendingReadsBuffer); |
| 209 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 210 | ErrorCode setUidReadTimeouts(const Control& control, |
| 211 | const std::vector<android::os::incremental::PerUidReadTimeouts>& |
| 212 | perUidReadTimeouts) const final { |
| 213 | return -ENOTSUP; |
| 214 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 217 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm); |
| 218 | |
| 219 | class RealTimedQueueWrapper : public TimedQueueWrapper { |
| 220 | public: |
| 221 | RealTimedQueueWrapper(JavaVM* jvm) { |
| 222 | mThread = std::thread([this, jvm]() { |
| 223 | (void)getOrAttachJniEnv(jvm); |
| 224 | runTimers(); |
| 225 | }); |
| 226 | } |
| 227 | ~RealTimedQueueWrapper() final { |
| 228 | CHECK(!mRunning) << "call stop first"; |
| 229 | CHECK(!mThread.joinable()) << "call stop first"; |
| 230 | } |
| 231 | |
| 232 | void addJob(MountId id, Milliseconds after, Job what) final { |
| 233 | const auto now = Clock::now(); |
| 234 | { |
| 235 | std::unique_lock lock(mMutex); |
| 236 | mJobs.insert(TimedJob{id, now + after, std::move(what)}); |
| 237 | } |
| 238 | mCondition.notify_all(); |
| 239 | } |
| 240 | void removeJobs(MountId id) final { |
| 241 | std::unique_lock lock(mMutex); |
| 242 | std::erase_if(mJobs, [id](auto&& item) { return item.id == id; }); |
| 243 | } |
| 244 | void stop() final { |
| 245 | { |
| 246 | std::unique_lock lock(mMutex); |
| 247 | mRunning = false; |
| 248 | } |
| 249 | mCondition.notify_all(); |
| 250 | mThread.join(); |
| 251 | mJobs.clear(); |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | void runTimers() { |
| 256 | static constexpr TimePoint kInfinityTs{Clock::duration::max()}; |
| 257 | TimePoint nextJobTs = kInfinityTs; |
| 258 | std::unique_lock lock(mMutex); |
| 259 | for (;;) { |
| 260 | mCondition.wait_until(lock, nextJobTs, [this, nextJobTs]() { |
| 261 | const auto now = Clock::now(); |
| 262 | const auto firstJobTs = !mJobs.empty() ? mJobs.begin()->when : kInfinityTs; |
| 263 | return !mRunning || firstJobTs <= now || firstJobTs < nextJobTs; |
| 264 | }); |
| 265 | if (!mRunning) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | const auto now = Clock::now(); |
| 270 | auto it = mJobs.begin(); |
| 271 | // Always acquire begin(). We can't use it after unlock as mTimedJobs can change. |
| 272 | for (; it != mJobs.end() && it->when <= now; it = mJobs.begin()) { |
| 273 | auto job = std::move(it->what); |
| 274 | mJobs.erase(it); |
| 275 | |
| 276 | lock.unlock(); |
| 277 | job(); |
| 278 | lock.lock(); |
| 279 | } |
| 280 | nextJobTs = it != mJobs.end() ? it->when : kInfinityTs; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | struct TimedJob { |
| 285 | MountId id; |
| 286 | TimePoint when; |
| 287 | Job what; |
| 288 | friend bool operator<(const TimedJob& lhs, const TimedJob& rhs) { |
| 289 | return lhs.when < rhs.when; |
| 290 | } |
| 291 | }; |
| 292 | bool mRunning = true; |
| 293 | std::set<TimedJob> mJobs; |
| 294 | std::condition_variable mCondition; |
| 295 | std::mutex mMutex; |
| 296 | std::thread mThread; |
| 297 | }; |
| 298 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 299 | class RealFsWrapper : public FsWrapper { |
| 300 | public: |
| 301 | RealFsWrapper() = default; |
| 302 | ~RealFsWrapper() = default; |
| 303 | |
| 304 | std::vector<std::string> listFilesRecursive(std::string_view directoryPath) const final { |
| 305 | std::vector<std::string> files; |
| 306 | for (const auto& entry : std::filesystem::recursive_directory_iterator(directoryPath)) { |
| 307 | if (!entry.is_regular_file()) { |
| 308 | continue; |
| 309 | } |
| 310 | files.push_back(entry.path().c_str()); |
| 311 | } |
| 312 | return files; |
| 313 | } |
| 314 | }; |
| 315 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 316 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 317 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 318 | |
| 319 | template <class INTERFACE> |
| 320 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 321 | sp<IBinder> binder = |
| 322 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 323 | if (!binder) { |
| 324 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 325 | } |
| 326 | return interface_cast<INTERFACE>(binder); |
| 327 | } |
| 328 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 329 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 330 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 331 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 332 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 333 | } |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 337 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 338 | sp<content::pm::IDataLoaderManager> manager = |
| 339 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 340 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 341 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 342 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 343 | } |
| 344 | return nullptr; |
| 345 | } |
| 346 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 347 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 348 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 351 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 352 | return std::make_unique<RealAppOpsManager>(); |
| 353 | } |
| 354 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 355 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 356 | return std::make_unique<RealJniWrapper>(mJvm); |
| 357 | } |
| 358 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 359 | std::unique_ptr<LooperWrapper> RealServiceManager::getLooper() { |
| 360 | return std::make_unique<RealLooperWrapper>(); |
| 361 | } |
| 362 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 363 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getTimedQueue() { |
| 364 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 365 | } |
| 366 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 367 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getProgressUpdateJobQueue() { |
| 368 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 369 | } |
| 370 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 371 | std::unique_ptr<FsWrapper> RealServiceManager::getFs() { |
| 372 | return std::make_unique<RealFsWrapper>(); |
| 373 | } |
| 374 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 375 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 376 | CHECK(env); |
| 377 | JavaVM* jvm = nullptr; |
| 378 | env->GetJavaVM(&jvm); |
| 379 | CHECK(jvm); |
| 380 | return jvm; |
| 381 | } |
| 382 | |
| 383 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 384 | JNIEnv* env; |
| 385 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 386 | return nullptr; |
| 387 | } |
| 388 | return env; |
| 389 | } |
| 390 | |
| 391 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 392 | if (!jvm) { |
| 393 | LOG(ERROR) << "No JVM instance"; |
| 394 | return nullptr; |
| 395 | } |
| 396 | |
| 397 | JNIEnv* env = getJniEnv(jvm); |
| 398 | if (!env) { |
| 399 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 400 | if (result != JNI_OK) { |
| 401 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 402 | return nullptr; |
| 403 | } |
| 404 | struct VmDetacher { |
| 405 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 406 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 407 | |
| 408 | private: |
| 409 | JavaVM* const mVm; |
| 410 | }; |
| 411 | static thread_local VmDetacher detacher(jvm); |
| 412 | } |
| 413 | |
| 414 | return env; |
| 415 | } |
| 416 | |
| 417 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 418 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 419 | } |
| 420 | |
| 421 | void RealJniWrapper::initializeForCurrentThread() const { |
| 422 | (void)getOrAttachJniEnv(mJvm); |
| 423 | } |
| 424 | |
| 425 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 426 | return getJavaVm(env); |
| 427 | } |
| 428 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 429 | } // namespace android::incremental |