File: dataloader_inline.h

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (156 lines) | stat: -rw-r--r-- 6,985 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include "dataloader.h"

namespace android::dataloader {
namespace details {

struct DataLoaderImpl : public ::DataLoader {
    DataLoaderImpl(DataLoaderPtr&& dataLoader) : mDataLoader(std::move(dataLoader)) {
        getFeatures = [](DataLoader* self) -> DataLoaderFeatures {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->getFeatures();
        };
        onStart = [](DataLoader* self) -> bool {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onStart();
        };
        onStop = [](DataLoader* self) {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onStop();
        };
        onDestroy = [](DataLoader* self) {
            auto me = static_cast<DataLoaderImpl*>(self);
            me->mDataLoader->onDestroy();
            delete me;
        };
        onPrepareImage = [](DataLoader* self, const ::DataLoaderInstallationFile addedFiles[],
                            int addedFilesCount) -> bool {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onPrepareImage(
                    DataLoaderInstallationFiles(addedFiles, addedFilesCount));
        };
        onPendingReads = [](DataLoader* self, const IncFsReadInfo pendingReads[],
                            int pendingReadsCount) {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onPendingReads(
                    PendingReads(pendingReads, pendingReadsCount));
        };
        onPageReads = [](DataLoader* self, const IncFsReadInfo pageReads[], int pageReadsCount) {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onPageReads(
                    PageReads(pageReads, pageReadsCount));
        };
        onPendingReadsWithUid = [](DataLoader* self, const IncFsReadInfoWithUid pendingReads[],
                                   int pendingReadsCount) {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onPendingReadsWithUid(
                    PendingReadsWithUid(pendingReads, pendingReadsCount));
        };
        onPageReadsWithUid = [](DataLoader* self, const IncFsReadInfoWithUid pageReads[],
                                int pageReadsCount) {
            return static_cast<DataLoaderImpl*>(self)->mDataLoader->onPageReadsWithUid(
                    PageReadsWithUid(pageReads, pageReadsCount));
        };
    }

private:
    DataLoaderPtr mDataLoader;
};

inline DataLoaderParams createParams(const ::DataLoaderParams* params) {
    const DataLoaderType type((DataLoaderType)params->type);
    std::string packageName(params->packageName);
    std::string className(params->className);
    std::string arguments(params->arguments);
    return DataLoaderParams(type, std::move(packageName), std::move(className),
                            std::move(arguments));
}

inline DataLoaderInstallationFile createInstallationFile(const ::DataLoaderInstallationFile* file) {
    const DataLoaderLocation location((DataLoaderLocation)file->location);
    std::string name(file->name);
    IncFsSize size(file->size);
    RawMetadata metadata(file->metadata.data, file->metadata.data + file->metadata.size);
    return DataLoaderInstallationFile(location, std::move(name), size, std::move(metadata));
}

struct DataLoaderFactoryImpl : public ::DataLoaderFactory {
    DataLoaderFactoryImpl(DataLoader::Factory&& factory) : mFactory(factory) {
        onCreate = [](::DataLoaderFactory* self, const ::DataLoaderParams* ndkParams,
                      ::DataLoaderFilesystemConnectorPtr fsConnector,
                      ::DataLoaderStatusListenerPtr statusListener, ::DataLoaderServiceVmPtr vm,
                      ::DataLoaderServiceConnectorPtr serviceConnector,
                      ::DataLoaderServiceParamsPtr serviceParams) {
            auto me = static_cast<DataLoaderFactoryImpl*>(self);
            ::DataLoader* result = nullptr;
            auto params = createParams(ndkParams);
            auto dataLoader = me->mFactory(vm, params);
            if (!dataLoader ||
                !dataLoader->onCreate(params, static_cast<FilesystemConnector*>(fsConnector),
                                      static_cast<StatusListener*>(statusListener),
                                      serviceConnector, serviceParams)) {
                return result;
            }
            result = new DataLoaderImpl(std::move(dataLoader));
            return result;
        };
    }

private:
    DataLoader::Factory mFactory;
};

} // namespace details

inline void DataLoader::initialize(DataLoader::Factory&& factory) {
    DataLoader_Initialize_WithFeatures(new details::DataLoaderFactoryImpl(std::move(factory)));
}

inline DataLoaderParams::DataLoaderParams(DataLoaderType type, std::string&& packageName,
                                          std::string&& className, std::string&& arguments)
      : mType(type),
        mPackageName(std::move(packageName)),
        mClassName(std::move(className)),
        mArguments(std::move(arguments)) {}

inline DataLoaderInstallationFile::DataLoaderInstallationFile(DataLoaderLocation location,
                                                              std::string&& name, IncFsSize size,
                                                              RawMetadata&& metadata)
      : mLocation(location), mName(std::move(name)), mSize(size), mMetadata(std::move(metadata)) {}

inline android::incfs::UniqueFd FilesystemConnector::openForSpecialOps(FileId fid) {
    return android::incfs::UniqueFd(DataLoader_FilesystemConnector_openForSpecialOps(this, fid));
}

inline int FilesystemConnector::writeBlocks(DataBlocks blocks) {
    return DataLoader_FilesystemConnector_writeBlocks(this, blocks.data(), blocks.size());
}

inline RawMetadata FilesystemConnector::getRawMetadata(FileId fid) {
    RawMetadata metadata(INCFS_MAX_FILE_ATTR_SIZE);
    size_t size = metadata.size();
    if (DataLoader_FilesystemConnector_getRawMetadata(this, fid, metadata.data(), &size) < 0) {
        return {};
    }
    metadata.resize(size);
    return metadata;
}

inline bool FilesystemConnector::setParams(DataLoaderFilesystemParams params) {
    return DataLoader_FilesystemConnector_setParams(this, params);
}

inline bool StatusListener::reportStatus(DataLoaderStatus status) {
    return DataLoader_StatusListener_reportStatus(this, status);
}

} // namespace android::dataloader