1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bms_mgr/form_bundle_event_callback.h"
17
18 #include "feature/bundle_forbidden/form_bundle_forbid_mgr.h"
19 #include "form_mgr/form_mgr_queue.h"
20 #include "feature/bundle_distributed/form_distributed_mgr.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const std::string BMS_EVENT_ADDITIONAL_INFO_CHANGED = "bms.event.ADDITIONAL_INFO_CHANGED";
26 } // namespace
27
FormBundleEventCallback()28 FormBundleEventCallback::FormBundleEventCallback()
29 {
30 HILOG_INFO("create");
31 }
32
~FormBundleEventCallback()33 FormBundleEventCallback::~FormBundleEventCallback()
34 {
35 HILOG_INFO("destroy");
36 }
37
OnReceiveEvent(const EventFwk::CommonEventData eventData)38 void FormBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
39 {
40 const AAFwk::Want& want = eventData.GetWant();
41 // action contains the change type of haps.
42 std::string action = want.GetAction();
43 std::string bundleName = want.GetElement().GetBundleName();
44 int userId = want.GetIntParam(KEY_USER_ID, 0);
45 // verify data
46 if (action.empty() || bundleName.empty()) {
47 HILOG_ERROR("empty action/bundleName");
48 return;
49 }
50
51 HILOG_INFO("action:%{public}s", action.c_str());
52
53 wptr<FormBundleEventCallback> weakThis = this;
54 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
55 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
56 // install or update
57 HILOG_WARN("bundleName:%{public}s changed", bundleName.c_str());
58 FormEventUtil::HandleBundleFormInfoChanged(bundleName, userId);
59 std::function<void()> taskFunc = [bundleName, userId]() {
60 FormEventUtil::HandleUpdateFormCloud(bundleName);
61 FormEventUtil::HandleProviderUpdated(bundleName, userId);
62 };
63 FormMgrQueue::GetInstance().ScheduleTask(0, taskFunc);
64 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
65 // uninstall module/bundle
66 int appIndex = want.GetIntParam("appIndex", 0);
67 if (appIndex > 0) {
68 HILOG_INFO("this application is a simulation. not support to remove the form.\
69 appIndex: %{public}d", appIndex);
70 return;
71 }
72 HILOG_WARN("package removed, bundleName:%{public}s, userId:%{public}d", bundleName.c_str(), userId);
73 FormEventUtil::HandleBundleFormInfoRemoved(bundleName, userId);
74 std::function<void()> taskFunc = [bundleName, userId]() {
75 FormEventUtil::HandleProviderRemoved(bundleName, userId);
76 // Ensure clear forbidden form db when bundle uninstall
77 // Health contol will set again when reinstall
78 FormBundleForbidMgr::GetInstance().SetBundleForbiddenStatus(bundleName, false);
79 DistributedModule distributedModule;
80 distributedModule.userId = userId;
81 FormDistributedMgr::GetInstance().SetBundleDistributedStatus(bundleName, false, distributedModule);
82 };
83 FormMgrQueue::GetInstance().ScheduleTask(0, taskFunc);
84 } else if (action == BMS_EVENT_ADDITIONAL_INFO_CHANGED) {
85 // additional info changed
86 HILOG_INFO("bundleName:%{public}s additional info changed", bundleName.c_str());
87 std::function<void()> taskFunc = [bundleName]() {
88 FormEventUtil::HandleAdditionalInfoChanged(bundleName);
89 };
90 FormMgrQueue::GetInstance().ScheduleTask(0, taskFunc);
91 }
92 }
93
94 } // namespace AppExecFwk
95 } // namespace OHOS