1 /* 2 * Copyright (c) 2022 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_DISTRIBUTED_ABILITY_MANAGER_JS_CONTINUATION_MANAGER_H 17 #define OHOS_DISTRIBUTED_ABILITY_MANAGER_JS_CONTINUATION_MANAGER_H 18 19 #include <utility> 20 21 #include "continuation_extra_params.h" 22 #include "js_device_selection_listener.h" 23 #include "native_engine/native_engine.h" 24 #include "nlohmann/json.hpp" 25 26 namespace OHOS { 27 namespace DistributedSchedule { 28 using JsDeviceSelectionListenerPtr = std::shared_ptr<JsDeviceSelectionListener>; 29 30 class JsContinuationManager final { 31 public: 32 JsContinuationManager() = default; 33 ~JsContinuationManager() = default; 34 35 static void Finalizer(NativeEngine* engine, void* data, void* hint); 36 static NativeValue* Register(NativeEngine* engine, NativeCallbackInfo* info); 37 static NativeValue* Unregister(NativeEngine* engine, NativeCallbackInfo* info); 38 static NativeValue* RegisterDeviceSelectionCallback(NativeEngine* engine, NativeCallbackInfo* info); 39 static NativeValue* UnregisterDeviceSelectionCallback(NativeEngine* engine, NativeCallbackInfo* info); 40 static NativeValue* UpdateConnectStatus(NativeEngine* engine, NativeCallbackInfo* info); 41 static NativeValue* StartDeviceManager(NativeEngine* engine, NativeCallbackInfo* info); 42 static NativeValue* InitDeviceConnectStateObject(NativeEngine* engine, NativeCallbackInfo* info); 43 static NativeValue* InitContinuationModeObject(NativeEngine* engine, NativeCallbackInfo* info); 44 static NativeValue* RegisterContinuation(NativeEngine* engine, NativeCallbackInfo* info); 45 static NativeValue* UnregisterContinuation(NativeEngine* engine, NativeCallbackInfo* info); 46 static NativeValue* UpdateContinuationState(NativeEngine* engine, NativeCallbackInfo* info); 47 static NativeValue* StartContinuationDeviceManager(NativeEngine* engine, NativeCallbackInfo* info); 48 49 private: 50 using CallbackPair = std::pair<std::unique_ptr<NativeReference>, sptr<JsDeviceSelectionListener>>; 51 NativeValue* OnRegister(NativeEngine &engine, NativeCallbackInfo &info); 52 NativeValue* OnUnregister(NativeEngine &engine, NativeCallbackInfo &info); 53 NativeValue* OnRegisterDeviceSelectionCallback(NativeEngine &engine, NativeCallbackInfo &info); 54 NativeValue* OnUnregisterDeviceSelectionCallback(NativeEngine &engine, NativeCallbackInfo &info); 55 NativeValue* OnUpdateConnectStatus(NativeEngine &engine, NativeCallbackInfo &info); 56 NativeValue* OnStartDeviceManager(NativeEngine &engine, NativeCallbackInfo &info); 57 NativeValue* OnInitDeviceConnectStateObject(NativeEngine &engine, NativeCallbackInfo &info); 58 NativeValue* OnInitContinuationModeObject(NativeEngine &engine, NativeCallbackInfo &info); 59 static napi_status SetEnumItem(const napi_env& env, napi_value object, const char* name, int32_t value); 60 NativeValue* OnRegisterContinuation(NativeEngine &engine, NativeCallbackInfo &info); 61 NativeValue* OnUnregisterContinuation(NativeEngine &engine, NativeCallbackInfo &info); 62 NativeValue* OnUpdateContinuationState(NativeEngine &engine, NativeCallbackInfo &info); 63 NativeValue* OnStartContinuationDeviceManager(NativeEngine &engine, NativeCallbackInfo &info); 64 65 static bool IsCallbackValid(NativeValue* listenerObj); 66 bool IsCallbackRegistered(int32_t token, const std::string& cbType); 67 bool UnWrapContinuationExtraParams(const napi_env& env, const napi_value& options, 68 std::shared_ptr<ContinuationExtraParams>& continuationExtraParams); 69 bool UnwrapJsonByPropertyName(const napi_env& env, const napi_value& param, 70 const std::string& field, nlohmann::json& jsonObj); 71 static bool PraseJson(const napi_env& env, const napi_value& jsonField, const napi_value& jsProNameList, 72 uint32_t jsProCount, nlohmann::json& jsonObj); 73 static int32_t ErrorCodeReturn(int32_t code); 74 static std::string ErrorMessageReturn(int32_t code); 75 static std::string OnRegisterDeviceSelectionCallbackParameterCheck(NativeEngine &engine, 76 NativeCallbackInfo &info, std::string &cbType, int32_t &token, NativeValue** jsListenerObj); 77 napi_value GenerateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 78 std::mutex jsCbMapMutex_; 79 std::map<int32_t, std::map<std::string, CallbackPair>> jsCbMap_; 80 }; 81 82 NativeValue* JsContinuationManagerInit(NativeEngine* engine, NativeValue* exportObj); 83 } // namespace DistributedSchedule 84 } // namespace OHOS 85 #endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_JS_CONTINUATION_MANAGER_H