1 /* 2 * Copyright (c) 2025 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 DEVICESTATUS_NAPI_H 17 #define DEVICESTATUS_NAPI_H 18 19 #include <map> 20 #include <tuple> 21 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include <uv.h> 25 26 #include "boomerang_callback_stub.h" 27 #include "boomerang_event.h" 28 #include "boomerang_data.h" 29 30 namespace OHOS { 31 namespace Msdp { 32 namespace DeviceStatus { 33 class BoomerangCallback : public BoomerangCallbackStub { 34 public: BoomerangCallback(napi_env env,napi_deferred deferred)35 explicit BoomerangCallback(napi_env env, napi_deferred deferred) : env_(env), deferred_(deferred) {} ~BoomerangCallback()36 virtual ~BoomerangCallback() {}; 37 void OnScreenshotResult(const BoomerangData& data) override; 38 void OnNotifyMetadata(const std::string& metadata) override; 39 void OnEncodeImageResult(std::shared_ptr<Media::PixelMap> pixelMap) override; 40 static void EmitOnEvent(BoomerangData* data); 41 static void EmitOnMetadata(napi_env env, std::string metadata, napi_deferred deferred); 42 static void EmitOnEncodeImage(napi_env env, std::shared_ptr<Media::PixelMap> pixelMap, 43 napi_deferred deferred); 44 private: 45 napi_env env_ { nullptr }; 46 napi_deferred deferred_; 47 std::mutex mutex_; 48 BoomerangData data_; 49 std::string metadata_; 50 std::shared_ptr<Media::PixelMap> pixelMap_; 51 }; 52 53 struct AsyncContext { 54 napi_env env = nullptr; 55 napi_async_work work = nullptr; 56 napi_deferred deferred = nullptr; 57 napi_handle_scope scope; 58 sptr<IRemoteBoomerangCallback> callback = nullptr; 59 std::string bundleName; 60 std::string metadata; 61 std::shared_ptr<Media::PixelMap> pixelMap; 62 int32_t result; 63 }; 64 65 class BoomerangNapi : public BoomerangEvent { 66 public: 67 explicit BoomerangNapi(napi_env env); 68 virtual ~BoomerangNapi(); 69 70 static napi_value Init(napi_env env, napi_value exports); 71 72 static bool InitNapiObject(napi_env env, napi_callback_info info); 73 static napi_value Register(napi_env env, napi_callback_info info); 74 static napi_value UnRegister(napi_env env, napi_callback_info info); 75 static napi_value NotifyMetadataBindingEvent(napi_env env, napi_callback_info info); 76 static napi_value SubmitMetadata(napi_env env, napi_callback_info info); 77 static napi_value BoomerangEncodeImage(napi_env env, napi_callback_info info); 78 static napi_value DecodeImage(napi_env env, napi_callback_info info); 79 static napi_value SubscribeMeatadataCallback(napi_env env, napi_callback_info info, napi_value handler, 80 int32_t type, std::string bundleName); 81 static int32_t ConvertTypeToInt(const std::string &type); 82 void OnScreenshot(int32_t type, int32_t value, bool isOnce); 83 void OnMetadata(napi_env env, std::string metadata, bool isOnce, napi_deferred deferred); 84 void OnEncodeImage(napi_env env, std::shared_ptr<Media::PixelMap> pixelMap, napi_deferred deferred); 85 static BoomerangNapi* GetDeviceStatusNapi(); 86 static std::map<int32_t, sptr<IRemoteBoomerangCallback>> callbacks_; 87 88 private: 89 static bool CheckArguments(napi_env env, napi_callback_info info, int32_t validataType); 90 static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 91 static bool CreateMetadataExecution(napi_env env, AsyncContext *asyncContext, std::string typeInt, 92 sptr<IRemoteBoomerangCallback> callbacks); 93 static void NotifyMetadataExecuteCB(napi_env env, void* data); 94 static void NotifyMetadataCompleteCB(napi_env env, napi_status status, void* data); 95 static bool CreateEncodeImageExecution(napi_env env, AsyncContext *asyncContext, 96 std::string metadata, std::shared_ptr<Media::PixelMap> pixelMap, sptr<IRemoteBoomerangCallback> callback); 97 static void EncodeImageExecuteCB(napi_env env, void* data); 98 static void EncodeImageCompleteCB(napi_env env, napi_status status, void* data); 99 static bool CreateDecodeImageExecution(napi_env env, AsyncContext *asyncContext, 100 std::shared_ptr<Media::PixelMap> pixelMap, sptr<IRemoteBoomerangCallback> callback); 101 static void DecodeImageExecuteCB(napi_env env, void* data); 102 static void DecodeImageCompleteCB(napi_env env, napi_status status, void* data); 103 static void ProcessErrorResult(napi_env env, int32_t result, int32_t code, AsyncContext* asyncContext); 104 static bool ValidateEncodeParam(const std::string& metadata, std::shared_ptr<Media::PixelMap>& pixelMap); 105 static napi_value HandleBoomerangEncodeImage(napi_env env, std::shared_ptr<Media::PixelMap>& pixelMap, 106 const std::string& metadata); 107 static napi_ref boomerangValueRef_; 108 napi_env env_ { nullptr }; 109 inline static std::mutex mutex_; 110 inline static std::mutex notifyMutex_; 111 inline static std::mutex encodeMutex_; 112 }; 113 } // namespace DeviceStatus 114 } // namespace Msdp 115 } // namespace OHOS 116 #endif // DEVICESTATUS_NAPI_H 117