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