1 /*
2 * Copyright (c) 2023 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 "ability_bundle_event_callback.h"
17
18 namespace OHOS {
19 namespace AAFwk {
AbilityBundleEventCallback()20 AbilityBundleEventCallback::AbilityBundleEventCallback() : eventHandler_(nullptr) {}
21
AbilityBundleEventCallback(std::shared_ptr<AbilityEventHandler> eventHandler)22 AbilityBundleEventCallback::AbilityBundleEventCallback(std::shared_ptr<AbilityEventHandler> eventHandler)
23 {
24 eventHandler_ = eventHandler;
25 }
26
OnReceiveEvent(const EventFwk::CommonEventData eventData)27 void AbilityBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
28 {
29 // env check
30 if (eventHandler_ == nullptr) {
31 HILOG_ERROR("OnReceiveEvent failed, eventHandler_ is nullptr");
32 return;
33 }
34 const Want& want = eventData.GetWant();
35 // action contains the change type of haps.
36 std::string action = want.GetAction();
37 std::string bundleName = want.GetElement().GetBundleName();
38 int uid = want.GetIntParam(KEY_UID, 0);
39 // verify data
40 if (action.empty() || bundleName.empty()) {
41 HILOG_ERROR("OnReceiveEvent failed, empty action/bundleName");
42 return;
43 }
44 HILOG_DEBUG("OnReceiveEvent, action:%{public}s.", action.c_str());
45
46 wptr<AbilityBundleEventCallback> weakThis = this;
47 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
48 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED ||
49 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
50 // install or update or uninstall module/bundle
51 auto task = [weakThis, bundleName, uid]() {
52 HILOG_DEBUG("OnReceiveEvent, bundle changed, bundleName: %{public}s", bundleName.c_str());
53 sptr<AbilityBundleEventCallback> sharedThis = weakThis.promote();
54 if (sharedThis) {
55 sharedThis->abilityEventHelper_.HandleModuleInfoUpdated(bundleName, uid);
56 }
57 };
58 eventHandler_->PostTask(task);
59 }
60 }
61
62 } // namespace AAFwk
63 } // namespace OHOS