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 * Description: handler module for send message and handle message in a looper 15 * Author: lijianzhao 16 * Create: 2022-01-19 17 */ 18 19 #ifndef HANDLER_H 20 #define HANDLER_H 21 22 #include <algorithm> 23 #include <chrono> 24 #include <map> 25 #include <mutex> 26 #include <thread> 27 #include <vector> 28 #include "message.h" 29 30 namespace OHOS { 31 namespace CastEngine { 32 namespace CastEngineService { 33 class Handler { 34 public: 35 Handler(); 36 virtual ~Handler(); 37 bool SendCastMessage(const Message &msg); 38 bool SendCastMessage(int what); 39 bool SendCastMessage(int what, int arg1); 40 bool SendCastMessage(int what, int arg1, int arg2); 41 bool SendCastMessageDelayed(int what, long uptimeMillis); 42 void RemoveMessage(const Message &msg); 43 void RemoveCallbackAndMessages(); 44 void StopSafty(bool stopSafty); 45 bool IsQuiting(); 46 virtual void HandleMessage(const Message &msg) = 0; 47 48 void ThreadJoin(); 49 50 private: 51 void HandleMessageInner(const Message &msg); 52 std::vector<Message> msgQueue_; 53 std::mutex queueMutex_; 54 std::condition_variable condition_; 55 std::thread looper_; 56 bool stop_; 57 bool stopWhenEmpty_; 58 }; 59 } // namespace CastEngineService 60 } // namespace CastEngine 61 } // namespace OHOS 62 63 #endif