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