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 #ifndef OHOS_ACE_UI_SERVICE_MANAGER_SERVICE_H
17 #define OHOS_ACE_UI_SERVICE_MANAGER_SERVICE_H
18 
19 #include <memory>
20 #include <singleton.h>
21 #include <thread_ex.h>
22 #include <unordered_map>
23 
24 #include "element_name.h"
25 #include "event_handler.h"
26 #include "event_runner.h"
27 #include "iremote_object.h"
28 
29 #include "system_ability.h"
30 #include "ui_service_mgr_stub.h"
31 
32 namespace OHOS {
33 namespace Ace {
34 using EventRunner = OHOS::AppExecFwk::EventRunner;
35 using EventHandler = OHOS::AppExecFwk::EventHandler;
36 enum class UIServiceRunningState { STATE_NOT_START, STATE_RUNNING };
37 
38 class UIMgrService : public SystemAbility,
39                           public UIServiceMgrStub,
40                           public std::enable_shared_from_this<UIMgrService> {
41     DECLARE_DELAYED_SINGLETON(UIMgrService)
42     DECLEAR_SYSTEM_ABILITY(UIMgrService)
43 public:
44     void OnStart() override;
45     void OnStop() override;
46     UIServiceRunningState QueryServiceState() const;
47 
48     int32_t RegisterCallBack(const AAFwk::Want& want, const sptr<IUIService>& uiService) override;
49     int32_t UnregisterCallBack(const AAFwk::Want& want) override;
50     int32_t Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
51         const std::string& data, const std::string& extraData) override;
52 
53     int32_t Request(const AAFwk::Want& want, const std::string& name, const std::string& data) override;
54     int32_t ReturnRequest(const AAFwk::Want& want, const std::string& source, const std::string& data,
55         const std::string& extraData) override;
56 
57     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
58 
59 private:
60     bool Init();
61     bool CheckCallBackFromMap(const std::string& key);
62     int32_t HandleRegister(const AAFwk::Want& want,  const sptr<IUIService>& uiService);
63     int32_t HandleUnregister(const AAFwk::Want& want);
64 
65     std::string GetCallBackKeyStr(const AAFwk::Want& want);
66     std::shared_ptr<EventRunner> eventLoop_;
67     std::shared_ptr<EventHandler> handler_;
68     UIServiceRunningState state_;
69 
70     std::map<std::string, sptr<IUIService>> callbackMap_;
71     std::recursive_mutex uiMutex_;
72 };
73 }  // namespace Ace
74 }  // namespace OHOS
75 #endif  // OHOS_AAFWK_UI_SERVICE_MANAGER_SERVICE_H
76