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