1 /* 2 * Copyright (c) 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 16 #include "module_update.h" 17 #include "module_update_task.h" 18 #include "log/log.h" 19 20 namespace OHOS { 21 namespace SysInstaller { 22 using namespace Updater; 23 namespace { TaskCallback(ModuleUpdateTask task)24void TaskCallback(ModuleUpdateTask task) 25 { 26 std::string hmpName = task.GetHmpName(); 27 LOG(INFO) << "module update callback, hmp name=" << hmpName; 28 ModuleUpdateStatus status; 29 status.hmpName = hmpName; 30 status.isHotInstall = false; 31 auto ret = ModuleUpdate::GetInstance().DoModuleUpdate(status); 32 ModuleUpdateTaskManager::GetInstance().SetTaskResult(ret); 33 } 34 } 35 ModuleUpdateTask(const std::string & hmpName)36ModuleUpdateTask::ModuleUpdateTask(const std::string &hmpName) 37 { 38 SetHmpName(hmpName); 39 } 40 SetHmpName(const std::string & hmpName)41void ModuleUpdateTask::SetHmpName(const std::string &hmpName) 42 { 43 hmpName_ = hmpName; 44 } 45 GetHmpName()46std::string &ModuleUpdateTask::GetHmpName() 47 { 48 return hmpName_; 49 } 50 SetTaskResult(bool result)51void ModuleUpdateTaskManager::SetTaskResult(bool result) 52 { 53 taskResult_ = taskResult_ && result; 54 } 55 GetTaskResult()56bool ModuleUpdateTaskManager::GetTaskResult() 57 { 58 return taskResult_; 59 } 60 ClearTask()61void ModuleUpdateTaskManager::ClearTask() 62 { 63 taskNum_ = 0; 64 } 65 AddTask(const std::string & hmpName)66bool ModuleUpdateTaskManager::AddTask(const std::string &hmpName) 67 { 68 LOG(INFO) << "add task, hmp name=" << hmpName; 69 if (taskNum_ >= MAX_TASK_NUM) { 70 LOG(ERROR) << "add task failed:" << taskNum_; 71 return false; 72 } 73 pool_.AddTask([hmpName] { 74 ModuleUpdateTask task = ModuleUpdateTask(hmpName); 75 TaskCallback(task); 76 }); 77 taskNum_++; 78 return true; 79 } 80 GetCurTaskNum()81size_t ModuleUpdateTaskManager::GetCurTaskNum() 82 { 83 return pool_.GetCurTaskNum(); 84 } 85 Stop()86void ModuleUpdateTaskManager::Stop() 87 { 88 pool_.Stop(); 89 } 90 Start()91void ModuleUpdateTaskManager::Start() 92 { 93 pool_.Start(1); 94 pool_.SetMaxTaskNum(MAX_TASK_NUM); 95 taskNum_ = 0; 96 } 97 } 98 }