• 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 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 
32 namespace OHOS {
33 namespace MiscServices {
34 enum ShareOption : int32_t { InApp = 0, LocalDevice, CrossDevice };
35 struct PasteDataProperty : public TLVObject {
36     PasteDataProperty() = default;
37     ~PasteDataProperty();
38     explicit PasteDataProperty(const PasteDataProperty &property);
39     PasteDataProperty& operator=(const PasteDataProperty &property);
40     AAFwk::WantParams additions;
41     std::vector<std::string> mimeTypes;
42     std::string tag;
43     std::int64_t timestamp;
44     bool localOnly;
45     ShareOption shareOption;
46     uint32_t tokenId = 0;
47     bool isRemote = false;
48     std::string bundleName;
49     std::string setTime;
50 
51     bool Encode(std::vector<std::uint8_t> &buffer) override;
52     bool Decode(const std::vector<std::uint8_t> &buffer) override;
53     bool DecodeTag(const std::vector<std::uint8_t> &buffer);
54     size_t Count() override;
55 };
56 
57 class PasteData : public TLVObject {
58 public:
59     static constexpr const std::uint32_t MAX_RECORD_NUM = 512;
60     PasteData();
61     ~PasteData();
62     explicit PasteData(const PasteData &data);
63     PasteData& operator=(const PasteData &data);
64     explicit PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records);
65 
66     void AddHtmlRecord(const std::string &html);
67     void AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
68     void AddPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
69     void AddTextRecord(const std::string &text);
70     void AddUriRecord(const OHOS::Uri &uri);
71     void AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want);
72     void AddRecord(std::shared_ptr<PasteDataRecord> record);
73     void AddRecord(PasteDataRecord &record);
74     std::vector<std::string> GetMimeTypes();
75     std::shared_ptr<std::string> GetPrimaryHtml();
76     std::shared_ptr<OHOS::Media::PixelMap> GetPrimaryPixelMap();
77     std::shared_ptr<std::string> GetPrimaryText();
78     std::shared_ptr<OHOS::Uri> GetPrimaryUri();
79     std::shared_ptr<std::string> GetPrimaryMimeType();
80     std::shared_ptr<OHOS::AAFwk::Want> GetPrimaryWant();
81     std::shared_ptr<PasteDataRecord> GetRecordAt(std::size_t index);
82     std::size_t GetRecordCount();
83     bool RemoveRecordAt(std::size_t number);
84     bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record);
85     bool HasMimeType(const std::string &mimeType);
86     PasteDataProperty GetProperty() const;
87     void SetProperty(const PasteDataProperty &property);
88     ShareOption GetShareOption();
89     void SetShareOption(ShareOption shareOption);
90     uint32_t GetTokenId();
91     void SetTokenId(uint32_t tokenId);
92     std::vector<std::shared_ptr<PasteDataRecord>> AllRecords() const;
93     bool IsDraggedData() const;
94     void SetDraggedDataFlag(bool isDraggedData);
95     bool IsLocalPaste() const;
96     void SetLocalPasteFlag(bool isLocalPaste);
97 
98     void SetBundleName(const std::string &bundleName);
99     std::string GetBundleName() const;
100     void SetOrginAuthority(const std::string &bundleName);
101     std::string GetOrginAuthority() const;
102     void SetRemote(bool isRemote);
103     bool IsRemote();
104     void SetTime(const std::string &time);
105     std::string GetTime();
106     void SetTag(std::string &tag);
107     std::string GetTag();
108     void SetAdditions(AAFwk::WantParams &additions);
109     void SetAddition(const std::string &key, AAFwk::IInterface *value);
110     void SetLocalOnly(bool localOnly);
111     bool GetLocalOnly();
112 
113     bool Encode(std::vector<std::uint8_t> &buffer) override;
114     bool Decode(const std::vector<std::uint8_t> &buffer) override;
115     size_t Count() override;
116     bool WriteUriFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient = true);
117     bool ReadUriFd(MessageParcel &parcel, UriHandler &uriHandler);
118     void ReplaceShareUri(int32_t userId);
119 
120     bool IsValid() const;
121     void SetInvalid();
122 
123     static void ShareOptionToString(ShareOption shareOption, std::string &out);
124     static std::string sharePath;
125     static std::string WEBVIEW_PASTEDATA_TAG;
126     static const std::string DISTRIBUTEDFILES_TAG;
127     static const std::string PATH_SHARE;
128     static const std::string FILE_SCHEME_PREFIX;
129     static const std::string IMG_LOCAL_URI;
130     static const std::string SHARE_PATH_PREFIX;
131     static const std::string SHARE_PATH_PREFIX_ACCOUNT;
132     static const std::string REMOTE_FILE_SIZE;
133 
134 private:
135     void RefreshMimeProp();
136 
137     PasteDataProperty props_;
138     std::vector<std::shared_ptr<PasteDataRecord>> records_;
139     std::string orginAuthority_;
140     bool valid_ = true;
141     bool isDraggedData_ = false;
142     bool isLocalPaste_ = false; // local in app paste
143 };
144 } // namespace MiscServices
145 } // namespace OHOS
146 #endif // PASTE_BOARD_DATA_H
147