ApkAssets.h revision d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1d
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#include "androidfw/misc.h"
29
30namespace android {
31
32// Holds an APK.
33class ApkAssets {
34 public:
35  static std::unique_ptr<const ApkAssets> Load(const std::string& path, bool system = false);
36  static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path,
37                                                              bool system = false);
38
39  std::unique_ptr<Asset> Open(const std::string& path,
40                              Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
41
42  bool ForEachFile(const std::string& path,
43                   const std::function<void(const StringPiece&, FileType)>& f) const;
44
45  inline const std::string& GetPath() const { return path_; }
46
47  inline const LoadedArsc* GetLoadedArsc() const { return loaded_arsc_.get(); }
48
49 private:
50  DISALLOW_COPY_AND_ASSIGN(ApkAssets);
51
52  static std::unique_ptr<const ApkAssets> LoadImpl(const std::string& path, bool system,
53                                                   bool load_as_shared_library);
54
55  ApkAssets() = default;
56
57  struct ZipArchivePtrCloser {
58    void operator()(::ZipArchiveHandle handle) { ::CloseArchive(handle); }
59  };
60
61  using ZipArchivePtr =
62      std::unique_ptr<typename std::remove_pointer<::ZipArchiveHandle>::type, ZipArchivePtrCloser>;
63
64  ZipArchivePtr zip_handle_;
65  std::string path_;
66  std::unique_ptr<Asset> resources_asset_;
67  std::unique_ptr<const LoadedArsc> loaded_arsc_;
68};
69
70}  // namespace android
71
72#endif /* APKASSETS_H_ */
73