From b5c608bb70bbae6594a672245040b9e29fec0aae 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 --- BUILD.gn | 3 + IBundleActiveService.idl | 5 +- .../bundle_active_event_vec_raw_data.h | 40 ++++++++ .../innerkits/src/bundle_active_client.cpp | 10 +- .../src/bundle_active_event_vec_raw_data.cpp | 94 +++++++++++++++++++ .../common/include/bundle_active_service.h | 4 +- services/common/src/bundle_active_service.cpp | 8 +- .../unittest/device_usage_statistics_test.cpp | 69 ++++++++++++++ 8 files changed, 225 insertions(+), 8 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..111e115 --- /dev/null +++ b/interfaces/innerkits/include/bundle_active_event_vec_raw_data.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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 +#define BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA + +#include "parcel.h" +#include +#include +#include +#include +#include "budle_active_event.h" +#include "budle_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); + ErrCode RawDataCpy(const void *inData); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA + diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index ac873cb..c457489 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,9 @@ 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); + bundleActiveEventVecRawData.Unmarshalling(bundleActiveEvents); BUNDLE_ACTIVE_LOGI("QueryBundleEvents bundleActiveEvents is %{public}zu", bundleActiveEvents.size()); return err; } @@ -170,7 +173,10 @@ ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vectorQueryCurrentBundleEvents(bundleActiveEvents, beginTime, endTime); + BundleActiveEventVecRawData bundleActiveEventVecRawData; + ret = bundleActiveProxy_->QueryCurrentBundleEvents(bundleActiveEventVecRawData, beginTime, endTime); + 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..89e6fc0 --- /dev/null +++ b/interfaces/innerkits/src/bundle_active_event_vec_raw_data.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022 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::sstream 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(); + data = reinterpret_cast(serializedData.data()); + return ERR_OK; + } + + ErrCode BundleActiveEventVecRawData::Unmarshalling(std::vector &out) const + { + std::sstream 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"); + } + 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::sstream ss; + ss.write(reinterpret_cast(inData), size); + serializedData = ss.str(); + data = reinterpret_cast(serializedData.data()); + return ERR_OK; + } +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_EVENT_VEC_RAW_DATA + diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 47edb99..a178c8f 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -105,7 +105,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 +138,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..1caebb6 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,12 @@ 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()); } + bundleActiveEventVecRawData.Marshalling(bundleActiveEvents); return ret; } @@ -563,7 +565,7 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vector& bundleActiveEvents, +ErrCode BundleActiveService::QueryCurrentBundleEvents(BundleActiveEventVecRawData& bundleActiveEventVecRawData, const int64_t beginTime, const int64_t endTime) { // get uid @@ -572,6 +574,7 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vector bundleActiveEvents; if (ret == ERR_OK && userId != -1) { std::string bundleName = ""; BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); @@ -587,6 +590,7 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vector data; + BundleActiveEvent bundleActiveEvent = {"test", "test"}; + data.push_back(bundleActiveEvent); + bundleActiveEventRawData.Marshalling(data); + std::vector out; + bundleActiveEventRawData.Unmarshalling(out); + + EXPECT_EQ(out.size(), 1); + EXPECT_EQ(out[0].bundleName_, "test"); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventRawData_002 + * @tc.desc: BundleActiveEventRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventRawData_002, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventRawData bundleActiveEventRawData; + std::vector out; + ErrCode ret = bundleActiveEventRawData.Unmarshalling(out); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventRawData_003 + * @tc.desc: BundleActiveEventRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventRawData_003, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventRawData bundleActiveEventRawData; + std::vector data; + BundleActiveEvent bundleActiveEvent = {"test", "test"}; + data.push_back(bundleActiveEvent); + bundleActiveEventRawData.Marshalling(data); + ErrCode ret = bundleActiveEventRawData.RawDataCpy(bundleActiveEventRawData.data); + EXPECT_EQ(ret, ERR_OK); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventRawData_004 + * @tc.desc: BundleActiveEventRawData + * @tc.type: FUNC + * @tc.require: rawdata + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventRawData_004, + Function | MediumTest | TestSize.Level0) +{ + BundleActiveEventRawData bundleActiveEventRawData; + ErrCode ret = bundleActiveEventRawData.RawDataCpy(bundleActiveEventRawData.data); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} } // namespace DeviceUsageStats } // namespace OHOS -- Gitee