Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37f24da8 authored by Ryan Mitchell's avatar Ryan Mitchell Committed by Automerger Merge Worker
Browse files

Merge changes If7ea17d5,If6364fd6 into rvc-dev am: 8275fb7d am: a6f2c4f6

Change-Id: Ifc7a8f395fd6716bbd91ad8e119da9ec8ee3fa61
parents fc8b3565 a6f2c4f6
Loading
Loading
Loading
Loading
+0 −4
Original line number Original line Diff line number Diff line
@@ -214,12 +214,8 @@ static jint CopyValue(JNIEnv* env, ApkAssetsCookie cookie, const Res_value& valu


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


static std::unique_ptr<DynamicLibManager> sDynamicLibManager =
    std::make_unique<DynamicLibManager>();

// Let the opaque type AAssetManager refer to a guarded AssetManager2 instance.
// Let the opaque type AAssetManager refer to a guarded AssetManager2 instance.
struct GuardedAssetManager : public ::AAssetManager {
struct GuardedAssetManager : public ::AAssetManager {
  GuardedAssetManager() : guarded_assetmanager(sDynamicLibManager.get()) {}
  Guarded<AssetManager2> guarded_assetmanager;
  Guarded<AssetManager2> guarded_assetmanager;
};
};


+0 −1
Original line number Original line Diff line number Diff line
@@ -44,7 +44,6 @@ cc_library {
        "AttributeResolution.cpp",
        "AttributeResolution.cpp",
        "ChunkIterator.cpp",
        "ChunkIterator.cpp",
        "ConfigDescription.cpp",
        "ConfigDescription.cpp",
        "DynamicLibManager.cpp",
        "Idmap.cpp",
        "Idmap.cpp",
        "LoadedArsc.cpp",
        "LoadedArsc.cpp",
        "Locale.cpp",
        "Locale.cpp",
+25 −48
Original line number Original line Diff line number Diff line
@@ -25,7 +25,6 @@


#include "android-base/logging.h"
#include "android-base/logging.h"
#include "android-base/stringprintf.h"
#include "android-base/stringprintf.h"
#include "androidfw/DynamicLibManager.h"
#include "androidfw/ResourceUtils.h"
#include "androidfw/ResourceUtils.h"
#include "androidfw/Util.h"
#include "androidfw/Util.h"
#include "utils/ByteOrder.h"
#include "utils/ByteOrder.h"
@@ -67,12 +66,7 @@ struct FindEntryResult {
  StringPoolRef entry_string_ref;
  StringPoolRef entry_string_ref;
};
};


AssetManager2::AssetManager2() : dynamic_lib_manager_(std::make_unique<DynamicLibManager>()) {
AssetManager2::AssetManager2() {
  memset(&configuration_, 0, sizeof(configuration_));
}

AssetManager2::AssetManager2(DynamicLibManager* dynamic_lib_manager)
    : dynamic_lib_manager_(dynamic_lib_manager) {
  memset(&configuration_, 0, sizeof(configuration_));
  memset(&configuration_, 0, sizeof(configuration_));
}
}


@@ -91,6 +85,9 @@ void AssetManager2::BuildDynamicRefTable() {
  package_groups_.clear();
  package_groups_.clear();
  package_ids_.fill(0xff);
  package_ids_.fill(0xff);


  // A mapping from apk assets path to the runtime package id of its first loaded package.
  std::unordered_map<std::string, uint8_t> apk_assets_package_ids;

  // Overlay resources are not directly referenced by an application so their resource ids
  // Overlay resources are not directly referenced by an application so their resource ids
  // can change throughout the application's lifetime. Assign overlay package ids last.
  // can change throughout the application's lifetime. Assign overlay package ids last.
  std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
  std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
@@ -98,38 +95,26 @@ void AssetManager2::BuildDynamicRefTable() {
    return !a->IsOverlay();
    return !a->IsOverlay();
  });
  });


  std::unordered_map<std::string, uint8_t> apk_assets_package_ids;
  // The assets cookie must map to the position of the apk assets in the unsorted apk assets list.
  std::unordered_map<std::string, uint8_t> package_name_package_ids;
  std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies;

  apk_assets_cookies.reserve(apk_assets_.size());
  // Assign stable package ids to application packages.
  for (size_t i = 0, n = apk_assets_.size(); i < n; i++) {
  uint8_t next_available_package_id = 0U;
    apk_assets_cookies[apk_assets_[i]] = static_cast<ApkAssetsCookie>(i);
  for (const auto& apk_assets : sorted_apk_assets) {
    for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
      uint8_t package_id = package->GetPackageId();
      if (package->IsOverlay()) {
        package_id = GetDynamicLibManager()->FindUnassignedId(next_available_package_id);
        next_available_package_id = package_id + 1;
      } else if (package->IsDynamic()) {
        package_id = GetDynamicLibManager()->GetAssignedId(package->GetPackageName());
  }
  }


      // Map the path of the apk assets to the package id of its first loaded package.
  // 0x01 is reserved for the android package.
      apk_assets_package_ids[apk_assets->GetPath()] = package_id;
  int next_package_id = 0x02;

  for (const ApkAssets* apk_assets : sorted_apk_assets) {
      // Map the package name of the package to the first loaded package with that package id.
    const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
      package_name_package_ids[package->GetPackageName()] = package_id;
    for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
    }
      // Get the package ID or assign one if a shared library.
      int package_id;
      if (package->IsDynamic()) {
        package_id = next_package_id++;
      } else {
        package_id = package->GetPackageId();
      }
      }


  const int apk_assets_count = apk_assets_.size();
  for (int i = 0; i < apk_assets_count; i++) {
    const auto& apk_assets = apk_assets_[i];
    for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
      const auto package_id_entry = package_name_package_ids.find(package->GetPackageName());
      CHECK(package_id_entry != package_name_package_ids.end())
          << "no package id assgined to package " << package->GetPackageName();
      const uint8_t package_id = package_id_entry->second;

      // Add the mapping for package ID to index if not present.
      // Add the mapping for package ID to index if not present.
      uint8_t idx = package_ids_[package_id];
      uint8_t idx = package_ids_[package_id];
      if (idx == 0xff) {
      if (idx == 0xff) {
@@ -162,7 +147,7 @@ void AssetManager2::BuildDynamicRefTable() {
            target_package_group.overlays_.push_back(
            target_package_group.overlays_.push_back(
                ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
                ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
                                                                      overlay_table.get()),
                                                                      overlay_table.get()),
                                  static_cast<ApkAssetsCookie>(i)});
                                  apk_assets_cookies[apk_assets]});
          }
          }
        }
        }


