1 /*
2  * Copyright (c) 2022 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 OHOS_COMMAND_SEND_LIMIT_H
17 #define OHOS_COMMAND_SEND_LIMIT_H
18 
19 #include <string>
20 #include <memory>
21 #include <mutex>
22 #include <functional>
23 #include "timer.h"
24 
25 namespace OHOS::AVSession {
26 class CommandSendLimit : public std::enable_shared_from_this<CommandSendLimit> {
27 public:
28     static CommandSendLimit& GetInstance();
29 
30     CommandSendLimit();
31     ~CommandSendLimit();
32 
33     bool IsCommandSendEnable(pid_t pid);
34     void StartTimer();
35     void StopTimer();
36 private:
37     std::recursive_mutex commandLimitLock_;
38     void ClearCommandLimits();
39     static std::shared_ptr<CommandSendLimit> instance_;
40     static std::once_flag onceFlag_;
41     std::map<pid_t, int32_t> commandLimits_;
42     std::unique_ptr<Utils::Timer> timer_;
43     uint32_t timerId_ = 0;
44     static constexpr int32_t COMMAND_SEND_NUM_MAX = 10;
45     static constexpr int32_t COMMAND_SEND_TIME = 1000;
46 };
47 } // namespace OHOS::AVSession
48 #endif // OHOS_COMMAND_SEND_LIMIT_H