provide GetResourceLimitKeys interface

Signed-off-by: zhuxiang <zhuxiang6@huawei.com>
This commit is contained in:
zhuxiang 2023-05-17 13:16:00 +00:00
parent af6f428de1
commit 8c87c079ec
8 changed files with 234 additions and 0 deletions

View File

@ -275,6 +275,13 @@ public:
*/ */
void AddSystemResource(const HapManager *systemHapManager); void AddSystemResource(const HapManager *systemHapManager);
/**
* Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
*
* @return the resource limit keys
*/
uint32_t GetResourceLimitKeys();
private: private:
void UpdateResConfigImpl(ResConfigImpl &resConfig); void UpdateResConfigImpl(ResConfigImpl &resConfig);

View File

@ -261,6 +261,13 @@ public:
return idValuesMap_.size(); return idValuesMap_.size();
} }
/**
* Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
*
* @return the resource limit keys
*/
uint32_t GetResourceLimitKeys() const;
private: private:
HapResource(const std::string path, time_t lastModTime, ResDesc *resDes, HapResource(const std::string path, time_t lastModTime, ResDesc *resDes,
bool isSystem = false, bool isOverlay = false); bool isSystem = false, bool isOverlay = false);
@ -269,6 +276,9 @@ private:
void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId); void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId);
uint32_t GetLimitPathsKeys(const std::vector<ValueUnderQualifierDir *> &limitPaths,
std::vector<bool> &keyTypes) const;
// must call Init() after constructor // must call Init() after constructor
bool Init(); bool Init();

View File

@ -497,6 +497,28 @@ public:
virtual RState GetStringFormatByName(const char *name, std::string &outValue, virtual RState GetStringFormatByName(const char *name, std::string &outValue,
std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams); std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams);
/**
* Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType},
* enum KeyType {
* LANGUAGES = 0,
* REGION = 1,
* SCREEN_DENSITY = 2,
* DIRECTION = 3,
* DEVICETYPE = 4,
* SCRIPT = 5,
* COLORMODE = 6
* MCC = 7,
* MNC = 8,
* // RESERVER 9
* INPUTDEVICE = 10,
* KEY_TYPE_MAX,
* }
*
* @return the resource limit keys, like if resource has LANGUAGES/REGION/DEVICETYPE, then the return value to
* binary bits is 0000010011
*/
virtual uint32_t GetResourceLimitKeys();
private: private:
RState GetString(const IdItem *idItem, std::string &outValue); RState GetString(const IdItem *idItem, std::string &outValue);

View File

@ -855,6 +855,17 @@ void HapManager::AddSystemResource(const HapManager *systemHapManager)
} }
} }
} }
uint32_t HapManager::GetResourceLimitKeys()
{
AutoMutex mutex(this->lock_);
uint32_t limitKeysValue = 0;
for (size_t i = 0; i < hapResources_.size(); i++) {
limitKeysValue |= hapResources_[i]->GetResourceLimitKeys();
}
HILOG_INFO("hap manager limit key is %{public}u", limitKeysValue);
return limitKeysValue;
}
} // namespace Resource } // namespace Resource
} // namespace Global } // namespace Global
} // namespace OHOS } // namespace OHOS

View File

