• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "command_send_limit.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 
20 namespace OHOS::AVSession {
21 std::shared_ptr<CommandSendLimit> CommandSendLimit::instance_;
22 std::once_flag CommandSendLimit::onceFlag_;
23 
GetInstance()24 CommandSendLimit& CommandSendLimit::GetInstance()
25 {
26     std::call_once(onceFlag_, [] {
27         instance_ = std::make_shared<CommandSendLimit>();
28     });
29     return *instance_;
30 }
31 
CommandSendLimit()32 CommandSendLimit::CommandSendLimit()
33 {
34     SLOGD("CommandSendLimit construct");
35 }
36 
~CommandSendLimit()37 CommandSendLimit::~CommandSendLimit()
38 {
39     SLOGI("CommandSendLimit destroy");
40     StopTimer();
41 }
42 
IsCommandSendEnable(pid_t pid)43 bool CommandSendLimit::IsCommandSendEnable(pid_t pid)
44 {
45     std::lock_guard lockGuard(commandLimitLock_);
46     if (timer_ == nullptr) {
47         return true;
48     }
49     if (commandLimits_.find(pid) != commandLimits_.end()) {
50         if (commandLimits_[pid] >= COMMAND_SEND_NUM_MAX) {
51             SLOGD("commandSendTime=%{public}d", commandLimits_[pid]);
52             return false;
53         }
54         SLOGD("commandSendTime=%{public}d", commandLimits_[pid]);
55         commandLimits_[pid] += 1;
56     } else {
57         SLOGD("commandSendTime=0");
58         commandLimits_.insert(std::make_pair(pid, 1));
59     }
60     return true;
61 }
62 
ClearCommandLimits()63 void CommandSendLimit::ClearCommandLimits()
64 {
65     std::lock_guard lockGuard(commandLimitLock_);
66     commandLimits_.clear();
67 }
StartTimer()68 void CommandSendLimit::StartTimer()
69 {
70     SLOGD("CommandSendLimit StartTimer");
71     std::lock_guard lockGuard(commandLimitLock_);
72     if (!timer_) {
73         timer_ = std::make_unique<OHOS::Utils::Timer>("CommandLimitTimer");
74         CHECK_AND_RETURN_LOG(timer_ != nullptr, "create Timer failed");
75         Utils::Timer::TimerCallback timerCallback = [this]() {
76             ClearCommandLimits();
77         };
78         timerId_ = timer_->Register(timerCallback, COMMAND_SEND_TIME, false);
79         timer_->Setup();
80     }
81 }
82 
StopTimer()83 void CommandSendLimit::StopTimer()
84 {
85     SLOGD("CommandSendLimit StopTimer");
86     std::lock_guard lockGuard(commandLimitLock_);
87     if (timer_) {
88         timer_->Shutdown();
89         timer_->Unregister(timerId_);
90         timer_ = nullptr;
91     }
92     commandLimits_.clear();
93 }
94 } // namespace OHOS::AVSession