1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci#ifndef N_NAPI_PASTE_H
16bc03f14fSopenharmony_ci#define N_NAPI_PASTE_H
17bc03f14fSopenharmony_ci
18bc03f14fSopenharmony_ci#include <atomic>
19bc03f14fSopenharmony_ci
20bc03f14fSopenharmony_ci#include "async_call.h"
21bc03f14fSopenharmony_ci#include "common/block_object.h"
22bc03f14fSopenharmony_ci#include "napi/native_api.h"
23bc03f14fSopenharmony_ci#include "napi/native_node_api.h"
24bc03f14fSopenharmony_ci#include "pasteboard_delay_getter.h"
25bc03f14fSopenharmony_ci#include "pasteboard_observer.h"
26bc03f14fSopenharmony_ci#include "pastedata_napi.h"
27bc03f14fSopenharmony_ci#include "pastedata_record_napi.h"
28bc03f14fSopenharmony_ci#include "pixel_map_napi.h"
29bc03f14fSopenharmony_ci#include "unified_data.h"
30bc03f14fSopenharmony_ci#include "unified_data_napi.h"
31bc03f14fSopenharmony_ci#include "uri.h"
32bc03f14fSopenharmony_ci
33bc03f14fSopenharmony_cinamespace OHOS {
34bc03f14fSopenharmony_cinamespace MiscServicesNapi {
35bc03f14fSopenharmony_ciclass PasteboardObserverInstance : public std::enable_shared_from_this<PasteboardObserverInstance> {
36bc03f14fSopenharmony_cipublic:
37bc03f14fSopenharmony_ci    class PasteboardObserverImpl : public MiscServices::PasteboardObserver {
38bc03f14fSopenharmony_ci    public:
39bc03f14fSopenharmony_ci        explicit PasteboardObserverImpl() = default;
40bc03f14fSopenharmony_ci        void OnPasteboardChanged() override;
41bc03f14fSopenharmony_ci        void SetObserverWrapper(const std::shared_ptr<PasteboardObserverInstance> &observerInstance);
42bc03f14fSopenharmony_ci
43bc03f14fSopenharmony_ci    private:
44bc03f14fSopenharmony_ci        std::weak_ptr<PasteboardObserverInstance> wrapper_;
45bc03f14fSopenharmony_ci    };
46bc03f14fSopenharmony_ci
47bc03f14fSopenharmony_ci    explicit PasteboardObserverInstance(const napi_env &env, const napi_ref &ref);
48bc03f14fSopenharmony_ci    ~PasteboardObserverInstance();
49bc03f14fSopenharmony_ci
50bc03f14fSopenharmony_ci    void OnPasteboardChanged();
51bc03f14fSopenharmony_ci    napi_env GetEnv()
52bc03f14fSopenharmony_ci    {
53bc03f14fSopenharmony_ci        return env_;
54bc03f14fSopenharmony_ci    }
55bc03f14fSopenharmony_ci    napi_ref GetRef()
56bc03f14fSopenharmony_ci    {
57bc03f14fSopenharmony_ci        return ref_;
58bc03f14fSopenharmony_ci    }
59bc03f14fSopenharmony_ci    sptr<PasteboardObserverImpl> GetStub();
60bc03f14fSopenharmony_ci
61bc03f14fSopenharmony_ciprivate:
62bc03f14fSopenharmony_ci    napi_env env_ = nullptr;
63bc03f14fSopenharmony_ci    napi_ref ref_ = nullptr;
64bc03f14fSopenharmony_ci    sptr<PasteboardObserverImpl> stub_ = nullptr;
65bc03f14fSopenharmony_ci};
66bc03f14fSopenharmony_ci
67bc03f14fSopenharmony_ciclass PasteboardDelayGetterInstance : public std::enable_shared_from_this<PasteboardDelayGetterInstance> {
68bc03f14fSopenharmony_cipublic:
69bc03f14fSopenharmony_ci    class PasteboardDelayGetterImpl : public MiscServices::PasteboardDelayGetter {
70bc03f14fSopenharmony_ci    public:
71bc03f14fSopenharmony_ci        explicit PasteboardDelayGetterImpl() = default;
72bc03f14fSopenharmony_ci        void GetPasteData(const std::string &type, MiscServices::PasteData &data) override;
73bc03f14fSopenharmony_ci        void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data) override;
74bc03f14fSopenharmony_ci        void SetDelayGetterWrapper(const std::shared_ptr<PasteboardDelayGetterInstance> observerInstance);
75bc03f14fSopenharmony_ci
76bc03f14fSopenharmony_ci    private:
77bc03f14fSopenharmony_ci        std::weak_ptr<PasteboardDelayGetterInstance> wrapper_;
78bc03f14fSopenharmony_ci    };
79bc03f14fSopenharmony_ci    explicit PasteboardDelayGetterInstance(const napi_env &env, const napi_ref &ref);
80bc03f14fSopenharmony_ci    ~PasteboardDelayGetterInstance();
81bc03f14fSopenharmony_ci
82bc03f14fSopenharmony_ci    void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data);
83bc03f14fSopenharmony_ci
84bc03f14fSopenharmony_ci    napi_env GetEnv()
85bc03f14fSopenharmony_ci    {
86bc03f14fSopenharmony_ci        return env_;
87bc03f14fSopenharmony_ci    }
88bc03f14fSopenharmony_ci
89bc03f14fSopenharmony_ci    napi_ref GetRef()
90bc03f14fSopenharmony_ci    {
91bc03f14fSopenharmony_ci        return ref_;
92bc03f14fSopenharmony_ci    }
93bc03f14fSopenharmony_ci
94bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterImpl> GetStub()
95bc03f14fSopenharmony_ci    {
96bc03f14fSopenharmony_ci        return stub_;
97bc03f14fSopenharmony_ci    }
98bc03f14fSopenharmony_ci
99bc03f14fSopenharmony_ciprivate:
100bc03f14fSopenharmony_ci    napi_env env_ = nullptr;
101bc03f14fSopenharmony_ci    napi_ref ref_ = nullptr;
102bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterImpl> stub_ = nullptr;
103bc03f14fSopenharmony_ci};
104bc03f14fSopenharmony_ci
105bc03f14fSopenharmony_cistruct PasteboardDataWorker {
106bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardObserverInstance> observer = nullptr;
107bc03f14fSopenharmony_ci};
108bc03f14fSopenharmony_ci
109bc03f14fSopenharmony_cistruct PasteboardDelayWorker {
110bc03f14fSopenharmony_ci    std::string dataType;
111bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterInstance> delayGetter = nullptr;
112bc03f14fSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> unifiedData = nullptr;
113bc03f14fSopenharmony_ci    bool complete;
114bc03f14fSopenharmony_ci    bool clean;
115bc03f14fSopenharmony_ci    std::condition_variable cv;
116bc03f14fSopenharmony_ci    std::mutex mutex;
117bc03f14fSopenharmony_ci};
118bc03f14fSopenharmony_ci
119bc03f14fSopenharmony_cistruct HasContextInfo : public AsyncCall::Context {
120bc03f14fSopenharmony_ci    bool hasPasteData;
121bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
122bc03f14fSopenharmony_ci    HasContextInfo() : Context(nullptr, nullptr){};
123bc03f14fSopenharmony_ci
124bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
125bc03f14fSopenharmony_ci    {
126bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
127bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
128bc03f14fSopenharmony_ci    }
129bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
130bc03f14fSopenharmony_ci    {
131bc03f14fSopenharmony_ci        if (status != napi_ok) {
132bc03f14fSopenharmony_ci            return status;
133bc03f14fSopenharmony_ci        }
134bc03f14fSopenharmony_ci        return Context::operator()(env, result);
135bc03f14fSopenharmony_ci    }
136bc03f14fSopenharmony_ci};
137bc03f14fSopenharmony_ci
138bc03f14fSopenharmony_cistruct SetContextInfo : public AsyncCall::Context {
139bc03f14fSopenharmony_ci    std::shared_ptr<MiscServices::PasteData> obj;
140bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
141bc03f14fSopenharmony_ci    SetContextInfo() : Context(nullptr, nullptr){};
142bc03f14fSopenharmony_ci
143bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
144bc03f14fSopenharmony_ci    {
145bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
146bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
147bc03f14fSopenharmony_ci    }
148bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
149bc03f14fSopenharmony_ci    {
150bc03f14fSopenharmony_ci        if (status != napi_ok) {
151bc03f14fSopenharmony_ci            return status;
152bc03f14fSopenharmony_ci        }
153bc03f14fSopenharmony_ci        return Context::operator()(env, result);
154bc03f14fSopenharmony_ci    }
155bc03f14fSopenharmony_ci};
156bc03f14fSopenharmony_ci
157bc03f14fSopenharmony_cistruct SetUnifiedContextInfo : public AsyncCall::Context {
158bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterInstance> delayGetter;
159bc03f14fSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> obj;
160bc03f14fSopenharmony_ci    bool isDelay = false;
161bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
162bc03f14fSopenharmony_ci    SetUnifiedContextInfo() : Context(nullptr, nullptr){};
163bc03f14fSopenharmony_ci
164bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
165bc03f14fSopenharmony_ci    {
166bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
167bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
168bc03f14fSopenharmony_ci    }
169bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
170bc03f14fSopenharmony_ci    {
171bc03f14fSopenharmony_ci        if (status != napi_ok) {
172bc03f14fSopenharmony_ci            return status;
173bc03f14fSopenharmony_ci        }
174bc03f14fSopenharmony_ci        return Context::operator()(env, result);
175bc03f14fSopenharmony_ci    }
176bc03f14fSopenharmony_ci};
177bc03f14fSopenharmony_ci
178bc03f14fSopenharmony_cistruct GetContextInfo : public AsyncCall::Context {
179bc03f14fSopenharmony_ci    std::shared_ptr<MiscServices::PasteData> pasteData;
180bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
181bc03f14fSopenharmony_ci    GetContextInfo() : Context(nullptr, nullptr){};
182bc03f14fSopenharmony_ci
183bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
184bc03f14fSopenharmony_ci    {
185bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
186bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
187bc03f14fSopenharmony_ci    }
188bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
189bc03f14fSopenharmony_ci    {
190bc03f14fSopenharmony_ci        if (status != napi_ok) {
191bc03f14fSopenharmony_ci            return status;
192bc03f14fSopenharmony_ci        }
193bc03f14fSopenharmony_ci        return Context::operator()(env, result);
194bc03f14fSopenharmony_ci    }
195bc03f14fSopenharmony_ci};
196bc03f14fSopenharmony_ci
197bc03f14fSopenharmony_cistruct GetUnifiedContextInfo : public AsyncCall::Context {
198bc03f14fSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> unifiedData;
199bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
200bc03f14fSopenharmony_ci    GetUnifiedContextInfo() : Context(nullptr, nullptr){};
201bc03f14fSopenharmony_ci
202bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
203bc03f14fSopenharmony_ci    {
204bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
205bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
206bc03f14fSopenharmony_ci    }
207bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
208bc03f14fSopenharmony_ci    {
209bc03f14fSopenharmony_ci        if (status != napi_ok) {
210bc03f14fSopenharmony_ci            return status;
211bc03f14fSopenharmony_ci        }
212bc03f14fSopenharmony_ci        return Context::operator()(env, result);
213bc03f14fSopenharmony_ci    }
214bc03f14fSopenharmony_ci};
215bc03f14fSopenharmony_ci
216bc03f14fSopenharmony_cistruct DetectPatternsContextInfo : public AsyncCall::Context {
217bc03f14fSopenharmony_ci    std::set<MiscServices::Pattern> patternsDetect;
218bc03f14fSopenharmony_ci    std::set<MiscServices::Pattern> patternsToCheck;
219bc03f14fSopenharmony_ci    napi_status status = napi_generic_failure;
220bc03f14fSopenharmony_ci    DetectPatternsContextInfo() : Context(nullptr, nullptr){};
221bc03f14fSopenharmony_ci
222bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
223bc03f14fSopenharmony_ci    {
224bc03f14fSopenharmony_ci        NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
225bc03f14fSopenharmony_ci        return Context::operator()(env, argc, argv, self);
226bc03f14fSopenharmony_ci    }
227bc03f14fSopenharmony_ci    napi_status operator()(napi_env env, napi_value *result) override
228bc03f14fSopenharmony_ci    {
229bc03f14fSopenharmony_ci        if (status != napi_ok) {
230bc03f14fSopenharmony_ci            return status;
231bc03f14fSopenharmony_ci        }
232bc03f14fSopenharmony_ci        return Context::operator()(env, result);
233bc03f14fSopenharmony_ci    }
234bc03f14fSopenharmony_ci};
235bc03f14fSopenharmony_ci
236bc03f14fSopenharmony_ciclass SystemPasteboardNapi {
237bc03f14fSopenharmony_cipublic:
238bc03f14fSopenharmony_ci    static napi_value SystemPasteboardInit(napi_env env, napi_value exports);
239bc03f14fSopenharmony_ci    static napi_value New(napi_env env, napi_callback_info info);
240bc03f14fSopenharmony_ci    static napi_status NewInstance(napi_env env, napi_value &instance);
241bc03f14fSopenharmony_ci    static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
242bc03f14fSopenharmony_ci    static void DeleteObserver(const std::shared_ptr<PasteboardObserverInstance> &observer);
243bc03f14fSopenharmony_ci    SystemPasteboardNapi();
244bc03f14fSopenharmony_ci    ~SystemPasteboardNapi();
245bc03f14fSopenharmony_ci
246bc03f14fSopenharmony_ciprivate:
247bc03f14fSopenharmony_ci    static napi_value On(napi_env env, napi_callback_info info);
248bc03f14fSopenharmony_ci    static napi_value Off(napi_env env, napi_callback_info info);
249bc03f14fSopenharmony_ci    static napi_value Clear(napi_env env, napi_callback_info info);
250bc03f14fSopenharmony_ci    static napi_value GetPasteData(napi_env env, napi_callback_info info);
251bc03f14fSopenharmony_ci    static napi_value SetPasteData(napi_env env, napi_callback_info info);
252bc03f14fSopenharmony_ci    static napi_value HasPasteData(napi_env env, napi_callback_info info);
253bc03f14fSopenharmony_ci    static napi_value ClearData(napi_env env, napi_callback_info info);
254bc03f14fSopenharmony_ci    static napi_value GetData(napi_env env, napi_callback_info info);
255bc03f14fSopenharmony_ci    static napi_value SetData(napi_env env, napi_callback_info info);
256bc03f14fSopenharmony_ci    static napi_value HasData(napi_env env, napi_callback_info info);
257bc03f14fSopenharmony_ci    static napi_value IsRemoteData(napi_env env, napi_callback_info info);
258bc03f14fSopenharmony_ci    static napi_value GetDataSource(napi_env env, napi_callback_info info);
259bc03f14fSopenharmony_ci    static napi_value HasDataType(napi_env env, napi_callback_info info);
260bc03f14fSopenharmony_ci    static napi_value DetectPatterns(napi_env env, napi_callback_info info);
261bc03f14fSopenharmony_ci    static napi_value ClearDataSync(napi_env env, napi_callback_info info);
262bc03f14fSopenharmony_ci    static napi_value GetDataSync(napi_env env, napi_callback_info info);
263bc03f14fSopenharmony_ci    static napi_value SetDataSync(napi_env env, napi_callback_info info);
264bc03f14fSopenharmony_ci    static napi_value HasDataSync(napi_env env, napi_callback_info info);
265bc03f14fSopenharmony_ci    static bool CheckAgrsOfOnAndOff(napi_env env, bool checkArgsCount, napi_value *argv, size_t argc);
266bc03f14fSopenharmony_ci    static void SetObserver(napi_ref ref, std::shared_ptr<PasteboardObserverInstance> observer);
267bc03f14fSopenharmony_ci    static std::shared_ptr<PasteboardObserverInstance> GetObserver(napi_env env, napi_value observer);
268bc03f14fSopenharmony_ci    static void GetDataCommon(std::shared_ptr<GetContextInfo> &context);
269bc03f14fSopenharmony_ci    static void SetDataCommon(std::shared_ptr<SetContextInfo> &context);
270bc03f14fSopenharmony_ci
271bc03f14fSopenharmony_ci    static void GetDataCommon(std::shared_ptr<GetUnifiedContextInfo> &context);
272bc03f14fSopenharmony_ci    static void SetDataCommon(std::shared_ptr<SetUnifiedContextInfo> &context);
273bc03f14fSopenharmony_ci
274bc03f14fSopenharmony_ci    static napi_value GetUnifiedData(napi_env env, napi_callback_info info);
275bc03f14fSopenharmony_ci    static napi_value GetUnifiedDataSync(napi_env env, napi_callback_info info);
276bc03f14fSopenharmony_ci    static napi_value SetUnifiedData(napi_env env, napi_callback_info info);
277bc03f14fSopenharmony_ci    static napi_value SetUnifiedDataSync(napi_env env, napi_callback_info info);
278bc03f14fSopenharmony_ci
279bc03f14fSopenharmony_ci    static napi_value SetAppShareOptions(napi_env env, napi_callback_info info);
280bc03f14fSopenharmony_ci    static napi_value RemoveAppShareOptions(napi_env env, napi_callback_info info);
281bc03f14fSopenharmony_ci
282bc03f14fSopenharmony_ci    static std::mutex delayMutex_;
283bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataNapi> value_;
284bc03f14fSopenharmony_ci    std::shared_ptr<MiscServices::PasteData> pasteData_;
285bc03f14fSopenharmony_ci    napi_env env_;
286bc03f14fSopenharmony_ci    static thread_local std::map<napi_ref, std::shared_ptr<PasteboardObserverInstance>> observers_;
287bc03f14fSopenharmony_ci    static std::shared_ptr<PasteboardDelayGetterInstance> delayGetter_;
288bc03f14fSopenharmony_ci};
289bc03f14fSopenharmony_ci} // namespace MiscServicesNapi
290bc03f14fSopenharmony_ci} // namespace OHOS
291bc03f14fSopenharmony_ci#endif