From 2554dab3a7959c822d41d5e3cb722fccb885657e Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Mon, 15 May 2023 20:15:58 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=A0bundleActiveProx?= =?UTF-8?q?y=5F=E5=AF=B9=E8=B1=A1=E6=9C=AA=E5=8A=A0=E9=94=81=E5=BC=95?= =?UTF-8?q?=E8=B5=B7=E5=B9=B6=E5=8F=91=E6=B5=8B=E8=AF=95crash=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- interfaces/innerkits/src/bundle_active_client.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 483e894..24cb832 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -34,7 +34,6 @@ ErrCode BundleActiveClient::GetBundleActiveProxy() if (bundleActiveProxy_ != nullptr) { return ERR_OK; } - std::lock_guard lock(mutex_); sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!samgr) { BUNDLE_ACTIVE_LOGE("Failed to get SystemAbilityManager."); @@ -75,6 +74,7 @@ ErrCode BundleActiveClient::GetBundleActiveProxy() ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t userId) { BUNDLE_ACTIVE_LOGI("BundleActiveClient::ReportEvent called"); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -84,6 +84,7 @@ ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t u ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -94,6 +95,7 @@ ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& PackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -104,6 +106,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -115,6 +118,7 @@ ErrCode BundleActiveClient::QueryBundleEvents(std::vector& bu ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -125,6 +129,7 @@ ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t ne ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActivePackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -135,6 +140,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -144,6 +150,7 @@ ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vector lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -158,6 +165,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< BUNDLE_ACTIVE_LOGI("maxNum is illegal, maxNum is %{public}d", maxNum); return ERR_MAX_RECORDS_NUM_BIGER_THEN_ONE_THOUSAND; } + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -167,6 +175,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -180,6 +189,7 @@ ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -194,6 +204,7 @@ ErrCode BundleActiveClient::UnRegisterAppGroupCallBack(const sptr& eventStats, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -204,6 +215,7 @@ ErrCode BundleActiveClient::QueryDeviceEventStats(int64_t beginTime, int64_t end ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64_t endTime, std::vector& eventStats, int32_t userId) { + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -236,6 +248,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wp void BundleActiveClient::BundleActiveClientDeathRecipient::OnServiceDiedInner() { + std::lock_guard lock(mutex_); while (BundleActiveClient::GetInstance().GetBundleActiveProxy() != ERR_OK) { sleep(SLEEP_TIME); } -- Gitee From f2b1d0fbe08fb03c11b43f0b1b04d7e57eba432c Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Mon, 15 May 2023 21:13:25 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=A0bundleActiveProx?= =?UTF-8?q?y=5F=E5=AF=B9=E8=B1=A1=E6=9C=AA=E5=8A=A0=E9=94=81=E5=BC=95?= =?UTF-8?q?=E8=B5=B7=E5=B9=B6=E5=8F=91=E6=B5=8B=E8=AF=95crash=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- interfaces/innerkits/src/bundle_active_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 24cb832..aac7e06 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -248,7 +248,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wp void BundleActiveClient::BundleActiveClientDeathRecipient::OnServiceDiedInner() { - std::lock_guard lock(mutex_); + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); while (BundleActiveClient::GetInstance().GetBundleActiveProxy() != ERR_OK) { sleep(SLEEP_TIME); } -- Gitee From 226c187aa4cedbf3b928843f39b4e875f6634ec5 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Tue, 16 May 2023 16:45:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E6=96=B9=E4=BD=8D=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=9C=AA=E5=8A=A0=E9=94=81=E5=BC=95=E8=B5=B7=E7=9A=84?= =?UTF-8?q?crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- .../innerkits/include/bundle_active_client.h | 2 +- .../innerkits/src/bundle_active_client.cpp | 31 +++++---- .../bundle_active_bundle_mgr_helper_mock.cpp | 68 +++++++++++++++++++ 3 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index bad8e3f..1985b2b 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -221,7 +221,7 @@ private: sptr recipient_; std::shared_ptr bundleClientRunner_ {nullptr}; std::shared_ptr bundleClientHandler_ {nullptr}; - std::mutex mutex_; + std::recursive_mutex mutex_; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index aac7e06..c24711d 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -74,7 +74,7 @@ ErrCode BundleActiveClient::GetBundleActiveProxy() ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t userId) { BUNDLE_ACTIVE_LOGI("BundleActiveClient::ReportEvent called"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -84,7 +84,7 @@ ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t u ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -95,7 +95,7 @@ ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& PackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -106,7 +106,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -118,7 +118,7 @@ ErrCode BundleActiveClient::QueryBundleEvents(std::vector& bu ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -129,7 +129,7 @@ ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t ne ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActivePackageStats, const int32_t intervalType, const int64_t beginTime, const int64_t endTime) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -140,7 +140,7 @@ ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector& bundleActiveEvents, const int64_t beginTime, const int64_t endTime) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -150,7 +150,7 @@ ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vector lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -165,7 +165,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< BUNDLE_ACTIVE_LOGI("maxNum is illegal, maxNum is %{public}d", maxNum); return ERR_MAX_RECORDS_NUM_BIGER_THEN_ONE_THOUSAND; } - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -175,7 +175,7 @@ ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector< ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -189,7 +189,7 @@ ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr &observer) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -204,7 +204,7 @@ ErrCode BundleActiveClient::UnRegisterAppGroupCallBack(const sptr& eventStats, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -215,7 +215,7 @@ ErrCode BundleActiveClient::QueryDeviceEventStats(int64_t beginTime, int64_t end ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64_t endTime, std::vector& eventStats, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); ErrCode ret = GetBundleActiveProxy(); if (ret != ERR_OK) { return ret; @@ -225,6 +225,7 @@ ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64 void BundleActiveClient::BundleActiveClientDeathRecipient::AddObserver(const sptr &observer) { + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); if (observer) { observer_ = observer; } @@ -232,6 +233,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::AddObserver(const spt void BundleActiveClient::BundleActiveClientDeathRecipient::RemoveObserver() { + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); if (observer_) { observer_ = nullptr; } @@ -240,6 +242,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::RemoveObserver() void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wptr &object) { (void)object; + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); BundleActiveClient::GetInstance().bundleActiveProxy_ = nullptr; BundleActiveClient::GetInstance().bundleClientHandler_->PostTask([this]() { this->OnServiceDiedInner(); @@ -248,7 +251,7 @@ void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wp void BundleActiveClient::BundleActiveClientDeathRecipient::OnServiceDiedInner() { - std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); + std::lock_guard lock(BundleActiveClient::GetInstance().mutex_); while (BundleActiveClient::GetInstance().GetBundleActiveProxy() != ERR_OK) { sleep(SLEEP_TIME); } diff --git a/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp b/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp new file mode 100644 index 0000000..63fd4f8 --- /dev/null +++ b/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp @@ -0,0 +1,68 @@ +/* + * 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_bundle_mgr_helper.h" + +namespace OHOS { +namespace DeviceUsageStats { +/* + * Copyright (c) 2023 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_bundle_mgr_helper.h" + +#include "accesstoken_kit.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "tokenid_kit.h" + +#include "continuous_task_log.h" + +namespace OHOS { +namespace DeviceUsageStats { +void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string& bundleName) +{ + if (uid == -1) { + bundleName = "com.ohos.camera"; + } else { + bundleName = "defaultBundleName"; + } +} + +bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, + const int userId, AppExecFwk::ApplicationInfo &appInfo) +{ + +} + +bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, + AppExecFwk::BundleInfo &bundleInfo, int32_t userId) +{ + +} +} // namespace DeviceUsageStats +} // namespace OHOS +} // namespace DeviceUsageStats +} // namespace OHOS \ No newline at end of file -- Gitee From fbfd1a9496c0416d5c5f961cbbbe132f02bd5b49 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Tue, 16 May 2023 16:45:52 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=AE=BF=E9=97=AE=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=9C=AA=E5=8A=A0=E9=94=81=E5=BC=95=E8=B5=B7=E7=9A=84?= =?UTF-8?q?crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- .../bundle_active_bundle_mgr_helper_mock.cpp | 68 ------------------- 1 file changed, 68 deletions(-) delete mode 100644 test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp diff --git a/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp b/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp deleted file mode 100644 index 63fd4f8..0000000 --- a/test/unittest/mock/bundle_active_bundle_mgr_helper_mock.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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_bundle_mgr_helper.h" - -namespace OHOS { -namespace DeviceUsageStats { -/* - * Copyright (c) 2023 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_bundle_mgr_helper.h" - -#include "accesstoken_kit.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" -#include "tokenid_kit.h" - -#include "continuous_task_log.h" - -namespace OHOS { -namespace DeviceUsageStats { -void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string& bundleName) -{ - if (uid == -1) { - bundleName = "com.ohos.camera"; - } else { - bundleName = "defaultBundleName"; - } -} - -bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, - const int userId, AppExecFwk::ApplicationInfo &appInfo) -{ - -} - -bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, - AppExecFwk::BundleInfo &bundleInfo, int32_t userId) -{ - -} -} // namespace DeviceUsageStats -} // namespace OHOS -} // namespace DeviceUsageStats -} // namespace OHOS \ No newline at end of file -- Gitee