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 OHOS_REQUEST_REQUEST_H 17 #define OHOS_REQUEST_REQUEST_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 23 #include "i_notify_data_listener.h" 24 #include "i_response_listener.h" 25 26 namespace OHOS::Request { 27 28 class Request { 29 public: 30 Request(const std::string &taskId); 31 const std::string &getId() const; 32 void AddListener(const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 33 void RemoveListener(const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 34 void AddListener(const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 35 void RemoveListener(const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 36 bool HasListener(); 37 void OnResponseReceive(const std::shared_ptr<Response> &response); 38 void OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData); 39 bool NeedNotify(const std::shared_ptr<NotifyData> ¬ifyData); 40 41 private: 42 const std::string taskId_; 43 std::mutex listenerMutex_; 44 std::shared_ptr<IResponseListener> responseListener_; 45 std::map<SubscribeType, std::shared_ptr<INotifyDataListener>> notifyDataListenerMap_; 46 std::map<SubscribeType, std::shared_ptr<NotifyData>> unusedNotifyData_; 47 bool needRemove_ = true; 48 }; 49 50 } // namespace OHOS::Request 51 52 #endif // OHOS_REQUEST_REQUEST_H