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 #pragma once
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "dataloader_ndk.h"
24 #include "incfs.h"
25 
26 namespace android::dataloader {
27 
28 using DataLoaderStatus = ::DataLoaderStatus;
29 template <class T>
30 using Span = android::incfs::Span<T>;
31 
32 struct DataLoader;
33 struct DataLoaderParams;
34 struct DataLoaderInstallationFile;
35 struct FilesystemConnector;
36 struct StatusListener;
37 
38 using FileId = IncFsFileId;
39 using ReadInfo = IncFsReadInfo;
40 using DataBlock = IncFsDataBlock;
41 
42 using FilesystemConnectorPtr = FilesystemConnector*;
43 using StatusListenerPtr = StatusListener*;
44 using ServiceConnectorPtr = DataLoaderServiceConnectorPtr;
45 using ServiceParamsPtr = DataLoaderServiceParamsPtr;
46 
47 using DataLoaderPtr = std::unique_ptr<DataLoader>;
48 using DataLoaderInstallationFiles = Span<const ::DataLoaderInstallationFile>;
49 using PendingReads = Span<const ReadInfo>;
50 using PageReads = Span<const ReadInfo>;
51 using RawMetadata = std::vector<char>;
52 using DataBlocks = Span<const DataBlock>;
53 
54 constexpr int kBlockSize = INCFS_DATA_FILE_BLOCK_SIZE;
55 
56 struct DataLoader {
57     using Factory = std::function<DataLoaderPtr(DataLoaderServiceVmPtr, const DataLoaderParams&)>;
58     static void initialize(Factory&& factory);
59 
~DataLoaderDataLoader60     virtual ~DataLoader() {}
61 
62     // Lifecycle.
63     virtual bool onCreate(const DataLoaderParams&, FilesystemConnectorPtr, StatusListenerPtr,
64                           ServiceConnectorPtr, ServiceParamsPtr) = 0;
65     virtual bool onStart() = 0;
66     virtual void onStop() = 0;
67     virtual void onDestroy() = 0;
68 
69     // FS callbacks.
70     virtual bool onPrepareImage(DataLoaderInstallationFiles addedFiles) = 0;
71 
72     // IFS callbacks.
73     virtual void onPendingReads(PendingReads pendingReads) = 0;
74     virtual void onPageReads(PageReads pageReads) = 0;
75 };
76 
77 struct DataLoaderParams {
typeDataLoaderParams78     DataLoaderType type() const { return mType; }
packageNameDataLoaderParams79     const std::string& packageName() const { return mPackageName; }
classNameDataLoaderParams80     const std::string& className() const { return mClassName; }
argumentsDataLoaderParams81     const std::string& arguments() const { return mArguments; }
82 
83     DataLoaderParams(DataLoaderType type, std::string&& packageName, std::string&& className,
84                      std::string&& arguments);
85 
86 private:
87     DataLoaderType const mType;
88     std::string const mPackageName;
89     std::string const mClassName;
90     std::string const mArguments;
91 };
92 
93 struct DataLoaderInstallationFile {
locationDataLoaderInstallationFile94     DataLoaderLocation location() const { return mLocation; }
nameDataLoaderInstallationFile95     const std::string& name() const { return mName; }
sizeDataLoaderInstallationFile96     IncFsSize size() const { return mSize; }
metadataDataLoaderInstallationFile97     const RawMetadata& metadata() const { return mMetadata; }
98 
99     DataLoaderInstallationFile(DataLoaderLocation location, std::string&& name, IncFsSize size,
100                                RawMetadata&& metadata);
101 
102 private:
103     DataLoaderLocation const mLocation;
104     std::string const mName;
105     IncFsSize const mSize;
106     RawMetadata const mMetadata;
107 };
108 
109 struct FilesystemConnector : public DataLoaderFilesystemConnector {
110     android::incfs::UniqueFd openForSpecialOps(FileId fid);
111     int writeBlocks(DataBlocks blocks);
112     RawMetadata getRawMetadata(FileId fid);
113     bool setParams(DataLoaderFilesystemParams);
114 };
115 
116 struct StatusListener : public DataLoaderStatusListener {
117     bool reportStatus(DataLoaderStatus status);
118 };
119 
120 } // namespace android::dataloader
121 
122 #include "dataloader_inline.h"
123