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 #ifndef ANDROID_INCREMENTAL_FILE_SYSTEM_DATA_LOADER_NDK_H
18 #define ANDROID_INCREMENTAL_FILE_SYSTEM_DATA_LOADER_NDK_H
19 
20 #include <incfs_ndk.h>
21 #include <jni.h>
22 
23 __BEGIN_DECLS
24 
25 #define DATALOADER_LIBRARY_NAME "libdataloader.so"
26 
27 // Keep in sync with IDataLoaderStatusListener.aidl
28 typedef enum {
29     DATA_LOADER_UNRECOVERABLE = 8,
30 
31     DATA_LOADER_FIRST_STATUS = DATA_LOADER_UNRECOVERABLE,
32     DATA_LOADER_LAST_STATUS = DATA_LOADER_UNRECOVERABLE,
33 } DataLoaderStatus;
34 
35 typedef enum {
36     DATA_LOADER_TYPE_NONE = 0,
37     DATA_LOADER_TYPE_STREAMING = 1,
38     DATA_LOADER_TYPE_INCREMENTAL = 2,
39 } DataLoaderType;
40 
41 typedef enum {
42     DATA_LOADER_LOCATION_DATA_APP = 0,
43     DATA_LOADER_LOCATION_MEDIA_OBB = 1,
44     DATA_LOADER_LOCATION_MEDIA_DATA = 2,
45 } DataLoaderLocation;
46 
47 struct DataLoaderParams {
48     int type;
49     const char* packageName;
50     const char* className;
51     const char* arguments;
52 };
53 
54 typedef struct {
55     int location;
56     const char* name;
57     IncFsSize size;
58     IncFsSpan metadata;
59 } DataLoaderInstallationFile;
60 
61 typedef struct {
62     bool readLogsEnabled;
63 } DataLoaderFilesystemParams;
64 
65 #ifdef __cplusplus
66 
67 typedef class DataLoaderFilesystemConnector {
68 } * DataLoaderFilesystemConnectorPtr;
69 typedef class DataLoaderStatusListener {
70 } * DataLoaderStatusListenerPtr;
71 
72 #else /* not __cplusplus */
73 
74 typedef void* DataLoaderFilesystemConnectorPtr;
75 typedef void* DataLoaderStatusListenerPtr;
76 
77 #endif /* not __cplusplus */
78 
79 typedef JavaVM* DataLoaderServiceVmPtr;
80 typedef jobject DataLoaderServiceConnectorPtr;
81 typedef jobject DataLoaderServiceParamsPtr;
82 
83 struct DataLoader {
84     bool (*onStart)(struct DataLoader* self);
85     void (*onStop)(struct DataLoader* self);
86     void (*onDestroy)(struct DataLoader* self);
87 
88     bool (*onPrepareImage)(struct DataLoader* self, const DataLoaderInstallationFile addedFiles[],
89                            int addedFilesCount);
90 
91     void (*onPendingReads)(struct DataLoader* self, const IncFsReadInfo pendingReads[],
92                            int pendingReadsCount);
93     void (*onPageReads)(struct DataLoader* self, const IncFsReadInfo pageReads[],
94                         int pageReadsCount);
95 };
96 
97 struct DataLoaderFactory {
98     struct DataLoader* (*onCreate)(struct DataLoaderFactory* self, const struct DataLoaderParams*,
99                                    DataLoaderFilesystemConnectorPtr, DataLoaderStatusListenerPtr,
100                                    DataLoaderServiceVmPtr, DataLoaderServiceConnectorPtr,
101                                    DataLoaderServiceParamsPtr);
102 };
103 void DataLoader_Initialize(struct DataLoaderFactory*);
104 
105 void DataLoader_FilesystemConnector_writeData(DataLoaderFilesystemConnectorPtr, jstring name,
106                                               jlong offsetBytes, jlong lengthBytes,
107                                               jobject incomingFd);
108 
109 // Returns a newly opened file descriptor and gives the ownership to the caller.
110 int DataLoader_FilesystemConnector_openForSpecialOps(DataLoaderFilesystemConnectorPtr,
111                                                      IncFsFileId fid);
112 
113 int DataLoader_FilesystemConnector_writeBlocks(DataLoaderFilesystemConnectorPtr,
114                                                const IncFsDataBlock blocks[], int blocksCount);
115 // INCFS_MAX_FILE_ATTR_SIZE
116 int DataLoader_FilesystemConnector_getRawMetadata(DataLoaderFilesystemConnectorPtr, IncFsFileId fid,
117                                                   char buffer[], size_t* bufferSize);
118 
119 bool DataLoader_FilesystemConnector_setParams(DataLoaderFilesystemConnectorPtr,
120                                               DataLoaderFilesystemParams params);
121 
122 int DataLoader_StatusListener_reportStatus(DataLoaderStatusListenerPtr listener,
123                                            DataLoaderStatus status);
124 
125 // DataLoaderService JNI
126 bool DataLoaderService_OnCreate(JNIEnv* env, jobject service, jint storageId, jobject control,
127                                 jobject params, jobject listener);
128 bool DataLoaderService_OnStart(JNIEnv* env, jint storageId);
129 bool DataLoaderService_OnStop(JNIEnv* env, jint storageId);
130 bool DataLoaderService_OnDestroy(JNIEnv* env, jint storageId);
131 
132 bool DataLoaderService_OnPrepareImage(JNIEnv* env, jint storageId, jobjectArray addedFiles,
133                                       jobjectArray removedFiles);
134 
135 __END_DECLS
136 
137 #endif // ANDROID_INCREMENTAL_FILE_SYSTEM_DATA_LOADER_NDK_H
138