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 int32_t SetDragSwitchState(napi_env env, bool enable); 47 int32_t SetAppDragSwitchState(napi_env env, bool enable, const std::string &pkgName); 48 49 private: 50 struct CallbackInfo : public RefBase { 51 napi_env env { nullptr }; 52 napi_ref ref { nullptr }; 53 DragState state; 54 }; 55 template <typename T> DeletePtr(T & ptr)56 static void DeletePtr(T &ptr) 57 { 58 if (ptr != nullptr) { 59 delete ptr; 60 ptr = nullptr; 61 } 62 } 63 64 static void CallDragMsg(sptr<CallbackInfo> cb); 65 void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback); 66 void ReleaseReference(); 67 bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 68 69 std::atomic_bool hasRegistered_ { false }; 70 inline static std::mutex mutex_; 71 inline static std::vector<sptr<CallbackInfo>> listeners_ {}; 72 }; 73 } // namespace DeviceStatus 74 } // namespace Msdp 75 } // namespace OHOS 76 #endif // JS_DRAG_MANAGER_H 77