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