1 /*
2  * Copyright (c) 2023 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 STATE_CHANGE_NOTIFY_H
17 #define STATE_CHANGE_NOTIFY_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 
23 #include "drag_data.h"
24 #include "i_socket_session.h"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 enum class MessageType : int32_t {
30     NOTIFY_STATE,
31     NOTIFY_STYLE,
32     NOTIFY_NONE
33 };
34 class StateChangeNotify final {
35 public:
36     StateChangeNotify() = default;
37     ~StateChangeNotify() = default;
38     struct MessageInfo {
39         MessageType msgType { MessageType::NOTIFY_NONE };
40         MessageId msgId { MessageId::INVALID };
41         SocketSessionPtr session { nullptr };
42         DragState state { DragState::ERROR };
43         DragCursorStyle style { DragCursorStyle::DEFAULT };
44         bool operator==(std::shared_ptr<MessageInfo> info)
45         {
46             if (info == nullptr || info->session == nullptr) {
47                 return false;
48             }
49             return session->GetPid() == info->session->GetPid();
50         }
51     };
52     void RemoveNotifyMsg(std::shared_ptr<MessageInfo> info);
53     void AddNotifyMsg(std::shared_ptr<MessageInfo> info);
54     int32_t StateChangedNotify(DragState state);
55     int32_t StyleChangedNotify(DragCursorStyle style);
56 
57 private:
58     template <typename T>
59     void OnDragInfoNotify(SocketSessionPtr session, MessageId msgId, T t);
60 
61 private:
62     std::list<std::shared_ptr<MessageInfo>> msgStateInfos_;
63     std::list<std::shared_ptr<MessageInfo>> msgStyleInfos_;
64     std::map<MessageType, std::list<std::shared_ptr<MessageInfo>>> msgInfos_ = {
65         { MessageType::NOTIFY_STATE, msgStateInfos_ },
66         { MessageType::NOTIFY_STYLE, msgStyleInfos_ }
67     };
68 };
69 } // namespace DeviceStatus
70 } // namespace Msdp
71 } // namespace OHOS
72 #endif // STATE_CHANGE_NOTIFY_H