• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "remote_file_share.h"
37 #include "system_ability.h"
38 #include "accesstoken_kit.h"
39 
40 namespace OHOS {
41 namespace Storage {
42 namespace DistributedFile {
43 using HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo;
44 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
45 
46 class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyable {
47     DECLARE_SYSTEM_ABILITY(Daemon);
48 
49 public:
SystemAbility(saID,runOnCreate)50     explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {};
51     virtual ~Daemon() = default;
52 
53     void OnStart() override;
54     void OnStop() override;
55     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
56     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
QueryServiceState()57     ServiceRunningState QueryServiceState() const
58     {
59         return state_;
60     }
61 
62     int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
63     int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
64     int32_t OpenP2PConnectionEx(const std::string &networkId, sptr<IFileDfsListener> remoteReverseObj) override;
65     int32_t CloseP2PConnectionEx(const std::string &networkId) override;
66     int32_t ConnectionCount(const DistributedHardware::DmDeviceInfo &deviceInfo);
67     int32_t CleanUp(const DistributedHardware::DmDeviceInfo &deviceInfo);
68     int32_t ConnectionAndMount(const DistributedHardware::DmDeviceInfo &deviceInfo,
69         const std::string &networkId, uint32_t callingTokenId, sptr<IFileDfsListener> remoteReverseObj);
70     int32_t InnerCopy(const std::string &srcUri, const std::string &dstUri,
71         const std::string &srcDeviceId, const sptr<IFileTransListener> &listener, HmdfsInfo &info);
72     int32_t PrepareSession(const std::string &srcUri,
73                            const std::string &dstUri,
74                            const std::string &srcDeviceId,
75                            const sptr<IRemoteObject> &listener,
76                            HmdfsInfo &info) override;
77     int32_t CancelCopyTask(const std::string &sessionName) override;
78     int32_t CancelCopyTask(const std::string &srcUri, const std::string &dstUri) override;
79     int32_t RequestSendFile(const std::string &srcUri,
80                             const std::string &dstPath,
81                             const std::string &dstDeviceId,
82                             const std::string &sessionName) override;
83     int32_t GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir) override;
84 
85     int32_t PushAsset(int32_t userId,
86                       const sptr<AssetObj> &assetObj,
87                       const sptr<IAssetSendCallback> &sendCallback) override;
88     int32_t RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override;
89     int32_t UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback) override;
90 
91     int32_t GetDfsUrisDirFromLocal(const std::vector<std::string> &uriList,
92                                    const int32_t userId,
93                                    std::unordered_map<std::string, AppFileService::ModuleRemoteFileShare::HmdfsUriInfo>
94                                    &uriToDfsUriMaps) override;
95     static int32_t Copy(const std::string &srcUri,
96                         const std::string &dstPath,
97                         const sptr<IDaemon> &daemon,
98                         const std::string &sessionName);
99     int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) override;
100     int32_t UpdateDfsSwitchStatus(int32_t switchStatus) override;
101     int32_t GetConnectedDeviceList(std::vector<DfsDeviceInfo> &deviceList) override;
102 
103 private:
104     Daemon();
105     ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START };
106     static sptr<Daemon> instance_;
107     static std::mutex instanceLock_;
108     bool registerToService_ { false };
109     std::shared_ptr<OsAccountObserver> subScriber_;
110     void PublishSA();
111     void RegisterOsAccount();
112     sptr<IDaemon> GetRemoteSA(const std::string &remoteDeviceId);
113     int32_t StoreSessionAndListener(const std::string &physicalPath,
114                                     std::string &sessionName,
115                                     const sptr<IFileTransListener> &listener);
116     void DeleteSessionAndListener(const std::string &sessionName);
117     int32_t GetRealPath(const std::string &srcUri,
118                         const std::string &dstUri,
119                         std::string &physicalPath,
120                         HmdfsInfo &info,
121                         const sptr<IDaemon> &daemon);
122     int32_t CheckCopyRule(std::string &physicalPath,
123                           const std::string &dstUri,
124                           HapTokenInfo &hapTokenInfo,
125                           const bool &isSrcFile,
126                           HmdfsInfo &info);
127     int32_t SendDfsDelayTask(const std::string &networkId);
128     void RemoveDfsDelayTask(const std::string &networkId);
129     void DisconnectDevice(const std::string networkId);
130     bool IsCallingDeviceTrusted();
131 
132     class DfsListenerDeathRecipient : public IRemoteObject::DeathRecipient {
133     public:
DfsListenerDeathRecipient()134         DfsListenerDeathRecipient(){};
135         ~DfsListenerDeathRecipient() = default;
136         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
137     };
138     static inline sptr<DfsListenerDeathRecipient> dfsListenerDeathRecipient_;
139     int32_t CopyBaseOnRPC(const std::string &srcUri,
140                           const std::string &dstUri,
141                           const std::string &srcDeviceId,
142                           const sptr<IFileTransListener> &listenerCallback,
143                           HmdfsInfo &info);
144 
145     void DisconnectByRemote(const std::string &networkId);
146     int32_t CreatControlLink(const std::string &networkId);
147     int32_t CancelControlLink(const std::string &networkId);
148     int32_t CheckRemoteAllowConnect(const std::string &networkId);
149     int32_t NotifyRemotePublishNotification(const std::string &networkId);
150     int32_t NotifyRemoteCancelNotification(const std::string &networkId);
151 private:
152     std::mutex connectMutex_;
153     std::mutex eventHandlerMutex_;
154     std::shared_ptr<DaemonEventHandler> eventHandler_;
155     std::shared_ptr<DaemonExecute> daemonExecute_;
156     void StartEventHandler();
157 };
158 } // namespace DistributedFile
159 } // namespace Storage
160 } // namespace OHOS
161 #endif // DAEMON_H
162