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 */ 15 16 #ifndef OHOS_WINDOW_SCENE_JS_RSS_SESSION_H 17 #define OHOS_WINDOW_SCENE_JS_RSS_SESSION_H 18 19 #include <unordered_map> 20 #include <list> 21 #include <memory> 22 #include <string> 23 #include <functional> 24 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "native_engine/native_engine.h" 28 #include "nlohmann/json.hpp" 29 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 30 #include "res_sched_event_listener.h" 31 #endif 32 33 namespace OHOS::Rosen { 34 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE 35 using OnRssEventCb = std::function<void(napi_env, napi_value, int32_t, 36 std::unordered_map<std::string, std::string>&&)>; 37 38 class RssEventListener : public ResourceSchedule::ResSchedEventListener { 39 public: 40 RssEventListener(napi_env env, napi_value callbackObj, OnRssEventCb callback); 41 virtual ~RssEventListener() = default; 42 void OnReceiveEvent(uint32_t eventType, uint32_t eventValue, 43 std::unordered_map<std::string, std::string> extraInfo) override; 44 45 private: 46 static void ThreadSafeCallBack(napi_env env, napi_value js_cb, void* context, void* data); 47 napi_threadsafe_function threadSafeFunction_ = nullptr; 48 napi_env napiEnv_ = nullptr; 49 OnRssEventCb eventCb_ = nullptr; 50 std::shared_ptr<NativeReference> callbackRef_ = nullptr; 51 }; 52 53 class RssSession { 54 public: 55 static RssSession& GetInstance(); 56 57 struct RssSessionCbInfo { RssSessionCbInfoRssSessionCbInfo58 explicit RssSessionCbInfo(napi_env env) : nativeEnv_(env) {} ~RssSessionCbInfoRssSessionCbInfo59 ~RssSessionCbInfo() {} 60 61 napi_ref callback_ = nullptr; 62 napi_async_work asyncWork_ = nullptr; 63 napi_deferred deferred_ = nullptr; 64 napi_env nativeEnv_ = nullptr; 65 std::unordered_map<std::string, std::string> extraInfo_; 66 }; 67 68 using CallBackPair = std::pair<std::unique_ptr<NativeReference>, sptr<RssEventListener>>; 69 // static function 70 static napi_value RegisterRssData(napi_env env, napi_callback_info info); 71 static napi_value UnregisterRssData(napi_env env, napi_callback_info info); 72 static napi_value DealRssReply(napi_env env, const nlohmann::json& payload, const nlohmann::json& reply); 73 74 void OnReceiveEvent(napi_env env, napi_value callbackObj, uint32_t eventType, 75 const std::unordered_map<std::string, std::string>& extraInfo); 76 77 private: 78 RssSession() = default; 79 ~RssSession() = default; 80 81 RssSession(const RssSession&) = delete; 82 RssSession& operator=(const RssSession&) = delete; 83 RssSession(RssSession&&) = delete; 84 RssSession& operator=(RssSession&&) = delete; 85 86 napi_value RegisterRssDataCallback(napi_env env, napi_callback_info info); 87 napi_value UnRegisterRssDataCallback(napi_env env, napi_callback_info info); 88 bool CheckCallbackParam(napi_env env, napi_callback_info info, uint32_t& eventType, napi_value* jsCallback); 89 90 static void ParseMutex(const std::string& mutexStr, const nlohmann::json& payload, std::string& detailStr); 91 static void ParseCallbackMutex(const std::string& mutexStr, std::string& bundleName); 92 static void CompleteCb(napi_env env, napi_status status, void* data); 93 94 // ONLY Accessed on main thread 95 std::unordered_map<uint32_t, std::list<CallBackPair>> jsCallBackMap_; 96 }; 97 #endif 98 } // OHOS 99 100 #endif // OHOS_WINDOW_SCENE_JS_RSS_SESSION_H