@@ -174,7 +159,7 @@ void AssetManager2::BuildDynamicRefTable() {


      // Add the package and to the set of packages with the same ID.
      // Add the package and to the set of packages with the same ID.
      package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
      package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
      package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i));
      package_group->cookies_.push_back(apk_assets_cookies[apk_assets]);


      // Add the package name -> build time ID mappings.
      // Add the package name -> build time ID mappings.
      for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
      for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
@@ -182,6 +167,8 @@ void AssetManager2::BuildDynamicRefTable() {
        package_group->dynamic_ref_table->mEntries.replaceValueFor(
        package_group->dynamic_ref_table->mEntries.replaceValueFor(
            package_name, static_cast<uint8_t>(entry.package_id));
            package_name, static_cast<uint8_t>(entry.package_id));
      }
      }

      apk_assets_package_ids.insert(std::make_pair(apk_assets->GetPath(), package_id));
    }
    }
  }
  }


@@ -1329,16 +1316,6 @@ uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const
  return 0;
  return 0;
}
}


DynamicLibManager* AssetManager2::GetDynamicLibManager() const {
  auto dynamic_lib_manager =
      std::get_if<std::unique_ptr<DynamicLibManager>>(&dynamic_lib_manager_);
  if (dynamic_lib_manager) {
    return (*dynamic_lib_manager).get();
  } else {
    return *std::get_if<DynamicLibManager*>(&dynamic_lib_manager_);
  }
}

std::unique_ptr<Theme> AssetManager2::NewTheme() {
std::unique_ptr<Theme> AssetManager2::NewTheme() {
  return std::unique_ptr<Theme>(new Theme(this));
  return std::unique_ptr<Theme>(new Theme(this));
}
}
+0 −34
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "androidfw/DynamicLibManager.h"

