• 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 
16 #ifndef PASTE_BOARD_SERVICE_H
17 #define PASTE_BOARD_SERVICE_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <ctime>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include <shared_mutex>
27 #include <stack>
28 #include <sys/time.h>
29 #include <system_ability_definition.h>
30 #include <thread>
31 
32 #include "bundle_mgr_interface.h"
33 #include "bundle_mgr_proxy.h"
34 #include "clip/clip_plugin.h"
35 #include "common/block_object.h"
36 #include "common/concurrent_map.h"
37 #include "distributed_module_config.h"
38 #include "eventcenter/event_center.h"
39 #include "pasteboard_switch.h"
40 #include "event_handler.h"
41 #include "iremote_object.h"
42 #include "i_pasteboard_delay_getter.h"
43 #include "i_pasteboard_observer.h"
44 #include "pasteboard_common_event_subscriber.h"
45 #include "paste_data.h"
46 #include "pasteboard_dump_helper.h"
47 #include "pasteboard_service_stub.h"
48 #include "system_ability.h"
49 #include "privacy_kit.h"
50 #include "input_manager.h"
51 #include "ffrt_utils.h"
52 #include "security_level.h"
53 
54 namespace OHOS {
55 namespace MiscServices {
56 const std::int32_t ERROR_USERID = -1;
57 const std::int32_t RESULT_OK = 0;
58 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
59 struct AppInfo {
60     std::string bundleName = "com.pasteboard.default";
61     int32_t tokenType = -1;
62     int32_t userId = ERROR_USERID;
63     uint32_t tokenId;
64 };
65 
66 struct HistoryInfo {
67     std::string time;
68     std::string bundleName;
69     std::string state;
70     std::string remote;
71 };
72 
73 struct PasteDateTime {
74     int32_t syncTime = 0;
75     std::shared_ptr<PasteData> data;
76     int32_t errorCode = 0;
77 };
78 
79 struct PasteDateResult {
80     int32_t syncTime = 0;
81     int32_t errorCode = 0;
82 };
83 class InputEventCallback : public MMI::IInputEventConsumer {
84 public:
85     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
86     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
87     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override;
88     bool IsCtrlVProcess(uint32_t callingPid, bool isFocused);
89     void Clear();
90 private:
91     static constexpr uint32_t EVENT_TIME_OUT = 2000;
92     mutable int32_t windowPid_;
93     mutable uint64_t actionTime_;
94     mutable std::shared_mutex inputEventMutex_;
95 };
96 
97 class PasteboardService final : public SystemAbility, public PasteboardServiceStub {
98     DECLARE_SYSTEM_ABILITY(PasteboardService)
99 
100 public:
101     API_EXPORT PasteboardService();
102     API_EXPORT ~PasteboardService();
103     virtual void Clear() override;
104     virtual int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry &value) override;
105     virtual int32_t GetPasteData(PasteData &data, int32_t &syncTime) override;
106     virtual bool HasPasteData() override;
107     virtual int32_t SetPasteData(PasteData &pasteData, const sptr<IPasteboardDelayGetter> delayGetter,
108         const sptr<IPasteboardEntryGetter> entryGetter) override;
109     virtual bool IsRemoteData() override;
110     virtual bool HasDataType(const std::string &mimeType) override;
111     virtual std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck) override;
112     virtual int32_t GetDataSource(std::string &bundleNme) override;
113     virtual void SubscribeObserver(PasteboardObserverType type,
114         const sptr<IPasteboardChangedObserver> &observer) override;
115     virtual void UnsubscribeObserver(PasteboardObserverType type,
116         const sptr<IPasteboardChangedObserver> &observer) override;
117     virtual void UnsubscribeAllObserver(PasteboardObserverType type) override;
118     virtual int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions) override;
119     virtual int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds) override;
120     virtual std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds) override;
121     virtual int32_t SetAppShareOptions(const ShareOption &shareOptions) override;
122     virtual int32_t RemoveAppShareOptions() override;
123     virtual void OnStart() override;
124     virtual void OnStop() override;
125     virtual void PasteStart(const std::string &pasteId) override;
126     virtual void PasteComplete(const std::string &deviceId, const std::string &pasteId) override;
127     virtual int32_t RegisterClientDeathObserver(sptr<IRemoteObject> observer) override;
128     static int32_t currentUserId;
129     static ScreenEvent currentScreenStatus;
130     size_t GetDataSize(PasteData &data) const;
131     bool SetPasteboardHistory(HistoryInfo &info);
132     int Dump(int fd, const std::vector<std::u16string> &args) override;
133     void NotifyDelayGetterDied(int32_t userId);
134     void NotifyEntryGetterDied(int32_t userId);
135     bool IsFocusedApp(uint32_t tokenId);
136 
137 private:
138     using Event = ClipPlugin::GlobalEvent;
139     using ServiceListenerFunc = void (PasteboardService::*)();
140     static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID,
141         WINDOW_MANAGER_SERVICE_ID, MEMORY_MANAGER_SA_ID };
142     static constexpr const char *PLUGIN_NAME = "distributed_clip";
143     static constexpr uint32_t PLAIN_INDEX = 0;
144     static constexpr uint32_t HTML_INDEX = 1;
145     static constexpr uint32_t URI_INDEX = 2;
146     static constexpr uint32_t WANT_INDEX = 3;
147     static constexpr uint32_t PIXELMAP_INDEX = 4;
148     static constexpr uint32_t MAX_INDEX_LENGTH = 8;
149     static constexpr const pid_t EDM_UID = 3057;
150     static constexpr const pid_t ROOT_UID = 0;
151     static constexpr uint32_t EXPIRATION_INTERVAL = 2;
152     static constexpr int MIN_TRANMISSION_TIME = 30 * 1000; //ms
153     static constexpr uint64_t ONE_HOUR_MILLISECONDS = 60 * 60 * 1000;
154     static constexpr uint32_t GET_REMOTE_DATA_WAIT_TIME = 30000;
155     class DelayGetterDeathRecipient final : public IRemoteObject::DeathRecipient {
156     public:
157         explicit DelayGetterDeathRecipient(int32_t userId, PasteboardService &service);
158         virtual ~DelayGetterDeathRecipient() = default;
159         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
160     private:
161         int32_t userId_ = ERROR_USERID;
162         PasteboardService &service_;
163     };
164 
165     class EntryGetterDeathRecipient final : public IRemoteObject::DeathRecipient {
166     public:
167         explicit EntryGetterDeathRecipient(int32_t userId, PasteboardService &service);
168         virtual ~EntryGetterDeathRecipient() = default;
169         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
170     private:
171         int32_t userId_ = ERROR_USERID;
172         PasteboardService &service_;
173     };
174 
175     class RemoteDataTaskManager {
176     public:
177         struct TaskContext {
178             std::atomic<bool> pasting_ = false;
179             ConcurrentMap<uint32_t, std::shared_ptr<BlockObject<bool>>>  getDataBlocks_;
180             std::shared_ptr<PasteDateTime>  data_;
181         };
182         using DataTask = std::pair<std::shared_ptr<PasteboardService::RemoteDataTaskManager::TaskContext>, bool>;
183         DataTask GetRemoteDataTask(const Event &event);
184         void Notify(const Event &event, std::shared_ptr<PasteDateTime> data);
185         void ClearRemoteDataTask(const Event &event);
186         std::shared_ptr<PasteDateTime> WaitRemoteData(const Event &event);
187     private:
188         std::atomic<uint32_t> mapKey_ = 0;
189         std::mutex mutex_;
190         std::map<std::string, std::shared_ptr<TaskContext>> dataTasks_;
191     };
192 
193     struct classcomp {
operatorclasscomp194         bool operator()(const sptr<IPasteboardChangedObserver> &l, const sptr<IPasteboardChangedObserver> &r) const
195         {
196             return l->AsObject() < r->AsObject();
197         }
198     };
199     using ObserverMap = std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>>;
200     void AddSysAbilityListener();
201     int32_t Init();
202     void InitScreenStatus();
203     static int32_t GetCurrentAccountId();
204     static ScreenEvent GetCurrentScreenStatus();
205     std::string DumpHistory() const;
206     std::string DumpData();
207     void NotifyObservers(std::string bundleName, PasteboardEventStatus status);
208     void InitServiceHandler();
209     bool IsCopyable(uint32_t tokenId) const;
210 
211     int32_t SavePasteData(std::shared_ptr<PasteData> &pasteData,
212         sptr<IPasteboardDelayGetter> delayGetter = nullptr,
213         sptr<IPasteboardEntryGetter> entryGetter = nullptr) override;
214     int32_t SaveData(std::shared_ptr<PasteData>& pasteData,
215         sptr<IPasteboardDelayGetter> delayGetter = nullptr,
216         sptr<IPasteboardEntryGetter> entryGetter = nullptr);
217     void HandleDelayDataAndRecord(std::shared_ptr<PasteData> &pasteData,
218         sptr<IPasteboardDelayGetter> delayGetter, sptr<IPasteboardEntryGetter> entryGetter, const AppInfo& appInfo);
219     int32_t PreParePasteData(std::shared_ptr<PasteData> &pasteData, const AppInfo &appInfo);
220     void RemovePasteData(const AppInfo &appInfo);
221     void SetPasteDataDot(PasteData &pasteData);
222     std::pair<bool, ClipPlugin::GlobalEvent> GetValidDistributeEvent(int32_t user);
223 
224     int32_t GetSdkVersion(uint32_t tokenId);
225     bool IsPermissionGranted(const std::string& perm, uint32_t tokenId);
226     int32_t GetData(uint32_t tokenId, PasteData &data, int32_t &syncTime);
227 
228     void GetPasteDataDot(PasteData &pasteData, const std::string &bundleName);
229     int32_t GetLocalData(const AppInfo &appInfo, PasteData &data);
230     int32_t GetRemoteData(int32_t userId, const Event &event, PasteData &data, int32_t &syncTime);
231     int32_t GetRemotePasteData(int32_t userId, const Event &event, PasteData &data, int32_t &syncTime);
232     int64_t GetFileSize(PasteData &data);
233     bool GetDelayPasteRecord(const AppInfo &appInfo, PasteData &data);
234     void GetDelayPasteData(const AppInfo &appInfo, PasteData &data);
235     void CheckUriPermission(PasteData &data, std::vector<Uri> &grantUris, const std::string &targetBundleName);
236     int32_t GrantUriPermission(PasteData &data, const std::string &targetBundleName);
237     void RevokeUriPermission(std::shared_ptr<PasteData> pasteData);
238     void GenerateDistributedUri(PasteData &data);
239     bool IsBundleOwnUriPermission(const std::string &bundleName, Uri &uri);
240     void CheckAppUriPermission(PasteData &data);
241     std::string GetAppLabel(uint32_t tokenId);
242     sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager();
243     void EstablishP2PLink(const std::string& networkId, const std::string &pasteId);
244     void CloseP2PLink(const std::string& networkId);
245     uint8_t GenerateDataType(PasteData &data);
246     bool HasDistributedDataType(const std::string &mimeType);
247 
248     std::pair<std::shared_ptr<PasteData>, PasteDateResult> GetDistributedData(const Event &event, int32_t user);
249     void GetFullDelayPasteData(int32_t userId, PasteData &data);
250     bool SetDistributedData(int32_t user, PasteData &data);
251     bool CleanDistributedData(int32_t user);
252     void OnConfigChange(bool isOn);
253     std::shared_ptr<ClipPlugin> GetClipPlugin();
254 
255     static std::string GetTime();
256     bool IsDataAged();
257     bool VerifyPermission(uint32_t tokenId);
258     int32_t IsDataVaild(PasteData &pasteData, uint32_t tokenId);
259     static AppInfo GetAppInfo(uint32_t tokenId);
260     static std::string GetAppBundleName(const AppInfo &appInfo);
261     bool IsDefaultIME(const AppInfo &appInfo);
262     static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData);
263     void ShowHintToast(uint32_t tokenId, uint32_t pid);
264     void SetWebViewPasteData(PasteData &pasteData, const std::string &bundleName);
265     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
266     void DMAdapterInit();
267     void NotifySaStatus();
268     void ReportUeCopyEvent(PasteData &pasteData, int32_t result);
269 
270     ServiceRunningState state_;
271     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
272     std::mutex observerMutex_;
273     ObserverMap observerLocalChangedMap_;
274     ObserverMap observerRemoteChangedMap_;
275     ObserverMap observerEventMap_;
276     ClipPlugin::GlobalEvent currentEvent_;
277     ClipPlugin::GlobalEvent remoteEvent_;
278     const std::string filePath_ = "";
279     ConcurrentMap<int32_t, std::shared_ptr<PasteData>> clips_;
280     ConcurrentMap<int32_t, std::pair<sptr<IPasteboardEntryGetter>, sptr<EntryGetterDeathRecipient>>> entryGetters_;
281     ConcurrentMap<int32_t, std::pair<sptr<IPasteboardDelayGetter>, sptr<DelayGetterDeathRecipient>>> delayGetters_;
282     ConcurrentMap<int32_t, uint64_t> copyTime_;
283     std::set<std::string> readBundles_;
284     std::shared_ptr<PasteBoardCommonEventSubscriber> commonEventSubscriber_ = nullptr;
285 
286     std::recursive_mutex mutex;
287     std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr;
288     std::atomic<uint16_t> sequenceId_ = 0;
289     std::atomic<uint32_t> dataId_ = 0;
290     static std::mutex historyMutex_;
291     std::mutex bundleMutex_;
292     static std::vector<std::string> dataHistory_;
293     static std::shared_ptr<Command> copyHistory;
294     static std::shared_ptr<Command> copyData;
295     std::atomic<bool> setting_ = false;
296     std::mutex remoteMutex_;
297     std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_;
298     std::map<std::string, int> typeMap_ = {
299         { MIMETYPE_TEXT_PLAIN, PLAIN_INDEX },
300         { MIMETYPE_TEXT_HTML, HTML_INDEX },
301         { MIMETYPE_TEXT_URI, URI_INDEX },
302         { MIMETYPE_TEXT_WANT, WANT_INDEX },
303         { MIMETYPE_PIXELMAP, PIXELMAP_INDEX }
304     };
305 
306     std::shared_ptr<FFRTTimer> ffrtTimer_;
307     ConcurrentMap<std::string, ConcurrentMap<std::string, int32_t>> p2pMap_;
308     ConcurrentMap<uint32_t, ShareOption> globalShareOptions_;
309     PastedSwitch switch_;
310 
311     void AddObserver(int32_t userId, const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap);
312     void RemoveSingleObserver(int32_t userId, const sptr<IPasteboardChangedObserver> &observer,
313         ObserverMap &observerMap);
314     void RemoveAllObserver(int32_t userId, ObserverMap &observerMap);
315     inline bool IsCallerUidValid();
316     bool HasLocalDataType(const std::string &mimeType);
317     void AddPermissionRecord(uint32_t tokenId, bool isReadGrant, bool isSecureGrant);
318     bool SubscribeKeyboardEvent();
319     bool IsAllowSendData();
320     void UpdateShareOption(PasteData &pasteData);
321     void PasteboardEventSubscriber();
322     void CommonEventSubscriber();
323     bool IsBasicType(const std::string &mimeType);
324     std::function<void(const OHOS::MiscServices::Event &)> RemotePasteboardChange();
325     std::shared_ptr<InputEventCallback> inputEventCallback_;
326     DistributedModuleConfig moduleConfig_;
327     std::vector<std::string> bundles_;
328     int32_t uid_ = -1;
329     RemoteDataTaskManager  taskMgr_;
330     pid_t setPasteDataUId_ = 0;
331     static constexpr const pid_t TESE_SERVER_UID = 3500;
332     std::mutex eventMutex_;
333     SecurityLevel securityLevel_;
334     class PasteboardDeathRecipient final : public IRemoteObject::DeathRecipient {
335     public:
336         PasteboardDeathRecipient(PasteboardService &service, sptr<IRemoteObject> observer, pid_t pid);
337         virtual ~PasteboardDeathRecipient() = default;
338         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
339 
340     private:
341         PasteboardService &service_;
342         sptr<IRemoteObject> observer_;
343         pid_t pid_;
344     };
345     int32_t AppExit(pid_t pid);
346     ConcurrentMap<pid_t, sptr<PasteboardDeathRecipient>> clients_;
347     static constexpr pid_t INVALID_UID = -1;
348     static constexpr pid_t INVALID_PID = -1;
349     static constexpr uint32_t INVALID_TOKEN = 0;
350 };
351 } // namespace MiscServices
352 } // namespace OHOS
353 #endif // PASTE_BOARD_SERVICE_H
354