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 #ifndef APP_TIMER_ADAPTER_H
16 #define APP_TIMER_ADAPTER_H
17 
18 #include <vector>
19 #include <mutex>
20 #include <string>
21 #include <map>
22 #include "IAppTimer.h"
23 #include "ISceneTimerInfrastructure.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class AppTimerAdapter : public IAppTimer, public ISceneTimerInfrastructure::ICb {
28 public:
29     const static unsigned int timeoutPeriod = 5 * 1000; // 5s
30 
31     AppTimerAdapter(int userId, ISceneTimerInfrastructure* infra);
32     ~AppTimerAdapter() override;
33     void Start(std::string key) override;
34     void Stop(std::string key) override;
35     void Expired(int id) override;
36     void SetCb(IAppTimer::ICb* cb);
37 private:
38     const int userId;
39 
40     IAppTimer::ICb* cb{nullptr};
41     ISceneTimerInfrastructure* impl;
42     std::vector<int> sessions;
43     std::mutex mut;
44 
45     std::vector<int> ids = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
46                             11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
47                             21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
48     std::map<int, std::string> idToBundle;
49     std::map<std::string, int> bundleToId;
50 
51     void ValidateDuplication(const int id);
52     void ValidateExistence(const int id);
53 
54     std::string IdToKey(const int id);
55     int KeyToId(const std::string& key);
56     int AssignId(const std::string& key);
57     void RecycleId(const int id);
58     void ValidateExistKey(const std::string& key);
59 };
60 } // HiviewDFX
61 } // OHOS
62 #endif
63