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 #ifndef SERVICES_INCLUDE_IME_AGING_MANAGER_H
17 #define SERVICES_INCLUDE_IME_AGING_MANAGER_H
18 
19 #include <chrono>
20 #include <map>
21 #include <utility>
22 
23 #include "global.h"
24 #include "i_input_method_agent.h"
25 #include "i_input_method_core.h"
26 #include "input_death_recipient.h"
27 #include "peruser_session.h"
28 #include "timer.h"
29 
30 namespace OHOS {
31 namespace MiscServices {
32 struct AgingIme {
33     ImeData data;
34     std::chrono::system_clock::time_point timestamp{};
AgingImeAgingIme35     AgingIme(ImeData data, std::chrono::system_clock::time_point timestamp)
36         : data(std::move(data)), timestamp(timestamp)
37     {
38     }
39 };
40 
41 class ImeAgingManager {
42 public:
43     static ImeAgingManager &GetInstance();
44     bool Push(const std::string &bundleName, const std::shared_ptr<ImeData> &imeData);
45     std::shared_ptr<ImeData> Pop(const std::string &bundleName);
46     void Clear();
47 
48 private:
49     ImeAgingManager();
50     void StartAging();
51     void StopAging();
52     void AgingCache();
53     void ClearOldest();
54     void ClearIme(const std::shared_ptr<AgingIme> &ime);
55 
56     std::mutex timerMutex_;
57     Utils::Timer timer_;
58     uint32_t timerId_;
59     std::recursive_mutex cacheMutex_;
60     std::map<std::string, std::shared_ptr<AgingIme>> imeCaches_;
61 };
62 } // namespace MiscServices
63 } // namespace OHOS
64 
65 #endif // SERVICES_INCLUDE_IME_AGING_MANAGER_H
66