1 /*
2 * Copyright (c) 2021-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 "ui_mgr_service.h"
17
18 #include "ui_service_mgr_errors.h"
19 #include "xcollie/watchdog.h"
20
21 #include "ui_service_hilog.h"
22 namespace OHOS {
23 namespace Ace {
24 namespace {
25 constexpr int32_t UI_MGR_SERVICE_SA_ID = 7001;
26 constexpr uint32_t WATCHDOG_TIMEVAL = 5000;
27 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton<UIMgrService>::GetInstance().get());
28 } // namespace
29
30 // UiservicePluginDialog UIMgrService::dialogPlugin_;
31
UIMgrService()32 UIMgrService::UIMgrService()
33 : SystemAbility(UI_MGR_SERVICE_SA_ID, true), eventLoop_(nullptr), handler_(nullptr),
34 state_(UIServiceRunningState::STATE_NOT_START)
35 {}
36
~UIMgrService()37 UIMgrService::~UIMgrService()
38 {
39 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
40 callbackMap_.clear();
41 }
42
Dump(int32_t fd,const std::vector<std::u16string> & args)43 int32_t UIMgrService::Dump(int32_t fd, const std::vector<std::u16string>& args)
44 {
45 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
46 dprintf(fd, "total callbacks: %u\n", callbackMap_.size());
47 if (!callbackMap_.empty()) {
48 dprintf(fd, "callback keys: \n");
49 }
50 for (const auto& callback : callbackMap_) {
51 dprintf(fd, " %s\n", callback.first.c_str());
52 }
53 return UI_SERVICE_NO_ERROR;
54 }
55
OnStart()56 void UIMgrService::OnStart()
57 {
58 if (state_ == UIServiceRunningState::STATE_RUNNING) {
59 return;
60 }
61 if (!Init()) {
62 return;
63 }
64 state_ = UIServiceRunningState::STATE_RUNNING;
65 eventLoop_->Run();
66
67 /* Publish service maybe failed, so we need call this function at the last,
68 * so it can't affect the TDD test program */
69 bool ret = Publish(DelayedSingleton<UIMgrService>::GetInstance().get());
70 if (!ret) {
71 return;
72 }
73 LOGI("Ace UImanager service OnStart");
74 }
75
Init()76 bool UIMgrService::Init()
77 {
78 eventLoop_ = AppExecFwk::EventRunner::Create("UIMgrService");
79 if (eventLoop_ == nullptr) {
80 return false;
81 }
82
83 handler_ = std::make_shared<AppExecFwk::EventHandler>(eventLoop_);
84 if (handler_ == nullptr) {
85 return false;
86 }
87
88 int32_t ret = HiviewDFX::Watchdog::GetInstance().AddThread("UIMgrService", handler_, WATCHDOG_TIMEVAL);
89 if (ret != 0) {
90 LOGW("Add watchdog thread failed");
91 }
92
93 LOGI("Ace UIservice init success");
94 return true;
95 }
96
OnStop()97 void UIMgrService::OnStop()
98 {
99 eventLoop_->Stop();
100 eventLoop_.reset();
101 handler_.reset();
102 state_ = UIServiceRunningState::STATE_NOT_START;
103 LOGI("Ace UImanager service stop");
104 }
105
QueryServiceState() const106 UIServiceRunningState UIMgrService::QueryServiceState() const
107 {
108 return state_;
109 }
110
RegisterCallBack(const AAFwk::Want & want,const sptr<IUIService> & uiService)111 int32_t UIMgrService::RegisterCallBack(const AAFwk::Want& want, const sptr<IUIService>& uiService)
112 {
113 if (uiService == nullptr) {
114 return UI_SERVICE_IS_NULL;
115 }
116 if (handler_ == nullptr) {
117 return UI_SERVICE_HANDLER_IS_NULL;
118 }
119 std::function<void()> registerFunc = std::bind(&UIMgrService::HandleRegister, shared_from_this(), want, uiService);
120 bool ret = handler_->PostTask(registerFunc);
121 if (!ret) {
122 return UI_SERVICE_POST_TASK_FAILED;
123 }
124 LOGI("UIServices register CallBack success");
125 return NO_ERROR;
126 }
127
UnregisterCallBack(const AAFwk::Want & want)128 int32_t UIMgrService::UnregisterCallBack(const AAFwk::Want& want)
129 {
130 if (handler_ == nullptr) {
131 return UI_SERVICE_HANDLER_IS_NULL;
132 }
133 std::function<void()> unregisterFunc = std::bind(&UIMgrService::HandleUnregister, shared_from_this(), want);
134 bool ret = handler_->PostTask(unregisterFunc);
135 if (!ret) {
136 return UI_SERVICE_POST_TASK_FAILED;
137 }
138 LOGI("UIServices unregister CallBack success");
139 return NO_ERROR;
140 }
141
Push(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)142 int32_t UIMgrService::Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
143 const std::string& data, const std::string& extraData)
144 {
145 std::map<std::string, sptr<IUIService>> callbackMap;
146 {
147 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
148 callbackMap = std::map<std::string, sptr<IUIService>>(callbackMap_);
149 }
150 for (auto iter = callbackMap.begin(); iter != callbackMap.end(); ++iter) {
151 sptr<IUIService> uiService = iter->second;
152 if (uiService == nullptr) {
153 return UI_SERVICE_IS_NULL;
154 }
155 uiService->OnPushCallBack(want, name, jsonPath, data, extraData);
156 }
157 return NO_ERROR;
158 }
159
Request(const AAFwk::Want & want,const std::string & name,const std::string & data)160 int32_t UIMgrService::Request(const AAFwk::Want& want, const std::string& name, const std::string& data)
161 {
162 std::map<std::string, sptr<IUIService>> callbackMap;
163 {
164 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
165 callbackMap = std::map<std::string, sptr<IUIService>>(callbackMap_);
166 }
167 for (auto iter = callbackMap.begin(); iter != callbackMap.end(); ++iter) {
168 sptr<IUIService> uiService = iter->second;
169 if (uiService == nullptr) {
170 return UI_SERVICE_IS_NULL;
171 }
172 uiService->OnRequestCallBack(want, name, data);
173 }
174 return NO_ERROR;
175 }
176
ReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)177 int32_t UIMgrService::ReturnRequest(
178 const AAFwk::Want& want, const std::string& source, const std::string& data, const std::string& extraData)
179 {
180 std::map<std::string, sptr<IUIService>> callbackMap;
181 {
182 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
183 callbackMap = std::map<std::string, sptr<IUIService>>(callbackMap_);
184 }
185 for (auto iter = callbackMap.begin(); iter != callbackMap.end(); ++iter) {
186 sptr<IUIService> uiService = iter->second;
187 if (uiService == nullptr) {
188 return UI_SERVICE_IS_NULL;
189 }
190 uiService->OnReturnRequest(want, source, data, extraData);
191 }
192 return NO_ERROR;
193 }
194
HandleRegister(const AAFwk::Want & want,const sptr<IUIService> & uiService)195 int32_t UIMgrService::HandleRegister(const AAFwk::Want& want, const sptr<IUIService>& uiService)
196 {
197 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
198 std::string keyStr = GetCallBackKeyStr(want);
199 bool exist = CheckCallBackFromMap(keyStr);
200 if (exist) {
201 callbackMap_.erase(keyStr);
202 }
203 callbackMap_.emplace(keyStr, uiService);
204 return NO_ERROR;
205 }
206
HandleUnregister(const AAFwk::Want & want)207 int32_t UIMgrService::HandleUnregister(const AAFwk::Want& want)
208 {
209 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
210 std::string keyStr = GetCallBackKeyStr(want);
211 bool exist = CheckCallBackFromMap(keyStr);
212 if (!exist) {
213 return NO_CALLBACK_FOR_KEY;
214 }
215 callbackMap_.erase(keyStr);
216 return NO_ERROR;
217 }
218
GetCallBackKeyStr(const AAFwk::Want & want)219 std::string UIMgrService::GetCallBackKeyStr(const AAFwk::Want& want)
220 {
221 AppExecFwk::ElementName element = want.GetElement();
222 std::string bundleName = element.GetBundleName();
223 std::string keyStr = bundleName;
224 return keyStr;
225 }
226
CheckCallBackFromMap(const std::string & key)227 bool UIMgrService::CheckCallBackFromMap(const std::string& key)
228 {
229 std::lock_guard<std::recursive_mutex> lock(uiMutex_);
230 auto it = callbackMap_.find(key);
231 if (it == callbackMap_.end()) {
232 return false;
233 }
234 return true;
235 }
236 } // namespace Ace
237 } // namespace OHOS
238