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 #include "form_bms_helper.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const std::string BMS_EVENT_ADDITIONAL_INFO_CHANGED = "bms.event.ADDITIONAL_INFO_CHANGED";
27 constexpr uint64_t CRYPTED_BUNDLE_DELAY_TIME = 1000;
28 } // namespace
29 
FormBundleEventCallback()30 FormBundleEventCallback::FormBundleEventCallback()
31 {
32     HILOG_INFO("create");
33 }
34 
~FormBundleEventCallback()35 FormBundleEventCallback::~FormBundleEventCallback()
36 {
37     HILOG_INFO("destroy");
38 }
39 
IsEncryptedBundle(std::string &bundleName, int32_t userId)40 bool FormBundleEventCallback::IsEncryptedBundle(std::string &bundleName, int32_t userId)
41 {
42     ApplicationInfo appInfo;
43     if (FormBmsHelper::GetInstance().GetApplicationInfo(bundleName, userId, appInfo) != ERR_OK) {
44         HILOG_ERROR("get app info failed");
45         return false;
46     }
47     return (appInfo.applicationReservedFlag &
48         static_cast<uint32_t>(AppExecFwk::ApplicationReservedFlag::ENCRYPTED_APPLICATION)) != 0;
49 }
50 
OnReceiveEvent(const EventFwk::CommonEventData eventData)51 void FormBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
52 {
53     const AAFwk::Want& want = eventData.GetWant();
54     // action contains the change type of haps.
55     std::string action = want.GetAction();
56     std::string bundleName = want.GetElement().GetBundleName();
57     int userId = want.GetIntParam(KEY_USER_ID, 0);
58     // verify data
59     if (action.empty() || bundleName.empty()) {
60         HILOG_ERROR("empty action/bundleName");
61         return;
62     }
63 
64     HILOG_INFO("action:%{public}s", action.c_str());
65 
66     wptr<FormBundleEventCallback> weakThis = this;
67     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
68         action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
69         // install or update
70         HILOG_INFO("bundleName:%{public}s changed", bundleName.c_str());
71         FormEventUtil::HandleBundleFormInfoChanged(bundleName, userId);
72         std::function<void()> taskFunc = [bundleName, userId]() {
73             FormEventUtil::HandleUpdateFormCloud(bundleName);
74             FormEventUtil::HandleProviderUpdated(bundleName, userId);
75         };
76         uint64_t delayMs = 0;
77         if (IsEncryptedBundle(bundleName, userId)) {
78             delayMs = CRYPTED_BUNDLE_DELAY_TIME;
79         }
80         FormTaskMgr::GetInstance().PostTask(taskFunc, delayMs);
81     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
82         // uninstall module/bundle
83         HILOG_INFO("bundleName:%{public}s removed", bundleName.c_str());
84         FormEventUtil::HandleBundleFormInfoRemoved(bundleName, userId);
85         std::function<void()> taskFunc = [bundleName, userId]() {
86             FormEventUtil::HandleProviderRemoved(bundleName, userId);
87             // Ensure clear forbidden form db when bundle uninstall
88             // Health contol will set again when reinstall
89             FormBundleForbidMgr::GetInstance().SetBundleForbiddenStatus(bundleName, false);
90         };
91         FormTaskMgr::GetInstance().PostTask(taskFunc, 0);
92     } else if (action == BMS_EVENT_ADDITIONAL_INFO_CHANGED) {
93         // additional info changed
94         HILOG_INFO("bundleName:%{public}s additional info changed", bundleName.c_str());
95         std::function<void()> taskFunc = [bundleName]() {
96             FormEventUtil::HandleAdditionalInfoChanged(bundleName);
97         };
98         FormTaskMgr::GetInstance().PostTask(taskFunc, 0);
99     }
100 }
101 
102 } // namespace AppExecFwk
103 } // namespace OHOS