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 "daemon_stub.h" 24 #include "dm_device_info.h" 25 #include "ipc/i_daemon.h" 26 #include "iremote_stub.h" 27 #include "multiuser/os_account_observer.h" 28 #include "nocopyable.h" 29 #include "refbase.h" 30 #include "system_ability.h" 31 32 namespace OHOS { 33 namespace Storage { 34 namespace DistributedFile { 35 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 36 37 class Daemon final : public SystemAbility, public DaemonStub, protected NoCopyable { 38 DECLARE_SYSTEM_ABILITY(Daemon); 39 40 public: SystemAbility(saID,runOnCreate)41 explicit Daemon(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate) {}; 42 virtual ~Daemon() = default; 43 44 void OnStart() override; 45 void OnStop() override; 46 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 47 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; QueryServiceState()48 ServiceRunningState QueryServiceState() const 49 { 50 return state_; 51 } 52 53 int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 54 int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 55 private: 56 Daemon(); 57 ServiceRunningState state_ { ServiceRunningState::STATE_NOT_START }; 58 static sptr<Daemon> instance_; 59 static std::mutex instanceLock_; 60 bool registerToService_ { false }; 61 std::shared_ptr<OsAccountObserver> subScriber_; 62 void PublishSA(); 63 void RegisterOsAccount(); 64 }; 65 } // namespace DistributedFile 66 } // namespace Storage 67 } // namespace OHOS 68 #endif // DAEMON_H 69