15e81a82fSopenharmony_ci/*
25e81a82fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
35e81a82fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45e81a82fSopenharmony_ci * you may not use this file except in compliance with the License.
55e81a82fSopenharmony_ci * You may obtain a copy of the License at
65e81a82fSopenharmony_ci *
75e81a82fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85e81a82fSopenharmony_ci *
95e81a82fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105e81a82fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115e81a82fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125e81a82fSopenharmony_ci * See the License for the specific language governing permissions and
135e81a82fSopenharmony_ci * limitations under the License.
145e81a82fSopenharmony_ci * Description: handler module for send message and handle message in a looper
155e81a82fSopenharmony_ci * Author: lijianzhao
165e81a82fSopenharmony_ci * Create: 2022-01-19
175e81a82fSopenharmony_ci */
185e81a82fSopenharmony_ci
195e81a82fSopenharmony_ci#ifndef HANDLER_H
205e81a82fSopenharmony_ci#define HANDLER_H
215e81a82fSopenharmony_ci
225e81a82fSopenharmony_ci#include <algorithm>
235e81a82fSopenharmony_ci#include <chrono>
245e81a82fSopenharmony_ci#include <map>
255e81a82fSopenharmony_ci#include <mutex>
265e81a82fSopenharmony_ci#include <thread>
275e81a82fSopenharmony_ci#include <vector>
285e81a82fSopenharmony_ci#include "message.h"
295e81a82fSopenharmony_ci
305e81a82fSopenharmony_cinamespace OHOS {
315e81a82fSopenharmony_cinamespace CastEngine {
325e81a82fSopenharmony_cinamespace CastEngineService {
335e81a82fSopenharmony_ciclass Handler {
345e81a82fSopenharmony_cipublic:
355e81a82fSopenharmony_ci    Handler();
365e81a82fSopenharmony_ci    virtual ~Handler();
375e81a82fSopenharmony_ci    bool SendCastMessage(const Message &msg);
385e81a82fSopenharmony_ci    bool SendCastMessage(int what);
395e81a82fSopenharmony_ci    bool SendCastMessage(int what, int arg1);
405e81a82fSopenharmony_ci    bool SendCastMessage(int what, int arg1, int arg2);
415e81a82fSopenharmony_ci    bool SendCastMessageDelayed(int what, long uptimeMillis);
425e81a82fSopenharmony_ci    void RemoveMessage(const Message &msg);
435e81a82fSopenharmony_ci    void RemoveCallbackAndMessages();
445e81a82fSopenharmony_ci    void StopSafty(bool stopSafty);
455e81a82fSopenharmony_ci    bool IsQuiting();
465e81a82fSopenharmony_ci    virtual void HandleMessage(const Message &msg) = 0;
475e81a82fSopenharmony_ci
485e81a82fSopenharmony_ci    void ThreadJoin();
495e81a82fSopenharmony_ci
505e81a82fSopenharmony_ciprivate:
515e81a82fSopenharmony_ci    void HandleMessageInner(const Message &msg);
525e81a82fSopenharmony_ci    std::vector<Message> msgQueue_;
535e81a82fSopenharmony_ci    std::mutex queueMutex_;
545e81a82fSopenharmony_ci    std::condition_variable condition_;
555e81a82fSopenharmony_ci    std::thread looper_;
565e81a82fSopenharmony_ci    bool stop_;
575e81a82fSopenharmony_ci    bool stopWhenEmpty_;
585e81a82fSopenharmony_ci};
595e81a82fSopenharmony_ci} // namespace CastEngineService
605e81a82fSopenharmony_ci} // namespace CastEngine
615e81a82fSopenharmony_ci} // namespace OHOS
625e81a82fSopenharmony_ci
635e81a82fSopenharmony_ci#endif