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 "form_bundle_event_callback.h"
17 
18 #include "form_bundle_forbid_mgr.h"
19 #include "form_task_mgr.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const std::string BMS_EVENT_ADDITIONAL_INFO_CHANGED = "bms.event.ADDITIONAL_INFO_CHANGED";
25 } // namespace
26 
FormBundleEventCallback()27 FormBundleEventCallback::FormBundleEventCallback()
28 {
29     HILOG_INFO("create");
30 }
31 
~FormBundleEventCallback()32 FormBundleEventCallback::~FormBundleEventCallback()
33 {
34     HILOG_INFO("destroy");
35 }
36 
OnReceiveEvent(const EventFwk::CommonEventData eventData)37 void FormBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
38 {
39     const AAFwk::Want& want = eventData.GetWant();
40     // action contains the change type of haps.
41     std::string action = want.GetAction();
42     std::string bundleName = want.GetElement().GetBundleName();
43     int userId = want.GetIntParam(KEY_USER_ID, 0);
44     // verify data
45     if (action.empty() || bundleName.empty()) {
46         HILOG_ERROR("empty action/bundleName");
47         return;
48     }
49 
50     HILOG_INFO("action:%{public}s", action.c_str());
51 
52     wptr<FormBundleEventCallback> weakThis = this;
53     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
54         action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
55         // install or update
56         HILOG_INFO("bundleName:%{public}s changed", bundleName.c_str());
57         FormEventUtil::HandleBundleFormInfoChanged(bundleName, userId);
58         std::function<void()> taskFunc = [bundleName, userId]() {
59             FormEventUtil::HandleUpdateFormCloud(bundleName);
60             FormEventUtil::HandleProviderUpdated(bundleName, userId);
61         };
62         FormTaskMgr::GetInstance().PostTask(taskFunc, 0);
63     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
64         // uninstall module/bundle
65         int appIndex = want.GetIntParam("appIndex", 0);
66         if (appIndex > 0) {
67             HILOG_INFO("this application is a simulation. not support to remove the form.\
68                 appIndex: %{public}d", appIndex);
69             return;
70         }
71         HILOG_INFO("bundleName:%{public}s removed", bundleName.c_str());
72         FormEventUtil::HandleBundleFormInfoRemoved(bundleName, userId);
73         std::function<void()> taskFunc = [bundleName, userId]() {
74             FormEventUtil::HandleProviderRemoved(bundleName, userId);
75             // Ensure clear forbidden form db when bundle uninstall
76             // Health contol will set again when reinstall
77             FormBundleForbidMgr::GetInstance().SetBundleForbiddenStatus(bundleName, false);
78         };
79         FormTaskMgr::GetInstance().PostTask(taskFunc, 0);
80     } else if (action == BMS_EVENT_ADDITIONAL_INFO_CHANGED) {
81         // additional info changed
82         HILOG_INFO("bundleName:%{public}s additional info changed", bundleName.c_str());
83         std::function<void()> taskFunc = [bundleName]() {
84             FormEventUtil::HandleAdditionalInfoChanged(bundleName);
85         };
86         FormTaskMgr::GetInstance().PostTask(taskFunc, 0);
87     }
88 }
89 
90 } // namespace AppExecFwk
91 } // namespace OHOS