• 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 <stack>
27 #include <sys/time.h>
28 #include <system_ability_definition.h>
29 #include <thread>
30 
31 #include "bundle_mgr_interface.h"
32 #include "bundle_mgr_proxy.h"
33 #include "clip/clip_plugin.h"
34 #include "event_handler.h"
35 #include "i_pasteboard_observer.h"
36 #include "iremote_object.h"
37 #include "pasteboard_common_event_subscriber.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 const std::int32_t RESULT_OK = 0;
47 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
48 struct AppInfo {
49     std::string bundleName = "com.pasteboard.default";
50     int32_t tokenType = -1;
51     int32_t userId = ERROR_USERID;
52     uint32_t tokenId;
53 };
54 
55 struct HistoryInfo {
56     std::string time;
57     std::string bundleName;
58     std::string state;
59     std::string remote;
60 };
61 
62 class PasteboardService final : public SystemAbility, public PasteboardServiceStub {
63     DECLARE_SYSTEM_ABILITY(PasteboardService)
64 
65 public:
66     API_EXPORT PasteboardService();
67     API_EXPORT ~PasteboardService();
68     virtual void Clear() override;
69     virtual int32_t GetPasteData(PasteData &data) override;
70     virtual bool HasPasteData() override;
71     virtual int32_t SetPasteData(PasteData &pasteData) override;
72     virtual bool IsRemoteData() override;
73     virtual bool HasDataType(const std::string &mimeType) override;
74     virtual int32_t GetDataSource(std::string &bundleNme) override;
75     virtual void AddPasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer) override;
76     virtual void RemovePasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer) override;
77     virtual void RemoveAllChangedObserver() override;
78     virtual void AddPasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override;
79     virtual void RemovePasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override;
80     virtual void RemoveAllEventObserver() override;
81     virtual void OnStart() override;
82     virtual void OnStop() override;
83     static int32_t currentUserId;
84     size_t GetDataSize(PasteData &data) const;
85     bool SetPasteboardHistory(HistoryInfo &info);
86     int Dump(int fd, const std::vector<std::u16string> &args) override;
87 
88 private:
89     using Event = ClipPlugin::GlobalEvent;
90     using ServiceListenerFunc = void (PasteboardService::*)();
91     static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID,
92         DISTRIBUTED_DEVICE_PROFILE_SA_ID, WINDOW_MANAGER_SERVICE_ID };
93     static constexpr const char *PLUGIN_NAME = "distributed_clip";
94     static constexpr uint32_t PLAIN_INDEX = 0;
95     static constexpr uint32_t HTML_INDEX = 1;
96     static constexpr uint32_t URI_INDEX = 2;
97     static constexpr uint32_t WANT_INDEX = 3;
98     static constexpr uint32_t PIXELMAP_INDEX = 4;
99     static constexpr uint32_t MAX_INDEX_LENGTH = 8;
100     static constexpr const pid_t EDM_UID = 3057;
101     static constexpr const pid_t ROOT_UID = 0;
102     static constexpr uint32_t EXPIRATION_INTERVAL = 2;
103     static constexpr int MIN_TRANMISSION_TIME = 600;
104     static constexpr uint64_t ONE_HOUR_MILLISECONDS = 60 * 60 * 1000;
105     struct classcomp {
operatorclasscomp106         bool operator()(const sptr<IPasteboardChangedObserver> &l, const sptr<IPasteboardChangedObserver> &r) const
107         {
108             return l->AsObject() < r->AsObject();
109         }
110     };
111     using ObserverMap = std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>>;
112     void AddSysAbilityListener();
113     int32_t Init();
114     static int32_t GetCurrentAccountId();
115     std::string DumpHistory() const;
116     std::string DumpData();
117     void NotifyObservers(std::string bundleName, PasteboardEventStatus status);
118     void InitServiceHandler();
119     bool IsCopyable(uint32_t tokenId) const;
120 
121     virtual int32_t SavePasteData(std::shared_ptr<PasteData> &pasteData) override;
122     void SetPasteDataDot(PasteData &pasteData);
123     void GetPasteDataDot(PasteData &pasteData, const std::string &bundleName);
124     bool GetPasteData(AppInfo &appInfo, PasteData &data, bool isFocusedApp);
125     bool CheckPasteData(AppInfo &appInfo, PasteData &data, bool isFocusedApp);
126     bool GetRemoteData(AppInfo &appInfo, PasteData &data, bool isFocusedApp);
127     void CheckUriPermission(PasteData &data, std::vector<Uri> &grantUris, const std::string &targetBundleName);
128     void GrantUriPermission(PasteData &data, const std::string &targetBundleName);
129     void RevokeUriPermission(PasteData &lastData);
130     void GenerateDistributedUri(PasteData &data);
131     bool IsPermissionGranted(const std::string& perm, uint32_t tokenId);
132     bool isBundleOwnUriPermission(const std::string &bundleName, Uri &uri);
133     void CheckAppUriPermission(PasteData &data);
134     std::string GetAppLabel(uint32_t tokenId);
135     sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager();
136     void EstablishP2PLink();
137     uint8_t GenerateDataType(PasteData &data);
138     bool HasDistributedDataType(const std::string &mimeType);
139 
140     std::shared_ptr<PasteData> GetDistributedData(int32_t user);
141     bool SetDistributedData(int32_t user, PasteData &data);
142     bool HasDistributedData(int32_t user);
143     bool GetDistributedEvent(std::shared_ptr<ClipPlugin> plugin, int32_t user, Event &event);
144     bool CleanDistributedData(int32_t user);
145     void OnConfigChange(bool isOn);
146     std::shared_ptr<ClipPlugin> GetClipPlugin();
147 
148     static std::string GetTime();
149     bool IsDataAged();
150     bool HasPastePermission(uint32_t tokenId, bool isFocusedApp, const std::shared_ptr<PasteData> &pasteData);
151     static AppInfo GetAppInfo(uint32_t tokenId);
152     static std::string GetAppBundleName(const AppInfo &appInfo);
153     static bool IsDefaultIME(const AppInfo &appInfo);
154     static bool IsFocusedApp(uint32_t tokenId);
155     static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData);
156     void ShowHintToast(bool isValid, uint32_t tokenId, const std::shared_ptr<PasteData> &pasteData);
157     void SetWebViewPasteData(PasteData &pasteData, const std::string &bundleName);
158     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
159     void DevManagerInit();
160     void DevProfileInit();
161     ServiceRunningState state_;
162     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
163     std::recursive_mutex clipMutex_;
164     std::mutex hintMutex_;
165     std::mutex observerMutex_;
166     ObserverMap observerChangedMap_;
167     ObserverMap observerEventMap_;
168     ClipPlugin::GlobalEvent currentEvent_;
169     ClipPlugin::GlobalEvent remoteEvent_;
170     const std::string filePath_ = "";
171     std::map<int32_t, std::shared_ptr<PasteData>> clips_;
172     std::map<int32_t, std::vector<int32_t>> hints_;
173     std::map<int32_t, uint64_t> copyTime_;
174     std::set<std::string> readBundles_;
175     std::shared_ptr<PasteBoardCommonEventSubscriber> commonEventSubscriber_ = nullptr;
176 
177     std::recursive_mutex mutex;
178     std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr;
179     std::atomic<uint32_t> sequenceId_ = 0;
180     static std::mutex historyMutex_;
181     static std::vector<std::string> dataHistory_;
182     static std::shared_ptr<Command> copyHistory;
183     static std::shared_ptr<Command> copyData;
184     std::atomic<bool> setting_ = false;
185     std::mutex remoteMutex_;
186     std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_;
187     std::map<std::string, int> typeMap_ = {
188         { MIMETYPE_TEXT_PLAIN, PLAIN_INDEX },
189         { MIMETYPE_TEXT_HTML, HTML_INDEX },
190         { MIMETYPE_TEXT_URI, URI_INDEX },
191         { MIMETYPE_TEXT_WANT, WANT_INDEX },
192         { MIMETYPE_PIXELMAP, PIXELMAP_INDEX }
193     };
194 
195     std::map<std::string, int> p2pMap_ = {};
196 
197     void AddObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap);
198     void RemoveSingleObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap);
199     void RemoveAllObserver(ObserverMap &observerMap);
200     inline bool IsCallerUidValid();
201     bool HasLocalDataType(const std::string &mimeType);
202 };
203 } // namespace MiscServices
204 } // namespace OHOS
205 #endif // PASTE_BOARD_SERVICE_H
206