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 std::vector<std::string> GetMimeTypes() override; 111 virtual bool HasDataType(const std::string &mimeType) override; 112 virtual std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck) override; 113 virtual int32_t GetDataSource(std::string &bundleNme) override; 114 virtual void SubscribeObserver(PasteboardObserverType type, 115 const sptr<IPasteboardChangedObserver> &observer) override; 116 virtual void UnsubscribeObserver(PasteboardObserverType type, 117 const sptr<IPasteboardChangedObserver> &observer) override; 118 virtual void UnsubscribeAllObserver(PasteboardObserverType type) override; 119 virtual int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions) override; 120 virtual int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds) override; 121 virtual std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds) override; 122 virtual int32_t SetAppShareOptions(const ShareOption &shareOptions) override; 123 virtual int32_t RemoveAppShareOptions() override; 124 virtual void OnStart() override; 125 virtual void OnStop() override; 126 virtual void PasteStart(const std::string &pasteId) override; 127 virtual void PasteComplete(const std::string &deviceId, const std::string &pasteId) override; 128 virtual int32_t RegisterClientDeathObserver(sptr<IRemoteObject> observer) override; 129 static int32_t currentUserId; 130 static ScreenEvent currentScreenStatus; 131 size_t GetDataSize(PasteData &data) const; 132 bool SetPasteboardHistory(HistoryInfo &info); 133 int Dump(int fd, const std::vector<std::u16string> &args) override; 134 void NotifyDelayGetterDied(int32_t userId); 135 void NotifyEntryGetterDied(int32_t userId); 136 bool IsFocusedApp(uint32_t tokenId); 137 138 private: 139 using Event = ClipPlugin::GlobalEvent; 140 using ServiceListenerFunc = void (PasteboardService::*)(); 141 static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, 142 WINDOW_MANAGER_SERVICE_ID, MEMORY_MANAGER_SA_ID }; 143 static constexpr const char *PLUGIN_NAME = "distributed_clip"; 144 static constexpr uint32_t PLAIN_INDEX = 0; 145 static constexpr uint32_t HTML_INDEX = 1; 146 static constexpr uint32_t URI_INDEX = 2; 147 static constexpr uint32_t WANT_INDEX = 3; 148 static constexpr uint32_t PIXELMAP_INDEX = 4; 149 static constexpr uint32_t MAX_INDEX_LENGTH = 8; 150 static constexpr const pid_t EDM_UID = 3057; 151 static constexpr const pid_t ROOT_UID = 0; 152 static constexpr uint32_t EXPIRATION_INTERVAL = 2; 153 static constexpr int MIN_TRANMISSION_TIME = 30 * 1000; //ms 154 static constexpr uint64_t ONE_HOUR_MILLISECONDS = 60 * 60 * 1000; 155 static constexpr uint32_t GET_REMOTE_DATA_WAIT_TIME = 30000; 156 class DelayGetterDeathRecipient final : public IRemoteObject::DeathRecipient { 157 public: 158 explicit DelayGetterDeathRecipient(int32_t userId, PasteboardService &service); 159 virtual ~DelayGetterDeathRecipient() = default; 160 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 161 private: 162 int32_t userId_ = ERROR_USERID; 163 PasteboardService &service_; 164 }; 165 166 class EntryGetterDeathRecipient final : public IRemoteObject::DeathRecipient { 167 public: 168 explicit EntryGetterDeathRecipient(int32_t userId, PasteboardService &service); 169 virtual ~EntryGetterDeathRecipient() = default; 170 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 171 private: 172 int32_t userId_ = ERROR_USERID; 173 PasteboardService &service_; 174 }; 175 176 class RemoteDataTaskManager { 177 public: 178 struct TaskContext { 179 std::atomic<bool> pasting_ = false; 180 ConcurrentMap<uint32_t, std::shared_ptr<BlockObject<bool>>> getDataBlocks_; 181 std::shared_ptr<PasteDateTime> data_; 182 }; 183 using DataTask = std::pair<std::shared_ptr<PasteboardService::RemoteDataTaskManager::TaskContext>, bool>; 184 DataTask GetRemoteDataTask(const Event &event); 185 void Notify(const Event &event, std::shared_ptr<PasteDateTime> data); 186 void ClearRemoteDataTask(const Event &event); 187 std::shared_ptr<PasteDateTime> WaitRemoteData(const Event &event); 188 private: 189 std::atomic<uint32_t> mapKey_ = 0; 190 std::mutex mutex_; 191 std::map<std::string, std::shared_ptr<TaskContext>> dataTasks_; 192 }; 193 194 struct classcomp { operatorclasscomp195 bool operator()(const sptr<IPasteboardChangedObserver> &l, const sptr<IPasteboardChangedObserver> &r) const 196 { 197 return l->AsObject() < r->AsObject(); 198 } 199 }; 200 using ObserverMap = std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>>; 201 void AddSysAbilityListener(); 202 int32_t Init(); 203 void InitScreenStatus(); 204 static int32_t GetCurrentAccountId(); 205 static ScreenEvent GetCurrentScreenStatus(); 206 std::string DumpHistory() const; 207 std::string DumpData(); 208 void NotifyObservers(std::string bundleName, PasteboardEventStatus status); 209 void InitServiceHandler(); 210 bool IsCopyable(uint32_t tokenId) const; 211 212 int32_t SavePasteData(std::shared_ptr<PasteData> &pasteData, 213 sptr<IPasteboardDelayGetter> delayGetter = nullptr, 214 sptr<IPasteboardEntryGetter> entryGetter = nullptr) override; 215 int32_t SaveData(std::shared_ptr<PasteData>& pasteData, 216 sptr<IPasteboardDelayGetter> delayGetter = nullptr, 217 sptr<IPasteboardEntryGetter> entryGetter = nullptr); 218 void HandleDelayDataAndRecord(std::shared_ptr<PasteData> &pasteData, 219 sptr<IPasteboardDelayGetter> delayGetter, sptr<IPasteboardEntryGetter> entryGetter, const AppInfo& appInfo); 220 int32_t PreParePasteData(std::shared_ptr<PasteData> &pasteData, const AppInfo &appInfo); 221 void RemovePasteData(const AppInfo &appInfo); 222 void SetPasteDataDot(PasteData &pasteData); 223 std::pair<bool, ClipPlugin::GlobalEvent> GetValidDistributeEvent(int32_t user); 224 225 int32_t GetSdkVersion(uint32_t tokenId); 226 bool IsPermissionGranted(const std::string& perm, uint32_t tokenId); 227 int32_t GetData(uint32_t tokenId, PasteData &data, int32_t &syncTime); 228 229 void GetPasteDataDot(PasteData &pasteData, const std::string &bundleName); 230 int32_t GetLocalData(const AppInfo &appInfo, PasteData &data); 231 int32_t GetRemoteData(int32_t userId, const Event &event, PasteData &data, int32_t &syncTime); 232 int32_t GetRemotePasteData(int32_t userId, const Event &event, PasteData &data, int32_t &syncTime); 233 int64_t GetFileSize(PasteData &data); 234 bool GetDelayPasteRecord(const AppInfo &appInfo, PasteData &data); 235 void GetDelayPasteData(const AppInfo &appInfo, PasteData &data); 236 void CheckUriPermission(PasteData &data, std::vector<Uri> &grantUris, const std::string &targetBundleName); 237 int32_t GrantUriPermission(PasteData &data, const std::string &targetBundleName); 238 void RevokeUriPermission(std::shared_ptr<PasteData> pasteData); 239 void GenerateDistributedUri(PasteData &data); 240 bool IsBundleOwnUriPermission(const std::string &bundleName, Uri &uri); 241 void CheckAppUriPermission(PasteData &data); 242 std::string GetAppLabel(uint32_t tokenId); 243 sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager(); 244 void EstablishP2PLink(const std::string& networkId, const std::string &pasteId); 245 void CloseP2PLink(const std::string& networkId); 246 uint8_t GenerateDataType(PasteData &data); 247 bool HasDistributedDataType(const std::string &mimeType); 248 249 std::pair<std::shared_ptr<PasteData>, PasteDateResult> GetDistributedData(const Event &event, int32_t user); 250 void GetFullDelayPasteData(int32_t userId, PasteData &data); 251 bool SetDistributedData(int32_t user, PasteData &data); 252 bool CleanDistributedData(int32_t user); 253 void OnConfigChange(bool isOn); 254 std::shared_ptr<ClipPlugin> GetClipPlugin(); 255 256 static std::string GetTime(); 257 bool IsDataAged(); 258 bool VerifyPermission(uint32_t tokenId); 259 int32_t IsDataVaild(PasteData &pasteData, uint32_t tokenId); 260 static AppInfo GetAppInfo(uint32_t tokenId); 261 static std::string GetAppBundleName(const AppInfo &appInfo); 262 bool IsDefaultIME(const AppInfo &appInfo); 263 static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData); 264 void ShowHintToast(uint32_t tokenId, uint32_t pid); 265 void SetWebViewPasteData(PasteData &pasteData, const std::string &bundleName); 266 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 267 void DMAdapterInit(); 268 void NotifySaStatus(); 269 void ReportUeCopyEvent(PasteData &pasteData, int32_t result); 270 271 ServiceRunningState state_; 272 std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 273 std::mutex observerMutex_; 274 ObserverMap observerLocalChangedMap_; 275 ObserverMap observerRemoteChangedMap_; 276 ObserverMap observerEventMap_; 277 ClipPlugin::GlobalEvent currentEvent_; 278 ClipPlugin::GlobalEvent remoteEvent_; 279 const std::string filePath_ = ""; 280 ConcurrentMap<int32_t, std::shared_ptr<PasteData>> clips_; 281 ConcurrentMap<int32_t, std::pair<sptr<IPasteboardEntryGetter>, sptr<EntryGetterDeathRecipient>>> entryGetters_; 282 ConcurrentMap<int32_t, std::pair<sptr<IPasteboardDelayGetter>, sptr<DelayGetterDeathRecipient>>> delayGetters_; 283 ConcurrentMap<int32_t, uint64_t> copyTime_; 284 std::set<std::string> readBundles_; 285 std::shared_ptr<PasteBoardCommonEventSubscriber> commonEventSubscriber_ = nullptr; 286 287 std::recursive_mutex mutex; 288 std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr; 289 std::atomic<uint16_t> sequenceId_ = 0; 290 std::atomic<uint32_t> dataId_ = 0; 291 static std::mutex historyMutex_; 292 std::mutex bundleMutex_; 293 static std::vector<std::string> dataHistory_; 294 static std::shared_ptr<Command> copyHistory; 295 static std::shared_ptr<Command> copyData; 296 std::atomic<bool> setting_ = false; 297 std::mutex remoteMutex_; 298 std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_; 299 std::map<std::string, int> typeMap_ = { 300 { MIMETYPE_TEXT_PLAIN, PLAIN_INDEX }, 301 { MIMETYPE_TEXT_HTML, HTML_INDEX }, 302 { MIMETYPE_TEXT_URI, URI_INDEX }, 303 { MIMETYPE_TEXT_WANT, WANT_INDEX }, 304 { MIMETYPE_PIXELMAP, PIXELMAP_INDEX } 305 }; 306 307 std::shared_ptr<FFRTTimer> ffrtTimer_; 308 ConcurrentMap<std::string, ConcurrentMap<std::string, int32_t>> p2pMap_; 309 enum GlobalShareOptionSource { 310 MDM = 0, 311 APP = 1, 312 }; 313 314 struct GlobalShareOption { 315 GlobalShareOptionSource source; 316 ShareOption shareOption; 317 }; 318 319 ConcurrentMap<uint32_t, GlobalShareOption> globalShareOptions_; 320 PastedSwitch switch_; 321 322 void AddObserver(int32_t userId, const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap); 323 void RemoveSingleObserver(int32_t userId, const sptr<IPasteboardChangedObserver> &observer, 324 ObserverMap &observerMap); 325 void RemoveAllObserver(int32_t userId, ObserverMap &observerMap); 326 inline bool IsCallerUidValid(); 327 std::vector<std::string> GetLocalMimeTypes(); 328 bool HasLocalDataType(const std::string &mimeType); 329 void AddPermissionRecord(uint32_t tokenId, bool isReadGrant, bool isSecureGrant); 330 bool SubscribeKeyboardEvent(); 331 bool IsAllowSendData(); 332 void UpdateShareOption(PasteData &pasteData); 333 bool CheckMdmShareOption(PasteData &pasteData); 334 void PasteboardEventSubscriber(); 335 void CommonEventSubscriber(); 336 bool IsBasicType(const std::string &mimeType); 337 std::function<void(const OHOS::MiscServices::Event &)> RemotePasteboardChange(); 338 std::shared_ptr<InputEventCallback> inputEventCallback_; 339 DistributedModuleConfig moduleConfig_; 340 std::vector<std::string> bundles_; 341 int32_t uid_ = -1; 342 RemoteDataTaskManager taskMgr_; 343 pid_t setPasteDataUId_ = 0; 344 static constexpr const pid_t TESE_SERVER_UID = 3500; 345 std::mutex eventMutex_; 346 SecurityLevel securityLevel_; 347 class PasteboardDeathRecipient final : public IRemoteObject::DeathRecipient { 348 public: 349 PasteboardDeathRecipient(PasteboardService &service, sptr<IRemoteObject> observer, pid_t pid); 350 virtual ~PasteboardDeathRecipient() = default; 351 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 352 353 private: 354 PasteboardService &service_; 355 sptr<IRemoteObject> observer_; 356 pid_t pid_; 357 }; 358 int32_t AppExit(pid_t pid); 359 ConcurrentMap<pid_t, sptr<PasteboardDeathRecipient>> clients_; 360 static constexpr pid_t INVALID_UID = -1; 361 static constexpr pid_t INVALID_PID = -1; 362 static constexpr uint32_t INVALID_TOKEN = 0; 363 }; 364 } // namespace MiscServices 365 } // namespace OHOS 366 #endif // PASTE_BOARD_SERVICE_H 367