@ -457,6 +457,41 @@ const std::vector<std::string> HapResource::GetQualifiers() const
} }
return result; return result;
} }
uint32_t HapResource::GetResourceLimitKeys() const
{
uint32_t limitKeyValue = 0;
std::vector<bool> keyTypes(KeyType::KEY_TYPE_MAX - 1, false);
for (auto iter = idValuesMap_.begin(); iter != idValuesMap_.end(); iter++) {
if (iter->second == nullptr) {
continue;
}
const std::vector<ValueUnderQualifierDir *> &limitPaths = iter->second->GetLimitPathsConst();
if (limitPaths.size() <= 0) {
continue;
}
limitKeyValue |= GetLimitPathsKeys(limitPaths, keyTypes);
}
return limitKeyValue;
}
uint32_t HapResource::GetLimitPathsKeys(const std::vector<ValueUnderQualifierDir *> &limitPaths,
std::vector<bool> &keyTypes) const
{
uint32_t limitKeyValue = 0;
const uint32_t limitKeysBase = 0x00000001;
for_each(limitPaths.begin(), limitPaths.end(), [&](auto &item) {
const std::vector<KeyParam *> &keyParams = item->keyParams_;
for_each(keyParams.begin(), keyParams.end(), [&](auto &keyParam) {
uint32_t typeValue = static_cast<uint32_t>(keyParam->type_);
if (keyParam->type_ < KeyType::KEY_TYPE_MAX && !keyTypes[typeValue]) {
keyTypes[typeValue] = true;
limitKeyValue |= limitKeysBase << typeValue;
}
});
});
return limitKeyValue;
}
} // namespace Resource } // namespace Resource
} // namespace Global } // namespace Global
} // namespace OHOS } // namespace OHOS

View File

@ -1155,6 +1155,15 @@ RState ResourceManagerImpl::GetStringFormatByName(const char *name, std::string
} }
return SUCCESS; return SUCCESS;
} }
uint32_t ResourceManagerImpl::GetResourceLimitKeys()
{
if (hapManager_ == nullptr) {
HILOG_ERROR("resource manager get limit keys failed, hapManager_ is nullptr");
return 0;
}
return hapManager_->GetResourceLimitKeys();
}
} // namespace Resource } // namespace Resource
} // namespace Global } // namespace Global
} // namespace OHOS } // namespace OHOS

View File

