From 6aa5353fcd4b26ded6d4a41af324adf0f2de3d9d Mon Sep 17 00:00:00 2001 From: yangzk Date: Sat, 22 Jan 2022 10:29:04 +0800 Subject: [PATCH 1/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:add extension_form_profile Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: I762bde10c9dd7c29c2c0b1a15047a2bd3ae2c9f0 --- interfaces/innerkits/form_manager/include/form_js_info.h | 2 +- services/formmgr/include/form_item_info.h | 2 +- services/formmgr/include/form_record.h | 2 +- services/formmgr/src/form_mgr_adapter.cpp | 2 +- services/formmgr/src/form_mgr_service.cpp | 2 ++ services/formmgr/src/form_sys_event_receiver.cpp | 6 +++++- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/interfaces/innerkits/form_manager/include/form_js_info.h b/interfaces/innerkits/form_manager/include/form_js_info.h index a6c71cb99a0..68fe090331d 100644 --- a/interfaces/innerkits/form_manager/include/form_js_info.h +++ b/interfaces/innerkits/form_manager/include/form_js_info.h @@ -19,7 +19,7 @@ #include #include "form_provider_data.h" -#include "form_info.h" +#include "form_info_base.h" #include "parcel.h" namespace OHOS { diff --git a/services/formmgr/include/form_item_info.h b/services/formmgr/include/form_item_info.h index f40dc1e6152..bb9f85873ea 100644 --- a/services/formmgr/include/form_item_info.h +++ b/services/formmgr/include/form_item_info.h @@ -20,8 +20,8 @@ #include #include #include +#include "form_info_base.h" #include "form_record.h" -#include "form_info.h" namespace OHOS { namespace AppExecFwk { diff --git a/services/formmgr/include/form_record.h b/services/formmgr/include/form_record.h index dcb0f453df3..4e25828e41e 100644 --- a/services/formmgr/include/form_record.h +++ b/services/formmgr/include/form_record.h @@ -17,8 +17,8 @@ #define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_RECORD_H #include +#include "form_info_base.h" #include "form_provider_info.h" -#include "form_info.h" namespace OHOS { namespace AppExecFwk { diff --git a/services/formmgr/src/form_mgr_adapter.cpp b/services/formmgr/src/form_mgr_adapter.cpp index 174d8fc3d24..64ed7e2c9dc 100644 --- a/services/formmgr/src/form_mgr_adapter.cpp +++ b/services/formmgr/src/form_mgr_adapter.cpp @@ -92,7 +92,7 @@ int FormMgrAdapter::AddForm(const int64_t formId, const Want &want, return checkCode; } - // get from comfig info + // get from config info FormItemInfo formItemInfo; int32_t errCode = GetFormConfigInfo(want, formItemInfo); formItemInfo.SetFormId(formId); diff --git a/services/formmgr/src/form_mgr_service.cpp b/services/formmgr/src/form_mgr_service.cpp index 4ba2987c930..7c47f0a143e 100644 --- a/services/formmgr/src/form_mgr_service.cpp +++ b/services/formmgr/src/form_mgr_service.cpp @@ -376,6 +376,8 @@ ErrCode FormMgrService::Init() if (formSysEventReceiver_ == nullptr) { EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); diff --git a/services/formmgr/src/form_sys_event_receiver.cpp b/services/formmgr/src/form_sys_event_receiver.cpp index 7f366b30da8..0a3ff9120f1 100644 --- a/services/formmgr/src/form_sys_event_receiver.cpp +++ b/services/formmgr/src/form_sys_event_receiver.cpp @@ -58,7 +58,11 @@ void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &event return; } APP_LOGI("%{public}s, action:%{public}s.", __func__, action.c_str()); - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { + APP_LOGI("%{public}s, bundle added, bundleName: %{public}s", __func__, bundleName.c_str()); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { + APP_LOGI("%{public}s, bundle changed, bundleName: %{public}s", __func__, bundleName.c_str()); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { APP_LOGI("%{public}s, bundle removed, bundleName: %{public}s", __func__, bundleName.c_str()); HandleProviderRemoved(bundleName); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED) { -- Gitee From a7f24f4c85a14fcbb71f004c14b323979e25094e Mon Sep 17 00:00:00 2001 From: yangzk Date: Sat, 22 Jan 2022 17:58:34 +0800 Subject: [PATCH 2/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:add form_info_mgr Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: I683d650bfd12ebe2782a50c96e00e78e31e75d79 --- services/formmgr/BUILD.gn | 1 + services/formmgr/include/form_info_mgr.h | 42 ++++++ services/formmgr/src/form_info_mgr.cpp | 120 ++++++++++++++++++ .../formmgr/src/form_sys_event_receiver.cpp | 11 ++ 4 files changed, 174 insertions(+) create mode 100644 services/formmgr/include/form_info_mgr.h create mode 100644 services/formmgr/src/form_info_mgr.cpp diff --git a/services/formmgr/BUILD.gn b/services/formmgr/BUILD.gn index 19332c1f376..79d5ddc4363 100644 --- a/services/formmgr/BUILD.gn +++ b/services/formmgr/BUILD.gn @@ -54,6 +54,7 @@ ohos_shared_library("libfms") { "src/form_event_notify_connection.cpp", "src/form_host_callback.cpp", "src/form_host_record.cpp", + "src/form_info_mgr.cpp", "src/form_item_info.cpp", "src/form_mgr_adapter.cpp", "src/form_mgr_service.cpp", diff --git a/services/formmgr/include/form_info_mgr.h b/services/formmgr/include/form_info_mgr.h new file mode 100644 index 00000000000..bf3a2599836 --- /dev/null +++ b/services/formmgr/include/form_info_mgr.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_MGR_H +#define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_MGR_H + +#include + +#include "appexecfwk_errors.h" +#include "bundle_info.h" +#include "form_info.h" + +namespace OHOS { +namespace AppExecFwk { +class FormInfoMgr final : public DelayedRefSingleton { +DECLARE_DELAYED_REF_SINGLETON(FormInfoMgr) + +public: + DISALLOW_COPY_AND_MOVE(FormInfoMgr); + + static ErrCode LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos); + +private: + static ErrCode LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); + + static ErrCode LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_MGR_H diff --git a/services/formmgr/src/form_info_mgr.cpp b/services/formmgr/src/form_info_mgr.cpp new file mode 100644 index 00000000000..cbce1ba2079 --- /dev/null +++ b/services/formmgr/src/form_info_mgr.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 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 "form_info_mgr.h" + +#include "app_log_wrapper.h" +#include "bundle_mgr_client.h" +#include "extension_form_profile.h" +#include "form_bms_helper.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +const std::string FORM_METADATA_NAME = "ohos.extension.form"; +} // namespace + +ErrCode FormInfoMgr::LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is invalid"); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + std::shared_ptr client = DelayedSingleton::GetInstance(); + if (client == nullptr) { + APP_LOGE("failed to get BundleMgrClient."); + return ERR_APPEXECFWK_FORM_GET_BMS_FAILED; + } + + BundleInfo bundleInfo; + if (!client->GetBundleInfo(bundleName, GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo)) { + APP_LOGE("failed to get bundle info."); + return ERR_APPEXECFWK_FORM_GET_INFO_FAILED; + } + + ErrCode errCode = LoadAbilityFormConfigInfo(bundleInfo, formInfos); + if (errCode != ERR_OK) { + APP_LOGE("failed to load FA form config info, error code=%{public}d.", errCode); + } + + errCode = LoadStageFormConfigInfo(bundleInfo, formInfos); + if (errCode != ERR_OK) { + APP_LOGE("failed to load stage form config info, error code=%{public}d.", errCode); + } + + return ERR_OK; +} + +ErrCode FormInfoMgr::LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) +{ + std::shared_ptr client = DelayedSingleton::GetInstance(); + if (client == nullptr) { + APP_LOGE("failed to get BundleMgrClient."); + return ERR_APPEXECFWK_FORM_GET_BMS_FAILED; + } + + for (auto const &extensionInfo: bundleInfo.extensionInfos) { + if (extensionInfo.type != ExtensionAbilityType::FORM) { + continue; + } + + std::vector profileInfos{}; + if (!client->GetResConfigFile(extensionInfo, FORM_METADATA_NAME, profileInfos)) { + APP_LOGE("failed to get form metadata."); + continue; + } + + for (const auto &profileInfo: profileInfos) { + std::vector extensionFormInfos; + ErrCode errCode = ExtensionFormProfile::TransformTo(profileInfo, extensionFormInfos); + if (errCode != ERR_OK) { + APP_LOGW("failed to transform profile to extension form info"); + continue; + } + for (const auto &extensionFormInfo: extensionFormInfos) { + formInfos.emplace_back(extensionInfo, extensionFormInfo); + } + } + } + return ERR_OK; +} + +ErrCode FormInfoMgr::LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) +{ + sptr iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("failed to get IBundleMgr."); + return ERR_APPEXECFWK_FORM_GET_BMS_FAILED; + } + + const std::string &bundleName = bundleInfo.name; + for (const auto &modelInfo: bundleInfo.hapModuleInfos) { + const std::string &moduleName = modelInfo.moduleName; + std::vector formInfoVec{}; + if (!iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfoVec)) { + continue; + } + for (const auto &formInfo: formInfoVec) { + // check form info + formInfos.push_back(formInfo); + } + } + + return ERR_OK; +} + +} // namespace AppExecFwk +} // namespace OHOS diff --git a/services/formmgr/src/form_sys_event_receiver.cpp b/services/formmgr/src/form_sys_event_receiver.cpp index 0a3ff9120f1..2ca2640ecfb 100644 --- a/services/formmgr/src/form_sys_event_receiver.cpp +++ b/services/formmgr/src/form_sys_event_receiver.cpp @@ -26,6 +26,7 @@ #include "form_data_mgr.h" #include "form_db_cache.h" #include "form_db_info.h" +#include "form_info_mgr.h" #include "form_provider_mgr.h" #include "form_sys_event_receiver.h" #include "form_timer_mgr.h" @@ -60,8 +61,18 @@ void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &event APP_LOGI("%{public}s, action:%{public}s.", __func__, action.c_str()); if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { APP_LOGI("%{public}s, bundle added, bundleName: %{public}s", __func__, bundleName.c_str()); + std::vector formInfos {}; + FormInfoMgr::LoadFormConfigInfoByBundleName(bundleName, formInfos); + for (const auto& formInfo : formInfos) { + APP_LOGE("y00424102 form name=%{public}s", formInfo.name.c_str()); + } } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { APP_LOGI("%{public}s, bundle changed, bundleName: %{public}s", __func__, bundleName.c_str()); + std::vector formInfos {}; + FormInfoMgr::LoadFormConfigInfoByBundleName(bundleName, formInfos); + for (const auto& formInfo : formInfos) { + APP_LOGE("y00424102 form name=%{public}s", formInfo.name.c_str()); + } } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { APP_LOGI("%{public}s, bundle removed, bundleName: %{public}s", __func__, bundleName.c_str()); HandleProviderRemoved(bundleName); -- Gitee From 472d835270dca2c0708023f28cca9f092ea14dca Mon Sep 17 00:00:00 2001 From: yangzk Date: Sat, 22 Jan 2022 22:01:19 +0800 Subject: [PATCH 3/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:add test ExtensionFormProfileTest Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: I226090c8c080888933d3afbb1c07e6be5507c292 --- services/formmgr/test/BUILD.gn | 1 + .../unittest/fms_form_info_mgr_test/BUILD.gn | 84 +++++++++ .../fms_form_info_mgr_test.cpp | 176 ++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 services/formmgr/test/unittest/fms_form_info_mgr_test/BUILD.gn create mode 100644 services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp diff --git a/services/formmgr/test/BUILD.gn b/services/formmgr/test/BUILD.gn index ae998e463c5..8380a15c303 100644 --- a/services/formmgr/test/BUILD.gn +++ b/services/formmgr/test/BUILD.gn @@ -46,6 +46,7 @@ group("unittest") { "unittest/fms_form_data_mgr_test:unittest", "unittest/fms_form_db_record_test:unittest", "unittest/fms_form_host_record_test:unittest", + "unittest/fms_form_info_mgr_test:unittest", "unittest/fms_form_mgr_add_form_test:unittest", "unittest/fms_form_mgr_cast_temp_form_test:unittest", "unittest/fms_form_mgr_death_callback_test:unittest", diff --git a/services/formmgr/test/unittest/fms_form_info_mgr_test/BUILD.gn b/services/formmgr/test/unittest/fms_form_info_mgr_test/BUILD.gn new file mode 100644 index 00000000000..09711d62e82 --- /dev/null +++ b/services/formmgr/test/unittest/fms_form_info_mgr_test/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2021 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. + +import("//build/test.gni") +import("//foundation/aafwk/standard/aafwk.gni") + +module_output_path = "form_runtime/formmgrservice" + +ohos_unittest("FmsFormInfoMgrTest") { + module_out_path = module_output_path + + sources = [ + "//foundation/aafwk/standard/frameworks/kits/fmskit/native/src/form_mgr.cpp", + "//foundation/aafwk/standard/services/formmgr/test/mock/src/mock_bundle_manager.cpp", + "//foundation/aafwk/standard/services/formmgr/test/mock/src/mock_form_host_client.cpp", + "//foundation/aafwk/standard/services/formmgr/test/mock/src/mock_form_provider_client.cpp", + ] + sources += [ "fms_form_info_mgr_test.cpp" ] + + include_dirs = [ + "//third_party/zlib/contrib/minizip", + "//third_party/zlib", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//foundation/aafwk/standard/frameworks/kits/fmskit/native/include", + "//foundation/appexecfwk/standard/common/log/include/", + "//foundation/aafwk/standard/services/formmgr/include", + "//foundation/appexecfwk/standard/services/bundlemgr/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include/", + "//foundation/aafwk/standard/interfaces/innerkits/form_manager/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include/", + "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include/", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", + ] + + configs = [ + "${services_path}/formmgr/test:formmgr_test_config", + "//foundation/aafwk/standard/services/abilitymgr:abilityms_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${aafwk_path}/frameworks/kits/fmskit:fmskit_native", + "${appexecfwk_path}/common:libappexecfwk_common", + "${appexecfwk_path}/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "${appexecfwk_path}/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "${appexecfwk_path}/libs/libeventhandler:libeventhandler_target", + "${services_path}/formmgr:fms_target", + "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard", + "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms_target", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "appspawn:appspawn_socket_client", + "form_runtime:form_manager", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":FmsFormInfoMgrTest" ] +} diff --git a/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp b/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp new file mode 100644 index 00000000000..24c18cd2a78 --- /dev/null +++ b/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2021 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 + +#include "extension_form_profile.h" +#include "nlohmann/json.hpp" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AppExecFwk; + +namespace { +const std::string JSON_NONE; +const std::string JSON_EMPTY = "{}"; +const nlohmann::json JSON_BAD_FORMS = R"({"forms":""})"_json; +const nlohmann::json JSON_EMPTY_FORMS = R"({"forms":[]})"_json; +const nlohmann::json JSON_FORMS = R"( +{ + "forms": [{ + "name": "Form_001", + "description": "$string:form_description", + "src": "./js/pages/card/index", + "window": { + "designWidth": 720, + "autoDesignWidth": true + }, + "colorMode": "auto", + "formConfigAbility": "ability://form_ability_001", + "formVisibleNotify": false, + "isDefault": true, + "updateEnabled": true, + "scheduledUpdateTime": "10:30", + "updateDuration": 1, + "defaultDimension": "2*2", + "supportDimensions": ["2*2"], + "metadata": [{ + "name": "string", + "value": "string" + } + ] + }, { + "name": "Form_002", + "formConfigAbility": "ability://form_ability_002", + "isDefault": true, + "updateEnabled": true, + "defaultDimension": "2*2", + "supportDimensions": ["2*2"] + } + ] +})"_json; +const int32_t JSON_FORMS_NUM = 2; + +std::string JsonToString(const nlohmann::json &json) +{ + std::ostringstream stream; + stream << json.dump(); + return stream.str(); +} + +HWTEST(ExtensionFormProfileTest, TransformTo_0100, TestSize.Level0) +{ + std::vector extensionFormInfos{}; + // none profile + EXPECT_EQ(ERR_APPEXECFWK_PARSE_BAD_PROFILE, ExtensionFormProfile::TransformTo(JSON_NONE, extensionFormInfos)); + EXPECT_EQ(0, extensionFormInfos.size()); + // empty profile + EXPECT_EQ(ERR_OK, ExtensionFormProfile::TransformTo(JSON_EMPTY, extensionFormInfos)); + EXPECT_EQ(0, extensionFormInfos.size()); + // bad forms profile + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR, + ExtensionFormProfile::TransformTo(JsonToString(JSON_BAD_FORMS), extensionFormInfos)); + EXPECT_EQ(0, extensionFormInfos.size()); + // empty forms profile + EXPECT_EQ(ERR_OK, ExtensionFormProfile::TransformTo(JsonToString(JSON_EMPTY_FORMS), extensionFormInfos)); + EXPECT_EQ(0, extensionFormInfos.size()); + // forms ok profile + EXPECT_EQ(ERR_OK, ExtensionFormProfile::TransformTo(JsonToString(JSON_FORMS), extensionFormInfos)); + EXPECT_EQ(JSON_FORMS_NUM, extensionFormInfos.size()); +} + +HWTEST(ExtensionFormProfileTest, TransformTo_0101, TestSize.Level0) +{ + std::vector extensionFormInfos{}; + int32_t numForms = 0; + + std::vector notMustPropKeys = { + ExtensionFormProfileReader::DESCRIPTION, + ExtensionFormProfileReader::SRC, + ExtensionFormProfileReader::WINDOW, + ExtensionFormProfileReader::COLOR_MODE, + ExtensionFormProfileReader::FORM_VISIBLE_NOTIFY, + ExtensionFormProfileReader::SCHEDULED_UPDATE_TIME, + ExtensionFormProfileReader::UPDATE_DURATION, + ExtensionFormProfileReader::METADATA + }; + for (const auto &prop: notMustPropKeys) { + nlohmann::json formsJson = JSON_FORMS; + formsJson[ExtensionFormProfileReader::FORMS][0].erase(prop); + numForms += JSON_FORMS_NUM; + EXPECT_EQ(ERR_OK, ExtensionFormProfile::TransformTo(JsonToString(formsJson), extensionFormInfos)); + EXPECT_EQ(numForms, extensionFormInfos.size()); + } + + std::vector mustPropKeys = { + ExtensionFormProfileReader::NAME, + ExtensionFormProfileReader::FORM_CONFIG_ABILITY, + ExtensionFormProfileReader::IS_DEFAULT, + ExtensionFormProfileReader::UPDATE_ENABLED, + ExtensionFormProfileReader::DEFAULT_DIMENSION, + ExtensionFormProfileReader::SUPPORT_DIMENSIONS + }; + for (const auto &prop: mustPropKeys) { + nlohmann::json errorJson = JSON_FORMS; + errorJson[ExtensionFormProfileReader::FORMS][0].erase(prop); + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP, + ExtensionFormProfile::TransformTo(JsonToString(errorJson), extensionFormInfos)); + EXPECT_EQ(numForms, extensionFormInfos.size()); + } +} + +HWTEST(ExtensionFormProfileTest, TransformTo_0102, TestSize.Level0) +{ + std::vector extensionFormInfos{}; + int32_t numForms = 0; + + // name + nlohmann::json errorJson = JSON_FORMS; + errorJson[ExtensionFormProfileReader::FORMS][0][ExtensionFormProfileReader::NAME] = ""; + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR, + ExtensionFormProfile::TransformTo(JsonToString(errorJson), extensionFormInfos)); + EXPECT_EQ(numForms, extensionFormInfos.size()); + // max name size + const int32_t maxFormName = 128; + std::string maxName(maxFormName, 'a'); + errorJson[ExtensionFormProfileReader::FORMS][0][ExtensionFormProfileReader::NAME] = maxName; + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR, + ExtensionFormProfile::TransformTo(JsonToString(errorJson), extensionFormInfos)); + EXPECT_EQ(numForms, extensionFormInfos.size()); +} + +HWTEST(ExtensionFormProfileTest, TransformTo_0103, TestSize.Level0) +{ + std::vector extensionFormInfos{}; + int32_t numForms = 0; + + // dimension + nlohmann::json errorJson = JSON_FORMS; + const std::string invalidDimension = "1*4"; + errorJson[ExtensionFormProfileReader::FORMS][0][ExtensionFormProfileReader::DEFAULT_DIMENSION] = invalidDimension; + errorJson[ExtensionFormProfileReader::FORMS][0][ExtensionFormProfileReader::SUPPORT_DIMENSIONS].push_back( + invalidDimension); + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR, + ExtensionFormProfile::TransformTo(JsonToString(errorJson), extensionFormInfos)); + EXPECT_EQ(numForms, extensionFormInfos.size()); + + // not in support + errorJson = JSON_FORMS; + const std::string validDimension = "1*2"; + errorJson[ExtensionFormProfileReader::FORMS][0][ExtensionFormProfileReader::DEFAULT_DIMENSION] = validDimension; + EXPECT_EQ(ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR, + ExtensionFormProfile::TransformTo(JsonToString(errorJson), extensionFormInfos)); +} +} // namespace -- Gitee From 682bf435d4c0eea739fe25e640a83700da397ca1 Mon Sep 17 00:00:00 2001 From: yangzk Date: Sun, 23 Jan 2022 18:29:39 +0800 Subject: [PATCH 4/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:add BundleFormInfo Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: If2c7b736473ce18bed2b2c2f82703adc108d4965 --- services/formmgr/include/form_info_mgr.h | 48 ++++- .../formmgr/include/form_sys_event_receiver.h | 2 + services/formmgr/src/form_info_mgr.cpp | 182 +++++++++++++++++- .../formmgr/src/form_sys_event_receiver.cpp | 26 +-- 4 files changed, 239 insertions(+), 19 deletions(-) diff --git a/services/formmgr/include/form_info_mgr.h b/services/formmgr/include/form_info_mgr.h index bf3a2599836..a9fbce5096c 100644 --- a/services/formmgr/include/form_info_mgr.h +++ b/services/formmgr/include/form_info_mgr.h @@ -16,7 +16,9 @@ #ifndef FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_MGR_H #define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_MGR_H +#include #include +#include #include "appexecfwk_errors.h" #include "bundle_info.h" @@ -24,18 +26,58 @@ namespace OHOS { namespace AppExecFwk { +class FormInfoHelper { +public: + static ErrCode LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos); + +private: + static ErrCode LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); + + static ErrCode LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); +}; + +class BundleFormInfo { +public: + explicit BundleFormInfo(std::string bundleName); + + ErrCode Update(); + + ErrCode Remove(); + + bool Empty(); + + ErrCode GetAllFormsInfo(std::vector &formInfos); + + ErrCode GetFormsInfoByModule(const std::string &moduleName, std::vector &formInfos); + +private: + std::string bundleName_ {}; + mutable std::shared_timed_mutex formInfosMutex_ {}; + std::vector formInfos_ {}; +}; + class FormInfoMgr final : public DelayedRefSingleton { DECLARE_DELAYED_REF_SINGLETON(FormInfoMgr) public: DISALLOW_COPY_AND_MOVE(FormInfoMgr); - static ErrCode LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos); + ErrCode Update(const std::string &bundleName); + + ErrCode Remove(const std::string &bundleName); + + ErrCode GetAllFormsInfo(std::vector &formInfos); + + ErrCode GetFormsInfoByBundle(const std::string &bundleName, std::vector &formInfos); + + ErrCode GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName, + std::vector &formInfos); private: - static ErrCode LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); + std::shared_ptr GetOrCreateBundleFromInfo(const std::string &bundleName); - static ErrCode LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos); + mutable std::shared_timed_mutex bundleFormInfoMapMutex_ {}; + std::unordered_map> bundleFormInfoMap_ {}; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/include/form_sys_event_receiver.h b/services/formmgr/include/form_sys_event_receiver.h index e6ccac514c2..b1ba1aa01cc 100644 --- a/services/formmgr/include/form_sys_event_receiver.h +++ b/services/formmgr/include/form_sys_event_receiver.h @@ -43,6 +43,8 @@ private: void HandleProviderUpdated(const std::string &bundleName); bool ProviderFormUpdated(const int64_t formId, const FormRecord &formRecord, const std::vector &targetForms); + void HandleBundleFormInfoChanged(const std::string &bundleName); + void HandleBundleFormInfoRemoved(const std::string &bundleName); void HandleProviderRemoved(const std::string &bundleName); void HandleBundleDataCleared(const std::string &bundleName, const int uid); void HandleFormHostDataCleared(const int uid); diff --git a/services/formmgr/src/form_info_mgr.cpp b/services/formmgr/src/form_info_mgr.cpp index cbce1ba2079..17eb9d12456 100644 --- a/services/formmgr/src/form_info_mgr.cpp +++ b/services/formmgr/src/form_info_mgr.cpp @@ -15,6 +15,8 @@ #include "form_info_mgr.h" +#include + #include "app_log_wrapper.h" #include "bundle_mgr_client.h" #include "extension_form_profile.h" @@ -26,7 +28,7 @@ namespace { const std::string FORM_METADATA_NAME = "ohos.extension.form"; } // namespace -ErrCode FormInfoMgr::LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos) +ErrCode FormInfoHelper::LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector &formInfos) { if (bundleName.empty()) { APP_LOGE("bundleName is invalid"); @@ -58,7 +60,7 @@ ErrCode FormInfoMgr::LoadFormConfigInfoByBundleName(const std::string &bundleNam return ERR_OK; } -ErrCode FormInfoMgr::LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) +ErrCode FormInfoHelper::LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) { std::shared_ptr client = DelayedSingleton::GetInstance(); if (client == nullptr) { @@ -71,7 +73,7 @@ ErrCode FormInfoMgr::LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std:: continue; } - std::vector profileInfos{}; + std::vector profileInfos {}; if (!client->GetResConfigFile(extensionInfo, FORM_METADATA_NAME, profileInfos)) { APP_LOGE("failed to get form metadata."); continue; @@ -92,7 +94,7 @@ ErrCode FormInfoMgr::LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std:: return ERR_OK; } -ErrCode FormInfoMgr::LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) +ErrCode FormInfoHelper::LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector &formInfos) { sptr iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr(); if (iBundleMgr == nullptr) { @@ -103,7 +105,7 @@ ErrCode FormInfoMgr::LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std const std::string &bundleName = bundleInfo.name; for (const auto &modelInfo: bundleInfo.hapModuleInfos) { const std::string &moduleName = modelInfo.moduleName; - std::vector formInfoVec{}; + std::vector formInfoVec {}; if (!iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfoVec)) { continue; } @@ -116,5 +118,175 @@ ErrCode FormInfoMgr::LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std return ERR_OK; } +BundleFormInfo::BundleFormInfo(std::string bundleName) : bundleName_(std::move(bundleName)) +{ +} + +ErrCode BundleFormInfo::Update() +{ + std::unique_lock guard(formInfosMutex_); + ErrCode errCode = FormInfoHelper::LoadFormConfigInfoByBundleName(bundleName_, formInfos_); + return errCode; +} + +ErrCode BundleFormInfo::Remove() +{ + std::unique_lock guard(formInfosMutex_); + formInfos_.clear(); + return ERR_OK; +} + +bool BundleFormInfo::Empty() +{ + std::shared_lock guard(formInfosMutex_); + return formInfos_.empty(); +} + +ErrCode BundleFormInfo::GetAllFormsInfo(std::vector &formInfos) +{ + std::shared_lock guard(formInfosMutex_); + for (const auto &formInfo : formInfos_) { + formInfos.push_back(formInfo); + } + return ERR_OK; +} + +ErrCode BundleFormInfo::GetFormsInfoByModule(const std::string &moduleName, std::vector &formInfos) +{ + std::shared_lock guard(formInfosMutex_); + for (const auto &formInfo : formInfos_) { + if (formInfo.moduleName == moduleName) { + formInfos.push_back(formInfo); + } + } + return ERR_OK; +} + +FormInfoMgr::FormInfoMgr() = default; + +FormInfoMgr::~FormInfoMgr() = default; + +ErrCode FormInfoMgr::Update(const std::string &bundleName) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + auto bundleFormInfoPtr = std::make_shared(bundleName); + ErrCode errCode = bundleFormInfoPtr->Update(); + if (errCode != ERR_OK) { + return errCode; + } + + if (bundleFormInfoPtr->Empty()) { + // no forms found, no need to be inserted into the map + return ERR_OK; + } + + std::unique_lock guard(bundleFormInfoMapMutex_); + bundleFormInfoMap_[bundleName] = bundleFormInfoPtr; + APP_LOGE("update forms info success, bundleName=%{public}s.", bundleName.c_str()); + return ERR_OK; +} + +ErrCode FormInfoMgr::Remove(const std::string &bundleName) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + std::unique_lock guard(bundleFormInfoMapMutex_); + auto bundleFormInfoIter = bundleFormInfoMap_.find(bundleName); + if (bundleFormInfoIter == bundleFormInfoMap_.end()) { + // BundleFormInfo not found, no need to remove + return ERR_OK; + } + + ErrCode errCode = ERR_OK; + if (bundleFormInfoIter->second != nullptr) { + errCode = bundleFormInfoIter->second->Remove(); + } + bundleFormInfoMap_.erase(bundleFormInfoIter); + APP_LOGE("remove forms info success, bundleName=%{public}s.", bundleName.c_str()); + return errCode; +} + +ErrCode FormInfoMgr::GetAllFormsInfo(std::vector &formInfos) +{ + std::shared_lock guard(bundleFormInfoMapMutex_); + for (const auto &bundleFormInfo: bundleFormInfoMap_) { + if (bundleFormInfo.second != nullptr) { + bundleFormInfo.second->GetAllFormsInfo(formInfos); + } + } + return ERR_OK; +} + +ErrCode FormInfoMgr::GetFormsInfoByBundle(const std::string &bundleName, std::vector &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + std::shared_lock guard(bundleFormInfoMapMutex_); + auto bundleFormInfoIter = bundleFormInfoMap_.find(bundleName); + if (bundleFormInfoIter == bundleFormInfoMap_.end()) { + APP_LOGE("no forms found."); + return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED; + } + + if (bundleFormInfoIter->second != nullptr) { + bundleFormInfoIter->second->GetAllFormsInfo(formInfos); + } + return ERR_OK; +} + +ErrCode FormInfoMgr::GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName, + std::vector &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + std::shared_lock guard(bundleFormInfoMapMutex_); + auto bundleFormInfoIter = bundleFormInfoMap_.find(bundleName); + if (bundleFormInfoIter == bundleFormInfoMap_.end()) { + APP_LOGE("no forms found."); + return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED; + } + + if (bundleFormInfoIter->second != nullptr) { + bundleFormInfoIter->second->GetFormsInfoByModule(moduleName, formInfos); + } + return ERR_OK; +} + +std::shared_ptr FormInfoMgr::GetOrCreateBundleFromInfo(const std::string &bundleName) +{ + { + std::shared_lock guard(bundleFormInfoMapMutex_); + auto bundleFormInfoIter = bundleFormInfoMap_.find(bundleName); + if (bundleFormInfoIter != bundleFormInfoMap_.end()) { + // found + return bundleFormInfoIter->second; + } + } + + // not found + std::unique_lock guard(bundleFormInfoMapMutex_); + auto bundleFormInfoIter = bundleFormInfoMap_.find(bundleName); + // try to find again + if (bundleFormInfoIter != bundleFormInfoMap_.end()) { + // found + return bundleFormInfoIter->second; + } + auto bundleFormInfoPtr = std::make_shared(bundleName); + bundleFormInfoMap_[bundleName] = bundleFormInfoPtr; + return bundleFormInfoPtr; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/src/form_sys_event_receiver.cpp b/services/formmgr/src/form_sys_event_receiver.cpp index 2ca2640ecfb..9022a76ed83 100644 --- a/services/formmgr/src/form_sys_event_receiver.cpp +++ b/services/formmgr/src/form_sys_event_receiver.cpp @@ -50,7 +50,7 @@ FormSysEventReceiver::FormSysEventReceiver(const EventFwk::CommonEventSubscribeI */ void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &eventData) { - AAFwk::Want want = eventData.GetWant(); + const AAFwk::Want& want = eventData.GetWant(); std::string action = want.GetAction(); std::string bundleName = want.GetElement().GetBundleName(); if (action.empty() || bundleName.empty()) { @@ -61,21 +61,14 @@ void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &event APP_LOGI("%{public}s, action:%{public}s.", __func__, action.c_str()); if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { APP_LOGI("%{public}s, bundle added, bundleName: %{public}s", __func__, bundleName.c_str()); - std::vector formInfos {}; - FormInfoMgr::LoadFormConfigInfoByBundleName(bundleName, formInfos); - for (const auto& formInfo : formInfos) { - APP_LOGE("y00424102 form name=%{public}s", formInfo.name.c_str()); - } + HandleBundleFormInfoChanged(bundleName); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { APP_LOGI("%{public}s, bundle changed, bundleName: %{public}s", __func__, bundleName.c_str()); - std::vector formInfos {}; - FormInfoMgr::LoadFormConfigInfoByBundleName(bundleName, formInfos); - for (const auto& formInfo : formInfos) { - APP_LOGE("y00424102 form name=%{public}s", formInfo.name.c_str()); - } + HandleBundleFormInfoChanged(bundleName); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { APP_LOGI("%{public}s, bundle removed, bundleName: %{public}s", __func__, bundleName.c_str()); HandleProviderRemoved(bundleName); + HandleBundleFormInfoRemoved(bundleName); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED) { APP_LOGI("%{public}s, bundle updated, bundleName: %{public}s", __func__, bundleName.c_str()); HandleProviderUpdated(bundleName); @@ -220,6 +213,17 @@ bool FormSysEventReceiver::ProviderFormUpdated(const int64_t formId, APP_LOGI("%{public}s, no updated form.", __func__); return false; } + +void FormSysEventReceiver::HandleBundleFormInfoChanged(const std::string &bundleName) +{ + FormInfoMgr::GetInstance().Update(bundleName); +} + +void FormSysEventReceiver::HandleBundleFormInfoRemoved(const std::string &bundleName) +{ + FormInfoMgr::GetInstance().Remove(bundleName); +} + void FormSysEventReceiver::HandleBundleDataCleared(const std::string &bundleName, const int uid) { APP_LOGD("%{public}s, bundleName:%{public}s, uid:%{public}d", __func__, bundleName.c_str(), uid); -- Gitee From e3e0dd7a5d678d6b26358a18406d832fce5b2647 Mon Sep 17 00:00:00 2001 From: yangzk Date: Mon, 24 Jan 2022 10:56:39 +0800 Subject: [PATCH 5/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:get form info form FMS Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: I8f923fb0fc2467cdba1541c59f01f1adb3f4af94 --- .../kits/ability/native/src/ability.cpp | 54 ++++++---- .../kits/fmskit/native/include/form_mgr.h | 26 +++++ .../kits/fmskit/native/src/form_mgr.cpp | 55 +++++++++++ .../form_manager/include/form_mgr_interface.h | 29 ++++++ .../form_manager/include/form_mgr_proxy.h | 27 +++++ .../form_manager/include/form_mgr_stub.h | 30 ++++++ .../form_manager/src/form_mgr_proxy.cpp | 99 +++++++++++++++++++ .../form_manager/src/form_mgr_stub.cpp | 96 ++++++++++++++++++ services/formmgr/include/form_mgr_adapter.h | 24 +++++ services/formmgr/include/form_mgr_service.h | 26 +++++ services/formmgr/src/form_mgr_adapter.cpp | 34 +++++++ services/formmgr/src/form_mgr_service.cpp | 37 +++++++ .../formmgr/src/form_sys_event_receiver.cpp | 14 +++ 13 files changed, 530 insertions(+), 21 deletions(-) diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 6cefe852b6e..d8f31af772b 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -2728,16 +2728,20 @@ bool Ability::CheckFMSReady() bool Ability::GetAllFormsInfo(std::vector &formInfos) { APP_LOGI("%{public}s called.", __func__); - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); return false; } - bool isHaveFormsInfo = iBundleMgr->GetAllFormsInfo(formInfos); - if (formInfos.size() == 0 || formInfos.empty()) { - isHaveFormsInfo = false; + + // GetAllFormsInfo request to fms + int resultCode = FormMgr::GetInstance().GetAllFormsInfo(formInfos); + if (resultCode != ERR_OK) { + APP_LOGE("%{public}s error, failed to GetAllFormsInfo, error code is %{public}d.", __func__, resultCode); + return false; } - return isHaveFormsInfo; + + return formInfos.empty(); } /** @@ -2754,16 +2758,20 @@ bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector & APP_LOGW("Failed to Get forms info, because empty bundle name"); return false; } - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); return false; } - bool isHaveFormsInfo = iBundleMgr->GetFormsInfoByApp(bundleName, formInfos); - if (formInfos.size() == 0 || formInfos.empty()) { - isHaveFormsInfo = false; + + // GetFormsInfoByApp request to fms + int resultCode = FormMgr::GetInstance().GetFormsInfoByApp(bundleName, formInfos); + if (resultCode != ERR_OK) { + APP_LOGE("%{public}s error, failed to GetFormsInfoByApp, error code is %{public}d.", __func__, resultCode); + return false; } - return isHaveFormsInfo; + + return formInfos.empty(); } /** @@ -2781,16 +2789,20 @@ bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleN APP_LOGW("Failed to Get forms info, because empty bundleName or moduleName"); return false; } - sptr iBundleMgr = GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + + if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { + APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); return false; } - bool isHaveFormsInfo = iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos); - if (formInfos.size() == 0 || formInfos.empty()) { - isHaveFormsInfo = false; + + // GetFormsInfoByModule request to fms + int resultCode = FormMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); + if (resultCode != ERR_OK) { + APP_LOGE("%{public}s error, failed to GetFormsInfoByModule, error code is %{public}d.", __func__, resultCode); + return false; } - return isHaveFormsInfo; + + return formInfos.empty(); } /** diff --git a/frameworks/kits/fmskit/native/include/form_mgr.h b/frameworks/kits/fmskit/native/include/form_mgr.h index 99f384a66cc..1c961be477f 100644 --- a/frameworks/kits/fmskit/native/include/form_mgr.h +++ b/frameworks/kits/fmskit/native/include/form_mgr.h @@ -22,6 +22,7 @@ #include "form_callback_interface.h" #include "form_constants.h" #include "form_death_callback.h" +#include "form_info.h" #include "form_js_info.h" #include "form_mgr_interface.h" #include "form_provider_data.h" @@ -231,6 +232,31 @@ public: * @return Returns ERR_OK on success, others on failure. */ int DistributedDataDeleteForm(const std::string &formId); + + /** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ + int GetAllFormsInfo(std::vector &formInfos); + + /** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos); + + /** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos); + private: /** * @brief Connect form manager service. diff --git a/frameworks/kits/fmskit/native/src/form_mgr.cpp b/frameworks/kits/fmskit/native/src/form_mgr.cpp index ae889405db9..f08663efeca 100644 --- a/frameworks/kits/fmskit/native/src/form_mgr.cpp +++ b/frameworks/kits/fmskit/native/src/form_mgr.cpp @@ -553,5 +553,60 @@ int FormMgr::DistributedDataDeleteForm(const std::string &formId) return remoteProxy_->DistributedDataDeleteForm(formId); } + +/** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgr::GetAllFormsInfo(std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + + int errCode = Connect(); + if (errCode != ERR_OK) { + return errCode; + } + + return remoteProxy_->GetAllFormsInfo(formInfos); +} + +/** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + + int errCode = Connect(); + if (errCode != ERR_OK) { + return errCode; + } + + return remoteProxy_->GetFormsInfoByApp(bundleName, formInfos); +} + +/** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + + int errCode = Connect(); + if (errCode != ERR_OK) { + return errCode; + } + + return remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos); +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/form_manager/include/form_mgr_interface.h b/interfaces/innerkits/form_manager/include/form_mgr_interface.h index a71ffc246fa..f48099b5342 100644 --- a/interfaces/innerkits/form_manager/include/form_mgr_interface.h +++ b/interfaces/innerkits/form_manager/include/form_mgr_interface.h @@ -17,6 +17,7 @@ #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_FORMMGR_FORM_MGR_INTERFACE_H #include +#include "form_info.h" #include "form_js_info.h" #include "form_provider_data.h" #include "ipc_types.h" @@ -181,6 +182,31 @@ public: */ virtual int DistributedDataDeleteForm(const std::string &formId) = 0; + /** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetAllFormsInfo(std::vector &formInfos) = 0; + + /** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) = 0; + + /** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) = 0; + enum class Message { // ipc id 1-1000 for kit // ipc id 1001-2000 for DMS @@ -209,6 +235,9 @@ public: FORM_MGR_CLEAR_FORM_RECORDS_ST, FORM_MGR_DISTRIBUTED_DATA_ADD_FORM__ST, FORM_MGR_DISTRIBUTED_DATA_DELETE_FORM__ST, + FORM_MGR_GET_ALL_FORMS_INFO, + FORM_MGR_GET_FORMS_INFO_BY_APP, + FORM_MGR_GET_FORMS_INFO_BY_MODULE, }; }; } // namespace AppExecFwk diff --git a/interfaces/innerkits/form_manager/include/form_mgr_proxy.h b/interfaces/innerkits/form_manager/include/form_mgr_proxy.h index 7d4f9e07db7..f4e48460c2c 100644 --- a/interfaces/innerkits/form_manager/include/form_mgr_proxy.h +++ b/interfaces/innerkits/form_manager/include/form_mgr_proxy.h @@ -17,6 +17,7 @@ #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_FORMMGR_FORM_MGR_PROXY_H #include "form_db_info.h" +#include "form_info.h" #include "form_mgr_interface.h" #include "iremote_proxy.h" @@ -174,6 +175,31 @@ public: * @return Returns ERR_OK on success, others on failure. */ virtual int DistributedDataDeleteForm(const std::string &formId) override; + + /** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetAllFormsInfo(std::vector &formInfos) override; + + /** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) override; + + /** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) override; private: template int GetParcelableInfos(MessageParcel &reply, std::vector &parcelableInfos); @@ -182,6 +208,7 @@ private: int GetParcelableInfo(IFormMgr::Message code, MessageParcel &data, T &parcelableInfo); int SendTransactCmd(IFormMgr::Message code, MessageParcel &data, MessageParcel &reply); int GetStringInfo(IFormMgr::Message code, MessageParcel &data, std::string &stringInfo); + int GetFormsInfo(IFormMgr::Message code, MessageParcel &data, std::vector &formInfos); private: static inline BrokerDelegator delegator_; }; diff --git a/interfaces/innerkits/form_manager/include/form_mgr_stub.h b/interfaces/innerkits/form_manager/include/form_mgr_stub.h index 30e369adf19..f2f7bca7e9b 100644 --- a/interfaces/innerkits/form_manager/include/form_mgr_stub.h +++ b/interfaces/innerkits/form_manager/include/form_mgr_stub.h @@ -171,11 +171,41 @@ private: * @return Returns ERR_OK on success, others on failure. */ int32_t HandleDistributedDataDeleteForm(MessageParcel &data, MessageParcel &reply); + /** + * @brief Handle GetAllFormsInfo message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t HandleGetAllFormsInfo(MessageParcel &data, MessageParcel &reply); + /** + * @brief Handle GetFormsInfoByApp message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t HandleGetFormsInfoByApp(MessageParcel &data, MessageParcel &reply); + /** + * @brief Handle GetFormsInfoByModule message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t HandleGetFormsInfoByModule(MessageParcel &data, MessageParcel &reply); private: using FormMgrFunc = int32_t (FormMgrStub::*)(MessageParcel &data, MessageParcel &reply); std::map memberFuncMap_; DISALLOW_COPY_AND_MOVE(FormMgrStub); + + /** + * @brief Write a parcelabe vector objects to the proxy node. + * @param parcelableVector Indicates the objects to be write. + * @param reply Indicates the reply to be sent; + * @return Returns true if objects send successfully; returns false otherwise. + */ + template + bool WriteParcelableVector(std::vector &parcelableVector, Parcel &reply); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp b/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp index d3965a704b9..d958ebb1da1 100644 --- a/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp +++ b/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp @@ -599,6 +599,23 @@ int FormMgrProxy::GetStringInfo(IFormMgr::Message code, MessageParcel &data, std APP_LOGD("get string info success"); return ERR_OK; } +int FormMgrProxy::GetFormsInfo(IFormMgr::Message code, MessageParcel &data, std::vector &formInfos) +{ + int error; + MessageParcel reply; + error = SendTransactCmd(code, data, reply); + if (error != ERR_OK) { + return error; + } + + error = reply.ReadInt32(); + if (error != ERR_OK) { + APP_LOGE("%{public}s, failed to read reply result", __func__); + return error; + } + + return GetParcelableInfos(reply, formInfos); +} template int FormMgrProxy::GetParcelableInfo(IFormMgr::Message code, MessageParcel &data, T &parcelableInfo) { @@ -702,5 +719,87 @@ int FormMgrProxy::DistributedDataDeleteForm(const std::string &formId) } return reply.ReadInt32(); } + +/** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrProxy::GetAllFormsInfo(std::vector &formInfos) +{ + MessageParcel data; + if (!WriteInterfaceToken(data)) { + APP_LOGE("%{public}s, failed to write interface token", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + int error = GetFormsInfo(IFormMgr::Message::FORM_MGR_GET_ALL_FORMS_INFO, data, formInfos); + if (error != ERR_OK) { + APP_LOGE("%{public}s, failed to GetAllFormsInfo: %{public}d", __func__, error); + } + + return error; +} + +/** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrProxy::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +{ + MessageParcel data; + if (!WriteInterfaceToken(data)) { + APP_LOGE("%{public}s, failed to write interface token", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + if (!data.WriteString(bundleName)) { + APP_LOGE("%{public}s, failed to write bundleName", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + int error = GetFormsInfo(IFormMgr::Message::FORM_MGR_GET_ALL_FORMS_INFO, data, formInfos); + if (error != ERR_OK) { + APP_LOGE("%{public}s, failed to GetAllFormsInfo: %{public}d", __func__, error); + } + + return error; +} + +/** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrProxy::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) +{ + MessageParcel data; + if (!WriteInterfaceToken(data)) { + APP_LOGE("%{public}s, failed to write interface token", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + if (!data.WriteString(bundleName)) { + APP_LOGE("%{public}s, failed to write bundleName", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + if (!data.WriteString(moduleName)) { + APP_LOGE("%{public}s, failed to write moduleName", __func__); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + int error = GetFormsInfo(IFormMgr::Message::FORM_MGR_GET_ALL_FORMS_INFO, data, formInfos); + if (error != ERR_OK) { + APP_LOGE("%{public}s, failed to GetAllFormsInfo: %{public}d", __func__, error); + } + + return error; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/form_manager/src/form_mgr_stub.cpp b/interfaces/innerkits/form_manager/src/form_mgr_stub.cpp index 15ed3749392..8a9b0f6e60b 100644 --- a/interfaces/innerkits/form_manager/src/form_mgr_stub.cpp +++ b/interfaces/innerkits/form_manager/src/form_mgr_stub.cpp @@ -15,6 +15,7 @@ #include "appexecfwk_errors.h" #include "app_log_wrapper.h" +#include "form_info.h" #include "form_mgr_stub.h" #include "ipc_skeleton.h" #include "ipc_types.h" @@ -75,6 +76,12 @@ FormMgrStub::FormMgrStub() &FormMgrStub::HandleDistributedDataAddForm; memberFuncMap_[static_cast(IFormMgr::Message::FORM_MGR_DISTRIBUTED_DATA_DELETE_FORM__ST)] = &FormMgrStub::HandleDistributedDataDeleteForm; + memberFuncMap_[static_cast(IFormMgr::Message::FORM_MGR_GET_ALL_FORMS_INFO)] = + &FormMgrStub::HandleGetAllFormsInfo; + memberFuncMap_[static_cast(IFormMgr::Message::FORM_MGR_GET_FORMS_INFO_BY_APP)] = + &FormMgrStub::HandleGetFormsInfoByApp; + memberFuncMap_[static_cast(IFormMgr::Message::FORM_MGR_GET_FORMS_INFO_BY_MODULE)] = + &FormMgrStub::HandleGetFormsInfoByModule; } FormMgrStub::~FormMgrStub() @@ -491,5 +498,94 @@ int32_t FormMgrStub::HandleDistributedDataDeleteForm(MessageParcel &data, Messag reply.WriteInt32(result); return result; } + +/** + * @brief Handle GetAllFormsInfo message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ +int32_t FormMgrStub::HandleGetAllFormsInfo(MessageParcel &data, MessageParcel &reply) +{ + APP_LOGI("%{public}s called.", __func__); + std::vector infos; + int32_t result = GetAllFormsInfo(infos); + reply.WriteInt32(result); + if (result) { + if (!WriteParcelableVector(infos, reply)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + return result; +} + +/** + * @brief Handle GetFormsInfoByApp message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ +int32_t FormMgrStub::HandleGetFormsInfoByApp(MessageParcel &data, MessageParcel &reply) +{ + APP_LOGI("%{public}s called.", __func__); + std::string bundleName = data.ReadString(); + std::vector infos; + int32_t result = GetFormsInfoByApp(bundleName, infos); + reply.WriteInt32(result); + if (result) { + if (!WriteParcelableVector(infos, reply)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + return result; +} + +/** + * @brief Handle GetFormsInfoByModule message. + * @param data input param. + * @param reply output param. + * @return Returns ERR_OK on success, others on failure. + */ +int32_t FormMgrStub::HandleGetFormsInfoByModule(MessageParcel &data, MessageParcel &reply) +{ + APP_LOGI("%{public}s called.", __func__); + std::string bundleName = data.ReadString(); + std::string moduleName = data.ReadString(); + std::vector infos; + int32_t result = GetFormsInfoByModule(bundleName, moduleName, infos); + reply.WriteInt32(result); + if (result) { + if (!WriteParcelableVector(infos, reply)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + return result; +} + +/** + * @brief Write a parcelabe vector objects to the proxy node. + * @param parcelableVector Indicates the objects to be write. + * @param reply Indicates the reply to be sent; + * @return Returns true if objects send successfully; returns false otherwise. + */ +template +bool FormMgrStub::WriteParcelableVector(std::vector &parcelableVector, Parcel &reply) +{ + if (!reply.WriteInt32(parcelableVector.size())) { + APP_LOGE("write ParcelableVector failed"); + return false; + } + + for (auto &parcelable: parcelableVector) { + if (!reply.WriteParcelable(&parcelable)) { + APP_LOGE("write ParcelableVector failed"); + return false; + } + } + return true; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/include/form_mgr_adapter.h b/services/formmgr/include/form_mgr_adapter.h index e9e6160e3fa..cc66dc131df 100644 --- a/services/formmgr/include/form_mgr_adapter.h +++ b/services/formmgr/include/form_mgr_adapter.h @@ -211,6 +211,30 @@ public: * @return Returns ERR_OK on success, others on failure. */ int DistributedDataDeleteForm(const std::string &formId); + + /** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ + int GetAllFormsInfo(std::vector &formInfos); + + /** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos); + + /** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos); private: /** * @brief Get form configure info. diff --git a/services/formmgr/include/form_mgr_service.h b/services/formmgr/include/form_mgr_service.h index 68a0559af55..ffb36aa484b 100644 --- a/services/formmgr/include/form_mgr_service.h +++ b/services/formmgr/include/form_mgr_service.h @@ -201,6 +201,32 @@ public: * @return Returns ERR_OK on success, others on failure. */ int DistributedDataDeleteForm(const std::string &formId) override; + + /** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ + int GetAllFormsInfo(std::vector &formInfos) override; + + /** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) override; + + /** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ + int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) override; + private: /** * @brief initialization of form manager service. diff --git a/services/formmgr/src/form_mgr_adapter.cpp b/services/formmgr/src/form_mgr_adapter.cpp index 64ed7e2c9dc..8b6e3314ec9 100644 --- a/services/formmgr/src/form_mgr_adapter.cpp +++ b/services/formmgr/src/form_mgr_adapter.cpp @@ -29,6 +29,7 @@ #include "form_delete_connection.h" #include "form_dump_mgr.h" #include "form_event_notify_connection.h" +#include "form_info_mgr.h" #include "form_mgr_adapter.h" #include "form_provider_info.h" #include "form_provider_interface.h" @@ -1694,5 +1695,38 @@ int32_t FormMgrAdapter::GetCurrentUserId(const int callingUid) int32_t userId = callingUid / UID_CALLINGUID_TRANSFORM_DIVISOR; return userId; } +/** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrAdapter::GetAllFormsInfo(std::vector &formInfos) +{ + return FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos); +} + +/** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrAdapter::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +{ + return FormInfoMgr::GetInstance().GetFormsInfoByBundle(bundleName, formInfos); +} + +/** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrAdapter::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) +{ + return FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/src/form_mgr_service.cpp b/services/formmgr/src/form_mgr_service.cpp index 7c47f0a143e..0f1d075091d 100644 --- a/services/formmgr/src/form_mgr_service.cpp +++ b/services/formmgr/src/form_mgr_service.cpp @@ -438,5 +438,42 @@ int FormMgrService::DistributedDataDeleteForm(const std::string &formId) APP_LOGI("%{public}s called.", __func__); return FormMgrAdapter::GetInstance().DistributedDataDeleteForm(formId); } + +/** + * @brief Get All FormsInfo. + * @param formInfos Return the forms' information of all forms provided. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrService::GetAllFormsInfo(std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + return FormMgrAdapter::GetInstance().GetAllFormsInfo(formInfos); +} + +/** + * @brief Get forms info by bundle name . + * @param bundleName Application name. + * @param formInfos Return the forms' information of the specify application name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrService::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + return FormMgrAdapter::GetInstance().GetFormsInfoByApp(bundleName, formInfos); +} + +/** + * @brief Get forms info by bundle name and module name. + * @param bundleName bundle name. + * @param moduleName Module name of hap. + * @param formInfos Return the forms' information of the specify bundle name and module name. + * @return Returns ERR_OK on success, others on failure. + */ +int FormMgrService::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, + std::vector &formInfos) +{ + APP_LOGI("%{public}s called.", __func__); + return FormMgrAdapter::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/src/form_sys_event_receiver.cpp b/services/formmgr/src/form_sys_event_receiver.cpp index 9022a76ed83..7e6668950f6 100644 --- a/services/formmgr/src/form_sys_event_receiver.cpp +++ b/services/formmgr/src/form_sys_event_receiver.cpp @@ -217,11 +217,25 @@ bool FormSysEventReceiver::ProviderFormUpdated(const int64_t formId, void FormSysEventReceiver::HandleBundleFormInfoChanged(const std::string &bundleName) { FormInfoMgr::GetInstance().Update(bundleName); + std::vector formInfos; + FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos); + APP_LOGD("yzk, GetAllFormsInfo %{public}d", formInfos.size()); + for (const auto &formInfo: formInfos) { + APP_LOGD("yzk, %{public}s-%{public}s-%{public}s", formInfo.bundleName.c_str(), formInfo.moduleName.c_str(), + formInfo.name.c_str()); + } } void FormSysEventReceiver::HandleBundleFormInfoRemoved(const std::string &bundleName) { FormInfoMgr::GetInstance().Remove(bundleName); + std::vector formInfos; + FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos); + APP_LOGD("yzk, GetAllFormsInfo %{public}d", formInfos.size()); + for (const auto &formInfo: formInfos) { + APP_LOGD("yzk, %{public}s-%{public}s-%{public}s", formInfo.bundleName.c_str(), formInfo.moduleName.c_str(), + formInfo.name.c_str()); + } } void FormSysEventReceiver::HandleBundleDataCleared(const std::string &bundleName, const int uid) -- Gitee From 07b1aff9e8e7ac803bf0bfa9e68b8553eedd50b3 Mon Sep 17 00:00:00 2001 From: yangzk Date: Mon, 24 Jan 2022 18:35:00 +0800 Subject: [PATCH 6/6] IssueNo:#I4PPZF:FMS adapts to new configuration file Description:add FormInfoStorageMgr Sig:SIG_ApplicationFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: yangzk Change-Id: Iffd53528aa2e71119f412b4954c23c5503129845 --- .../kits/ability/native/src/ability.cpp | 6 +- .../kits/fmskit/native/src/form_mgr.cpp | 1 - .../form_manager/src/form_mgr_proxy.cpp | 4 +- .../form_manager/src/form_mgr_stub.cpp | 6 +- services/formmgr/BUILD.gn | 1 + services/formmgr/include/form_info_mgr.h | 4 + .../formmgr/include/form_info_storage_mgr.h | 79 +++++ services/formmgr/src/form_info_mgr.cpp | 65 +++- .../formmgr/src/form_info_storage_mgr.cpp | 324 ++++++++++++++++++ services/formmgr/src/form_mgr_adapter.cpp | 25 +- services/formmgr/src/form_mgr_service.cpp | 5 +- .../formmgr/src/form_sys_event_receiver.cpp | 20 +- .../fms_form_info_mgr_test.cpp | 14 +- 13 files changed, 495 insertions(+), 59 deletions(-) create mode 100644 services/formmgr/include/form_info_storage_mgr.h create mode 100644 services/formmgr/src/form_info_storage_mgr.cpp diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index d8f31af772b..318f0c6049f 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -2741,7 +2741,7 @@ bool Ability::GetAllFormsInfo(std::vector &formInfos) return false; } - return formInfos.empty(); + return !formInfos.empty(); } /** @@ -2771,7 +2771,7 @@ bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector & return false; } - return formInfos.empty(); + return !formInfos.empty(); } /** @@ -2802,7 +2802,7 @@ bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleN return false; } - return formInfos.empty(); + return !formInfos.empty(); } /** diff --git a/frameworks/kits/fmskit/native/src/form_mgr.cpp b/frameworks/kits/fmskit/native/src/form_mgr.cpp index f08663efeca..09a26dfbefd 100644 --- a/frameworks/kits/fmskit/native/src/form_mgr.cpp +++ b/frameworks/kits/fmskit/native/src/form_mgr.cpp @@ -607,6 +607,5 @@ int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleNa return remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos); } - } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp b/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp index d958ebb1da1..229ac6861ed 100644 --- a/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp +++ b/interfaces/innerkits/form_manager/src/form_mgr_proxy.cpp @@ -760,7 +760,7 @@ int FormMgrProxy::GetFormsInfoByApp(std::string &bundleName, std::vector infos; int32_t result = GetAllFormsInfo(infos); reply.WriteInt32(result); - if (result) { + if (result == ERR_OK) { if (!WriteParcelableVector(infos, reply)) { APP_LOGE("write failed"); return ERR_APPEXECFWK_PARCEL_ERROR; @@ -533,7 +533,7 @@ int32_t FormMgrStub::HandleGetFormsInfoByApp(MessageParcel &data, MessageParcel std::vector infos; int32_t result = GetFormsInfoByApp(bundleName, infos); reply.WriteInt32(result); - if (result) { + if (result == ERR_OK) { if (!WriteParcelableVector(infos, reply)) { APP_LOGE("write failed"); return ERR_APPEXECFWK_PARCEL_ERROR; @@ -556,7 +556,7 @@ int32_t FormMgrStub::HandleGetFormsInfoByModule(MessageParcel &data, MessageParc std::vector infos; int32_t result = GetFormsInfoByModule(bundleName, moduleName, infos); reply.WriteInt32(result); - if (result) { + if (result == ERR_OK) { if (!WriteParcelableVector(infos, reply)) { APP_LOGE("write failed"); return ERR_APPEXECFWK_PARCEL_ERROR; diff --git a/services/formmgr/BUILD.gn b/services/formmgr/BUILD.gn index 79d5ddc4363..ab4013b5c57 100644 --- a/services/formmgr/BUILD.gn +++ b/services/formmgr/BUILD.gn @@ -55,6 +55,7 @@ ohos_shared_library("libfms") { "src/form_host_callback.cpp", "src/form_host_record.cpp", "src/form_info_mgr.cpp", + "src/form_info_storage_mgr.cpp", "src/form_item_info.cpp", "src/form_mgr_adapter.cpp", "src/form_mgr_service.cpp", diff --git a/services/formmgr/include/form_info_mgr.h b/services/formmgr/include/form_info_mgr.h index a9fbce5096c..42bfdb441b6 100644 --- a/services/formmgr/include/form_info_mgr.h +++ b/services/formmgr/include/form_info_mgr.h @@ -40,6 +40,8 @@ class BundleFormInfo { public: explicit BundleFormInfo(std::string bundleName); + ErrCode InitFromJson(const std::string &formInfosJson); + ErrCode Update(); ErrCode Remove(); @@ -62,6 +64,8 @@ DECLARE_DELAYED_REF_SINGLETON(FormInfoMgr) public: DISALLOW_COPY_AND_MOVE(FormInfoMgr); + ErrCode Start(); + ErrCode Update(const std::string &bundleName); ErrCode Remove(const std::string &bundleName); diff --git a/services/formmgr/include/form_info_storage_mgr.h b/services/formmgr/include/form_info_storage_mgr.h new file mode 100644 index 00000000000..a7d88f7d2f5 --- /dev/null +++ b/services/formmgr/include/form_info_storage_mgr.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_STORAGE_MGR_H +#define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_STORAGE_MGR_H + +#include +#include +#include +#include "appexecfwk_errors.h" +#include "distributed_kv_data_manager.h" +#include "kvstore_death_recipient.h" + +namespace OHOS { +namespace AppExecFwk { +class KvStoreDeathRecipientCallbackFormInfoStorage : public DistributedKv::KvStoreDeathRecipient { +public: + KvStoreDeathRecipientCallbackFormInfoStorage(); + + virtual ~KvStoreDeathRecipientCallbackFormInfoStorage(); + + virtual void OnRemoteDied() override; +}; + +/** + * @class FormInfoStorageMgr + * Form info storage. + */ +class FormInfoStorageMgr final : public DelayedRefSingleton { +DECLARE_DELAYED_REF_SINGLETON(FormInfoStorageMgr) + +public: + DISALLOW_COPY_AND_MOVE(FormInfoStorageMgr); + + ErrCode LoadFormInfos(std::vector> &formInfos); + + ErrCode GetBundleFormInfos(const std::string &bundleName, std::string &formInfos); + + ErrCode SaveBundleFormInfos(const std::string &bundleName, const std::string &formInfos); + + ErrCode RemoveBundleFormInfos(const std::string &bundleName); + + ErrCode UpdateBundleFormInfos(const std::string &bundleName, const std::string &formInfos); + + bool ResetKvStore(); + +private: + void RegisterKvStoreDeathListener(); + + DistributedKv::Status GetKvStore(); + + bool CheckKvStore(); + + DistributedKv::Status GetEntries(std::vector &allEntries); + + const DistributedKv::AppId appId_ {"form_storage"}; + const DistributedKv::StoreId storeId_ {"form_infos"}; + DistributedKv::DistributedKvDataManager dataManager_; + std::shared_ptr kvStorePtr_; + mutable std::mutex kvStorePtrMutex_; + const int32_t MAX_TIMES = 600; // 1min + const int32_t SLEEP_INTERVAL = 100 * 1000; // 100ms +}; +} // namespace AppExecFwk +} // namespace OHOS + +#endif // FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_INFO_STORAGE_MGR_H diff --git a/services/formmgr/src/form_info_mgr.cpp b/services/formmgr/src/form_info_mgr.cpp index 17eb9d12456..794a40ddef5 100644 --- a/services/formmgr/src/form_info_mgr.cpp +++ b/services/formmgr/src/form_info_mgr.cpp @@ -21,6 +21,8 @@ #include "bundle_mgr_client.h" #include "extension_form_profile.h" #include "form_bms_helper.h" +#include "form_info_storage_mgr.h" +#include "json_serializer.h" namespace OHOS { namespace AppExecFwk { @@ -122,10 +124,36 @@ BundleFormInfo::BundleFormInfo(std::string bundleName) : bundleName_(std::move(b { } +ErrCode BundleFormInfo::InitFromJson(const std::string &formInfosJson) +{ + nlohmann::json jsonObject = nlohmann::json::parse(formInfosJson, nullptr, false); + if (jsonObject.is_discarded()) { + APP_LOGE("bad profile"); + return ERR_APPEXECFWK_PARSE_BAD_PROFILE; + } + std::unique_lock guard(formInfosMutex_); + formInfos_ = jsonObject.get>(); + return ERR_OK; +} + ErrCode BundleFormInfo::Update() { std::unique_lock guard(formInfosMutex_); ErrCode errCode = FormInfoHelper::LoadFormConfigInfoByBundleName(bundleName_, formInfos_); + if (errCode != ERR_OK) { + return errCode; + } + if (formInfos_.empty()) { + return ERR_OK; + } + + nlohmann::json jsonObject = formInfos_; + if (jsonObject.is_discarded()) { + APP_LOGE("bad form infos."); + return ERR_APPEXECFWK_PARSE_BAD_PROFILE; + } + std::string formInfoStr = jsonObject.dump(Constants::DUMP_INDENT); + errCode = FormInfoStorageMgr::GetInstance().UpdateBundleFormInfos(bundleName_, formInfoStr); return errCode; } @@ -133,7 +161,8 @@ ErrCode BundleFormInfo::Remove() { std::unique_lock guard(formInfosMutex_); formInfos_.clear(); - return ERR_OK; + ErrCode errCode = FormInfoStorageMgr::GetInstance().RemoveBundleFormInfos(bundleName_); + return errCode; } bool BundleFormInfo::Empty() @@ -145,7 +174,7 @@ bool BundleFormInfo::Empty() ErrCode BundleFormInfo::GetAllFormsInfo(std::vector &formInfos) { std::shared_lock guard(formInfosMutex_); - for (const auto &formInfo : formInfos_) { + for (const auto &formInfo: formInfos_) { formInfos.push_back(formInfo); } return ERR_OK; @@ -154,7 +183,7 @@ ErrCode BundleFormInfo::GetAllFormsInfo(std::vector &formInfos) ErrCode BundleFormInfo::GetFormsInfoByModule(const std::string &moduleName, std::vector &formInfos) { std::shared_lock guard(formInfosMutex_); - for (const auto &formInfo : formInfos_) { + for (const auto &formInfo: formInfos_) { if (formInfo.moduleName == moduleName) { formInfos.push_back(formInfo); } @@ -162,10 +191,38 @@ ErrCode BundleFormInfo::GetFormsInfoByModule(const std::string &moduleName, std: return ERR_OK; } -FormInfoMgr::FormInfoMgr() = default; +FormInfoMgr::FormInfoMgr() +{ + APP_LOGI("FormInfoMgr is created"); +} FormInfoMgr::~FormInfoMgr() = default; +ErrCode FormInfoMgr::Start() +{ + std::vector> formInfos; + ErrCode errCode = FormInfoStorageMgr::GetInstance().LoadFormInfos(formInfos); + if (errCode != ERR_OK) { + APP_LOGE("LoadFormData failed."); + return errCode; + } + + std::unique_lock guard(bundleFormInfoMapMutex_); + for (const auto &item: formInfos) { + const std::string &bundleName = item.first; + const std::string &formInfosJson = item.second; + auto bundleFormInfoPtr = std::make_shared(bundleName); + errCode = bundleFormInfoPtr->InitFromJson(formInfosJson); + if (errCode != ERR_OK) { + continue; + } + APP_LOGE("load bundle %{public}s form infos success.", bundleName.c_str()); + bundleFormInfoMap_[bundleName] = bundleFormInfoPtr; + } + APP_LOGI("load bundle form infos from db done."); + return ERR_OK; +} + ErrCode FormInfoMgr::Update(const std::string &bundleName) { if (bundleName.empty()) { diff --git a/services/formmgr/src/form_info_storage_mgr.cpp b/services/formmgr/src/form_info_storage_mgr.cpp new file mode 100644 index 00000000000..b19a809a238 --- /dev/null +++ b/services/formmgr/src/form_info_storage_mgr.cpp @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2021 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 "form_info_storage_mgr.h" +#include +#include +#include "app_log_wrapper.h" +#include "kvstore_death_recipient_callback.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +const int32_t CHECK_TIMES = 300; +const int32_t CHECK_INTERVAL = 100000; // 100ms +} // namespace + +KvStoreDeathRecipientCallbackFormInfoStorage::KvStoreDeathRecipientCallbackFormInfoStorage() +{ + APP_LOGI("create kvstore death recipient callback instance %{public}p", this); +} + +KvStoreDeathRecipientCallbackFormInfoStorage::~KvStoreDeathRecipientCallbackFormInfoStorage() +{ + APP_LOGI("destroy kvstore death recipient callback instance %{public}p", this); +} + +void KvStoreDeathRecipientCallbackFormInfoStorage::OnRemoteDied() +{ + APP_LOGI("OnRemoteDied, register data change listener begin"); + std::thread([] { + int32_t times = 0; + FormInfoStorageMgr &formInfoStorageMgr = FormInfoStorageMgr::GetInstance(); + while (times < CHECK_TIMES) { + times++; + // init kvStore. + if (formInfoStorageMgr.ResetKvStore()) { + // register data change listener again. + APP_LOGI("current times is %{public}d", times); + break; + } + usleep(CHECK_INTERVAL); + } + }).detach(); + + APP_LOGI("OnRemoteDied, register data change listener end"); +} + +FormInfoStorageMgr::FormInfoStorageMgr() +{ + DistributedKv::Status status = GetKvStore(); + if (status == DistributedKv::Status::IPC_ERROR) { + status = GetKvStore(); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + RegisterKvStoreDeathListener(); + APP_LOGI("FormInfoStorageMgr is created"); +} + +FormInfoStorageMgr::~FormInfoStorageMgr() +{ + dataManager_.CloseKvStore(appId_, kvStorePtr_); +} + +ErrCode FormInfoStorageMgr::LoadFormInfos(std::vector> &formInfos) +{ + APP_LOGI("FormInfoStorageMgr load all form infos"); + { + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + APP_LOGE("kvStore is nullptr"); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + } + + DistributedKv::Status status; + std::vector allEntries; + status = GetEntries(allEntries); + if (status == DistributedKv::Status::IPC_ERROR) { + status = GetEntries(allEntries); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("get entries error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + + for (const auto &item: allEntries) { + formInfos.emplace_back(item.key.ToString(), item.value.ToString()); + } + + return ERR_OK; +} + +ErrCode FormInfoStorageMgr::GetBundleFormInfos(const std::string &bundleName, std::string &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + APP_LOGI("FormInfoStorageMgr get form info, bundleName=%{public}s", bundleName.c_str()); + { + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + APP_LOGE("kvStore is nullptr"); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + } + + DistributedKv::Status status = DistributedKv::Status::ERROR; + std::vector allEntries; + DistributedKv::Key key(bundleName); + if (kvStorePtr_) { + // sync call GetEntries, the callback will be trigger at once + status = kvStorePtr_->GetEntries(key, allEntries); + } + + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("get entries error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + + if (allEntries.empty()) { + APP_LOGE("%{public}s not match any FormInfo", bundleName.c_str()); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + + formInfos = allEntries.front().value.ToString(); + return ERR_OK; +} + +ErrCode FormInfoStorageMgr::SaveBundleFormInfos(const std::string &bundleName, const std::string &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + APP_LOGI("FormInfoStorageMgr save form info, bundleName=%{public}s", bundleName.c_str()); + { + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + APP_LOGE("kvStore is nullptr"); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + } + + DistributedKv::Key key(bundleName); + DistributedKv::Value value(formInfos); + DistributedKv::Status status; + { + std::lock_guard lock(kvStorePtrMutex_); + status = kvStorePtr_->Put(key, value); + if (status == DistributedKv::Status::IPC_ERROR) { + status = kvStorePtr_->Put(key, value); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + } + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("save formInfos to kvStore error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + return ERR_OK; +} + +ErrCode FormInfoStorageMgr::RemoveBundleFormInfos(const std::string &bundleName) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + APP_LOGI("FormInfoStorageMgr remove form info, bundleName=%{public}s", bundleName.c_str()); + { + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + APP_LOGE("kvStore is nullptr"); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + } + + DistributedKv::Key key(bundleName); + DistributedKv::Status status; + { + std::lock_guard lock(kvStorePtrMutex_); + status = kvStorePtr_->Delete(key); + if (status == DistributedKv::Status::IPC_ERROR) { + status = kvStorePtr_->Delete(key); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + } + + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("remove formInfos from kvStore error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + return ERR_OK; +} + +ErrCode FormInfoStorageMgr::UpdateBundleFormInfos(const std::string &bundleName, const std::string &formInfos) +{ + if (bundleName.empty()) { + APP_LOGE("bundleName is empty."); + return ERR_APPEXECFWK_FORM_INVALID_PARAM; + } + + APP_LOGI("FormInfoStorageMgr update form info, bundleName=%{public}s", bundleName.c_str()); + { + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + APP_LOGE("kvStore is nullptr"); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + } + + DistributedKv::Key key(bundleName); + DistributedKv::Status status; + std::lock_guard lock(kvStorePtrMutex_); + status = kvStorePtr_->Delete(key); + if (status == DistributedKv::Status::IPC_ERROR) { + status = kvStorePtr_->Delete(key); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + if (status != DistributedKv::Status::SUCCESS && status != DistributedKv::Status::KEY_NOT_FOUND) { + APP_LOGE("update formInfos to kvStore error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + + DistributedKv::Value value(formInfos); + status = kvStorePtr_->Put(key, value); + if (status == DistributedKv::Status::IPC_ERROR) { + status = kvStorePtr_->Put(key, value); + APP_LOGW("distribute database ipc error and try to call again, result = %{public}d", status); + } + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("update formInfos to kvStore error: %{public}d", status); + return ERR_APPEXECFWK_FORM_COMMON_CODE; + } + return ERR_OK; +} + +DistributedKv::Status FormInfoStorageMgr::GetKvStore() +{ + DistributedKv::Options options = { + .createIfMissing = true, + .encrypt = false, + .autoSync = true, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION + }; + + DistributedKv::Status status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); + if (status != DistributedKv::Status::SUCCESS) { + APP_LOGE("return error: %{public}d", status); + } else { + APP_LOGI("get kvStore success"); + } + return status; +} + +bool FormInfoStorageMgr::CheckKvStore() +{ + if (kvStorePtr_ != nullptr) { + return true; + } + int32_t tryTimes = MAX_TIMES; + while (tryTimes > 0) { + DistributedKv::Status status = GetKvStore(); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) { + return true; + } + APP_LOGI("CheckKvStore, Times: %{public}d", tryTimes); + usleep(SLEEP_INTERVAL); + tryTimes--; + } + return kvStorePtr_ != nullptr; +} + +void FormInfoStorageMgr::RegisterKvStoreDeathListener() +{ + APP_LOGI("register kvStore death listener"); + std::shared_ptr callback = + std::make_shared(); + dataManager_.RegisterKvStoreServiceDeathRecipient(callback); +} + +bool FormInfoStorageMgr::ResetKvStore() +{ + std::lock_guard lock(kvStorePtrMutex_); + kvStorePtr_ = nullptr; + DistributedKv::Status status = GetKvStore(); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) { + return true; + } + APP_LOGW("failed"); + return false; +} + +DistributedKv::Status FormInfoStorageMgr::GetEntries(std::vector &allEntries) +{ + DistributedKv::Status status = DistributedKv::Status::ERROR; + // if prefix is empty, get all entries. + DistributedKv::Key key(""); + if (kvStorePtr_) { + // sync call GetEntries, the callback will be trigger at once + status = kvStorePtr_->GetEntries(key, allEntries); + } + APP_LOGI("get all entries status: %{public}d", status); + return status; +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/services/formmgr/src/form_mgr_adapter.cpp b/services/formmgr/src/form_mgr_adapter.cpp index 8b6e3314ec9..bf6eab01c7b 100644 --- a/services/formmgr/src/form_mgr_adapter.cpp +++ b/services/formmgr/src/form_mgr_adapter.cpp @@ -1049,18 +1049,7 @@ ErrCode FormMgrAdapter::GetBundleInfo(const AAFwk::Want &want, BundleInfo &bundl return ERR_APPEXECFWK_FORM_NO_SUCH_MODULE; } - for (const auto &abilityInfo : bundleInfo.abilityInfos) { - if (abilityInfo.bundleName == bundleName && abilityInfo.moduleName == moduleName - && abilityInfo.name == abilityName) { - packageName = bundleName + moduleName; - break; - } - } - - if (packageName.empty()) { - APP_LOGE("GetBundleInfo can not find target ability %{public}s", abilityName.c_str()); - return ERR_APPEXECFWK_FORM_NO_SUCH_ABILITY; - } + packageName = bundleName + moduleName; APP_LOGD("GetBundleMgr end."); return ERR_OK; } @@ -1081,15 +1070,11 @@ ErrCode FormMgrAdapter::GetFormInfo(const AAFwk::Want &want, FormInfo &formInfo) APP_LOGE("addForm bundleName or abilityName or moduleName is invalid"); return ERR_APPEXECFWK_FORM_INVALID_PARAM; } - sptr iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("GetFormInfo, failed to get IBundleMgr."); - return ERR_APPEXECFWK_FORM_GET_BMS_FAILED; - } - std::vector formInfos; - if (iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos) == false) { - APP_LOGE("GetFormsInfoByModule, failed to get form config info."); + std::vector formInfos {}; + ErrCode errCode = FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); + if (errCode != ERR_OK) { + APP_LOGE("GetFormsInfoByModule, failed to get form config info."); return ERR_APPEXECFWK_FORM_GET_INFO_FAILED; } diff --git a/services/formmgr/src/form_mgr_service.cpp b/services/formmgr/src/form_mgr_service.cpp index 0f1d075091d..57ddd5b3dbc 100644 --- a/services/formmgr/src/form_mgr_service.cpp +++ b/services/formmgr/src/form_mgr_service.cpp @@ -29,6 +29,7 @@ #include "form_constants.h" #include "form_data_mgr.h" #include "form_db_cache.h" +#include "form_info_mgr.h" #include "form_mgr_adapter.h" #include "form_task_mgr.h" #include "form_timer_mgr.h" @@ -376,7 +377,6 @@ ErrCode FormMgrService::Init() if (formSysEventReceiver_ == nullptr) { EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED); @@ -389,6 +389,7 @@ ErrCode FormMgrService::Init() EventFwk::CommonEventManager::SubscribeCommonEvent(formSysEventReceiver_); } FormDbCache::GetInstance().Start(); + FormInfoMgr::GetInstance().Start(); APP_LOGI("init success"); return ERR_OK; @@ -470,7 +471,7 @@ int FormMgrService::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) + std::vector &formInfos) { APP_LOGI("%{public}s called.", __func__); return FormMgrAdapter::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); diff --git a/services/formmgr/src/form_sys_event_receiver.cpp b/services/formmgr/src/form_sys_event_receiver.cpp index 7e6668950f6..0ede506f439 100644 --- a/services/formmgr/src/form_sys_event_receiver.cpp +++ b/services/formmgr/src/form_sys_event_receiver.cpp @@ -59,10 +59,8 @@ void FormSysEventReceiver::OnReceiveEvent(const EventFwk::CommonEventData &event return; } APP_LOGI("%{public}s, action:%{public}s.", __func__, action.c_str()); - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { - APP_LOGI("%{public}s, bundle added, bundleName: %{public}s", __func__, bundleName.c_str()); - HandleBundleFormInfoChanged(bundleName); - } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { + // install or update APP_LOGI("%{public}s, bundle changed, bundleName: %{public}s", __func__, bundleName.c_str()); HandleBundleFormInfoChanged(bundleName); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { @@ -217,25 +215,11 @@ bool FormSysEventReceiver::ProviderFormUpdated(const int64_t formId, void FormSysEventReceiver::HandleBundleFormInfoChanged(const std::string &bundleName) { FormInfoMgr::GetInstance().Update(bundleName); - std::vector formInfos; - FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos); - APP_LOGD("yzk, GetAllFormsInfo %{public}d", formInfos.size()); - for (const auto &formInfo: formInfos) { - APP_LOGD("yzk, %{public}s-%{public}s-%{public}s", formInfo.bundleName.c_str(), formInfo.moduleName.c_str(), - formInfo.name.c_str()); - } } void FormSysEventReceiver::HandleBundleFormInfoRemoved(const std::string &bundleName) { FormInfoMgr::GetInstance().Remove(bundleName); - std::vector formInfos; - FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos); - APP_LOGD("yzk, GetAllFormsInfo %{public}d", formInfos.size()); - for (const auto &formInfo: formInfos) { - APP_LOGD("yzk, %{public}s-%{public}s-%{public}s", formInfo.bundleName.c_str(), formInfo.moduleName.c_str(), - formInfo.name.c_str()); - } } void FormSysEventReceiver::HandleBundleDataCleared(const std::string &bundleName, const int uid) diff --git a/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp b/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp index 24c18cd2a78..3d28488cb52 100644 --- a/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp +++ b/services/formmgr/test/unittest/fms_form_info_mgr_test/fms_form_info_mgr_test.cpp @@ -29,7 +29,8 @@ const nlohmann::json JSON_BAD_FORMS = R"({"forms":""})"_json; const nlohmann::json JSON_EMPTY_FORMS = R"({"forms":[]})"_json; const nlohmann::json JSON_FORMS = R"( { - "forms": [{ + "forms": [ + { "name": "Form_001", "description": "$string:form_description", "src": "./js/pages/card/index", @@ -46,7 +47,8 @@ const nlohmann::json JSON_FORMS = R"( "updateDuration": 1, "defaultDimension": "2*2", "supportDimensions": ["2*2"], - "metadata": [{ + "metadata": [ + { "name": "string", "value": "string" } @@ -72,7 +74,7 @@ std::string JsonToString(const nlohmann::json &json) HWTEST(ExtensionFormProfileTest, TransformTo_0100, TestSize.Level0) { - std::vector extensionFormInfos{}; + std::vector extensionFormInfos {}; // none profile EXPECT_EQ(ERR_APPEXECFWK_PARSE_BAD_PROFILE, ExtensionFormProfile::TransformTo(JSON_NONE, extensionFormInfos)); EXPECT_EQ(0, extensionFormInfos.size()); @@ -93,7 +95,7 @@ HWTEST(ExtensionFormProfileTest, TransformTo_0100, TestSize.Level0) HWTEST(ExtensionFormProfileTest, TransformTo_0101, TestSize.Level0) { - std::vector extensionFormInfos{}; + std::vector extensionFormInfos {}; int32_t numForms = 0; std::vector notMustPropKeys = { @@ -133,7 +135,7 @@ HWTEST(ExtensionFormProfileTest, TransformTo_0101, TestSize.Level0) HWTEST(ExtensionFormProfileTest, TransformTo_0102, TestSize.Level0) { - std::vector extensionFormInfos{}; + std::vector extensionFormInfos {}; int32_t numForms = 0; // name @@ -153,7 +155,7 @@ HWTEST(ExtensionFormProfileTest, TransformTo_0102, TestSize.Level0) HWTEST(ExtensionFormProfileTest, TransformTo_0103, TestSize.Level0) { - std::vector extensionFormInfos{}; + std::vector extensionFormInfos {}; int32_t numForms = 0; // dimension -- Gitee