1 /*
2  * Copyright (c) 2022 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_BACKGROUND_TASK_MGR_SERVICES_RESOURCES_INCLUDE_RESOURCES_SUBSCRIBER_MGR_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_RESOURCES_INCLUDE_RESOURCES_SUBSCRIBER_MGR_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 "bgtaskmgr_inner_errors.h"
28 #include "ibackground_task_subscriber.h"
29 
30 namespace OHOS {
31 namespace BackgroundTaskMgr {
32 class ObserverDeathRecipient;
33 enum class EfficiencyResourcesEventType: uint32_t {
34     APP_RESOURCE_APPLY,
35     APP_RESOURCE_RESET,
36     RESOURCE_APPLY,
37     RESOURCE_RESET,
38 };
39 
40 enum : int32_t {
41     HISYSEVENT_APP_RESOURCE_APPLY = -1,
42     HISYSEVENT_APP_RESOURCE_RESET = -2,
43     HISYSEVENT_RESOURCE_APPLY = -3,
44     HISYSEVENT_RESOURCE_RESET = -4,
45 };
46 
47 class ResourcesSubscriberMgr : public std::enable_shared_from_this<ResourcesSubscriberMgr>  {
48     DECLARE_DELAYED_SINGLETON(ResourcesSubscriberMgr);
49 public:
50     ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
51     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
52     void OnResourceChanged(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
53         EfficiencyResourcesEventType type);
54     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
55 
56 private:
57     void HiSysEventResources(const std::shared_ptr<ResourceCallbackInfo> callbackInfo,
58         EfficiencyResourcesEventType type);
59     void HiSysEventSubmit(const std::shared_ptr<ResourceCallbackInfo> callbackInfo,
60         int32_t hiSysEventType, const std::string &eventType);
61 
62     std::mutex subscriberLock_;
63     std::list<sptr<IBackgroundTaskSubscriber>> subscriberList_ {};
64     sptr<ObserverDeathRecipient> deathRecipient_ {nullptr};
65 };
66 
67 class ObserverDeathRecipient final : public IRemoteObject::DeathRecipient {
68 public:
69     DISALLOW_COPY_AND_MOVE(ObserverDeathRecipient);
70     explicit ObserverDeathRecipient();
71     ~ObserverDeathRecipient() override;
72     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
73 };
74 }  // namespace BackgroundTaskMgr
75 }  // namespace OHOS
76 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_RESOURCES_INCLUDE_RESOURCES_SUBSCRIBER_MGR_H