diff --git a/BUILD.gn b/BUILD.gn index ed3b8b122d491415c709169c320904cf9989506c..91883cc85f803f52bada20dc08e87673fff01b9f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -262,6 +262,7 @@ ohos_shared_library("usagestatservice") { "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", + "cJSON:cjson", "config_policy:configpolicy_util", "ffrt:libffrt", "hicollie:libhicollie", @@ -269,7 +270,6 @@ ohos_shared_library("usagestatservice") { "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -342,6 +342,7 @@ ohos_static_library("usagestatservice_static") { "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", + "cJSON:cjson", "config_policy:configpolicy_util", "ffrt:libffrt", "hicollie:libhicollie", @@ -349,7 +350,6 @@ ohos_static_library("usagestatservice_static") { "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/bundle.json b/bundle.json index 95085d8e4ee3dc4da8eab12f019ea402859c5a70..74fcdc4e46c2194dee855d43c0c215dc45fc729a 100644 --- a/bundle.json +++ b/bundle.json @@ -42,7 +42,6 @@ "selinux_adapter", "time_service", "init", - "jsoncpp", "ffrt", "hisysevent" ], diff --git a/services/common/include/bundle_active_config_reader.h b/services/common/include/bundle_active_config_reader.h index 1400e4efe278cf8dc51634d6ef75a507e5226024..a320202146096c69b6ea50461590daae8bd44da0 100644 --- a/services/common/include/bundle_active_config_reader.h +++ b/services/common/include/bundle_active_config_reader.h @@ -16,7 +16,7 @@ #ifndef BUNDLE_ACTIVE_CONFIG_READER_H #define BUNDLE_ACTIVE_CONFIG_READER_H #include -#include "json/json.h" +#include "cJSON.h" namespace OHOS { namespace DeviceUsageStats { @@ -31,7 +31,7 @@ public: AppUsePeriodicallyConfig GetApplicationUsePeriodicallyConfig(); private: void LoadApplicationUsePeriodically(const char* path); - bool GetJsonFromFile(const char* filePath, Json::Value& root); + bool GetJsonFromFile(const char* filePath, cJSON *&root); bool ConvertFullPath(const std::string& partialPath, std::string& fullPath); AppUsePeriodicallyConfig appUsePeriodicallyConfig_; }; diff --git a/services/common/src/bundle_active_config_reader.cpp b/services/common/src/bundle_active_config_reader.cpp index 95c27f8ffc660ef519da619be92677845c816790..fe13125f54cd97647ff4c19b7af9ab8320a81521 100644 --- a/services/common/src/bundle_active_config_reader.cpp +++ b/services/common/src/bundle_active_config_reader.cpp @@ -21,11 +21,11 @@ using namespace std; namespace OHOS { namespace DeviceUsageStats { -const char* CONFIG_PATH = "etc/device_usage_statistics/device_usage_statistics_config.json"; -const std::string APPLICATION_USE_PERIODICALLY_KEY = "application_use_periodically"; -const std::string MIN_USE_TIMES = "MinUseTimes"; -const std::string MAX_USE_TIMES = "MaxUseTimes"; -const std::string MIN_USE_DAYS = "MinUseDays"; +const static char* CONFIG_PATH = "etc/device_usage_statistics/device_usage_statistics_config.json"; +const static char* APPLICATION_USE_PERIODICALLY_KEY = "application_use_periodically"; +const static char* MIN_USE_TIMES = "MinUseTimes"; +const static char* MAX_USE_TIMES = "MaxUseTimes"; +const static char* MIN_USE_DAYS = "MinUseDays"; const int32_t DEFAULT_MIN_USE_TIMES = 1; const int32_t DEFAULT_MAX_USE_TIMES = 10; const int32_t DEFAULT_MIN_USE_DAYS = 3; @@ -54,39 +54,43 @@ void BundleActiveConfigReader::LoadApplicationUsePeriodically(const char *filePa if (!filePath) { return; } - Json::Value root; - if (!GetJsonFromFile(filePath, root) || root.empty()) { + cJSON *root = nullptr; + if (!GetJsonFromFile(filePath, root) || !root) { BUNDLE_ACTIVE_LOGE("file is empty %{private}s", filePath); return; } - if (!root.isMember(APPLICATION_USE_PERIODICALLY_KEY)) { - BUNDLE_ACTIVE_LOGE("not have application_use_periodically key"); - return; - } - Json::Value appUsePeriodicallyRoot = root[APPLICATION_USE_PERIODICALLY_KEY]; - if (appUsePeriodicallyRoot.empty() || !appUsePeriodicallyRoot.isObject()) { + cJSON *appUsePeriodicallyRoot = cJSON_GetObjectItem(root, APPLICATION_USE_PERIODICALLY_KEY); + if (!appUsePeriodicallyRoot || cJSON_IsObject(appUsePeriodicallyRoot)) { BUNDLE_ACTIVE_LOGE("application_use_periodically content is empty"); + cJSON_Delete(root); return; } - if (appUsePeriodicallyRoot[MIN_USE_TIMES].empty() || !appUsePeriodicallyRoot[MIN_USE_TIMES].isInt()) { + cJSON *minUseTimesItem = cJSON_GetObjectItem(appUsePeriodicallyRoot, MIN_USE_TIMES); + if (!minUseTimesItem || !cJSON_IsNumber(minUseTimesItem)) { BUNDLE_ACTIVE_LOGE("not have MinUseTimes key"); + cJSON_Delete(root); return; } - int32_t minUseTimes = appUsePeriodicallyRoot[MIN_USE_TIMES].asInt(); - if (appUsePeriodicallyRoot[MAX_USE_TIMES].empty() || !appUsePeriodicallyRoot[MAX_USE_TIMES].isInt()) { + int32_t minUseTimes = static_cast(minUseTimesItem->valueint); + cJSON *maxUseTimesItem = cJSON_GetObjectItem(appUsePeriodicallyRoot, MAX_USE_TIMES); + if (!maxUseTimesItem || !cJSON_IsNumber(maxUseTimesItem)) { BUNDLE_ACTIVE_LOGE("not have MaxUseTimes key"); + cJSON_Delete(root); return; } - int32_t maxUseTimes = appUsePeriodicallyRoot[MAX_USE_TIMES].asInt(); - if (appUsePeriodicallyRoot[MIN_USE_DAYS].empty() || !appUsePeriodicallyRoot[MIN_USE_DAYS].isInt()) { + int32_t maxUseTimes = static_cast(maxUseTimesItem->valueint); + cJSON *minUseDaysItem = cJSON_GetObjectItem(appUsePeriodicallyRoot, MIN_USE_DAYS); + if (!minUseDaysItem || !cJSON_IsNumber(minUseDaysItem)) { BUNDLE_ACTIVE_LOGE("not have MinUseDays key"); + cJSON_Delete(root); return; } - int32_t minUseDays = appUsePeriodicallyRoot[MIN_USE_DAYS].asInt(); + int32_t minUseDays = static_cast(minUseDaysItem->valueint); appUsePeriodicallyConfig_ = { minUseTimes, maxUseTimes, minUseDays}; + cJSON_Delete(root); }; -bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, Json::Value &root) +bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, cJSON *&root) { std::string realPath; if (!BundleActiveConfigReader::ConvertFullPath(filePath, realPath)) { @@ -102,11 +106,8 @@ bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, Json::Value if (data.empty()) { return false; } - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - const unique_ptr jsonReader(readerBuilder.newCharReader()); - bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &root, &errs); - if (!res || !errs.empty()) { + root = cJSON_Parse(data.c_str()); + if (!root) { BUNDLE_ACTIVE_LOGE("parse %{private}s json error", realPath.c_str()); return false; } diff --git a/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn b/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn index fc39fe002043cc4373d16a225d1221a38ee6c5d8..cd387413eff6e7cf1910a09411fd3dbd1d82ebfd 100644 --- a/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn +++ b/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn @@ -63,7 +63,6 @@ ohos_fuzztest("AppgroupcallbackstubFuzzTest") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn b/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn index 82bbb01275412492dca0b67faf0186fe35e15c65..8930eb055edcba6f1af2ddcc7b5a37e54a927c34 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("BundleActiveCommonFuzzTest") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp index 1cb8599efd4ebf412611b84cf209595ef5dd7654..f8e3a5310e6dd40d4cc1dae6773143a02eddf0dc 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp @@ -49,7 +49,7 @@ namespace DeviceUsageStats { bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST2_PATH); bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST3_PATH); bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST4_PATH); - Json::Value root; + cJSON *root = nullptr; bundleActiveConfigReader->GetJsonFromFile(filePath.c_str(), root); bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST_PATH, root); std::string fullPath = ""; diff --git a/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn b/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn index 79672306220b4a19aa6ccc4a131569c2036eb6e6..d791cf86c5c17e8201100227516a57b119a67530 100644 --- a/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn @@ -68,7 +68,6 @@ ohos_fuzztest("BundleActiveObserverFuzzTest") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn b/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn index 9aa6d3421045a0bfb8d6b4a463ce0301f054ac3d..1705aa0e37e188e7b496bd350aa41b97d478df46 100644 --- a/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn @@ -62,7 +62,6 @@ ohos_fuzztest("BundleActiveOnRemoteRequestFuzzTest") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn b/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn index 48062b526fac6c96877b536c878b5767ebe3527d..9a85eed73b598378bbe5a186421c5715f68b48d5 100644 --- a/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("BundleActivePowerstateCallbackproxyFuzzTest") { "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 05fcc7eba6d3864c4f8183f5baf6ce5f321c8506..c4626fcd8d1a3e8da89957209e5b0b7b0f8031d5 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -53,11 +53,11 @@ ohos_unittest("BundleActiveTotalTest") { "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", + "cJSON:cjson", "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -107,11 +107,11 @@ ohos_unittest("DeviceUsageStatsTest") { "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "cJSON:cjson", "cJSON:cjson_static", "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -162,11 +162,11 @@ ohos_unittest("DeviceUsageStatsMultiTest") { "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "cJSON:cjson", "cJSON:cjson_static", "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -217,11 +217,11 @@ ohos_unittest("DeviceUsageStatsServiceTest") { "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "cJSON:cjson", "cJSON:cjson_static", "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -279,10 +279,10 @@ ohos_unittest("DeviceUsageStatsMockTest") { "ability_runtime:app_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "cJSON:cjson", "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -330,10 +330,10 @@ ohos_unittest("DeviceUsagePackageUsageTest") { "ability_runtime:app_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "cJSON:cjson", "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 2f567aa2fed2baee4a433239363a884adbc9d357..aaab253cb79314ef3d710a4262a29dda98de1766 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -1546,7 +1546,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Conf auto bundleActiveConfigReader = std::make_shared(); EXPECT_NE(bundleActiveConfigReader, nullptr); const char *path = "test"; - Json::Value root; + cJSON *root = nullptr; bool result = bundleActiveConfigReader->GetJsonFromFile(path, root); EXPECT_EQ(result, false); }