1cb7eb8c9Sopenharmony_ci/* 2cb7eb8c9Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License. 5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at 6cb7eb8c9Sopenharmony_ci * 7cb7eb8c9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cb7eb8c9Sopenharmony_ci * 9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and 13cb7eb8c9Sopenharmony_ci * limitations under the License. 14cb7eb8c9Sopenharmony_ci */ 15cb7eb8c9Sopenharmony_ci 16cb7eb8c9Sopenharmony_ci#ifndef DAEMON_H 17cb7eb8c9Sopenharmony_ci#define DAEMON_H 18cb7eb8c9Sopenharmony_ci 19cb7eb8c9Sopenharmony_ci#include <memory> 20cb7eb8c9Sopenharmony_ci#include <mutex> 21cb7eb8c9Sopenharmony_ci#include <string> 22cb7eb8c9Sopenharmony_ci 23cb7eb8c9Sopenharmony_ci#include "accesstoken_kit.h" 24cb7eb8c9Sopenharmony_ci#include "daemon_event.h" 25cb7eb8c9Sopenharmony_ci#include "daemon_eventhandler.h" 26cb7eb8c9Sopenharmony_ci#include "daemon_execute.h" 27cb7eb8c9Sopenharmony_ci#include "daemon_stub.h" 28cb7eb8c9Sopenharmony_ci#include "dm_device_info.h" 29cb7eb8c9Sopenharmony_ci#include "file_trans_listener_proxy.h" 30cb7eb8c9Sopenharmony_ci#include "hmdfs_info.h" 31cb7eb8c9Sopenharmony_ci#include "ipc/i_daemon.h" 32cb7eb8c9Sopenharmony_ci#include "iremote_stub.h" 33cb7eb8c9Sopenharmony_ci#include "multiuser/os_account_observer.h" 34cb7eb8c9Sopenharmony_ci#include "nocopyable.h" 35cb7eb8c9Sopenharmony_ci#include "refbase.h" 36cb7eb8c9Sopenharmony_ci#include "system_ability.h" 37cb7eb8c9Sopenharmony_ci#include "accesstoken_kit.h" 38cb7eb8c9Sopenharmony_ci 39cb7eb8c9Sopenharmony_cinamespace OHOS { 40cb7eb8c9Sopenharmony_cinamespace Storage { 41cb7eb8c9Sopenharmony_cinamespace DistributedFile { 42cb7eb8c9Sopenharmony_ciusing HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo; 43cb7eb8c9Sopenharmony_cienum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 44cb7eb8c9Sopenharmony_ci 45cb7eb8c9Sopenharmony_ciclass Daemon final : public SystemAbility, public DaemonStub, protected NoCopyable { 46cb7eb8c9Sopenharmony_ci DECLARE_SYSTEM_ABILITY(Daemon); 47cb7eb8c9Sopenharmony_ci 48cb7eb8c9Sopenharmony_cipublic: 49cb7eb8c9Sopenharmony_ci explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {}; 50cb7eb8c9Sopenharmony_ci virtual ~Daemon() = default; 51cb7eb8c9Sopenharmony_ci 52cb7eb8c9Sopenharmony_ci void OnStart() override; 53cb7eb8c9Sopenharmony_ci void OnStop() override; 54cb7eb8c9Sopenharmony_ci void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 55cb7eb8c9Sopenharmony_ci void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 56cb7eb8c9Sopenharmony_ci ServiceRunningState QueryServiceState() const 57cb7eb8c9Sopenharmony_ci { 58cb7eb8c9Sopenharmony_ci return state_; 59cb7eb8c9Sopenharmony_ci } 60cb7eb8c9Sopenharmony_ci 61cb7eb8c9Sopenharmony_ci int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 62cb7eb8c9Sopenharmony_ci int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 63cb7eb8c9Sopenharmony_ci int32_t OpenP2PConnectionEx(const std::string &networkId, sptr<IFileDfsListener> remoteReverseObj) override; 64cb7eb8c9Sopenharmony_ci int32_t CloseP2PConnectionEx(const std::string &networkId) override; 65cb7eb8c9Sopenharmony_ci int32_t ConnectionCount(const DistributedHardware::DmDeviceInfo &deviceInfo); 66cb7eb8c9Sopenharmony_ci int32_t CleanUp(const DistributedHardware::DmDeviceInfo &deviceInfo, 67cb7eb8c9Sopenharmony_ci const std::string &networkId, uint32_t callingTokenId); 68cb7eb8c9Sopenharmony_ci int32_t ConnectionAndMount(const DistributedHardware::DmDeviceInfo &deviceInfo, 69cb7eb8c9Sopenharmony_ci const std::string &networkId, uint32_t callingTokenId); 70cb7eb8c9Sopenharmony_ci int32_t PrepareSession(const std::string &srcUri, 71cb7eb8c9Sopenharmony_ci const std::string &dstUri, 72cb7eb8c9Sopenharmony_ci const std::string &srcDeviceId, 73cb7eb8c9Sopenharmony_ci const sptr<IRemoteObject> &listener, 74cb7eb8c9Sopenharmony_ci HmdfsInfo &info) override; 75cb7eb8c9Sopenharmony_ci int32_t CancelCopyTask(const std::string &sessionName) override; 76cb7eb8c9Sopenharmony_ci int32_t RequestSendFile(const std::string &srcUri, 77cb7eb8c9Sopenharmony_ci const std::string &dstPath, 78cb7eb8c9Sopenharmony_ci const std::string &dstDeviceId, 79cb7eb8c9Sopenharmony_ci const std::string &sessionName) override; 80cb7eb8c9Sopenharmony_ci int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override; 81cb7eb8c9Sopenharmony_ci 82cb7eb8c9Sopenharmony_ci int32_t PushAsset(int32_t userId, 83cb7eb8c9Sopenharmony_ci const sptr<AssetObj> &assetObj, 84cb7eb8c9Sopenharmony_ci const sptr<IAssetSendCallback> &sendCallback) override; 85cb7eb8c9Sopenharmony_ci int32_t RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 86cb7eb8c9Sopenharmony_ci int32_t UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override; 87cb7eb8c9Sopenharmony_ci 88cb7eb8c9Sopenharmony_ciprivate: 89cb7eb8c9Sopenharmony_ci Daemon(); 90cb7eb8c9Sopenharmony_ci ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START }; 91cb7eb8c9Sopenharmony_ci static sptr<Daemon> instance_; 92cb7eb8c9Sopenharmony_ci static std::mutex instanceLock_; 93cb7eb8c9Sopenharmony_ci bool registerToService_ { false }; 94cb7eb8c9Sopenharmony_ci std::shared_ptr<OsAccountObserver> subScriber_; 95cb7eb8c9Sopenharmony_ci void PublishSA(); 96cb7eb8c9Sopenharmony_ci void RegisterOsAccount(); 97cb7eb8c9Sopenharmony_ci sptr<IDaemon> GetRemoteSA(const std::string &remoteDeviceId); 98cb7eb8c9Sopenharmony_ci void StoreSessionAndListener(const std::string &physicalPath, 99cb7eb8c9Sopenharmony_ci const std::string &sessionName, 100cb7eb8c9Sopenharmony_ci const sptr<IFileTransListener> &listener); 101cb7eb8c9Sopenharmony_ci int32_t GetRealPath(const std::string &srcUri, 102cb7eb8c9Sopenharmony_ci const std::string &dstUri, 103cb7eb8c9Sopenharmony_ci std::string &physicalPath, 104cb7eb8c9Sopenharmony_ci HmdfsInfo &info, 105cb7eb8c9Sopenharmony_ci const sptr<IDaemon> &daemon); 106cb7eb8c9Sopenharmony_ci int32_t CheckCopyRule(std::string &physicalPath, 107cb7eb8c9Sopenharmony_ci const std::string &dstUri, 108cb7eb8c9Sopenharmony_ci HapTokenInfo &hapTokenInfo, 109cb7eb8c9Sopenharmony_ci const bool &isSrcFile, 110cb7eb8c9Sopenharmony_ci HmdfsInfo &info); 111cb7eb8c9Sopenharmony_ci int32_t Copy(const std::string &srcUri, 112cb7eb8c9Sopenharmony_ci const std::string &dstPath, 113cb7eb8c9Sopenharmony_ci const sptr<IDaemon> &daemon, 114cb7eb8c9Sopenharmony_ci const std::string &sessionName); 115cb7eb8c9Sopenharmony_ci void DeleteSessionAndListener(const std::string &sessionName, const int32_t socketId); 116cb7eb8c9Sopenharmony_ci 117cb7eb8c9Sopenharmony_ci class DfsListenerDeathRecipient : public IRemoteObject::DeathRecipient { 118cb7eb8c9Sopenharmony_ci public: 119cb7eb8c9Sopenharmony_ci DfsListenerDeathRecipient(){}; 120cb7eb8c9Sopenharmony_ci ~DfsListenerDeathRecipient() = default; 121cb7eb8c9Sopenharmony_ci void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 122cb7eb8c9Sopenharmony_ci }; 123cb7eb8c9Sopenharmony_ci static inline sptr<DfsListenerDeathRecipient> dfsListenerDeathRecipient_; 124cb7eb8c9Sopenharmony_ciprivate: 125cb7eb8c9Sopenharmony_ci std::mutex eventHandlerMutex_; 126cb7eb8c9Sopenharmony_ci std::shared_ptr<DaemonEventHandler> eventHandler_; 127cb7eb8c9Sopenharmony_ci std::shared_ptr<DaemonExecute> daemonExecute_; 128cb7eb8c9Sopenharmony_ci void StartEventHandler(); 129cb7eb8c9Sopenharmony_ci}; 130cb7eb8c9Sopenharmony_ci} // namespace DistributedFile 131cb7eb8c9Sopenharmony_ci} // namespace Storage 132cb7eb8c9Sopenharmony_ci} // namespace OHOS 133cb7eb8c9Sopenharmony_ci#endif // DAEMON_H