ApkAssets.h revision 0c40524953f3d36a880f91183302a2ea5c722930
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<const ApkAssets> Load(const std::string& path, bool system = false);
35  static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path,
36                                                              bool system = false);
37
38  std::unique_ptr<Asset> Open(const std::string& path,
39                              Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
40
41  inline const std::string& GetPath() const { return path_; }
42
43  inline const LoadedArsc* GetLoadedArsc() const { return loaded_arsc_.get(); }
44
45 private:
46  DISALLOW_COPY_AND_ASSIGN(ApkAssets);
47
48  static std::unique_ptr<const ApkAssets> LoadImpl(const std::string& path, bool system,
49                                                   bool load_as_shared_library);
50
51  ApkAssets() = default;
52
53  struct ZipArchivePtrCloser {
54    void operator()(::ZipArchiveHandle handle) { ::CloseArchive(handle); }
55  };
56
57  using ZipArchivePtr =
58      std::unique_ptr<typename std::remove_pointer<::ZipArchiveHandle>::type, ZipArchivePtrCloser>;
59  ZipArchivePtr zip_handle_;
60  std::string path_;
61  std::unique_ptr<Asset> resources_asset_;
62  std::unique_ptr<const LoadedArsc> loaded_arsc_;
63};
64
65}  // namespace android
66
67#endif /* APKASSETS_H_ */
68