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 "AppTimerAdapter.h" 17 18namespace OHOS { 19namespace HiviewDFX { 20AppTimerAdapter::AppTimerAdapter(int userId, ISceneTimerInfrastructure* infra) : userId(userId) 21{ 22 this->impl = infra; 23 impl->RegUser(userId, this); 24} 25 26AppTimerAdapter::~AppTimerAdapter() 27{ 28 impl->UnRegUser(userId); 29} 30 31void AppTimerAdapter::Start(std::string key) 32{ 33 std::lock_guard<std::mutex> uniqueLock(mut); 34 int id = AssignId(key); 35 ValidateDuplication(id); 36 impl->Start(userId, id, timeoutPeriod); 37 sessions.push_back(id); 38} 39 40void AppTimerAdapter::Stop(std::string key) 41{ 42 std::lock_guard<std::mutex> uniqueLock(mut); 43 int id = KeyToId(key); 44 ValidateExistence(id); 45 impl->Stop(userId, id); 46 sessions.erase(std::find(sessions.begin(), sessions.end(), id)); 47 RecycleId(id); 48} 49 50void AppTimerAdapter::Expired(int id) 51{ 52 std::lock_guard<std::mutex> uniqueLock(mut); 53 std::string key = IdToKey(id); 54 if (!key.empty()) { 55 cb->Expired(key); 56 } 57 sessions.erase(std::find(sessions.begin(), sessions.end(), id)); 58 RecycleId(id); 59} 60 61void AppTimerAdapter::SetCb(IAppTimer::ICb* cb) 62{ 63 this->cb = cb; 64} 65 66void AppTimerAdapter::ValidateDuplication(const int id) 67{ 68 if (std::find(sessions.begin(), sessions.end(), id) != sessions.end()) { 69 throw std::invalid_argument("duplicated id"); 70 } 71} 72 73void AppTimerAdapter::ValidateExistence(const int id) 74{ 75 if (std::find(sessions.begin(), sessions.end(), id) == sessions.end()) { 76 throw std::invalid_argument("non-existing id"); 77 } 78} 79 80 81int AppTimerAdapter::AssignId(const std::string& key) 82{ 83 if (ids.empty()) { 84 throw std::invalid_argument("container ids is empty"); 85 } 86 ValidateExistKey(key); 87 int id = ids.back(); 88 ids.pop_back(); 89 idToBundle[id] = key; 90 bundleToId[key] = id; 91 return id; 92} 93 94void AppTimerAdapter::RecycleId(const int id) 95{ 96 std::string bundle = idToBundle[id]; 97 bundleToId.erase(bundle); 98 idToBundle.erase(id); 99 ids.push_back(id); 100} 101 102std::string AppTimerAdapter::IdToKey(const int id) 103{ 104 if (idToBundle.find(id) != idToBundle.end()) { 105 return idToBundle[id]; 106 } 107 return ""; 108} 109 110int AppTimerAdapter::KeyToId(const std::string& key) 111{ 112 if (bundleToId.find(key) != bundleToId.end()) { 113 return bundleToId[key]; 114 } 115 return -1; 116} 117 118void AppTimerAdapter::ValidateExistKey(const std::string& key) 119{ 120 if (bundleToId.find(key) != bundleToId.end()) { 121 throw std::invalid_argument("bundleToId key exist duplicated this record"); 122 } 123} 124} // HiviewDFX 125} // OHOS