From 274f6384795afdb66c1717c5a174429f6bb40a48 Mon Sep 17 00:00:00 2001 From: zxf Date: Tue, 12 Aug 2025 21:41:07 +0800 Subject: [PATCH] =?UTF-8?q?queryEvent=E6=8E=A5=E5=8F=A3=E5=88=87rawdata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zxf Signed-off-by: xiaofeizhudekongjian --- BUILD.gn | 3 + IBundleActiveService.idl | 5 +- .../bundle_active_event_vec_raw_data.h | 40 ++++++++ .../innerkits/src/bundle_active_client.cpp | 14 ++- .../src/bundle_active_event_vec_raw_data.cpp | 95 +++++++++++++++++++ .../common/include/bundle_active_service.h | 5 +- services/common/src/bundle_active_service.cpp | 12 ++- .../bundleactivecommon_fuzzer.cpp | 12 ++- .../device_usage_statistics_mock_test.cpp | 7 +- .../unittest/device_usage_statistics_test.cpp | 69 ++++++++++++++ 10 files changed, 247 insertions(+), 15 deletions(-) create mode 100644 interfaces/innerkits/include/bundle_active_event_vec_raw_data.h create mode 100644 interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp diff --git a/BUILD.gn b/BUILD.gn index c7e3014..666989e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -55,6 +55,7 @@ ohos_source_set("bundle_active_service_stub") { } output_values = get_target_outputs(":bundle_active_interface") sources = filter_include(output_values, [ "*_stub.cpp" ]) + sources += [ "interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp" ] public_configs = [] deps = [ ":bundle_active_interface" ] external_deps = [ @@ -72,6 +73,7 @@ ohos_shared_library("usagestatsinner") { "interfaces/innerkits/src/app_group_callback_info.cpp", "interfaces/innerkits/src/bundle_active_client.cpp", "interfaces/innerkits/src/bundle_active_event.cpp", + "interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp", "interfaces/innerkits/src/bundle_active_event_stats.cpp", "interfaces/innerkits/src/bundle_active_form_record.cpp", "interfaces/innerkits/src/bundle_active_module_record.cpp", @@ -395,6 +397,7 @@ ohos_static_library("usagestatsinner_static") { "interfaces/innerkits/src/app_group_callback_info.cpp", "interfaces/innerkits/src/bundle_active_client.cpp", "interfaces/innerkits/src/bundle_active_event.cpp", + "interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp", "interfaces/innerkits/src/bundle_active_event_stats.cpp", "interfaces/innerkits/src/bundle_active_form_record.cpp", "interfaces/innerkits/src/bundle_active_module_record.cpp", diff --git a/IBundleActiveService.idl b/IBundleActiveService.idl index d2b2893..9278ddb 100644 --- a/IBundleActiveService.idl +++ b/IBundleActiveService.idl @@ -18,6 +18,7 @@ sequenceable OHOS.DeviceUsageStats.BundleActiveHighFrequencyPeriod; sequenceable OHOS.DeviceUsageStats.BundleActivePackageStats; sequenceable OHOS.DeviceUsageStats.BundleActiveModuleRecord; sequenceable OHOS.DeviceUsageStats.BundleActiveEventStats; +rawdata OHOS.DeviceUsageStats.BundleActiveEventVecRawData; interface OHOS.DeviceUsageStats.IAppGroupCallback; interface OHOS.DeviceUsageStats.IBundleActiveService { [oneway] void ReportEvent([in] BundleActiveEvent event, [in] int userId); @@ -25,11 +26,11 @@ interface OHOS.DeviceUsageStats.IBundleActiveService { void IsBundleUsePeriod([out] boolean isUsePeriod, [in] String bundleName, [in] int userId); void QueryBundleStatsInfoByInterval([out] List packageStats, [in] int intervalType, [in] long beginTime, [in] long endTime, [in] int userId); - void QueryBundleEvents([out] List bundleActiveEvents, [in] long beginTime, + void QueryBundleEvents([out] BundleActiveEventVecRawData bundleActiveEventVecRawData, [in] long beginTime, [in] long endTime, [in] int userId); void QueryBundleStatsInfos([out] List bundleActivePackageStats, [in] int intervalType, [in] long beginTime, [in] long endTime); - void QueryCurrentBundleEvents([out] List bundleActiveEvents, + void QueryCurrentBundleEvents([out] BundleActiveEventVecRawData bundleActiveEventVecRawData, [in] long beginTime, [in]long endTime); void QueryAppGroup([out] int appGroup, [in] String bundleName, [in] int userId); void SetAppGroup([in] String bundleName, [in] int newGroup, [in] int userId); diff --git a/interfaces/innerkits/include/bundle_active_event_vec_raw_data.h b/interfaces/innerkits/include/bundle_active_event_vec_raw_data.h new file mode 100644 index 0000000..31a86f8 --- /dev/null +++ b/interfaces/innerkits/include/bundle_active_event_vec_raw_data.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA_H +#define BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA_H + +#include "parcel.h" +#include +#include +#include +#include +#include "bundle_active_event.h" +#include "bundle_state_inner_errors.h" + +namespace OHOS { +namespace DeviceUsageStats { +struct BundleActiveEventVecRawData { + uint32_t size; + const void *data; + std::string serializedData; + ErrCode Marshalling(const std::vector &inData); + ErrCode Unmarshalling(std::vector &out) const; + ErrCode RawDataCpy(const void *inData); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA_H + diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index ac873cb..9e9ab60 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -16,6 +16,7 @@ #include "ffrt.h" #include "bundle_active_log.h" #include "bundle_active_client.h" +#include "bundle_active_event_vec_raw_data.h" namespace OHOS { namespace DeviceUsageStats { @@ -136,7 +137,11 @@ ErrCode BundleActiveClient::QueryBundleEvents(std::vector& bu if (ret != ERR_OK) { return ret; } - auto err = bundleActiveProxy_->QueryBundleEvents(bundleActiveEvents, beginTime, endTime, userId); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + auto err = bundleActiveProxy_->QueryBundleEvents(bundleActiveEventVecRawData, beginTime, endTime, userId); + if (err == ERR_OK) { + err = bundleActiveEventVecRawData.Unmarshalling(bundleActiveEvents); + } BUNDLE_ACTIVE_LOGI("QueryBundleEvents bundleActiveEvents is %{public}zu", bundleActiveEvents.size()); return err; } @@ -170,7 +175,12 @@ ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vectorQueryCurrentBundleEvents(bundleActiveEvents, beginTime, endTime); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + ret = bundleActiveProxy_->QueryCurrentBundleEvents(bundleActiveEventVecRawData, beginTime, endTime); + if (ret == ERR_OK) { + ret = bundleActiveEventVecRawData.Unmarshalling(bundleActiveEvents); + } + return ret; } ErrCode BundleActiveClient::QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId) diff --git a/interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp b/interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp new file mode 100644 index 0000000..5966816 --- /dev/null +++ b/interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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 "bundle_active_event_vec_raw_data.h" + +#include "bundle_active_log.h" +#include "string_ex.h" + +namespace OHOS { +namespace DeviceUsageStats { + constexpr size_t PARCEL_MAX_CAPACITY_IN_BYTE = 1024 * 1024 * 10; + ErrCode BundleActiveEventVecRawData::Marshalling(const std::vector &inData) + { + std::stringstream ss; + size_t eventCount = inData.size(); + ss.write(reinterpret_cast(&eventCount), sizeof(eventCount)); + OHOS::Parcel parcel; + parcel.SetMaxCapacity(PARCEL_MAX_CAPACITY_IN_BYTE); + for (const auto &event : inData) { + parcel.RewindWrite(0); + if (!event.Marshalling(parcel)) { + BUNDLE_ACTIVE_LOGE("Marshalling failed,budle:%{public}s", event.bundleName_.c_str()); + return ERR_INVALID_VALUE; + } + size_t eventDataSize = parcel.GetWritePosition(); + ss.write(reinterpret_cast(&eventDataSize), sizeof(eventDataSize)); + ss.write(reinterpret_cast(parcel.GetData()), eventDataSize); + } + serializedData = ss.str(); + size = serializedData.length(); + data = reinterpret_cast(serializedData.data()); + return ERR_OK; + } + + ErrCode BundleActiveEventVecRawData::Unmarshalling(std::vector &out) const + { + std::stringstream ss; + if (data == nullptr) { + BUNDLE_ACTIVE_LOGE("Unmarshalling data is null"); + return ERR_INVALID_VALUE; + } + ss.write(reinterpret_cast(data), size); + size_t eventCount = 0; + ss.read(reinterpret_cast(&eventCount), sizeof(eventCount)); + for (uint32_t i = 0; i < eventCount; i++) { + OHOS::Parcel parcel; + if (!parcel.SetMaxCapacity(PARCEL_MAX_CAPACITY_IN_BYTE)) { + BUNDLE_ACTIVE_LOGE("parcel SetMaxCapacity failed"); + return ERR_INVALID_VALUE; + } + size_t eventDataSize = 0; + ss.read(reinterpret_cast(&eventDataSize), sizeof(eventDataSize)); + std::string eventBuf; + eventBuf.resize(eventDataSize); + ss.read(eventBuf.data(), eventDataSize); + parcel.WriteBuffer(eventBuf.data(), eventDataSize); + + BundleActiveEvent bundleActiveEvent; + BundleActiveEvent* result = bundleActiveEvent.Unmarshalling(parcel); + if (!result) { + BUNDLE_ACTIVE_LOGE("bundleActiveEvent Unmarshalling failed, eventDataSize:%{public}zu", eventDataSize); + return ERR_INVALID_VALUE; + } + out.push_back(*result); + } + return ERR_OK; + } + + ErrCode BundleActiveEventVecRawData::RawDataCpy(const void *inData) + { + if (inData == nullptr) { + BUNDLE_ACTIVE_LOGE("RawDataCpy inData is null"); + return ERR_INVALID_VALUE; + } + std::stringstream ss; + ss.write(reinterpret_cast(inData), size); + serializedData = ss.str(); + data = reinterpret_cast(serializedData.data()); + return ERR_OK; + } +} // namespace DeviceUsageStats +} // namespace OHOS + diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 47edb99..b5e6528 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -35,6 +35,7 @@ #include "string_ex.h" #include "system_ability.h" #include "ffrt.h" +#include "bundle_active_event_vec_raw_data.h" namespace OHOS { namespace DeviceUsageStats { @@ -105,7 +106,7 @@ public: * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. * @return errCode. */ - ErrCode QueryBundleEvents(std::vector& bundleActiveEvents, const int64_t beginTime, + ErrCode QueryBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime, int32_t userId) override; /** @@ -138,7 +139,7 @@ public: * @param endTime . * @return errCode. */ - ErrCode QueryCurrentBundleEvents(std::vector& bundleActiveEvents, + ErrCode QueryCurrentBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime) override; /** diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index d256d4c..4caa29a 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -415,7 +415,7 @@ int32_t BundleActiveService::GetNameAndIndexForUid(int32_t uid) return appIndex; } -ErrCode BundleActiveService::QueryBundleEvents(std::vector& bundleActiveEvents, +ErrCode BundleActiveService::QueryBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime, int32_t userId) { ErrCode ret = ERR_OK; @@ -431,10 +431,14 @@ ErrCode BundleActiveService::QueryBundleEvents(std::vector& b } BUNDLE_ACTIVE_LOGI("QueryBundleEvents userid is %{public}d", userId); ret = CheckSystemAppOrNativePermission(callingUid, tokenId); + std::vector bundleActiveEvents; if (ret == ERR_OK) { ret = bundleActiveCore_->QueryBundleEvents(bundleActiveEvents, userId, beginTime, endTime, ""); BUNDLE_ACTIVE_LOGI("QueryBundleEvents result is %{public}zu", bundleActiveEvents.size()); } + if (ret == ERR_OK) { + ret = bundleActiveEventVecRawData.Marshalling(bundleActiveEvents); + } return ret; } @@ -563,7 +567,7 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vector& bundleActiveEvents, +ErrCode BundleActiveService::QueryCurrentBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime) { // get uid @@ -572,6 +576,7 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vector bundleActiveEvents; if (ret == ERR_OK && userId != -1) { std::string bundleName = ""; BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); @@ -584,6 +589,9 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vectorQueryBundleEvents(bundleActiveEvents, userId, beginTime, endTime, bundleName); + if (ret == ERR_OK) { + ret = bundleActiveEventVecRawData.Marshalling(bundleActiveEvents); + } } // LCOV_EXCL_STOP } diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp index d1de88d..b96eb02 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp @@ -71,8 +71,8 @@ namespace DeviceUsageStats { int64_t beginTime = fdp->ConsumeIntegral(); int64_t endTime = fdp->ConsumeIntegral(); bundleActiveService->QueryBundleStatsInfoByInterval(packageStats, intervalType, beginTime, endTime, userId); - std::vector events; - bundleActiveService->QueryBundleEvents(events, beginTime, endTime, userId); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + bundleActiveService->QueryBundleEvents(bundleActiveEventVecRawData, beginTime, endTime, userId); std::vector eventStats; eventStats.clear(); int32_t newGroup = fdp->ConsumeIntegral(); @@ -117,7 +117,9 @@ namespace DeviceUsageStats { int64_t endTime = fdp->ConsumeIntegral(); std::vector events; std::vector eventStats; - bundleActiveService->QueryCurrentBundleEvents(events, beginTime, endTime); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + bundleActiveService->QueryCurrentBundleEvents(bundleActiveEventVecRawData, beginTime, endTime); + bundleActiveEventVecRawData.Unmarshalling(events); eventStats.clear(); int32_t appGroup = 0; bundleActiveService->QueryAppGroup(appGroup, bundleName, userId); @@ -148,7 +150,9 @@ namespace DeviceUsageStats { int64_t endTime = fdp->ConsumeIntegral(); bundleActiveServiceProxy->QueryBundleStatsInfos(bundleActivePackageStats, intervalType, beginTime, endTime); std::vector bundleActiveEvent; - bundleActiveServiceProxy->QueryCurrentBundleEvents(bundleActiveEvent, beginTime, endTime); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + bundleActiveServiceProxy->QueryCurrentBundleEvents(bundleActiveEventVecRawData, beginTime, endTime); + bundleActiveEventVecRawData.Unmarshalling(bundleActiveEvent); int32_t appGroup = fdp->ConsumeIntegral(); std::string bundleName = fdp->ConsumeRandomLengthString(); int32_t userId = fdp->ConsumeIntegral(); diff --git a/test/unittest/device_usage_statistics_mock_test.cpp b/test/unittest/device_usage_statistics_mock_test.cpp index 8f9bcc8..7e34762 100644 --- a/test/unittest/device_usage_statistics_mock_test.cpp +++ b/test/unittest/device_usage_statistics_mock_test.cpp @@ -27,6 +27,7 @@ #include "bundle_active_usage_database.h" #include "bundle_active_test_util.h" #include "bundle_active_high_frequency_period.h" +#include "bundle_active_event_vec_raw_data.h" using namespace testing::ext; @@ -244,12 +245,12 @@ HWTEST_F(DeviceUsageStatisticsMockTest, DeviceUsageStatisticsMockTest_AppGroupCa HWTEST_F(DeviceUsageStatisticsMockTest, DeviceUsageStatisticsMockTest_QueryBundleEvents_001, Function | MediumTest | TestSize.Level0) { - std::vector bundleActiveEvents; + BundleActiveEventVecRawData bundleActiveEventVecRawData; ErrCode code = DelayedSingleton::GetInstance() - ->QueryBundleEvents(bundleActiveEvents, 0, g_largeNum, -1); + ->QueryBundleEvents(bundleActiveEventVecRawData, 0, g_largeNum, -1); EXPECT_NE(code, ERR_OK); code = DelayedSingleton::GetInstance() - ->QueryCurrentBundleEvents(bundleActiveEvents, 0, g_largeNum); + ->QueryCurrentBundleEvents(bundleActiveEventVecRawData, 0, g_largeNum); EXPECT_NE(code, ERR_OK); } diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 6588487..2ddf932 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -35,6 +35,7 @@ #include "nativetoken_kit.h" #include "bundle_active_test_util.h" #include "bundle_active_high_frequency_period.h" +#include "bundle_active_event_vec_raw_data.h" using namespace testing::ext; @@ -726,6 +727,74 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveHighFr originalBundle.highFreqHours_.push_back(2); EXPECT_EQ(originalBundle.ToString(), "bundle name is test, highFreqHours_ is 1, 2\n"); } + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventVecRawData_001 + * @tc.desc: BundleActiveEventVecRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventVecRawData_001, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventVecRawData bundleActiveEventVecRawData; + std::vector data; + BundleActiveEvent bundleActiveEvent = {"test", "test"}; + data.push_back(bundleActiveEvent); + bundleActiveEventVecRawData.Marshalling(data); + std::vector out; + bundleActiveEventVecRawData.Unmarshalling(out); + + EXPECT_EQ(out.size(), 1); + EXPECT_EQ(out[0].bundleName_, "test"); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventVecRawData_002 + * @tc.desc: BundleActiveEventVecRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventVecRawData_002, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventVecRawData bundleActiveEventVecRawData; + std::vector out; + ErrCode ret = bundleActiveEventVecRawData.Unmarshalling(out); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventVecRawData_003 + * @tc.desc: BundleActiveEventVecRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventVecRawData_003, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventVecRawData bundleActiveEventVecRawData; + std::vector data; + BundleActiveEvent bundleActiveEvent = {"test", "test"}; + data.push_back(bundleActiveEvent); + bundleActiveEventVecRawData.Marshalling(data); + ErrCode ret = bundleActiveEventVecRawData.RawDataCpy(bundleActiveEventVecRawData.data); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventVecRawData_004 + * @tc.desc: BundleActiveEventVecRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventVecRawData_004, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventVecRawData bundleActiveEventVecRawData; + ErrCode ret = bundleActiveEventVecRawData.RawDataCpy(bundleActiveEventVecRawData.data); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} } // namespace DeviceUsageStats } // namespace OHOS -- Gitee