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 namespace OHOS {
19 namespace AppExecFwk {
OnReceiveEvent(const EventFwk::CommonEventData eventData)20 void FormBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
21 {
22 // env check
23 if (eventHandler_ == nullptr) {
24 HILOG_ERROR("%{public}s failed, eventHandler_ is nullptr", __func__);
25 return;
26 }
27 const AAFwk::Want& want = eventData.GetWant();
28 // action contains the change type of haps.
29 std::string action = want.GetAction();
30 std::string bundleName = want.GetElement().GetBundleName();
31 int userId = want.GetIntParam(KEY_USER_ID, 0);
32 // verify data
33 if (action.empty() || bundleName.empty()) {
34 HILOG_ERROR("%{public}s failed, empty action/bundleName", __func__);
35 return;
36 }
37 if (eventHandler_ == nullptr) {
38 HILOG_ERROR("%{public}s fail, invalid event handler.", __func__);
39 return;
40 }
41 HILOG_INFO("%{public}s, action:%{public}s.", __func__, action.c_str());
42
43 wptr<FormBundleEventCallback> weakThis = this;
44 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
45 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
46 // install or update
47 auto task = [weakThis, bundleName, userId]() {
48 HILOG_INFO("%{public}s, bundle changed, bundleName: %{public}s", __func__, bundleName.c_str());
49 sptr<FormBundleEventCallback> sharedThis = weakThis.promote();
50 if (sharedThis) {
51 sharedThis->formEventHelper_.HandleBundleFormInfoChanged(bundleName, userId);
52 sharedThis->formEventHelper_.HandleProviderUpdated(bundleName, userId);
53 }
54 };
55 eventHandler_->PostSyncTask(task);
56 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
57 // uninstall module/bundle
58 auto task = [weakThis, bundleName, userId]() {
59 HILOG_INFO("%{public}s, bundle removed, bundleName: %{public}s", __func__, bundleName.c_str());
60 sptr<FormBundleEventCallback> sharedThis = weakThis.promote();
61 if (sharedThis) {
62 sharedThis->formEventHelper_.HandleBundleFormInfoRemoved(bundleName, userId);
63 sharedThis->formEventHelper_.HandleProviderRemoved(bundleName, userId);
64 }
65 };
66 eventHandler_->PostSyncTask(task);
67 }
68 }
69
70 } // namespace AppExecFwk
71 } // namespace OHOS