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: 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 int32_t ConnectionAndMount(const DistributedHardware::DmDeviceInfo &deviceInfo, 68 const std::string &networkId, uint32_t callingTokenId, sptr<IFileDfsListener> remoteReverseObj); 69 int32_t PrepareSession(const std::string &srcUri, 70 const std::string &dstUri, 71 const std::string &srcDeviceId, 72 const sptr<IRemoteObject> &listener, 73 HmdfsInfo &info) override; 74 int32_t CancelCopyTask(const std::string &sessionName) override; 75 int32_t RequestSendFile(const std::string &srcUri, 76 const std::string &dstPath, 77 const std::string &dstDeviceId, 78 const std::string &sessionName) override; 79 int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override; 80 81 int32_t PushAsset(int32_t userId, 82 const sptr<AssetObj> &assetObj, 83 const sptr<IAssetSendCallback> &sendCallback) override; 84 int32_t RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 85 int32_t UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 86 87 static int32_t Copy(const std::string &srcUri, 88 const std::string &dstPath, 89 const sptr<IDaemon> &daemon, 90 const std::string &sessionName); 91 static void DeleteSessionAndListener(const std::string &sessionName, const int32_t socketId); 92 93 private: 94 Daemon(); 95 ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START }; 96 static sptr<Daemon> instance_; 97 static std::mutex instanceLock_; 98 bool registerToService_ { false }; 99 std::shared_ptr<OsAccountObserver> subScriber_; 100 void PublishSA(); 101 void RegisterOsAccount(); 102 sptr<IDaemon> GetRemoteSA(const std::string &remoteDeviceId); 103 void StoreSessionAndListener(const std::string &physicalPath, 104 const std::string &sessionName, 105 const sptr<IFileTransListener> &listener); 106 int32_t GetRealPath(const std::string &srcUri, 107 const std::string &dstUri, 108 std::string &physicalPath, 109 HmdfsInfo &info, 110 const sptr<IDaemon> &daemon); 111 int32_t CheckCopyRule(std::string &physicalPath, 112 const std::string &dstUri, 113 HapTokenInfo &hapTokenInfo, 114 const bool &isSrcFile, 115 HmdfsInfo &info); 116 117 class DfsListenerDeathRecipient : public IRemoteObject::DeathRecipient { 118 public: DfsListenerDeathRecipient()119 DfsListenerDeathRecipient(){}; 120 ~DfsListenerDeathRecipient() = default; 121 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 122 }; 123 static inline sptr<DfsListenerDeathRecipient> dfsListenerDeathRecipient_; 124 private: 125 std::mutex connectMutex_; 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