1 /* 2 * Copyright (c) 2021-2024 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 DAEMON_H 17 #define DAEMON_H 18 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 23 #include "accesstoken_kit.h" 24 #include "daemon_event.h" 25 #include "daemon_eventhandler.h" 26 #include "daemon_execute.h" 27 #include "daemon_stub.h" 28 #include "dm_device_info.h" 29 #include "file_trans_listener_proxy.h" 30 #include "hmdfs_info.h" 31 #include "ipc/i_daemon.h" 32 #include "iremote_stub.h" 33 #include "multiuser/os_account_observer.h" 34 #include "nocopyable.h" 35 #include "refbase.h" 36 #include "system_ability.h" 37 #include "accesstoken_kit.h" 38 39 namespace OHOS { 40 namespace Storage { 41 namespace DistributedFile { 42 using HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo; 43 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 44 45 class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyable { 46 DECLARE_SYSTEM_ABILITY(Daemon); 47 48 public: SystemAbility(saID,runOnCreate)49 explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {}; 50 virtual ~Daemon() = default; 51 52 void OnStart() override; 53 void OnStop() override; 54 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 55 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; QueryServiceState()56 ServiceRunningState QueryServiceState() const 57 { 58 return state_; 59 } 60 61 int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 62 int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 63 int32_t OpenP2PConnectionEx(const std::string &networkId, sptr<IFileDfsListener> remoteReverseObj) override; 64 int32_t CloseP2PConnectionEx(const std::string &networkId) override; 65 int32_t ConnectionCount(const DistributedHardware::DmDeviceInfo &deviceInfo); 66 int32_t CleanUp(const DistributedHardware::DmDeviceInfo &deviceInfo, 67 const std::string &networkId, uint32_t callingTokenId); 68 int32_t ConnectionAndMount(const DistributedHardware::DmDeviceInfo &deviceInfo, 69 const std::string &networkId, uint32_t callingTokenId); 70 int32_t PrepareSession(const std::string &srcUri, 71 const std::string &dstUri, 72 const std::string &srcDeviceId, 73 const sptr<IRemoteObject> &listener, 74 HmdfsInfo &info) override; 75 int32_t CancelCopyTask(const std::string &sessionName) override; 76 int32_t RequestSendFile(const std::string &srcUri, 77 const std::string &dstPath, 78 const std::string &dstDeviceId, 79 const std::string &sessionName) override; 80 int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override; 81 82 int32_t PushAsset(int32_t userId, 83 const sptr<AssetObj> &assetObj, 84 const sptr<IAssetSendCallback> &sendCallback) override; 85 int32_t RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 86 int32_t UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 87 88 static int32_t Copy(const std::string &srcUri, 89 const std::string &dstPath, 90 const sptr<IDaemon> &daemon, 91 const std::string &sessionName); 92 static void DeleteSessionAndListener(const std::string &sessionName, const int32_t socketId); 93 94 private: 95 Daemon(); 96 ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START }; 97 static sptr<Daemon> instance_; 98 static std::mutex instanceLock_; 99 bool registerToService_ { false }; 100 std::shared_ptr<OsAccountObserver> subScriber_; 101 void PublishSA(); 102 void RegisterOsAccount(); 103 sptr<IDaemon> GetRemoteSA(const std::string &remoteDeviceId); 104 void StoreSessionAndListener(const std::string &physicalPath, 105 const std::string &sessionName, 106 const sptr<IFileTransListener> &listener); 107 int32_t GetRealPath(const std::string &srcUri, 108 const std::string &dstUri, 109 std::string &physicalPath, 110 HmdfsInfo &info, 111 const sptr<IDaemon> &daemon); 112 int32_t CheckCopyRule(std::string &physicalPath, 113 const std::string &dstUri, 114 HapTokenInfo &hapTokenInfo, 115 const bool &isSrcFile, 116 HmdfsInfo &info); 117 118 class DfsListenerDeathRecipient : public IRemoteObject::DeathRecipient { 119 public: DfsListenerDeathRecipient()120 DfsListenerDeathRecipient(){}; 121 ~DfsListenerDeathRecipient() = default; 122 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 123 }; 124 static inline sptr<DfsListenerDeathRecipient> dfsListenerDeathRecipient_; 125 private: 126 std::mutex eventHandlerMutex_; 127 std::shared_ptr<DaemonEventHandler> eventHandler_; 128 std::shared_ptr<DaemonExecute> daemonExecute_; 129 void StartEventHandler(); 130 }; 131 } // namespace DistributedFile 132 } // namespace Storage 133 } // namespace OHOS 134 #endif // DAEMON_H