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 "access_manager.h"
17
18 #include "string_utils.h"
19 #include "update_define.h"
20 #include "update_log.h"
21
22 namespace OHOS {
23 namespace UpdateEngine {
AccessManager()24 AccessManager::AccessManager()
25 {
26 ENGINE_LOGD("AccessManager constructor");
27 }
28
~AccessManager()29 AccessManager::~AccessManager()
30 {
31 ENGINE_LOGD("AccessManager deConstructor");
32 }
33
IsIdle()34 bool AccessManager::IsIdle()
35 {
36 if (!isRemoteIdle_) {
37 ENGINE_LOGI("AccessManager remote not idle");
38 return false;
39 }
40
41 std::lock_guard<std::recursive_mutex> guard(mutex_);
42 for (auto &[type, access] : accessMap_) {
43 if (access != nullptr && !access->IsIdle()) {
44 return false;
45 }
46 }
47 return true;
48 }
49
GetScheduleTasks()50 std::vector<ScheduleTask> AccessManager::GetScheduleTasks()
51 {
52 std::lock_guard<std::recursive_mutex> guard(mutex_);
53 ENGINE_LOGI("AccessManager GetScheduleTasks");
54 std::vector<ScheduleTask> scheduleTasks;
55 for (auto &[type, access] : accessMap_) {
56 if (access != nullptr) {
57 std::vector<ScheduleTask> tasks = access->GetScheduleTasks();
58 scheduleTasks.insert(scheduleTasks.end(), tasks.begin(), tasks.end());
59 }
60 }
61 return scheduleTasks;
62 }
63
Exit()64 bool AccessManager::Exit()
65 {
66 std::lock_guard<std::recursive_mutex> guard(mutex_);
67 ENGINE_LOGI("AccessManager Exit");
68 for (auto &[type, access] : accessMap_) {
69 if (access != nullptr && !access->Exit()) {
70 return false;
71 }
72 }
73 return true;
74 }
75
Register(AccessType type,const std::shared_ptr<IAccess> & access)76 bool AccessManager::Register(AccessType type, const std::shared_ptr<IAccess> &access)
77 {
78 if (access == nullptr) {
79 ENGINE_LOGE("AccessManager Register access is null");
80 return false;
81 }
82 std::lock_guard<std::recursive_mutex> guard(mutex_);
83 ENGINE_LOGI("AccessManager Register: type is %{public}d", CAST_INT(type));
84 accessMap_[type] = access;
85 return true;
86 }
87
Unregister(AccessType type)88 bool AccessManager::Unregister(AccessType type)
89 {
90 std::lock_guard<std::recursive_mutex> guard(mutex_);
91 if (accessMap_.find(type) == accessMap_.end()) {
92 ENGINE_LOGI("AccessManager Unregister type: %{public}d not exist", CAST_INT(type));
93 return false;
94 }
95 ENGINE_LOGI("AccessManager Unregister: type is %{public}d", CAST_INT(type));
96 accessMap_.erase(type);
97 return true;
98 }
99
SetRemoteIdle(bool isRemoteIdle)100 void AccessManager::SetRemoteIdle(bool isRemoteIdle)
101 {
102 ENGINE_LOGI("AccessManager SetRemoteIdle %{public}s", StringUtils::GetBoolStr(isRemoteIdle).c_str());
103 isRemoteIdle_ = isRemoteIdle;
104 }
105 } // namespace UpdateEngine
106 } // namespace OHOS