@ -43,6 +43,16 @@ static const char *g_systemResFilePath = "system/assets/entry/resources.index";
static const char *g_overlayResFilePath = "overlay/assets/entry/resources.index"; static const char *g_overlayResFilePath = "overlay/assets/entry/resources.index";
static constexpr uint32_t DATA_ALL_HAP_LIMIT_KEYS = 1495;
static constexpr uint32_t COLOR_MODE_RES_LIMIT_KEYS = 84;
static constexpr uint32_t MCC_MNC_RES_LIMIT_KEYS = 471;
static constexpr uint32_t SYSTEM_RES_LIMIT_KEYS = 75;
static constexpr uint32_t ALL_TEST_RES_LIMIT_KEYS = 1503;
class ResourceManagerTest : public testing::Test { class ResourceManagerTest : public testing::Test {
public: public:
static void SetUpTestCase(void); static void SetUpTestCase(void);
@ -123,6 +133,8 @@ public:
void TestGetMediaWithDensityByName(HapResource *tmp) const; void TestGetMediaWithDensityByName(HapResource *tmp) const;
void TestGetMediaByName(HapResource *tmp) const; void TestGetMediaByName(HapResource *tmp) const;
void TestGetResourceLimitKeys(uint32_t expectedLimitKeys) const;
}; };
void ResourceManagerTest::SetUpTestCase(void) void ResourceManagerTest::SetUpTestCase(void)
@ -589,6 +601,12 @@ void ResourceManagerTest::TestGetIntArrayByName(const char* intarray1) const
EXPECT_EQ(101, outValue[2]); EXPECT_EQ(101, outValue[2]);
} }
void ResourceManagerTest::TestGetResourceLimitKeys(uint32_t expectedLimitKeys) const
{
uint32_t limitKeys = rm->GetResourceLimitKeys();
EXPECT_EQ(limitKeys, expectedLimitKeys);
}
/* /*
* @tc.name: ResourceManagerAddResourceTest001 * @tc.name: ResourceManagerAddResourceTest001
* @tc.desc: Test AddResource function, file case. * @tc.desc: Test AddResource function, file case.
@ -5928,4 +5946,124 @@ HWTEST_F(ResourceManagerTest, ResourceManagerUtilsTest001, TestSize.Level1)
bool ret = Utils::endWithTail(path, tail); bool ret = Utils::endWithTail(path, tail);
ASSERT_FALSE(ret); ASSERT_FALSE(ret);
} }
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest001
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest001, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
ASSERT_TRUE(ret);
// all/assets/entry/resources.index contains limit keys for LANGUAGE/REGION/
// SCREEN_DENSITY/DEVICETYPE/COLORMODE/MCC/MNC/INPUTDEVICE
TestGetResourceLimitKeys(DATA_ALL_HAP_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest002
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest002, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
ASSERT_TRUE(ret);
// all.hap contains limit keys for LANGUAGE/REGION/
// SCREEN_DENSITY/DEVICETYPE/COLORMODE/MCC/MNC/INPUTDEVICE
TestGetResourceLimitKeys(DATA_ALL_HAP_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest003
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest003, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_colorModeResFilePath).c_str());
ASSERT_TRUE(ret);
// colormode/assets/entry/resources.index contains limit keys for SCREEN_DENSITY/
// DEVICETYPE/COLORMODE
TestGetResourceLimitKeys(COLOR_MODE_RES_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest004
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest004, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_mccMncResFilePath).c_str());
ASSERT_TRUE(ret);
// mccmnc/assets/entry/resources.index contains limit keys for LANGUAGE/REGION/
// SCREEN_DENSITY/DEVICETYPE/COLORMODE/MCC/MNC
TestGetResourceLimitKeys(MCC_MNC_RES_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest005
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest005, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_systemResFilePath).c_str());
ASSERT_TRUE(ret);
// system/assets/entry/resources.index contains limit keys for LANGUAGE/REGION/
// DIRECTION/COLORMODE
TestGetResourceLimitKeys(SYSTEM_RES_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest006
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest006, TestSize.Level1)
{
std::vector<std::string> overlayPaths;
overlayPaths.push_back(FormatFullPath(g_overlayResFilePath).c_str());
bool ret = ((ResourceManagerImpl *)rm)->AddResource(FormatFullPath(g_systemResFilePath).c_str(), overlayPaths);
ASSERT_TRUE(ret);
// system/assets/entry/resources.index and overlay/assets/entry/resources.index contains
// LANGUAGE/REGION/DIRECTION/COLORMODE
TestGetResourceLimitKeys(SYSTEM_RES_LIMIT_KEYS);
}
/*
* @tc.name: ResourceManagerGetResourceLimitKeysTest007
* @tc.desc: Test GetResourceLimitKeys function
* @tc.type: FUNC
* @tc.require: issueI73ZQ8
*/
HWTEST_F(ResourceManagerTest, ResourceManagerGetResourceLimitKeysTest007, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
ASSERT_TRUE(ret);
ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
ASSERT_TRUE(ret);
ret = rm->AddResource(FormatFullPath(g_colorModeResFilePath).c_str());
ASSERT_TRUE(ret);
ret = rm->AddResource(FormatFullPath(g_mccMncResFilePath).c_str());
ASSERT_TRUE(ret);
std::vector<std::string> overlayPaths;
overlayPaths.push_back(FormatFullPath(g_overlayResFilePath).c_str());
ret = ((ResourceManagerImpl *)rm)->AddResource(FormatFullPath(g_systemResFilePath).c_str(), overlayPaths);
ASSERT_TRUE(ret);
// all test resource limit keys
TestGetResourceLimitKeys(ALL_TEST_RES_LIMIT_KEYS);
}
} }

View File

@ -167,6 +167,8 @@ public:
virtual RState GetStringFormatByName(const char *name, std::string &outValue, virtual RState GetStringFormatByName(const char *name, std::string &outValue,
std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0; std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0;
virtual uint32_t GetResourceLimitKeys() = 0;
}; };
EXPORT_FUNC ResourceManager *CreateResourceManager(); EXPORT_FUNC ResourceManager *CreateResourceManager();