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 PASTE_BOARD_DATA_H
16 #define PASTE_BOARD_DATA_H
17 
18 #include <chrono>
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "paste_data_record.h"
26 #include "pasteboard_hilog.h"
27 #include "tlv_object.h"
28 #include "uri.h"
29 #include "want.h"
30 #include "want_params.h"
31 #include "parcel.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 enum ShareOption : int32_t { InApp = 0, LocalDevice, CrossDevice };
36 enum ScreenEvent : int32_t { Default = 0, ScreenLocked, ScreenUnlocked };
37 struct API_EXPORT PasteDataProperty : public TLVObject {
38     PasteDataProperty() = default;
39     ~PasteDataProperty();
40     explicit PasteDataProperty(const PasteDataProperty &property);
41     PasteDataProperty& operator=(const PasteDataProperty &property);
42     AAFwk::WantParams additions;
43     std::vector<std::string> mimeTypes;
44     std::string tag;
45     std::int64_t timestamp;
46     bool localOnly;
47     ShareOption shareOption;
48     uint32_t tokenId = 0;
49     bool isRemote = false;
50     std::string bundleName;
51     std::string setTime;
52     ScreenEvent screenStatus = ScreenEvent::Default;
53 
54     bool Encode(std::vector<std::uint8_t> &buffer) override;
55     bool Decode(const std::vector<std::uint8_t> &buffer) override;
56     bool DecodeTag(const std::vector<std::uint8_t> &buffer);
57     size_t Count() override;
58 };
59 
60 class API_EXPORT PasteData : public TLVObject, public Parcelable {
61 public:
62     static constexpr const std::uint32_t MAX_RECORD_NUM = 512;
63     PasteData();
64     ~PasteData();
65     PasteData(const PasteData &data);
66     PasteData& operator=(const PasteData &data);
67     explicit PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records);
68 
69     void AddHtmlRecord(const std::string &html);
70     void AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
71     void AddPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
72     void AddTextRecord(const std::string &text);
73     void AddUriRecord(const OHOS::Uri &uri);
74     void AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want);
75     void AddRecord(std::shared_ptr<PasteDataRecord> record);
76     void AddRecord(PasteDataRecord &record);
77     std::vector<std::string> GetMimeTypes();
78     std::vector<std::string> GetMimeTypesWithEntry();
79     std::shared_ptr<std::string> GetPrimaryHtml();
80     std::shared_ptr<OHOS::Media::PixelMap> GetPrimaryPixelMap();
81     std::shared_ptr<std::string> GetPrimaryText();
82     std::shared_ptr<OHOS::Uri> GetPrimaryUri();
83     std::shared_ptr<std::string> GetPrimaryMimeType();
84     std::shared_ptr<OHOS::AAFwk::Want> GetPrimaryWant();
85     std::shared_ptr<PasteDataRecord> GetRecordAt(std::size_t index) const;
86     std::size_t GetRecordCount() const;
87     bool RemoveRecordAt(std::size_t number);
88     bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record);
89     bool HasMimeType(const std::string &mimeType);
90     PasteDataProperty GetProperty() const;
91     void SetProperty(const PasteDataProperty &property);
92     ShareOption GetShareOption();
93     void SetShareOption(ShareOption shareOption);
94     uint32_t GetTokenId();
95     void SetTokenId(uint32_t tokenId);
96     std::vector<std::shared_ptr<PasteDataRecord>> AllRecords() const;
97     bool IsDraggedData() const;
98     void SetDraggedDataFlag(bool isDraggedData);
99     bool IsLocalPaste() const;
100     void SetLocalPasteFlag(bool isLocalPaste);
101 
102     void SetBundleName(const std::string &bundleName);
103     std::string GetBundleName() const;
104     void SetOrginAuthority(const std::string &bundleName);
105     std::string GetOrginAuthority() const;
106     void SetRemote(bool isRemote);
107     bool IsRemote();
108     void SetTime(const std::string &time);
109     std::string GetTime();
110     void SetScreenStatus(ScreenEvent screenStatus);
111     ScreenEvent GetScreenStatus();
112     void SetTag(std::string &tag);
113     std::string GetTag();
114     void SetAdditions(AAFwk::WantParams &additions);
115     void SetAddition(const std::string &key, AAFwk::IInterface *value);
116     void SetLocalOnly(bool localOnly);
117     bool GetLocalOnly();
118 
119     bool Encode(std::vector<std::uint8_t> &buffer) override;
120     bool Decode(const std::vector<std::uint8_t> &buffer) override;
121     size_t Count() override;
122     bool WriteUriFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient = true);
123     bool ReadUriFd(MessageParcel &parcel, UriHandler &uriHandler);
124     void ReplaceShareUri(int32_t userId);
125 
126     bool IsValid() const;
127     void SetInvalid();
128 
129     void SetDelayData(bool isDelay);
130     bool IsDelayData() const;
131     void SetDelayRecord(bool isDelay);
132     bool IsDelayRecord() const;
133     void SetDataId(uint32_t dataId);
134     uint32_t GetDataId() const;
135     void SetPasteId(const std::string &pasteId);
136     std::string GetPasteId() const;
137     std::string GetDeviceId() const;
138 
139     static void ShareOptionToString(ShareOption shareOption, std::string &out);
140     bool Marshalling(Parcel &parcel) const override;
141     static PasteData* Unmarshalling(Parcel &parcel);
142     static std::string sharePath;
143     static std::string WEBVIEW_PASTEDATA_TAG;
144     static const std::string DISTRIBUTEDFILES_TAG;
145     static const std::string PATH_SHARE;
146     static const std::string FILE_SCHEME_PREFIX;
147     static const std::string IMG_LOCAL_URI;
148     static const std::string SHARE_PATH_PREFIX;
149     static const std::string SHARE_PATH_PREFIX_ACCOUNT;
150     static const std::string REMOTE_FILE_SIZE;
151     std::string deviceId_;
152     static const std::string REMOTE_FILE_SIZE_LONG;
153 
154 private:
155     void RefreshMimeProp();
156     bool ReadFromParcel(Parcel &parcel);
157 
158     PasteDataProperty props_;
159     std::vector<std::shared_ptr<PasteDataRecord>> records_;
160     std::string orginAuthority_;
161     bool valid_ = true;
162     bool isDraggedData_ = false;
163     bool isLocalPaste_ = false; // local in app paste
164     bool isDelayData_ = false;
165     std::string pasteId_;
166     bool isDelayRecord_ = false;
167     uint32_t dataId_ = 0;
168     uint32_t recordId_ = 0;
169 
170     using Func = std::function<void(bool &ret, const std::vector<std::uint8_t> &buffer, TLVHead &head)>;
171     std::map<uint16_t, Func> decodeMap_;
172     void InitDecodeMap();
173 
174 };
175 } // namespace MiscServices
176 } // namespace OHOS
177 #endif // PASTE_BOARD_DATA_H
178