ApkAssets.h revision da431a22da38f9c4085b5d71ed9a9c6122c6a5a6
1/*
2 * Copyright (C) 2016 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 APKASSETS_H_
18#define APKASSETS_H_
19
20#include <memory>
21#include <string>
22
23#include "android-base/macros.h"
24#include "ziparchive/zip_archive.h"
25
26#include "androidfw/Asset.h"
27#include "androidfw/LoadedArsc.h"
28
29namespace android {
30
31// Holds an APK.
32class ApkAssets {
33 public:
34  static std::unique_ptr<ApkAssets> Load(const std::string& path);
35  static std::unique_ptr<ApkAssets> LoadAsSharedLibrary(const std::string& path);
36
37  std::unique_ptr<Asset> Open(const std::string& path,
38                              Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
39
40  inline const std::string& GetPath() const { return path_; }
41
42  inline const LoadedArsc* GetLoadedArsc() const { return loaded_arsc_.get(); }
43
44 private:
45  DISALLOW_COPY_AND_ASSIGN(ApkAssets);
46
47  static std::unique_ptr<ApkAssets> LoadImpl(const std::string& path, bool load_as_shared_library);
48
49  ApkAssets() = default;
50
51  struct ZipArchivePtrCloser {
52    void operator()(::ZipArchiveHandle handle) { ::CloseArchive(handle); }
53  };
54
55  using ZipArchivePtr =
56      std::unique_ptr<typename std::remove_pointer<::ZipArchiveHandle>::type, ZipArchivePtrCloser>;
57  ZipArchivePtr zip_handle_;
58  std::string path_;
59  std::unique_ptr<Asset> resources_asset_;
60  std::unique_ptr<LoadedArsc> loaded_arsc_;
61};
62
63}  // namespace android
64
65#endif /* APKASSETS_H_ */
66