namespace android {

uint8_t DynamicLibManager::GetAssignedId(const std::string& library_package_name) {
  auto lib_entry = shared_lib_package_ids_.find(library_package_name);
  if (lib_entry != shared_lib_package_ids_.end()) {
    return lib_entry->second;
  }

  return shared_lib_package_ids_[library_package_name] = next_package_id_++;
}

uint8_t DynamicLibManager::FindUnassignedId(uint8_t start_package_id) {
  return (start_package_id < next_package_id_) ? next_package_id_ : start_package_id;
}

} // namespace android
+3 −10
Original line number Original line Diff line number Diff line
@@ -27,7 +27,6 @@
#include "androidfw/ApkAssets.h"
#include "androidfw/ApkAssets.h"
#include "androidfw/Asset.h"
#include "androidfw/Asset.h"
#include "androidfw/AssetManager.h"
#include "androidfw/AssetManager.h"
#include "androidfw/DynamicLibManager.h"
#include "androidfw/ResourceTypes.h"
#include "androidfw/ResourceTypes.h"
#include "androidfw/Util.h"
#include "androidfw/Util.h"


@@ -95,7 +94,6 @@ class AssetManager2 {
  };
  };


  AssetManager2();
  AssetManager2();
  explicit AssetManager2(DynamicLibManager* dynamic_lib_manager);


  // Sets/resets the underlying ApkAssets for this AssetManager. The ApkAssets
  // Sets/resets the underlying ApkAssets for this AssetManager. The ApkAssets
  // are not owned by the AssetManager, and must have a longer lifetime.
  // are not owned by the AssetManager, and must have a longer lifetime.
@@ -126,6 +124,9 @@ class AssetManager2 {
  // This may be nullptr if the APK represented by `cookie` has no resource table.
  // This may be nullptr if the APK represented by `cookie` has no resource table.
  std::shared_ptr<const DynamicRefTable> GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const;
  std::shared_ptr<const DynamicRefTable> GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const;


  // Retrieve the assigned package id of the package if loaded into this AssetManager
  uint8_t GetAssignedPackageId(const LoadedPackage* package) const;

  // Returns a string representation of the overlayable API of a package.
  // Returns a string representation of the overlayable API of a package.
  bool GetOverlayablesToString(const android::StringPiece& package_name,
  bool GetOverlayablesToString(const android::StringPiece& package_name,
                               std::string* out) const;
                               std::string* out) const;
@@ -370,11 +371,6 @@ class AssetManager2 {
  // been seen while traversing bag parents.
  // been seen while traversing bag parents.
  const ResolvedBag* GetBag(uint32_t resid, std::vector<uint32_t>& child_resids);
  const ResolvedBag* GetBag(uint32_t resid, std::vector<uint32_t>& child_resids);


  // Retrieve the assigned package id of the package if loaded into this AssetManager
  uint8_t GetAssignedPackageId(const LoadedPackage* package) const;

  DynamicLibManager* GetDynamicLibManager() const;

  // The ordered list of ApkAssets to search. These are not owned by the AssetManager, and must
  // The ordered list of ApkAssets to search. These are not owned by the AssetManager, and must
  // have a longer lifetime.
  // have a longer lifetime.
  std::vector<const ApkAssets*> apk_assets_;
  std::vector<const ApkAssets*> apk_assets_;
@@ -393,9 +389,6 @@ class AssetManager2 {
  // may need to be purged.
  // may need to be purged.
  ResTable_config configuration_;
  ResTable_config configuration_;


  // Component responsible for assigning package ids to shared libraries.
  std::variant<std::unique_ptr<DynamicLibManager>, DynamicLibManager*> dynamic_lib_manager_;

  // Cached set of bags. These are cached because they can inherit keys from parent bags,
  // Cached set of bags. These are cached because they can inherit keys from parent bags,
  // which involves some calculation.
  // which involves some calculation.
  std::unordered_map<uint32_t, util::unique_cptr<ResolvedBag>> cached_bags_;
  std::unordered_map<uint32_t, util::unique_cptr<ResolvedBag>> cached_bags_;
Loading