1 /* 2 * Copyright (c) 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 #include <unistd.h> 17 18 #include "container_manager_client.h" 19 20 #include "string_ex.h" 21 #include "icontainer_manager.h" 22 #include "hilog_wrapper.h" 23 #include "if_system_ability_manager.h" 24 #include "ipc_skeleton.h" 25 #include "iservice_registry.h" 26 #include "system_ability_definition.h" 27 28 namespace OHOS { 29 namespace AAFwk { 30 const int32_t CONTAINER_MANAGER_ABILITY_ID = 19000; 31 const int32_t MAX_RETRY_TIME = 50; 32 const int32_t RETRY_INTERVAL = 100 * 1000; 33 std::shared_ptr<ContainerManagerClient> ContainerManagerClient::instance_ = nullptr; 34 std::mutex ContainerManagerClient::mutex_; 35 36 #define CHECK_REMOTE_OBJECT_AND_RETURN(object, value) \ 37 if (!(object)) { \ 38 if (ERR_OK != Connect()) { \ 39 HILOG_ERROR("container manager can't connect."); \ 40 return (value); \ 41 } \ 42 } 43 GetInstance()44std::shared_ptr<ContainerManagerClient> ContainerManagerClient::GetInstance() 45 { 46 if (instance_ == nullptr) { 47 std::lock_guard<std::mutex> lock_l(mutex_); 48 if (instance_ == nullptr) { 49 instance_ = std::make_shared<ContainerManagerClient>(); 50 } 51 } 52 return instance_; 53 } 54 ContainerManagerClient()55ContainerManagerClient::ContainerManagerClient() 56 {} 57 ~ContainerManagerClient()58ContainerManagerClient::~ContainerManagerClient() 59 {} 60 Connect()61ErrCode ContainerManagerClient::Connect() 62 { 63 std::lock_guard<std::mutex> lock(mutex_); 64 if (remoteObject_ != nullptr) { 65 return ERR_OK; 66 } 67 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 68 if (systemManager == nullptr) { 69 HILOG_ERROR("Fail to get registry."); 70 return -1; 71 } 72 remoteObject_ = systemManager->GetSystemAbility(CONTAINER_MANAGER_ABILITY_ID); 73 74 uint32_t waitCnt = 0; 75 while (waitCnt < MAX_RETRY_TIME && remoteObject_ == nullptr) { 76 usleep(RETRY_INTERVAL); // 100ms 77 remoteObject_ = systemManager->GetSystemAbility(CONTAINER_MANAGER_ABILITY_ID); 78 waitCnt++; 79 } 80 81 if (remoteObject_ == nullptr) { 82 HILOG_ERROR("Fail to connect container manager service."); 83 return -1; 84 } 85 86 HILOG_DEBUG("Connect container manager service success."); 87 return ERR_OK; 88 } 89 NotifyBootComplete(int state)90ErrCode ContainerManagerClient::NotifyBootComplete(int state) 91 { 92 CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, -1); 93 sptr<IContainerManager> container_manager = iface_cast<IContainerManager>(remoteObject_); 94 if (container_manager == nullptr) { 95 HILOG_ERROR("ContainerManagerClient::NotifyBootComplete, container_manager is nullptr"); 96 return ERR_NULL_OBJECT; 97 } 98 HILOG_INFO("NotifyBootComplete"); 99 return container_manager->NotifyBootComplete(state); 100 } 101 } // namespace AAFwk 102 } // namespace AAFwk