1 /* 2 * Copyright (c) 2021-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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_FLOW_CONTROL_SERVICE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_FLOW_CONTROL_SERVICE_H 18 19 #include <ctime> 20 #include <list> 21 #include <memory> 22 #include <mutex> 23 24 #include "errors.h" 25 #include "singleton.h" 26 #include "ans_const_define.h" 27 #include "notification_record.h" 28 29 namespace OHOS { 30 namespace Notification { 31 struct FlowControlThreshold { 32 uint32_t maxCreateNumPerSecond = MAX_CREATE_NUM_PERSECOND; 33 uint32_t maxUpdateNumPerSecond = MAX_UPDATE_NUM_PERSECOND; 34 uint32_t maxCreateNumPerSecondPerApp = MAX_CREATE_NUM_PERSECOND_PERAPP; 35 uint32_t maxUpdateNumPerSecondPerApp = MAX_UPDATE_NUM_PERSECOND_PERAPP; 36 }; 37 38 class FlowControlService : public DelayedSingleton<FlowControlService> { 39 public: 40 FlowControlService(); 41 ErrCode FlowControl(const std::shared_ptr<NotificationRecord> &record, 42 const int32_t callingUid, bool isNotificationExists); 43 44 private: 45 ErrCode PublishFlowCtrl(const std::shared_ptr<NotificationRecord> &record, const int32_t callingUid); 46 ErrCode PublishGlobalFlowCtrl(const std::shared_ptr<NotificationRecord> &record, 47 std::chrono::system_clock::time_point now); 48 ErrCode PublishSingleAppFlowCtrl(const std::shared_ptr<NotificationRecord> &record, 49 std::chrono::system_clock::time_point now, const int32_t callingUid); 50 void PublishRecordTimestamp(const std::shared_ptr<NotificationRecord> &record, 51 std::chrono::system_clock::time_point now, const int32_t callingUid); 52 void PublishSingleAppFlowCtrlRemoveExpire(std::chrono::system_clock::time_point now); 53 54 ErrCode UpdateFlowCtrl(const std::shared_ptr<NotificationRecord> &record, const int32_t callingUid); 55 ErrCode UpdateGlobalFlowCtrl(const std::shared_ptr<NotificationRecord> &record, 56 std::chrono::system_clock::time_point now); 57 ErrCode UpdateSingleAppFlowCtrl(const std::shared_ptr<NotificationRecord> &record, 58 std::chrono::system_clock::time_point now, const int32_t callingUid); 59 void UpdateRecordTimestamp(const std::shared_ptr<NotificationRecord> &record, 60 std::chrono::system_clock::time_point now, const int32_t callingUid); 61 void UpdateSingleAppFlowCtrlRemoveExpire(std::chrono::system_clock::time_point now); 62 63 private: 64 static std::mutex flowControlMutex_; 65 std::list<std::chrono::system_clock::time_point> flowControlUpdateTimestampList_; 66 std::list<std::chrono::system_clock::time_point> flowControlPublishTimestampList_; 67 static std::mutex systemFlowControlMutex_; 68 std::list<std::chrono::system_clock::time_point> systemFlowControlUpdateTimestampList_; 69 std::list<std::chrono::system_clock::time_point> systemFlowControlPublishTimestampList_; 70 static std::mutex singleAppFlowControlMutex_; 71 std::map<int32_t, 72 std::shared_ptr<std::list<std::chrono::system_clock::time_point>>> singleAppFlowControlUpdateTimestampMap_; 73 std::map<int32_t, 74 std::shared_ptr<std::list<std::chrono::system_clock::time_point>>> singleAppFlowControlPublishTimestampMap_; 75 76 FlowControlThreshold threshold_; 77 }; 78 } // namespace Notification 79 } // namespace OHOS 80 81 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_FLOW_CONTROL_SERVICE_H