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 #include <mutex> 17 18 #include "iservice_registry.h" 19 20 #include "asset_log.h" 21 22 namespace { 23 const int32_t LOAD_TIMEOUT_IN_SECONDS = 2; 24 std::mutex g_serviceLock; 25 } 26 LoadService(int32_t saId)27extern "C" bool LoadService(int32_t saId) 28 { 29 auto samgrProxy = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 30 if (samgrProxy == nullptr) { 31 LOGE("[FATAL][SA]Get system ability manager proxy failed."); 32 return false; 33 } 34 auto object = samgrProxy->CheckSystemAbility(saId); 35 if (object != nullptr) { 36 return true; 37 } 38 39 std::lock_guard<std::mutex> lock(g_serviceLock); 40 object = samgrProxy->CheckSystemAbility(saId); 41 if (object != nullptr) { 42 return true; 43 } 44 return samgrProxy->LoadSystemAbility(saId, LOAD_TIMEOUT_IN_SECONDS) != nullptr; 45 } 46