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 "i18n_hilog.h" 17 #include "i18n_service_ability_load_callback.h" 18 #include "i18n_service_ability_load_manager.h" 19 20 namespace OHOS { 21 namespace Global { 22 namespace I18n { 23 static constexpr int32_t I18N_LOAD_SA_TIMEOUT_MS = 1000; 24 I18nServiceAbilityLoadManager()25I18nServiceAbilityLoadManager::I18nServiceAbilityLoadManager() 26 { 27 } 28 ~I18nServiceAbilityLoadManager()29I18nServiceAbilityLoadManager::~I18nServiceAbilityLoadManager() 30 { 31 } 32 GetI18nServiceAbility(int32_t systemAbilityId)33sptr<IRemoteObject> I18nServiceAbilityLoadManager::GetI18nServiceAbility(int32_t systemAbilityId) 34 { 35 sptr<ISystemAbilityManager> samgr = LoadI18nServiceAbility(systemAbilityId); 36 if (samgr == nullptr) { 37 // try again 38 samgr = LoadI18nServiceAbility(systemAbilityId); 39 } 40 if (samgr == nullptr) { 41 return nullptr; 42 } 43 return samgr->GetSystemAbility(systemAbilityId, ""); 44 } 45 LoadI18nServiceAbility(int32_t systemAbilityId)46sptr<ISystemAbilityManager> I18nServiceAbilityLoadManager::LoadI18nServiceAbility(int32_t systemAbilityId) 47 { 48 InitLoadState(); 49 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 50 if (samgr == nullptr) { 51 HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility can't get samgr"); 52 return nullptr; 53 } 54 sptr<I18nServiceAbilityLoadCallback> i18nSaLoadCallback = new I18nServiceAbilityLoadCallback(); 55 int32_t ret = samgr->LoadSystemAbility(systemAbilityId, i18nSaLoadCallback); 56 if (ret != ERR_OK) { 57 HILOG_ERROR_I18N( 58 "I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility LoadSystemAbility failed."); 59 return nullptr; 60 } 61 bool status = WaitLoadStateChange(systemAbilityId); 62 if (!status) { 63 HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility wait overtime."); 64 return nullptr; 65 } 66 return samgr; 67 } 68 InitLoadState()69void I18nServiceAbilityLoadManager::InitLoadState() 70 { 71 std::unique_lock<std::mutex> lock(loadStateMutex); 72 loadState = false; 73 } 74 WaitLoadStateChange(int32_t systemAbilityId)75bool I18nServiceAbilityLoadManager::WaitLoadStateChange(int32_t systemAbilityId) 76 { 77 std::unique_lock<std::mutex> lock(loadStateMutex); 78 auto isLoadSuccess = loadStateCondition.wait_for(lock, std::chrono::milliseconds(I18N_LOAD_SA_TIMEOUT_MS), [this] { 79 return loadState; 80 }); 81 if (!isLoadSuccess) { 82 HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::WaitLoadStateChange timeout."); 83 } 84 return isLoadSuccess; 85 } 86 UnloadI18nService(int32_t systemAbilityId)87bool I18nServiceAbilityLoadManager::UnloadI18nService(int32_t systemAbilityId) 88 { 89 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 90 if (samgr == nullptr) { 91 HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService can't get samgr."); 92 return false; 93 } 94 int32_t ret = samgr->UnloadSystemAbility(systemAbilityId); 95 if (ret != ERR_OK) { 96 HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService sa unload failed."); 97 return false; 98 } 99 return true; 100 } 101 LoadSystemAbilitySuccess()102void I18nServiceAbilityLoadManager::LoadSystemAbilitySuccess() 103 { 104 std::unique_lock<std::mutex> lock(loadStateMutex); 105 loadState = true; 106 loadStateCondition.notify_one(); 107 } 108 LoadSystemAbilityFail()109void I18nServiceAbilityLoadManager::LoadSystemAbilityFail() 110 { 111 std::unique_lock<std::mutex> lock(loadStateMutex); 112 loadState = false; 113 loadStateCondition.notify_one(); 114 } 115 } // namespace I18n 116 } // namespace Global 117 } // namespace OHOS