1 /* 2 * Copyright (C) 2021 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 #ifndef N_NAPI_PASTEDATA_RECORD_H 16 #define N_NAPI_PASTEDATA_RECORD_H 17 18 #include "entry_getter.h" 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "paste_data.h" 22 #include "paste_data_record.h" 23 #include "pasteboard_client.h" 24 #include "pasteboard_common.h" 25 #include "pixel_map_napi.h" 26 #include "uri.h" 27 28 namespace OHOS { 29 namespace MiscServicesNapi { 30 class PastedataRecordEntryGetterInstance : public std::enable_shared_from_this<PastedataRecordEntryGetterInstance> { 31 public: 32 class PastedataRecordEntryGetterImpl : public UDMF::EntryGetter { 33 public: 34 explicit PastedataRecordEntryGetterImpl() = default; 35 UDMF::ValueType GetValueByType(const std::string &utdId) override; 36 void SetEntryGetterWrapper(const std::shared_ptr<PastedataRecordEntryGetterInstance> instance); 37 private: 38 std::shared_ptr<PastedataRecordEntryGetterInstance> wrapper_; 39 }; 40 explicit PastedataRecordEntryGetterInstance(const napi_env &env, const napi_ref &ref); 41 ~PastedataRecordEntryGetterInstance(); 42 43 UDMF::ValueType GetValueByType(const std::string &utdId); 44 GetEnv()45 napi_env GetEnv() 46 { 47 return env_; 48 } 49 GetRef()50 napi_ref GetRef() 51 { 52 return ref_; 53 } 54 GetStub()55 std::shared_ptr<PastedataRecordEntryGetterImpl> GetStub() 56 { 57 return stub_; 58 } 59 60 private: 61 napi_env env_ = nullptr; 62 napi_ref ref_ = nullptr; 63 std::shared_ptr<PastedataRecordEntryGetterImpl> stub_ = nullptr; 64 }; 65 66 struct PasteboardEntryGetterWorker { 67 std::string utdId; 68 std::shared_ptr<PastedataRecordEntryGetterInstance> entryGetter = nullptr; 69 std::shared_ptr<UDMF::ValueType> entryValue = nullptr; 70 bool complete = false; 71 bool clean = false; 72 std::condition_variable cv; 73 std::mutex mutex; 74 }; 75 76 class PasteDataRecordNapi { 77 public: 78 static napi_value PasteDataRecordInit(napi_env env, napi_value exports); 79 static napi_value New(napi_env env, napi_callback_info info); 80 static napi_status NewInstance(napi_env env, napi_value &instance); 81 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 82 static bool NewInstanceByRecord( 83 napi_env env, napi_value &instance, const std::shared_ptr<MiscServices::PasteDataRecord> &record); 84 static bool NewHtmlTextRecordInstance(napi_env env, const std::string &text, napi_value &instance); 85 static bool NewPlainTextRecordInstance(napi_env env, const std::string &text, napi_value &instance); 86 static bool NewPixelMapRecordInstance( 87 napi_env env, const std::shared_ptr<OHOS::Media::PixelMap> pixelMap, napi_value &instance); 88 static bool NewUriRecordInstance(napi_env env, const std::string &text, napi_value &instance); 89 static bool NewWantRecordInstance( 90 napi_env env, const std::shared_ptr<OHOS::AAFwk::Want> want, napi_value &instance); 91 static bool NewKvRecordInstance( 92 napi_env env, const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer, napi_value &instance); 93 static bool NewEntryGetterRecordInstance( 94 const std::vector<std::string> mimeTypes, 95 std::shared_ptr<PastedataRecordEntryGetterInstance> entryGetter, 96 napi_value &instance); 97 static napi_value CreatKvData(napi_env env, std::shared_ptr<MiscServices::MineCustomData> customData); 98 static std::shared_ptr<MiscServices::MineCustomData> GetNativeKvData(napi_env env, napi_value napiValue); 99 napi_value SetNapiKvData(napi_env env, std::shared_ptr<MiscServices::MineCustomData> customData); 100 PasteDataRecordNapi(); 101 ~PasteDataRecordNapi(); 102 103 static napi_value ConvertToText(napi_env env, napi_callback_info info); 104 static napi_value ConvertToTextV9(napi_env env, napi_callback_info info); 105 static napi_value ToPlainText(napi_env env, napi_callback_info info); 106 static napi_value AddEntry(napi_env env, napi_callback_info info); 107 static napi_value GetValidTypes(napi_env env, napi_callback_info info); 108 static napi_value GetRecordData(napi_env env, napi_callback_info info); 109 110 std::shared_ptr<MiscServices::PasteDataRecord> value_; 111 112 private: 113 void JSFillInstance(napi_env env, napi_value &instance); 114 void SetNamedPropertyByStr(napi_env env, napi_value &instance, const char *propName, const char *propValue); 115 std::shared_ptr<PastedataRecordEntryGetterInstance> entryGetter_; 116 napi_env env_; 117 }; 118 } // namespace MiscServicesNapi 119 } // namespace OHOS 120 #endif