From a51961b17253c4e7f75b101ec0f839ccc6b75965 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Wed, 11 May 2022 15:45:02 +0800 Subject: [PATCH] =?UTF-8?q?stub=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/common/include/bundle_active_stub.h | 13 + .../common/include/ibundle_active_service.h | 1 + services/common/src/bundle_active_stub.cpp | 290 +++++++++++------- 3 files changed, 188 insertions(+), 116 deletions(-) diff --git a/services/common/include/bundle_active_stub.h b/services/common/include/bundle_active_stub.h index c5147cf..152905e 100644 --- a/services/common/include/bundle_active_stub.h +++ b/services/common/include/bundle_active_stub.h @@ -32,6 +32,19 @@ public: */ int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) override; + +private: + static const std::map> interfaces_; + int32_t HandleReportEvent(MessageParcel& data, MessageParcel& reply); + int32_t HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply); + int32_t HandleQueryPackageStats(MessageParcel& data, MessageParcel& reply); + int32_t HandleQueryEvents(MessageParcel &data, MessageParcel &reply); + int32_t HandleQueryCurrentPackageStats(MessageParcel &data, MessageParcel &reply); + int32_t HandleQueryCurrentEvents(MessageParcel& data, MessageParcel& reply); + int32_t HandleQueryPackageGroup(MessageParcel& data, MessageParcel& reply); + int32_t HandleSetBundleGroup(MessageParcel& data, MessageParcel& reply); + int32_t HandleQueryFormStatistics(MessageParcel& data, MessageParcel& reply); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index 63d332c..b705917 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "iremote_broker.h" #include "iremote_stub.h" diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index ad5037c..c529e64 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -20,132 +20,190 @@ namespace OHOS { namespace DeviceUsageStats { -int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, - MessageOption &option) +const std::map> + BundleActiveStub::interfaces_ = { + {BundleActiveStub::REPORT_EVENT, + std::bind(&BundleActiveStub::HandleReportEvent, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::IS_BUNDLE_IDLE, + std::bind(&BundleActiveStub::HandleIsBundleIdle, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_USAGE_STATS, + std::bind(&BundleActiveStub::HandleQueryPackageStats, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_EVENTS, + std::bind(&BundleActiveStub::HandleQueryEvents, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_CURRENT_USAGE_STATS, + std::bind(&BundleActiveStub::HandleQueryCurrentPackageStats, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_CURRENT_EVENTS, + std::bind(&BundleActiveStub::HandleQueryCurrentEvents, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_BUNDLE_GROUP, + std::bind(&BundleActiveStub::HandleQueryPackageGroup, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::SET_BUNDLE_GROUP, + std::bind(&BundleActiveStub::HandleSetBundleGroup, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, + {BundleActiveStub::QUERY_FORM_STATS, + std::bind(&BundleActiveStub::HandleQueryFormStatistics, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}, +}; + +int32_t BundleActiveStub::HandleReportEvent(MessageParcel& data, MessageParcel& reply) { - if (data.ReadInterfaceToken() != GetDescriptor()) { + int32_t userId = data.ReadInt32(); + std::shared_ptr tmpEvent; + tmpEvent = tmpEvent->UnMarshalling(data); + if (!tmpEvent) { return -1; } - switch (code) { - case REPORT_EVENT: { - int32_t userId = data.ReadInt32(); - std::shared_ptr tmpEvent; - tmpEvent = tmpEvent->UnMarshalling(data); - if (!tmpEvent) { - return -1; - } - int32_t result = ReportEvent(*tmpEvent, userId); - return reply.WriteInt32(result); - } - case IS_BUNDLE_IDLE: { - std::string bundleName = data.ReadString(); - int32_t result = IsBundleIdle(bundleName); - return reply.WriteInt32(result); - } - case QUERY_USAGE_STATS: { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - int32_t errCode = data.ReadInt32(); - result = QueryPackageStats(intervalType, beginTime, endTime, errCode, userId); - reply.WriteInt32(errCode); - int32_t size = static_cast(result.size()); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS result size is %{public}d", size); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case QUERY_EVENTS: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - int32_t errCode = data.ReadInt32(); - result = QueryEvents(beginTime, endTime, errCode, userId); - int32_t size = static_cast(result.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case SET_BUNDLE_GROUP: { - std::string bundleName = data.ReadString(); - int32_t newGroup = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - SetBundleGroup(bundleName, newGroup, userId); - return 0; + int32_t result = ReportEvent(*tmpEvent, userId); + return reply.WriteInt32(result); +} + +int32_t BundleActiveStub::HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply) +{ + std::string bundleName = data.ReadString(); + int32_t result = IsBundleIdle(bundleName); + return reply.WriteInt32(result); +} + +int32_t BundleActiveStub::HandleQueryPackageStats(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + int32_t errCode = data.ReadInt32(); + result = QueryPackageStats(intervalType, beginTime, endTime, errCode, userId); + reply.WriteInt32(errCode); + int32_t size = static_cast(result.size()); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS result size is %{public}d", size); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; } - case QUERY_CURRENT_USAGE_STATS: { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - result = QueryCurrentPackageStats(intervalType, beginTime, endTime); - int32_t size = static_cast(result.size()); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS result size is %{public}d", size); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + } + return size == 0; +} + +int32_t BundleActiveStub::HandleQueryEvents(MessageParcel &data, MessageParcel &reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + int32_t errCode = data.ReadInt32(); + result = QueryEvents(beginTime, endTime, errCode, userId); + int32_t size = static_cast(result.size()); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; } - case QUERY_CURRENT_EVENTS: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - result = QueryCurrentEvents(beginTime, endTime); - int32_t size = static_cast(result.size()); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + } + return size == 0; +} + +int32_t BundleActiveStub::HandleQueryCurrentPackageStats(MessageParcel &data, MessageParcel &reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + result = QueryCurrentPackageStats(intervalType, beginTime, endTime); + int32_t size = static_cast(result.size()); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS result size is %{public}d", size); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; } - case QUERY_BUNDLE_GROUP: { - int32_t result = -1; - result = QueryPackageGroup(); - return reply.WriteInt32(result); + } + return size == 0; +} + +int32_t BundleActiveStub::HandleQueryCurrentEvents(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + result = QueryCurrentEvents(beginTime, endTime); + int32_t size = static_cast(result.size()); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; } - case QUERY_FORM_STATS: { - std::vector results; - int32_t maxNum = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - int32_t errCode = QueryFormStatistics(maxNum, results, userId); - int32_t size = static_cast(results.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = results[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + } + return size == 0; +} + +int32_t BundleActiveStub::HandleQueryPackageGroup(MessageParcel& data, MessageParcel& reply) +{ + int32_t result = -1; + result = QueryPackageGroup(); + return reply.WriteInt32(result); +} + +int32_t BundleActiveStub::HandleSetBundleGroup(MessageParcel& data, MessageParcel& reply) +{ + std::string bundleName = data.ReadString(); + int32_t newGroup = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + SetBundleGroup(bundleName, newGroup, userId); + return 0; +} + +int32_t BundleActiveStub::HandleQueryFormStatistics(MessageParcel& data, MessageParcel& reply) +{ + std::vector results; + int32_t maxNum = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + int32_t errCode = QueryFormStatistics(maxNum, results, userId); + int32_t size = static_cast(results.size()); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = results[i].Marshalling(reply); + if (tmp == false) { + return 1; } - default: - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } + return size == 0; +} + +int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, + MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + return -1; + } + auto it = interfaces_.find(code); + if (it == interfaces_.end()) { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + + auto fun = it->second; + if (fun == nullptr) { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + + int32_t result = fun(this, data, reply); + return result; } + } // namespace DeviceUsageStats } // namespace OHOS -- Gitee