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_NOTIFICATION_INCLUDE_NOTIFICATION_HELPER_H
17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_NOTIFICATION_INCLUDE_NOTIFICATION_HELPER_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <algorithm>
23 
24 #include "singleton.h"
25 #include "iremote_object.h"
26 
27 #include "allow_type.h"
28 #include "single_instance.h"
29 #include "standby_service_errors.h"
30 #include "istandby_service_subscriber.h"
31 #include "standby_state.h"
32 #include "istate_manager_adapter.h"
33 
34 namespace OHOS {
35 namespace DevStandbyMgr {
36 class SubscriberDeathRecipient;
37 class StandbyStateSubscriber : public std::enable_shared_from_this<StandbyStateSubscriber> {
38 DECLARE_SINGLE_INSTANCE(StandbyStateSubscriber);
39 
40 public:
41     ErrCode AddSubscriber(const sptr<IStandbyServiceSubscriber>& subscriber);
42     ErrCode RemoveSubscriber(const sptr<IStandbyServiceSubscriber>& subscriber);
43     void ReportStandbyState(uint32_t curState);
44     void ReportAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType, bool added);
45     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
46     void ShellDump(const std::vector<std::string>& argsInStr, std::string& result);
47     void NotifyAllowChangedByCommonEvent(int32_t uid, const std::string& name, uint32_t allowType, bool added);
48     void NotifyPowerOverusedByCallback(const std::string& module, uint32_t level);
49 
50 private:
51     void NotifyIdleModeByCallback(bool napped, bool sleeping);
52     void NotifyIdleModeByCommonEvent(bool napped, bool sleeping);
53     void NotifyAllowChangedByCallback(int32_t uid, const std::string& name, uint32_t allowType, bool added);
54     std::list<sptr<IStandbyServiceSubscriber>>::iterator FindSubcriberObject(sptr<IRemoteObject>& proxy);
55     void NotifyPowerOnRegister(const sptr<IStandbyServiceSubscriber>& subscriber);
56 
57 private:
58     std::mutex subscriberLock_ {};
59     std::list<sptr<IStandbyServiceSubscriber>> subscriberList_ {};
60     sptr<SubscriberDeathRecipient> deathRecipient_ {nullptr};
61     std::shared_ptr<IStateManagerAdapter> standbyStateManager_ {nullptr};
62     std::unordered_map<std::string, uint32_t> modulePowerMap_;
63     int32_t curDate_;
64 };
65 
66 class SubscriberDeathRecipient final : public IRemoteObject::DeathRecipient {
67 public:
68     DISALLOW_COPY_AND_MOVE(SubscriberDeathRecipient);
69     explicit SubscriberDeathRecipient();
70     ~SubscriberDeathRecipient() override;
71     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
72 };
73 }  // namespace DevStandbyMgr
74 }  // namespace OHOS
75 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_NOTIFICATION_INCLUDE_NOTIFICATION_HELPER_H