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 INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_MANAGER_NAPI_H 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_MANAGER_NAPI_H 18 19 #include <mutex> 20 #include <vector> 21 #include <map> 22 23 #include "data_ability_helper.h" 24 #include "data_ability_observer_stub.h" 25 #include "data_ability_predicates.h" 26 #include "media_asset_data_handler.h" 27 #include "media_file_utils.h" 28 #include "media_library_napi.h" 29 #include "napi_base_context.h" 30 #include "napi_error.h" 31 #include "napi/native_api.h" 32 #include "napi/native_node_api.h" 33 34 namespace OHOS { 35 namespace Media { 36 enum class MultiStagesCapturePhotoStatus { 37 QUERY_INNER_FAIL = 0, 38 HIGH_QUALITY_STATUS, 39 LOW_QUALITY_STATUS, 40 }; 41 42 struct RequestImageAsyncContext { 43 size_t argc = ARGS_FOUR; 44 napi_value argv[ARGS_FOUR] = {nullptr}; 45 napi_value thisVar = nullptr; 46 int fileId = -1; // default value of request file id 47 std::string photoUri; 48 std::string displayName; 49 std::string photoPath; 50 std::string callingPkgName; 51 DeliveryMode deliveryMode; 52 SourceMode sourceMode; 53 ReturnDataType returnDataType; 54 }; 55 56 class MultiStagesTaskObserver : public DataShare::DataShareObserver { 57 public: MultiStagesTaskObserver(std::string uri,int fileId,SourceMode sourceMode)58 MultiStagesTaskObserver(std::string uri, int fileId, SourceMode sourceMode) 59 : requestUri_(uri), fileId_(fileId), sourceMode_(sourceMode) {}; 60 void OnChange(const ChangeInfo &changelnfo) override; 61 private: 62 std::string requestUri_; 63 int fileId_; 64 SourceMode sourceMode_; 65 }; 66 67 class MediaAssetManagerNapi { 68 public: 69 MediaAssetManagerNapi() = default; 70 ~MediaAssetManagerNapi() = default; 71 EXPORT static napi_value Init(napi_env env, napi_value exports); 72 static napi_env GetMediaAssetManagerJsEnv(); 73 static void SetMediaAssetManagerJsEnv(napi_env env); 74 static MultiStagesCapturePhotoStatus queryPhotoStatus(int fileId); 75 static void notifyImageDataPrepared(const std::string requstUri, SourceMode sourceMode); 76 static void notifyDataPreparedWithoutRegister(std::string &requestUri, napi_value napiMediaDataHandler, 77 ReturnDataType returnDataType, SourceMode sourceMode); 78 static void RequestImage(std::string photoId); 79 static void DeleteInProcessMapRecord(const std::string &requestUri); 80 81 private: 82 static napi_value Constructor(napi_env env, napi_callback_info info); 83 static void Destructor(napi_env env, void* nativeObject, void* finalizeHint); 84 static bool InitUserFileClient(napi_env env, napi_callback_info info); 85 static napi_status ParseRequestImageArgs(napi_env env, napi_callback_info info, 86 unique_ptr<RequestImageAsyncContext> &asyncContext); 87 static napi_value JSRequestImage(napi_env env, napi_callback_info info); 88 static napi_value JSRequestImageData(napi_env env, napi_callback_info info); 89 static void RegisterTaskObserver(const std::string &photoUri, const int fileId, napi_value napiMediaDataHandler, 90 ReturnDataType returnDataType, SourceMode sourceMode); 91 static void ProcessImage(const int fileId, const int deliveryMode, const std::string &packageName); 92 static void AddImage(const int fileId, DeliveryMode deliveryMode); 93 static void onHandleRequestImage(const unique_ptr<RequestImageAsyncContext> &asyncContext); 94 static void GetByteArrayNapiObject(std::string requestUri, napi_value& arrayBuffer, bool isSource); 95 static void GetImageSourceNapiObject(std::string fileUri, napi_value& imageSourceNapiObj, bool isSource); 96 public: 97 static napi_env env_; 98 std::mutex sMediaAssetMutex_; 99 }; 100 } // Media 101 } // OHOS 102 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_MANAGER_NAPI_H 103