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