• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <sys/time.h>
20 #include <system_ability_definition.h>
21 
22 #include <atomic>
23 #include <condition_variable>
24 #include <ctime>
25 #include <map>
26 #include <memory>
27 #include <mutex>
28 #include <set>
29 #include <stack>
30 #include <thread>
31 
32 #include "bundle_mgr_interface.h"
33 #include "bundle_mgr_proxy.h"
34 #include "clip/clip_plugin.h"
35 #include "event_handler.h"
36 #include "i_pasteboard_observer.h"
37 #include "iremote_object.h"
38 #include "paste_data.h"
39 #include "pasteboard_dump_helper.h"
40 #include "pasteboard_service_stub.h"
41 #include "system_ability.h"
42 
43 namespace OHOS {
44 namespace MiscServices {
45 const std::int32_t ERROR_USERID = -1;
46 enum class ServiceRunningState {
47     STATE_NOT_START,
48     STATE_RUNNING
49 };
50 struct AppInfo {
51     std::string bundleName = "com.pasteboard.default";
52     int32_t tokenType = -1;
53     int32_t userId = ERROR_USERID;
54 };
55 
56 struct HistoryInfo {
57     std::string time;
58     std::string bundleName;
59     std::string state;
60     std::string pop;
61     std::string remote;
62 };
63 
64 class PasteboardService final : public SystemAbility, public PasteboardServiceStub {
65     DECLARE_SYSTEM_ABILITY(PasteboardService)
66 
67 public:
68     API_EXPORT PasteboardService();
69     API_EXPORT ~PasteboardService();
70     virtual void Clear() override;
71     virtual int32_t GetPasteData(PasteData& data) override;
72     virtual bool HasPasteData() override;
73     virtual int32_t SetPasteData(PasteData& pasteData) override;
74     virtual void AddPasteboardChangedObserver(const sptr<IPasteboardChangedObserver>& observer) override;
75     virtual void RemovePasteboardChangedObserver(const sptr<IPasteboardChangedObserver>& observer) override;
76     virtual void RemoveAllChangedObserver() override;
77     virtual void OnStart() override;
78     virtual void OnStop() override;
79     size_t GetDataSize(PasteData& data) const;
80     bool SetPasteboardHistory(HistoryInfo &info);
81     int Dump(int fd, const std::vector<std::u16string> &args) override;
82 
83 private:
84     using Event = ClipPlugin::GlobalEvent;
85     using ServiceListenerFunc = void (PasteboardService::*)();
86     static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID,
87         DISTRIBUTED_DEVICE_PROFILE_SA_ID, WINDOW_MANAGER_SERVICE_ID };
88     static constexpr const char *PLUGIN_NAME = "distributed_clip";
89     static constexpr uint32_t EXPIRATION_INTERVAL = 2;
90     struct classcomp {
operatorclasscomp91         bool operator() (const sptr<IPasteboardChangedObserver>& l, const sptr<IPasteboardChangedObserver>& r) const
92         {
93             return l->AsObject() < r->AsObject();
94         }
95     };
96     void AddSysAbilityListener();
97     int32_t Init();
98     int32_t GetUserIdByToken(uint32_t tokenId);
99     std::string DumpHistory() const;
100     std::string DumpData();
101     void NotifyObservers(std::string bundleName, PasteboardEventStatus status);
102     void InitServiceHandler();
103     bool IsCopyable(uint32_t tokenId) const;
104 
105     void SetPasteDataDot(PasteData &pasteData);
106     void GetPasteDataDot(PasteData &pasteData, const std::string &pop, uint32_t tokenId);
107     bool GetPasteData(PasteData& data, uint32_t tokenId, bool isFocusedApp);
108     std::string GetAppLabel(uint32_t tokenId);
109     sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager();
110     std::string GetDeviceName();
111     void SetDeviceName(const std::string &device = "");
112 
113     std::shared_ptr<PasteData> GetDistributedData(int32_t user);
114     bool SetDistributedData(int32_t user, PasteData& data);
115     bool HasDistributedData(int32_t user);
116     bool GetDistributedEvent(std::shared_ptr<ClipPlugin> plugin, int32_t user, Event &event);
117     bool CleanDistributedData(int32_t user);
118     void OnConfigChange(bool isOn);
119     std::shared_ptr<ClipPlugin> GetClipPlugin();
120 
121     std::string GetTime();
122     static bool HasPastePermission(uint32_t tokenId, bool isFocusedApp, const std::shared_ptr<PasteData> &pasteData);
123     static AppInfo GetAppInfo(uint32_t tokenId);
124     static std::string GetAppBundleName(uint32_t tokenId);
125     static bool IsDefaultIME(const AppInfo &appInfo);
126     static bool IsFocusedApp(int32_t tokenId);
127     static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData);
128     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
129     void DevManagerInit();
130     void DevProfileInit();
131     static void ShareOptionToString(ShareOption shareOption, std::string &out);
132     ServiceRunningState state_;
133     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
134     std::mutex clipMutex_;
135     std::mutex observerMutex_;
136     std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>> observerMap_;
137     ClipPlugin::GlobalEvent currentEvent_;
138     const std::string filePath_ = "";
139     std::map<int32_t, std::shared_ptr<PasteData>> clips_;
140 
141     std::recursive_mutex mutex;
142     std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr;
143     std::atomic<uint32_t> sequenceId_ = 0;
144     static std::mutex historyMutex_;
145     static std::vector<std::string> dataHistory_;
146     static std::shared_ptr<Command> copyHistory;
147     static std::shared_ptr<Command> copyData;
148     std::atomic<bool> pasting_ = false;
149     std::atomic<bool> setting_ = false;
150     std::mutex deviceMutex_;
151     std::string fromDevice_;
152     std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_;
153 };
154 } // MiscServices
155 } // OHOS
156 #endif // PASTE_BOARD_SERVICE_H
157