1 /*
2  * Copyright (c) 2023 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_ABILITY_RUNTIME_SERVICE_ROUTER_FRAMEWORK_INCLUDE_SERVICE_ROUTER_SA_HEALPER_H
17 #define OHOS_ABILITY_RUNTIME_SERVICE_ROUTER_FRAMEWORK_INCLUDE_SERVICE_ROUTER_SA_HEALPER_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <singleton.h>
22 
23 #include "service_router_death_recipient.h"
24 #include "service_router_mgr_interface.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 
29 /**
30  * @class SystemAbilityManager Helper
31  * Bms helpler.
32  */
33 class ServiceRouterMgrHelper final : public DelayedRefSingleton<ServiceRouterMgrHelper> {
34     DECLARE_DELAYED_REF_SINGLETON(ServiceRouterMgrHelper)
35 
36 public:
37     DISALLOW_COPY_AND_MOVE(ServiceRouterMgrHelper);
38 
39     /**
40      * @brief Acquire a service router manager, if it not existed,
41      * @return returns the service router manager ipc object, or nullptr for failed.
42      */
43     sptr<IServiceRouterManager> GetServiceRouterMgr();
44 
45     void LoadSA();
46     void FinishStartSAFail();
47     void FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject);
48     void OnRemoteDiedHandle();
49 
50 private:
51     void SetServiceRouterMgr(const sptr<IServiceRouterManager> &serviceRouterMgr);
52     sptr<IServiceRouterManager> InnerGetServiceRouterMgr();
53 
54     std::mutex mgrMutex_;
55     std::mutex cvLock_;
56     bool isReady = false;
57     std::condition_variable mgrConn_;
58     sptr<ServiceRouterDeathRecipient> serviceDeathObserver_;
59     sptr<IServiceRouterManager> routerMgr_ = nullptr;
60 };
61 } // namespace AbilityRuntime
62 }  // namespace OHOS
63 #endif // OHOS_ABILITY_RUNTIME_SERVICE_ROUTER_FRAMEWORK_INCLUDE_SERVICE_ROUTER_SA_HEALPER_H
64