• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::string ADD = "add";
29     const std::string UPDATE = "update";
30     const std::string 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)137 void BundleMonitorCallback::BundleMonitorEmit(const std::string &type, std::string &bundleName, int32_t userId)
138 {
139     APP_LOGD("BundleMonitorEmit enter type is %{public}s", type.c_str());
140     if (type != ADD && type != UPDATE && type != REMOVE) {
141         return;
142     }
143     if (type == ADD) {
144         std::unique_lock<std::shared_mutex> lock(g_addListenersMutex);
145         EventListenerEmit(bundleName, userId, addListeners);
146     } else if (type == UPDATE) {
147         std::unique_lock<std::shared_mutex> lock(g_updateListenersMutex);
148         EventListenerEmit(bundleName, userId, updateListeners);
149     } else {
150         std::unique_lock<std::shared_mutex> lock(g_removeListenersMutex);
151         EventListenerEmit(bundleName, userId, removeListeners);
152     }
153 }
154 
EventListenerEmit(std::string & bundleName,int32_t userId,const std::vector<std::shared_ptr<EventListener>> & eventListeners)155 void BundleMonitorCallback::EventListenerEmit(std::string &bundleName, int32_t userId,
156     const std::vector<std::shared_ptr<EventListener>> &eventListeners)
157 {
158     for (auto listener : eventListeners) {
159         listener->Emit(bundleName, userId);
160     }
161 }
162 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)163 void BundleMonitorCallback::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
164 {
165     APP_LOGD("OnReceiveEvent Enter");
166     OHOS::AAFwk::Want want = eventData.GetWant();
167     std::string action = want.GetAction();
168     std::string bundleName = want.GetElement().GetBundleName();
169     int userId = want.GetIntParam(Constants::USER_ID, Constants::INVALID_USERID);
170     APP_LOGD("OnReceiveEvent action = %{public}s, bundle = %{public}s, userId = %{public}d",
171         action.c_str(), bundleName.c_str(), userId);
172     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
173         BundleMonitorEmit(ADD, bundleName, userId);
174     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
175         BundleMonitorEmit(UPDATE, bundleName, userId);
176     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
177         BundleMonitorEmit(REMOVE, bundleName, userId);
178     } else {
179         APP_LOGI("OnReceiveEvent action = %{public}s not support", action.c_str());
180     }
181 }
182 }
183 }