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 <uv.h>
17 #include <shared_mutex>
18
19 #include "bundle_monitor_callback.h"
20
21 #include "app_log_wrapper.h"
22 #include "bundle_constants.h"
23 #include "napi/native_common.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const char* ADD = "add";
29 const char* UPDATE = "update";
30 const char* REMOVE = "remove";
31 static std::shared_mutex g_addListenersMutex;
32 static std::shared_mutex g_updateListenersMutex;
33 static std::shared_mutex g_removeListenersMutex;
34 }
35
BundleMonitorCallback(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)36 BundleMonitorCallback::BundleMonitorCallback(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
37 : EventFwk::CommonEventSubscriber(subscribeInfo) {}
38
~BundleMonitorCallback()39 BundleMonitorCallback::~BundleMonitorCallback() {}
40
BundleMonitorOn(napi_env env,napi_value handler,const std::string & type)41 void BundleMonitorCallback::BundleMonitorOn(napi_env env, napi_value handler, const std::string &type)
42 {
43 APP_LOGD("BundleMonitorOn Enter");
44 if (type != ADD && type != UPDATE && type != REMOVE) {
45 APP_LOGE("input wrong type for on interface");
46 return;
47 }
48 if (type == ADD) {
49 std::unique_lock<std::shared_mutex> lock(g_addListenersMutex);
50 EventListenerAdd(env, handler, addListeners, type);
51 } else if (type == UPDATE) {
52 std::unique_lock<std::shared_mutex> lock(g_updateListenersMutex);
53 EventListenerAdd(env, handler, updateListeners, type);
54 } else {
55 std::unique_lock<std::shared_mutex> lock(g_removeListenersMutex);
56 EventListenerAdd(env, handler, removeListeners, type);
57 }
58 }
59
EventListenerAdd(napi_env env,napi_value handler,std::vector<std::shared_ptr<EventListener>> & eventListeners,const std::string & type)60 void BundleMonitorCallback::EventListenerAdd(napi_env env, napi_value handler,
61 std::vector<std::shared_ptr<EventListener>> &eventListeners, const std::string &type)
62 {
63 for (uint32_t i = 0; i < eventListeners.size(); ++i) {
64 if (eventListeners[i]->HasSameEnv(env)) {
65 eventListeners[i]->Add(env, handler);
66 return;
67 }
68 }
69 std::shared_ptr<EventListener> listener = std::make_shared<EventListener>(env, type);
70 listener->Add(env, handler);
71 eventListeners.push_back(listener);
72 }
73
BundleMonitorOff(napi_env env,napi_value handler,const std::string & type)74 void BundleMonitorCallback::BundleMonitorOff(napi_env env, napi_value handler, const std::string &type)
75 {
76 APP_LOGD("BundleMonitorOff Enter");
77 if (type != ADD && type != UPDATE && type != REMOVE) {
78 APP_LOGE("input wrong type for off interface");
79 return;
80 }
81 if (type == ADD) {
82 std::unique_lock<std::shared_mutex> lock(g_addListenersMutex);
83 EventListenerDelete(env, handler, addListeners);
84 } else if (type == UPDATE) {
85 std::unique_lock<std::shared_mutex> lock(g_updateListenersMutex);
86 EventListenerDelete(env, handler, updateListeners);
87 } else {
88 std::unique_lock<std::shared_mutex> lock(g_removeListenersMutex);
89 EventListenerDelete(env, handler, removeListeners);
90 }
91 }
92
BundleMonitorOff(napi_env env,const std::string & type)93 void BundleMonitorCallback::BundleMonitorOff(napi_env env, const std::string &type)
94 {
95 APP_LOGD("BundleMonitorOff Enter");
96 if (type != ADD && type != UPDATE && type != REMOVE) {
97 APP_LOGE("input wrong type for off interface");
98 return;
99 }
100 if (type == ADD) {
101 std::unique_lock<std::shared_mutex> lock(g_addListenersMutex);
102 EventListenerDeleteAll(env, addListeners);
103 } else if (type == UPDATE) {
104 std::unique_lock<std::shared_mutex> lock(g_updateListenersMutex);
105 EventListenerDeleteAll(env, updateListeners);
106 } else {
107 std::unique_lock<std::shared_mutex> lock(g_removeListenersMutex);
108 EventListenerDeleteAll(env, removeListeners);
109 }
110 }
111
EventListenerDelete(napi_env env,napi_value handler,const std::vector<std::shared_ptr<EventListener>> & eventListeners)112 void BundleMonitorCallback::EventListenerDelete(napi_env env, napi_value handler,
113 const std::vector<std::shared_ptr<EventListener>> &eventListeners)
114 {
115 APP_LOGD("EventListenerDelete Enter");
116 for (auto listener : eventListeners) {
117 if (listener->HasSameEnv(env)) {
118 listener->Delete(env, handler);
119 return;
120 }
121 }
122 }
123
EventListenerDeleteAll(napi_env env,const std::vector<std::shared_ptr<EventListener>> & eventListeners)124 void BundleMonitorCallback::EventListenerDeleteAll(napi_env env,
125 const std::vector<std::shared_ptr<EventListener>> &eventListeners)
126 {
127 APP_LOGD("EventListenerDeleteAll Enter");
128 for (auto listener : eventListeners) {
129 if (listener->HasSameEnv(env)) {
130 listener->DeleteAll();
131 return;
132 }
133 }
134 }
135
136 // operator on js thread
BundleMonitorEmit(const std::string & type,std::string & bundleName,int32_t userId,int32_t appIndex)137 void BundleMonitorCallback::BundleMonitorEmit(const std::string &type,
138 std::string &bundleName, int32_t userId, int32_t appIndex)
139 {
140 APP_LOGD("BundleMonitorEmit enter type is %{public}s", type.c_str());
141 if (type != ADD && type != UPDATE && type != REMOVE) {
142 return;
143 }
144 if (type == ADD) {
145 std::unique_lock<std::shared_mutex> lock(g_addListenersMutex);
146 EventListenerEmit(bundleName, userId, appIndex, addListeners);
147 } else if (type == UPDATE) {
148 std::unique_lock<std::shared_mutex> lock(g_updateListenersMutex);
149 EventListenerEmit(bundleName, userId, appIndex, updateListeners);
150 } else {
151 std::unique_lock<std::shared_mutex> lock(g_removeListenersMutex);
152 EventListenerEmit(bundleName, userId, appIndex, removeListeners);
153 }
154 }
155
EventListenerEmit(std::string & bundleName,int32_t userId,int32_t appIndex,const std::vector<std::shared_ptr<EventListener>> & eventListeners)156 void BundleMonitorCallback::EventListenerEmit(std::string &bundleName, int32_t userId, int32_t appIndex,
157 const std::vector<std::shared_ptr<EventListener>> &eventListeners)
158 {
159 for (auto listener : eventListeners) {
160 listener->Emit(bundleName, userId, appIndex);
161 }
162 }
163
OnReceiveEvent(const EventFwk::CommonEventData & eventData)164 void BundleMonitorCallback::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
165 {
166 APP_LOGD("OnReceiveEvent Enter");
167 OHOS::AAFwk::Want want = eventData.GetWant();
168 std::string action = want.GetAction();
169 std::string bundleName = want.GetElement().GetBundleName();
170 int userId = want.GetIntParam(Constants::USER_ID, Constants::INVALID_USERID);
171 int32_t appIndex = want.GetIntParam(Constants::APP_INDEX, Constants::DEFAULT_APP_INDEX);
172 APP_LOGI_NOFUNC("monitor callback OnReceiveEvent action:%{public}s -n %{public}s -u %{public}d -i %{public}d",
173 action.c_str(), bundleName.c_str(), userId, appIndex);
174 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
175 BundleMonitorEmit(ADD, bundleName, userId, appIndex);
176 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
177 BundleMonitorEmit(UPDATE, bundleName, userId, appIndex);
178 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
179 BundleMonitorEmit(REMOVE, bundleName, userId, appIndex);
180 } else {
181 APP_LOGI("OnReceiveEvent action = %{public}s not support", action.c_str());
182 }
183 }
184 }
185 }