1 /* 2 * Copyright (c) 2020 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 <cstring> 17 #include "ability_record.h" 18 #include "ability_list.h" 19 20 21 namespace OHOS { 22 23 namespace { 24 constexpr static uint16_t ABILITY_LIST_CAPACITY = 10240; 25 } 26 Add(AbilityRecord * abilityRecord)27void AbilityList::Add(AbilityRecord *abilityRecord) 28 { 29 if (abilityRecord == nullptr || abilityList_.Size() >= ABILITY_LIST_CAPACITY) { 30 return; 31 } 32 33 if (Get(abilityRecord->GetToken()) == nullptr) { 34 abilityList_.PushBack(abilityRecord); 35 } 36 } 37 Get(uint16_t token) const38AbilityRecord *AbilityList::Get(uint16_t token) const 39 { 40 for (auto node = abilityList_.Begin(); node != abilityList_.End(); node = node->next_) { 41 AbilityRecord *record = node->value_; 42 if (record == nullptr) { 43 continue; 44 } 45 if (record->GetToken() == token) { 46 return record; 47 } 48 } 49 50 return nullptr; 51 } 52 Get(const char * bundleName) const53AbilityRecord *AbilityList::Get(const char *bundleName) const 54 { 55 if (bundleName == nullptr) { 56 return nullptr; 57 } 58 59 for (auto node = abilityList_.Begin(); node != abilityList_.End(); node = node->next_) { 60 AbilityRecord *record = node->value_; 61 if (record == nullptr || record->GetAppName() == nullptr) { 62 continue; 63 } 64 if (strcmp(bundleName, record->GetAppName()) == 0) { 65 return record; 66 } 67 } 68 69 return nullptr; 70 } 71 GetByTaskId(uint32_t taskId) const72AbilityRecord *AbilityList::GetByTaskId(uint32_t taskId) const 73 { 74 for (auto node = abilityList_.Begin(); node != abilityList_.End(); node = node->next_) { 75 AbilityRecord *record = node->value_; 76 if (record == nullptr) { 77 continue; 78 } 79 if (record->GetTaskId() == taskId) { 80 return record; 81 } 82 } 83 84 return nullptr; 85 } 86 Erase(uint16_t token)87void AbilityList::Erase(uint16_t token) 88 { 89 for (auto node = abilityList_.Begin(); node != abilityList_.End(); node = node->next_) { 90 AbilityRecord *record = node->value_; 91 if (record == nullptr) { 92 continue; 93 } 94 if (record->GetToken() == token) { 95 abilityList_.Remove(node); 96 return; 97 } 98 } 99 } 100 } // namespace OHOS 101