17ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski/*
27ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * Copyright (C) 2016 The Android Open Source Project
37ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski *
47ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
57ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * you may not use this file except in compliance with the License.
67ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * You may obtain a copy of the License at
77ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski *
87ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
97ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski *
107ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * Unless required by applicable law or agreed to in writing, software
117ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
127ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * See the License for the specific language governing permissions and
147ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski * limitations under the License.
157ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski */
167ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
177ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#ifndef APKASSETS_H_
187ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#define APKASSETS_H_
197ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
207ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include <memory>
217ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include <string>
227ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
237ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include "android-base/macros.h"
247ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include "ziparchive/zip_archive.h"
257ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
267ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include "androidfw/Asset.h"
277ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#include "androidfw/LoadedArsc.h"
28d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski#include "androidfw/misc.h"
297ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
307ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinskinamespace android {
317ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
327ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski// Holds an APK.
337ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinskiclass ApkAssets {
347ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski public:
350c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski  static std::unique_ptr<const ApkAssets> Load(const std::string& path, bool system = false);
360c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski  static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path,
370c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski                                                              bool system = false);
387ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
397ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  std::unique_ptr<Asset> Open(const std::string& path,
407ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski                              Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
417ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
42d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski  bool ForEachFile(const std::string& path,
43d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski                   const std::function<void(const StringPiece&, FileType)>& f) const;
44d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski
457ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  inline const std::string& GetPath() const { return path_; }
467ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
477ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  inline const LoadedArsc* GetLoadedArsc() const { return loaded_arsc_.get(); }
487ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
497ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski private:
507ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  DISALLOW_COPY_AND_ASSIGN(ApkAssets);
517ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
520c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski  static std::unique_ptr<const ApkAssets> LoadImpl(const std::string& path, bool system,
530c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski                                                   bool load_as_shared_library);
54da431a22da38f9c4085b5d71ed9a9c6122c6a5a6Adam Lesinski
557ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  ApkAssets() = default;
567ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
577ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  struct ZipArchivePtrCloser {
587ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski    void operator()(::ZipArchiveHandle handle) { ::CloseArchive(handle); }
597ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  };
607ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
617ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  using ZipArchivePtr =
627ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski      std::unique_ptr<typename std::remove_pointer<::ZipArchiveHandle>::type, ZipArchivePtrCloser>;
63d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski
647ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  ZipArchivePtr zip_handle_;
657ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  std::string path_;
667ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski  std::unique_ptr<Asset> resources_asset_;
670c40524953f3d36a880f91183302a2ea5c722930Adam Lesinski  std::unique_ptr<const LoadedArsc> loaded_arsc_;
687ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski};
697ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
707ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski}  // namespace android
717ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski
727ad1110ecd6a840fcd2895c62668828a1ca029c6Adam Lesinski#endif /* APKASSETS_H_ */
73