• 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 AddPasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override;
78     virtual void RemovePasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override;
79     virtual void RemoveAllEventObserver() override;
80     virtual void OnStart() override;
81     virtual void OnStop() override;
82     size_t GetDataSize(PasteData& data) const;
83     bool SetPasteboardHistory(HistoryInfo &info);
84     int Dump(int fd, const std::vector<std::u16string> &args) override;
85 
86 private:
87     using Event = ClipPlugin::GlobalEvent;
88     using ServiceListenerFunc = void (PasteboardService::*)();
89     static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID,
90         DISTRIBUTED_DEVICE_PROFILE_SA_ID, WINDOW_MANAGER_SERVICE_ID };
91     static constexpr const char *PLUGIN_NAME = "distributed_clip";
92     static constexpr const pid_t EDM_UID = 3057;
93     static constexpr const pid_t ROOT_UID = 0;
94     static constexpr uint32_t EXPIRATION_INTERVAL = 2;
95     struct classcomp {
operatorclasscomp96         bool operator() (const sptr<IPasteboardChangedObserver>& l, const sptr<IPasteboardChangedObserver>& r) const
97         {
98             return l->AsObject() < r->AsObject();
99         }
100     };
101     using ObserverMap = std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>>;
102     void AddSysAbilityListener();
103     int32_t Init();
104     int32_t GetUserIdByToken(uint32_t tokenId);
105     std::string DumpHistory() const;
106     std::string DumpData();
107     void NotifyObservers(std::string bundleName, PasteboardEventStatus status);
108     void InitServiceHandler();
109     bool IsCopyable(uint32_t tokenId) const;
110 
111     void SetPasteDataDot(PasteData &pasteData);
112     void GetPasteDataDot(PasteData &pasteData, const std::string &pop, uint32_t tokenId);
113     bool GetPasteData(PasteData& data, uint32_t tokenId, bool isFocusedApp);
114     std::string GetAppLabel(uint32_t tokenId);
115     sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager();
116     std::string GetDeviceName();
117     void SetDeviceName(const std::string &device = "");
118 
119     std::shared_ptr<PasteData> GetDistributedData(int32_t user);
120     bool SetDistributedData(int32_t user, PasteData& data);
121     bool HasDistributedData(int32_t user);
122     bool GetDistributedEvent(std::shared_ptr<ClipPlugin> plugin, int32_t user, Event &event);
123     bool CleanDistributedData(int32_t user);
124     void OnConfigChange(bool isOn);
125     std::shared_ptr<ClipPlugin> GetClipPlugin();
126 
127     std::string GetTime();
128     static bool HasPastePermission(uint32_t tokenId, bool isFocusedApp, const std::shared_ptr<PasteData> &pasteData);
129     static AppInfo GetAppInfo(uint32_t tokenId);
130     static std::string GetAppBundleName(uint32_t tokenId);
131     static bool IsDefaultIME(const AppInfo &appInfo);
132     static bool IsFocusedApp(int32_t tokenId);
133     static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData);
134     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
135     void DevManagerInit();
136     void DevProfileInit();
137     static void ShareOptionToString(ShareOption shareOption, std::string &out);
138     ServiceRunningState state_;
139     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
140     std::mutex clipMutex_;
141     std::mutex observerMutex_;
142     ObserverMap observerChangedMap_;
143     ObserverMap observerEventMap_;
144     ClipPlugin::GlobalEvent currentEvent_;
145     const std::string filePath_ = "";
146     std::map<int32_t, std::shared_ptr<PasteData>> clips_;
147 
148     std::recursive_mutex mutex;
149     std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr;
150     std::atomic<uint32_t> sequenceId_ = 0;
151     static std::mutex historyMutex_;
152     static std::vector<std::string> dataHistory_;
153     static std::shared_ptr<Command> copyHistory;
154     static std::shared_ptr<Command> copyData;
155     std::atomic<bool> pasting_ = false;
156     std::atomic<bool> setting_ = false;
157     std::mutex deviceMutex_;
158     std::string fromDevice_;
159     std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_;
160 
161     void AddObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap);
162     void RemoveSingleObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap);
163     void RemoveAllObserver(ObserverMap &observerMap);
164     inline bool IsCallerUidValid();
165 };
166 } // MiscServices
167 } // OHOS
168 #endif // PASTE_BOARD_SERVICE_H
169