• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Uid = IncFsUid;
40 using ReadInfo = IncFsReadInfo;
41 using ReadInfoWithUid = IncFsReadInfoWithUid;
42 using DataBlock = IncFsDataBlock;
43 
44 using FilesystemConnectorPtr = FilesystemConnector*;
45 using StatusListenerPtr = StatusListener*;
46 using ServiceConnectorPtr = DataLoaderServiceConnectorPtr;
47 using ServiceParamsPtr = DataLoaderServiceParamsPtr;
48 
49 using DataLoaderPtr = std::unique_ptr<DataLoader>;
50 using DataLoaderInstallationFiles = Span<const ::DataLoaderInstallationFile>;
51 using PendingReads = Span<const ReadInfo>;
52 using PendingReadsWithUid = Span<const ReadInfoWithUid>;
53 using PageReads = Span<const ReadInfo>;
54 using PageReadsWithUid = Span<const ReadInfoWithUid>;
55 using RawMetadata = std::vector<char>;
56 using DataBlocks = Span<const DataBlock>;
57 
58 constexpr int kBlockSize = INCFS_DATA_FILE_BLOCK_SIZE;
59 
60 struct DataLoader {
61     using Factory = std::function<DataLoaderPtr(DataLoaderServiceVmPtr, const DataLoaderParams&)>;
62     static void initialize(Factory&& factory);
63 
~DataLoaderDataLoader64     virtual ~DataLoader() {}
65 
66     // Bitmask of supported features.
67     virtual DataLoaderFeatures getFeatures() const = 0;
68 
69     // Lifecycle.
70     virtual bool onCreate(const DataLoaderParams&, FilesystemConnectorPtr, StatusListenerPtr,
71                           ServiceConnectorPtr, ServiceParamsPtr) = 0;
72     virtual bool onStart() = 0;
73     virtual void onStop() = 0;
74     virtual void onDestroy() = 0;
75 
76     // FS callbacks.
77     virtual bool onPrepareImage(DataLoaderInstallationFiles addedFiles) = 0;
78 
79     // IFS callbacks.
80     virtual void onPendingReads(PendingReads pendingReads) = 0;
81     virtual void onPageReads(PageReads pageReads) = 0;
82 
83     virtual void onPendingReadsWithUid(PageReadsWithUid pendingReads) = 0;
84     virtual void onPageReadsWithUid(PendingReadsWithUid pageReads) = 0;
85 };
86 
87 struct DataLoaderParams {
typeDataLoaderParams88     DataLoaderType type() const { return mType; }
packageNameDataLoaderParams89     const std::string& packageName() const { return mPackageName; }
classNameDataLoaderParams90     const std::string& className() const { return mClassName; }
argumentsDataLoaderParams91     const std::string& arguments() const { return mArguments; }
92 
93     DataLoaderParams(DataLoaderType type, std::string&& packageName, std::string&& className,
94                      std::string&& arguments);
95 
96 private:
97     DataLoaderType const mType;
98     std::string const mPackageName;
99     std::string const mClassName;
100     std::string const mArguments;
101 };
102 
103 struct DataLoaderInstallationFile {
locationDataLoaderInstallationFile104     DataLoaderLocation location() const { return mLocation; }
nameDataLoaderInstallationFile105     const std::string& name() const { return mName; }
sizeDataLoaderInstallationFile106     IncFsSize size() const { return mSize; }
metadataDataLoaderInstallationFile107     const RawMetadata& metadata() const { return mMetadata; }
108 
109     DataLoaderInstallationFile(DataLoaderLocation location, std::string&& name, IncFsSize size,
110                                RawMetadata&& metadata);
111 
112 private:
113     DataLoaderLocation const mLocation;
114     std::string const mName;
115     IncFsSize const mSize;
116     RawMetadata const mMetadata;
117 };
118 
119 struct FilesystemConnector : public DataLoaderFilesystemConnector {
120     android::incfs::UniqueFd openForSpecialOps(FileId fid);
121     int writeBlocks(DataBlocks blocks);
122     RawMetadata getRawMetadata(FileId fid);
123     bool setParams(DataLoaderFilesystemParams);
124 };
125 
126 struct StatusListener : public DataLoaderStatusListener {
127     bool reportStatus(DataLoaderStatus status);
128 };
129 
130 } // namespace android::dataloader
131 
132 #include "dataloader_inline.h"
133