1 /*
2  * Copyright (c) 2023-2024 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 #include "service_router_mgr_helper.h"
16 
17 #include <unistd.h>
18 
19 #include "bundle_constants.h"
20 #include "hilog_tag_wrapper.h"
21 #include "iservice_registry.h"
22 #include "service_router_load_callback.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28 static const int LOAD_SA_TIMEOUT_MS = 60000;
29 }
ServiceRouterMgrHelper()30 ServiceRouterMgrHelper::ServiceRouterMgrHelper()
31 {}
32 
~ServiceRouterMgrHelper()33 ServiceRouterMgrHelper::~ServiceRouterMgrHelper()
34 {}
35 
OnRemoteDiedHandle()36 void ServiceRouterMgrHelper::OnRemoteDiedHandle()
37 {
38     TAG_LOGE(AAFwkTag::SER_ROUTER, "Remove died");
39     SetServiceRouterMgr(nullptr);
40     std::unique_lock<std::mutex> lock(cvLock_);
41     isReady = false;
42 }
43 
SetServiceRouterMgr(const sptr<IServiceRouterManager> & serviceRouterMgr)44 void ServiceRouterMgrHelper::SetServiceRouterMgr(const sptr<IServiceRouterManager> &serviceRouterMgr)
45 {
46     std::unique_lock<std::mutex> lock(mgrMutex_);
47     routerMgr_ = serviceRouterMgr;
48 }
49 
InnerGetServiceRouterMgr()50 sptr<IServiceRouterManager> ServiceRouterMgrHelper::InnerGetServiceRouterMgr()
51 {
52     std::unique_lock<std::mutex> lock(mgrMutex_);
53     return routerMgr_;
54 }
55 
LoadSA()56 void ServiceRouterMgrHelper::LoadSA()
57 {
58     {
59         std::unique_lock<std::mutex> lock(cvLock_);
60         isReady = false;
61     }
62     sptr<ISystemAbilityManager> saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63     if (saManager == nullptr) {
64         TAG_LOGE(AAFwkTag::SER_ROUTER, "null saManager");
65         return;
66     }
67 
68     sptr<ServiceRouterLoadCallback> loadCallback = new (std::nothrow) ServiceRouterLoadCallback();
69     if (loadCallback == nullptr) {
70         TAG_LOGE(AAFwkTag::SER_ROUTER, "null loadCallback");
71         return;
72     }
73     int32_t result = saManager->LoadSystemAbility(OHOS::SERVICE_ROUTER_MGR_SERVICE_ID, loadCallback);
74     if (result != ERR_OK) {
75         TAG_LOGE(AAFwkTag::SER_ROUTER, "LoadSystemAbility result: %{public}d", result);
76         return;
77     }
78 }
79 
FinishStartSASuccess(const sptr<IRemoteObject> & remoteObject)80 void ServiceRouterMgrHelper::FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject)
81 {
82     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
83     SetServiceRouterMgr(OHOS::iface_cast<IServiceRouterManager>(remoteObject));
84 
85     {
86         std::unique_lock<std::mutex> lock(cvLock_);
87         isReady = true;
88     }
89     mgrConn_.notify_one();
90 
91     serviceDeathObserver_ = new (std::nothrow) ServiceRouterDeathRecipient();
92     if (serviceDeathObserver_ != nullptr) {
93         remoteObject->AddDeathRecipient(serviceDeathObserver_);
94     }
95 }
96 
FinishStartSAFail()97 void ServiceRouterMgrHelper::FinishStartSAFail()
98 {
99     TAG_LOGI(AAFwkTag::SER_ROUTER, "Called");
100     SetServiceRouterMgr(nullptr);
101 
102     {
103         std::unique_lock<std::mutex> lock(cvLock_);
104         isReady = false;
105     }
106     mgrConn_.notify_one();
107 }
108 
GetServiceRouterMgr()109 sptr<IServiceRouterManager> ServiceRouterMgrHelper::GetServiceRouterMgr()
110 {
111     auto routerMgr = InnerGetServiceRouterMgr();
112     if (routerMgr != nullptr) {
113         return routerMgr;
114     }
115 
116     LoadSA();
117 
118     {
119         std::unique_lock<std::mutex> lock(cvLock_);
120         auto waitState = mgrConn_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
121             [this]() {
122                 return isReady;
123             });
124         if (!waitState) {
125             return nullptr;
126         }
127     }
128 
129     routerMgr = InnerGetServiceRouterMgr();
130     if (routerMgr == nullptr) {
131         TAG_LOGE(AAFwkTag::SER_ROUTER, "null routerMgr");
132     }
133     return routerMgr;
134 }
135 } // namespace AbilityRuntime
136 } // namespace OHOS
137