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 "paste_data.h" 38 #include "pasteboard_dump_helper.h" 39 #include "pasteboard_service_stub.h" 40 #include "system_ability.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 const std::int32_t ERROR_USERID = -1; 45 const std::int32_t RESULT_OK = 0; 46 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 47 struct AppInfo { 48 std::string bundleName = "com.pasteboard.default"; 49 int32_t tokenType = -1; 50 int32_t userId = ERROR_USERID; 51 uint32_t tokenId; 52 }; 53 54 struct HistoryInfo { 55 std::string time; 56 std::string bundleName; 57 std::string state; 58 std::string pop; 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 void AddPasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer) override; 73 virtual void RemovePasteboardChangedObserver(const sptr<IPasteboardChangedObserver> &observer) override; 74 virtual void RemoveAllChangedObserver() override; 75 virtual void AddPasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override; 76 virtual void RemovePasteboardEventObserver(const sptr<IPasteboardChangedObserver> &observer) override; 77 virtual void RemoveAllEventObserver() override; 78 virtual void OnStart() override; 79 virtual void OnStop() override; 80 size_t GetDataSize(PasteData &data) const; 81 bool SetPasteboardHistory(HistoryInfo &info); 82 int Dump(int fd, const std::vector<std::u16string> &args) override; 83 84 private: 85 using Event = ClipPlugin::GlobalEvent; 86 using ServiceListenerFunc = void (PasteboardService::*)(); 87 static constexpr const int32_t LISTENING_SERVICE[] = { DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, 88 DISTRIBUTED_DEVICE_PROFILE_SA_ID, WINDOW_MANAGER_SERVICE_ID }; 89 static constexpr const char *PLUGIN_NAME = "distributed_clip"; 90 static constexpr const pid_t EDM_UID = 3057; 91 static constexpr const pid_t ROOT_UID = 0; 92 static constexpr uint32_t EXPIRATION_INTERVAL = 2; 93 static constexpr uint32_t OPEN_P2P_SLEEP_TIME = 5; 94 static constexpr size_t TRANMISSION_BASELINE = 30 * 1024 * 1024; 95 static constexpr size_t MIN_TRANMISSION_TIME = 35; 96 struct classcomp { operatorclasscomp97 bool operator()(const sptr<IPasteboardChangedObserver> &l, const sptr<IPasteboardChangedObserver> &r) const 98 { 99 return l->AsObject() < r->AsObject(); 100 } 101 }; 102 using ObserverMap = std::map<int32_t, std::shared_ptr<std::set<sptr<IPasteboardChangedObserver>, classcomp>>>; 103 void AddSysAbilityListener(); 104 int32_t Init(); 105 static int32_t GetCurrentAccountId(); 106 std::string DumpHistory() const; 107 std::string DumpData(); 108 void NotifyObservers(std::string bundleName, PasteboardEventStatus status); 109 void InitServiceHandler(); 110 bool IsCopyable(uint32_t tokenId) const; 111 112 virtual int32_t SavePasteData(std::shared_ptr<PasteData> &pasteData) override; 113 void SetPasteDataDot(PasteData &pasteData); 114 void GetPasteDataDot(PasteData &pasteData, const std::string &pop, const std::string &bundleName); 115 bool GetPasteData(AppInfo &appInfo, PasteData &data, bool isFocusedApp); 116 bool CheckPasteData(AppInfo &appInfo, PasteData &data, bool isFocusedApp); 117 bool GetRemoteData(AppInfo &appInfo, PasteData &data, std::string &pop, bool isFocusedApp, uint32_t tokenId); 118 void GrantUriPermission(PasteData &data, const std::string &targetBundleName); 119 void RevokeUriPermission(PasteData &lastData); 120 void GenerateDistributedUri(PasteData &data); 121 bool isBundleOwnUriPermission(const std::string &bundleName, Uri &uri); 122 void CheckAppUriPermission(PasteData &data); 123 std::string GetAppLabel(uint32_t tokenId); 124 sptr<OHOS::AppExecFwk::IBundleMgr> GetAppBundleManager(); 125 std::string GetDeviceName(); 126 void SetDeviceName(const std::string &device = ""); 127 void EstablishP2PLink(int fileSize); 128 129 std::shared_ptr<PasteData> GetDistributedData(int32_t user); 130 bool SetDistributedData(int32_t user, PasteData &data); 131 bool HasDistributedData(int32_t user); 132 bool GetDistributedEvent(std::shared_ptr<ClipPlugin> plugin, int32_t user, Event &event); 133 bool CleanDistributedData(int32_t user); 134 void OnConfigChange(bool isOn); 135 std::shared_ptr<ClipPlugin> GetClipPlugin(); 136 137 static std::string GetTime(); 138 static bool HasPastePermission(uint32_t tokenId, bool isFocusedApp, const std::shared_ptr<PasteData> &pasteData); 139 static AppInfo GetAppInfo(uint32_t tokenId); 140 static std::string GetAppBundleName(const AppInfo &appInfo); 141 static bool IsDefaultIME(const AppInfo &appInfo); 142 static bool IsFocusedApp(const std::string &bundleName); 143 static void SetLocalPasteFlag(bool isCrossPaste, uint32_t tokenId, PasteData &pasteData); 144 void ShowHintToast(bool isValid, uint32_t tokenId, const std::shared_ptr<PasteData> &pasteData); 145 void SetWebViewPasteData(PasteData &pasteData, const std::string &bundleName); 146 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 147 void DevManagerInit(); 148 void DevProfileInit(); 149 ServiceRunningState state_; 150 std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 151 std::recursive_mutex clipMutex_; 152 std::mutex hintMutex_; 153 std::mutex observerMutex_; 154 ObserverMap observerChangedMap_; 155 ObserverMap observerEventMap_; 156 ClipPlugin::GlobalEvent currentEvent_; 157 const std::string filePath_ = ""; 158 std::map<int32_t, std::shared_ptr<PasteData>> clips_; 159 std::map<int32_t, std::vector<int32_t>> hints_; 160 std::set<std::string> readBundles_; 161 162 std::recursive_mutex mutex; 163 std::shared_ptr<ClipPlugin> clipPlugin_ = nullptr; 164 std::atomic<uint32_t> sequenceId_ = 0; 165 static std::mutex historyMutex_; 166 static std::vector<std::string> dataHistory_; 167 static std::shared_ptr<Command> copyHistory; 168 static std::shared_ptr<Command> copyData; 169 std::atomic<bool> setting_ = false; 170 std::mutex remoteMutex_; 171 std::mutex deviceMutex_; 172 std::string fromDevice_; 173 std::map<int32_t, ServiceListenerFunc> ServiceListenerFunc_; 174 175 void AddObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap); 176 void RemoveSingleObserver(const sptr<IPasteboardChangedObserver> &observer, ObserverMap &observerMap); 177 void RemoveAllObserver(ObserverMap &observerMap); 178 inline bool IsCallerUidValid(); 179 }; 180 } // namespace MiscServices 181 } // namespace OHOS 182 #endif // PASTE_BOARD_SERVICE_H 183