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 JS_DRAG_MANAGER_H 17 #define JS_DRAG_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <string> 22 #include <uv.h> 23 #include <vector> 24 25 #include "napi/native_node_api.h" 26 #include "nocopyable.h" 27 #include "refbase.h" 28 29 #include "i_drag_listener.h" 30 #include "i_subscript_listener.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class JsDragManager : public IDragListener, public std::enable_shared_from_this<JsDragManager> { 36 public: 37 JsDragManager() = default; 38 DISALLOW_COPY_AND_MOVE(JsDragManager); 39 ~JsDragManager() = default; 40 41 void ResetEnv(); 42 void OnDragMessage(DragState state) override; 43 void RegisterListener(napi_env env, napi_value handle); 44 void UnregisterListener(napi_env env, napi_value handle = nullptr); 45 napi_value GetDataSummary(napi_env env); 46 47 private: 48 struct CallbackInfo : public RefBase { 49 napi_env env { nullptr }; 50 napi_ref ref { nullptr }; 51 DragState state; 52 }; 53 template <typename T> DeletePtr(T & ptr)54 static void DeletePtr(T &ptr) 55 { 56 if (ptr != nullptr) { 57 delete ptr; 58 ptr = nullptr; 59 } 60 } 61 62 static void CallDragMsg(uv_work_t *work, int32_t status); 63 void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback); 64 void ReleaseReference(); 65 bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 66 67 std::atomic_bool hasRegistered_ { false }; 68 inline static std::mutex mutex_; 69 inline static std::vector<sptr<CallbackInfo>> listeners_ {}; 70 }; 71 } // namespace DeviceStatus 72 } // namespace Msdp 73 } // namespace OHOS 74 #endif // JS_DRAG_MANAGER_H 75