1 /*
2  * Copyright (c) 2024 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 #include "system_pasteboard_impl.h"
17 #include "pasteboard_client.h"
18 #include "pasteboard_error.h"
19 
20 using namespace OHOS::MiscServices;
21 
22 namespace OHOS {
23 namespace MiscServicesCj {
24 
25 static sptr<SystemPasteboardImpl> g_systemPasteboard_instance = nullptr;
26 
GetClassType()27 OHOS::FFI::RuntimeType *SystemPasteboardImpl::GetClassType()
28 {
29     static OHOS::FFI::RuntimeType runtimeType =
30         OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("SystemPasteboardImpl");
31     return &runtimeType;
32 }
33 
SystemPasteboardImpl()34 SystemPasteboardImpl::SystemPasteboardImpl()
35 {
36     value_ = nullptr;
37     pasteData_ = nullptr;
38 }
39 
NewInstance(sptr<SystemPasteboardImpl> & instance)40 int NewInstance(sptr<SystemPasteboardImpl> &instance)
41 {
42     if (g_systemPasteboard_instance != nullptr) {
43         instance = g_systemPasteboard_instance;
44         return 0;
45     }
46     g_systemPasteboard_instance = FFI::FFIData::Create<SystemPasteboardImpl>();
47     instance = g_systemPasteboard_instance;
48     return 0;
49 }
50 
GetSystemPasteboardImpl(int64_t & id)51 int32_t SystemPasteboardImpl::GetSystemPasteboardImpl(int64_t &id)
52 {
53     sptr<SystemPasteboardImpl> instence = nullptr;
54     int status = NewInstance(instence);
55     if (status != 0) {
56         LOGE("[SystemPasteboardImpl] CJgetSystemPasteboard create instance failed");
57         return status;
58     }
59     id = instence->GetID();
60     return 0;
61 }
62 
SetData(sptr<PasteDataImpl> dataImpl,std::shared_ptr<MiscServices::PasteData> data)63 int32_t SystemPasteboardImpl::SetData(sptr<PasteDataImpl> dataImpl, std::shared_ptr<MiscServices::PasteData> data)
64 {
65     if (data == nullptr) {
66         LOGE("[SystemPasteboardImpl] SetData data nullptr");
67         return PASTEBOARD_INVALID_PARAMETERS;
68     }
69     int32_t ret = PasteboardClient::GetInstance()->SetPasteData(*data);
70     int32_t res = PASTEBOARD_SUCCESS;
71     if (ret == static_cast<int>(PasteboardError::E_OK)) {
72         value_ = dataImpl;
73         pasteData_ = data;
74         LOGI("[SystemPasteboardImpl] SetData OK");
75     } else if (ret == static_cast<int>(PasteboardError::PROHIBIT_COPY)) {
76         res = PASTEBOARD_COPY_FORBIDDEN;
77         LOGE("[SystemPasteboardImpl] SetData ERR PROHIBIT_COPY");
78     } else if (ret == static_cast<int>(PasteboardError::TASK_PROCESSING)) {
79         LOGE("[SystemPasteboardImpl] SetData ERR TASK_PROCESSING");
80         res = PASTEBOARD_IS_BEGING_PROCESSED;
81     }
82 
83     return res;
84 }
85 
GetSystemPasteboardPasteDataImpl()86 sptr<PasteDataImpl> SystemPasteboardImpl::GetSystemPasteboardPasteDataImpl()
87 {
88     if (value_ == nullptr) {
89         return nullptr;
90     }
91 
92     return value_;
93 }
94 
GetData(MiscServices::PasteData & pasteData)95 int32_t SystemPasteboardImpl::GetData(MiscServices::PasteData &pasteData)
96 {
97     int32_t ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
98     return ret;
99 }
100 
HasData()101 bool SystemPasteboardImpl::HasData()
102 {
103     bool res = PasteboardClient::GetInstance()->HasPasteData();
104     return res;
105 }
106 
ClearData()107 void SystemPasteboardImpl::ClearData()
108 {
109     PasteboardClient::GetInstance()->Clear();
110     value_ = nullptr;
111     pasteData_ = nullptr;
112 }
113 
IsRemoteData()114 bool SystemPasteboardImpl::IsRemoteData()
115 {
116     bool res = PasteboardClient::GetInstance()->IsRemoteData();
117     return res;
118 }
119 
HasDataType(std::string mimeType)120 bool SystemPasteboardImpl::HasDataType(std::string mimeType)
121 {
122     bool res = PasteboardClient::GetInstance()->HasDataType(mimeType);
123     return res;
124 }
125 
GetDataSource()126 std::string SystemPasteboardImpl::GetDataSource()
127 {
128     std::string res;
129     PasteboardClient::GetInstance()->GetDataSource(res);
130     return res;
131 }
132 
133 }
134 }