1 /*
2  * Copyright (c) 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 FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_H
17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_H
18 
19 #include <ctime>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <set>
25 #include <string>
26 
27 #include "singleton.h"
28 #include "system_ability.h"
29 
30 #include "standby_service_stub.h"
31 #include "single_instance.h"
32 
33 namespace OHOS {
34 namespace DevStandbyMgr {
35 enum class ServiceRunningState {
36     STATE_NOT_START,
37     STATE_RUNNING
38 };
39 
40 class StandbyService final : public SystemAbility, public StandbyServiceStub {
41     DECLARE_SYSTEM_ABILITY(StandbyService);
42     DECLARE_SINGLE_INSTANCE(StandbyService);
43 public:
44     StandbyService(const int32_t systemAbilityId, bool runOnCreate);
45     void OnStart() final;
46     void OnStop() final;
47 
48     ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber) override;
49     ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber) override;
50     ErrCode ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest) override;
51     ErrCode UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest) override;
52     ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
53         uint32_t reasonCode) override;
54     ErrCode IsDeviceInStandby(bool& isStandby) override;
55     ErrCode SetNatInterval(uint32_t& type, bool& enable, uint32_t& interval) override;
56     ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName) override;
57     ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
58         uint32_t reasonCode) override;
59     ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled) override;
60     ErrCode ReportDeviceStateChanged(DeviceStateType type, bool enabled) override;
61     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
62     void AddPluginSysAbilityListener(int32_t systemAbilityId);
63     ErrCode NotifySystemAbilityStatusChanged(bool isAdded, int32_t systemAbilityId);
64     ErrCode HandleEvent(const uint32_t resType, const int64_t value, const std::string &sceneInfo) override;
65     ErrCode ReportPowerOverused(const std::string &module, uint32_t level) override;
66 
67 private:
68     void DumpUsage(std::string& result);
69     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
70     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
71     bool CheckProcessNamePermission(const std::string& processName);
72 
73 private:
74     std::mutex systemAbilityLock_ {};
75     ServiceRunningState state_ {ServiceRunningState::STATE_NOT_START};
76     uint32_t dependsReady_ = 0;
77     std::mutex listenedSALock_ {};
78 };
79 }  // namespace DevStandbyMgr
80 }  // namespace OHOS
81 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_H