• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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_stub.h"
25 #include "dm_device_info.h"
26 #include "file_trans_listener_proxy.h"
27 #include "ipc/i_daemon.h"
28 #include "iremote_stub.h"
29 #include "multiuser/os_account_observer.h"
30 #include "nocopyable.h"
31 #include "refbase.h"
32 #include "system_ability.h"
33 #include "accesstoken_kit.h"
34 
35 namespace OHOS {
36 namespace Storage {
37 namespace DistributedFile {
38 using HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo;
39 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
40 
41 class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyable {
42     DECLARE_SYSTEM_ABILITY(Daemon);
43 
44 public:
SystemAbility(saID,runOnCreate)45     explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {};
46     virtual ~Daemon() = default;
47 
48     void OnStart() override;
49     void OnStop() override;
50     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
51     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
QueryServiceState()52     ServiceRunningState QueryServiceState() const
53     {
54         return state_;
55     }
56 
57     int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
58     int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
59     int32_t PrepareSession(const std::string &srcUri,
60                            const std::string &dstUri,
61                            const std::string &srcDeviceId,
62                            const sptr<IRemoteObject> &listener) override;
63     int32_t RequestSendFile(const std::string &srcUri,
64                             const std::string &dstPath,
65                             const std::string &dstDeviceId,
66                             const std::string &sessionName) override;
67     int32_t GetRealPath(const std::string &dstUri,
68                         const HapTokenInfo &hapTokenInfo,
69                         const std::string &sessionName,
70                         std::string &physicalPath);
71 
72 private:
73     Daemon();
74     ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START };
75     static sptr<Daemon> instance_;
76     static std::mutex instanceLock_;
77     bool registerToService_ { false };
78     std::shared_ptr<OsAccountObserver> subScriber_;
79     void PublishSA();
80     void RegisterOsAccount();
81     int32_t LoadRemoteSA(const std::string &srcUri,
82                          const std::string &dstPath,
83                          const std::string &localDeviceId,
84                          const std::string &remoteDeviceId,
85                          const std::string &sessionName);
86     void RemoveSession(const std::string &sessionName);
87     int32_t CancelWait(const std::string &sessionName, const sptr<IFileTransListener> &listenerCallback);
88 };
89 } // namespace DistributedFile
90 } // namespace Storage
91 } // namespace OHOS
92 #endif // DAEMON_H