• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 #ifndef N_NAPI_PASTE_H
16 #define N_NAPI_PASTE_H
17 
18 #include "async_call.h"
19 #include "common/block_object.h"
20 #include "pasteboard_error.h"
21 #include "pastedata_napi.h"
22 #include "pasteboard_observer_napi.h"
23 #include "pastedata_record_napi.h"
24 #include "unified_data_napi.h"
25 
26 namespace OHOS {
27 namespace MiscServicesNapi {
28 class PasteboardDelayGetterInstance : public std::enable_shared_from_this<PasteboardDelayGetterInstance> {
29 public:
30     class PasteboardDelayGetterImpl : public MiscServices::PasteboardDelayGetter {
31     public:
32         explicit PasteboardDelayGetterImpl() = default;
33         void GetPasteData(const std::string &type, MiscServices::PasteData &data) override;
34         void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data) override;
35         void SetDelayGetterWrapper(const std::shared_ptr<PasteboardDelayGetterInstance> observerInstance);
36 
37     private:
38         std::weak_ptr<PasteboardDelayGetterInstance> wrapper_;
39     };
40     explicit PasteboardDelayGetterInstance(const napi_env &env, const napi_ref &ref);
41     ~PasteboardDelayGetterInstance();
42 
43     void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data);
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<PasteboardDelayGetterImpl> GetStub()
56     {
57         return stub_;
58     }
59 
60 private:
61     napi_env env_ = nullptr;
62     napi_ref ref_ = nullptr;
63     std::shared_ptr<PasteboardDelayGetterImpl> stub_ = nullptr;
64 };
65 
66 struct PasteboardDelayWorker {
67     std::string dataType;
68     std::shared_ptr<PasteboardDelayGetterInstance> delayGetter = nullptr;
69     std::shared_ptr<UDMF::UnifiedData> unifiedData = nullptr;
70     bool complete;
71     bool clean;
72     std::condition_variable cv;
73     std::mutex mutex;
74 };
75 
76 struct HasContextInfo : public AsyncCall::Context {
77     bool hasPasteData;
78     napi_status status = napi_generic_failure;
HasContextInfoHasContextInfo79     HasContextInfo() : Context(nullptr, nullptr){};
80 
operatorHasContextInfo81     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
82     {
83         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
84         return Context::operator()(env, argc, argv, self);
85     }
operatorHasContextInfo86     napi_status operator()(napi_env env, napi_value *result) override
87     {
88         if (status != napi_ok) {
89             return status;
90         }
91         return Context::operator()(env, result);
92     }
93 };
94 
95 struct SetContextInfo : public AsyncCall::Context {
96     std::shared_ptr<MiscServices::PasteData> obj;
97     napi_status status = napi_generic_failure;
SetContextInfoSetContextInfo98     SetContextInfo() : Context(nullptr, nullptr){};
99 
operatorSetContextInfo100     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
101     {
102         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
103         return Context::operator()(env, argc, argv, self);
104     }
operatorSetContextInfo105     napi_status operator()(napi_env env, napi_value *result) override
106     {
107         if (status != napi_ok) {
108             return status;
109         }
110         return Context::operator()(env, result);
111     }
112 };
113 
114 struct SetUnifiedContextInfo : public AsyncCall::Context {
115     std::shared_ptr<PasteboardDelayGetterInstance> delayGetter;
116     std::shared_ptr<UDMF::UnifiedData> obj;
117     bool isDelay = false;
118     napi_status status = napi_generic_failure;
SetUnifiedContextInfoSetUnifiedContextInfo119     SetUnifiedContextInfo() : Context(nullptr, nullptr){};
120 
operatorSetUnifiedContextInfo121     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
122     {
123         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
124         return Context::operator()(env, argc, argv, self);
125     }
operatorSetUnifiedContextInfo126     napi_status operator()(napi_env env, napi_value *result) override
127     {
128         if (status != napi_ok) {
129             return status;
130         }
131         return Context::operator()(env, result);
132     }
133 };
134 
135 struct GetContextInfo : public AsyncCall::Context {
136     std::shared_ptr<MiscServices::PasteData> pasteData;
137     napi_status status = napi_generic_failure;
GetContextInfoGetContextInfo138     GetContextInfo() : Context(nullptr, nullptr){};
139 
operatorGetContextInfo140     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
141     {
142         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
143         return Context::operator()(env, argc, argv, self);
144     }
operatorGetContextInfo145     napi_status operator()(napi_env env, napi_value *result) override
146     {
147         if (status != napi_ok) {
148             return status;
149         }
150         return Context::operator()(env, result);
151     }
152 };
153 
154 struct GetUnifiedContextInfo : public AsyncCall::Context {
155     std::shared_ptr<UDMF::UnifiedData> unifiedData;
156     napi_status status = napi_generic_failure;
GetUnifiedContextInfoGetUnifiedContextInfo157     GetUnifiedContextInfo() : Context(nullptr, nullptr){};
158 
operatorGetUnifiedContextInfo159     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
160     {
161         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
162         return Context::operator()(env, argc, argv, self);
163     }
operatorGetUnifiedContextInfo164     napi_status operator()(napi_env env, napi_value *result) override
165     {
166         if (status != napi_ok) {
167             return status;
168         }
169         return Context::operator()(env, result);
170     }
171 };
172 
173 struct GetMimeTypesContextInfo : public AsyncCall::Context {
174     std::vector<std::string> mimeTypes;
175     napi_status status = napi_generic_failure;
GetMimeTypesContextInfoGetMimeTypesContextInfo176     GetMimeTypesContextInfo() : Context(nullptr, nullptr){};
177 
operatorGetMimeTypesContextInfo178     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
179     {
180         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
181         return Context::operator()(env, argc, argv, self);
182     }
operatorGetMimeTypesContextInfo183     napi_status operator()(napi_env env, napi_value *result) override
184     {
185         if (status != napi_ok) {
186             return status;
187         }
188         return Context::operator()(env, result);
189     }
190 };
191 
192 struct DetectPatternsContextInfo : public AsyncCall::Context {
193     std::set<MiscServices::Pattern> patternsDetect;
194     std::set<MiscServices::Pattern> patternsToCheck;
195     napi_status status = napi_generic_failure;
DetectPatternsContextInfoDetectPatternsContextInfo196     DetectPatternsContextInfo() : Context(nullptr, nullptr){};
197 
operatorDetectPatternsContextInfo198     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
199     {
200         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
201         return Context::operator()(env, argc, argv, self);
202     }
operatorDetectPatternsContextInfo203     napi_status operator()(napi_env env, napi_value *result) override
204     {
205         if (status != napi_ok) {
206             return status;
207         }
208         return Context::operator()(env, result);
209     }
210 };
211 
212 struct GetDataParamsContextInfo : public AsyncCall::Context {
213     std::shared_ptr<MiscServices::PasteData> pasteData;
214     std::shared_ptr<MiscServices::GetDataParams> getDataParams;
215     napi_status status = napi_generic_failure;
GetDataParamsContextInfoGetDataParamsContextInfo216     GetDataParamsContextInfo() : Context(nullptr, nullptr){};
217 
operatorGetDataParamsContextInfo218     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
219     {
220         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
221         return Context::operator()(env, argc, argv, self);
222     }
operatorGetDataParamsContextInfo223     napi_status operator()(napi_env env, napi_value *result) override
224     {
225         if (status != napi_ok) {
226             return status;
227         }
228         return Context::operator()(env, result);
229     }
230 };
231 
232 typedef struct {
233     napi_threadsafe_function tsFunction;
234     napi_value jsCallback;
235 } ProgressListenerFn;
236 
237 const std::map<MiscServices::PasteboardError, std::pair<MiscServices::JSErrorCode, std::string>> ErrorCodeMap = {
238     {MiscServices::PasteboardError::TASK_PROCESSING,
239      {MiscServices::JSErrorCode::OTHER_COPY_OR_PASTE_IN_PROCESSING, "Another copy or paste operation is in progress."}},
240     {MiscServices::PasteboardError::COPY_FILE_ERROR,
241      {MiscServices::JSErrorCode::ERR_COPY_FILE_ERROR, "Invalid destUri or file system error."}},
242     {MiscServices::PasteboardError::PROGRESS_START_ERROR,
243      {MiscServices::JSErrorCode::ERR_PROGRESS_START_ERROR, "Failed to start progress."}},
244     {MiscServices::PasteboardError::PROGRESS_ABNORMAL,
245      {MiscServices::JSErrorCode::ERR_PROGRESS_ABNORMAL, "Progress exits abnormally."}},
246     {MiscServices::PasteboardError::PERMISSION_VERIFICATION_ERROR,
247      {MiscServices::JSErrorCode::NO_PERMISSION,
248       "Permission verification failed. A non-permission application calls a API."}},
249     {MiscServices::PasteboardError::INVALID_PARAM_ERROR,
250      {MiscServices::JSErrorCode::INVALID_PARAMETERS, "Parameter error. Parameter verification failed."}},
251 };
252 
253 class SystemPasteboardNapi {
254 public:
255     static napi_value SystemPasteboardInit(napi_env env, napi_value exports);
256     static napi_value New(napi_env env, napi_callback_info info);
257     static napi_status NewInstance(napi_env env, napi_value &instance);
258     static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
259     SystemPasteboardNapi();
260     ~SystemPasteboardNapi();
261 
262 private:
263     static napi_value On(napi_env env, napi_callback_info info);
264     static napi_value Off(napi_env env, napi_callback_info info);
265     static napi_value Clear(napi_env env, napi_callback_info info);
266     static napi_value GetPasteData(napi_env env, napi_callback_info info);
267     static napi_value SetPasteData(napi_env env, napi_callback_info info);
268     static napi_value HasPasteData(napi_env env, napi_callback_info info);
269     static napi_value ClearData(napi_env env, napi_callback_info info);
270     static napi_value GetData(napi_env env, napi_callback_info info);
271     static napi_value SetData(napi_env env, napi_callback_info info);
272     static napi_value HasData(napi_env env, napi_callback_info info);
273     static napi_value IsRemoteData(napi_env env, napi_callback_info info);
274     static napi_value GetDataSource(napi_env env, napi_callback_info info);
275     static napi_value GetChangeCount(napi_env env, napi_callback_info info);
276     static napi_value GetMimeTypes(napi_env env, napi_callback_info info);
277     static napi_value HasDataType(napi_env env, napi_callback_info info);
278     static napi_value DetectPatterns(napi_env env, napi_callback_info info);
279     static napi_value ClearDataSync(napi_env env, napi_callback_info info);
280     static napi_value GetDataSync(napi_env env, napi_callback_info info);
281     static napi_value SetDataSync(napi_env env, napi_callback_info info);
282     static napi_value HasDataSync(napi_env env, napi_callback_info info);
283     static bool CheckArgsOfOnAndOff(napi_env env, bool checkArgsCount, napi_value *argv, size_t argc);
284     static sptr<PasteboardObserverInstance> GetObserver(napi_env env, napi_value jsCallback);
285     static void AddObserver(napi_env env, napi_value jsCallback);
286     static void DeleteObserver(napi_env env, const sptr<PasteboardObserverInstance> &observer);
287     static void GetDataCommon(std::shared_ptr<GetContextInfo> &context);
288     static void SetDataCommon(std::shared_ptr<SetContextInfo> &context);
289 
290     static void GetDataCommon(std::shared_ptr<GetUnifiedContextInfo> &context);
291     static void SetDataCommon(std::shared_ptr<SetUnifiedContextInfo> &context);
292 
293     static napi_value GetUnifiedData(napi_env env, napi_callback_info info);
294     static napi_value GetUnifiedDataSync(napi_env env, napi_callback_info info);
295     static napi_value SetUnifiedData(napi_env env, napi_callback_info info);
296     static napi_value SetUnifiedDataSync(napi_env env, napi_callback_info info);
297 
298     static napi_value SetAppShareOptions(napi_env env, napi_callback_info info);
299     static napi_value RemoveAppShareOptions(napi_env env, napi_callback_info info);
300 
301     static void ProgressNotify(std::shared_ptr<MiscServices::GetDataParams> params);
302     static void CallJsProgressNotify(napi_env env, napi_value jsFunction, void *context, void *data);
303     static bool ParseJsGetDataWithProgress(napi_env env, napi_value in,
304         std::shared_ptr<MiscServices::GetDataParams> &getDataParam);
305     static bool AddProgressListener(napi_env env, std::shared_ptr<MiscServices::GetDataParams> getDataParam,
306         const std::shared_ptr<ProgressListenerFn> listenerFn);
307     static bool CreateThreadSafeFunc(napi_env env, const std::shared_ptr<ProgressListenerFn> listenerFn);
308     static void GetDataWithProgressParam(std::shared_ptr<GetDataParamsContextInfo> &context);
309     static napi_value GetDataWithProgress(napi_env env, napi_callback_info info);
310     static bool CheckParamsType(napi_env env, napi_value in, napi_valuetype expectedType);
311 
312     static std::mutex delayMutex_;
313     std::shared_ptr<PasteDataNapi> value_;
314     std::shared_ptr<MiscServices::PasteData> pasteData_;
315     napi_env env_;
316     static thread_local std::map<napi_ref, sptr<PasteboardObserverInstance>> observers_;
317     static std::shared_ptr<PasteboardDelayGetterInstance> delayGetter_;
318 
319     static std::recursive_mutex listenerMutex_;
320     static std::map<std::string, std::shared_ptr<ProgressListenerFn>> listenerMap_;
321 };
322 } // namespace MiscServicesNapi
323 } // namespace OHOS
324 #endif