• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class ResourcesSubscriberMgr : public std::enable_shared_from_this<ResourcesSubscriberMgr>  {
41     DECLARE_DELAYED_SINGLETON(ResourcesSubscriberMgr);
42 public:
43     ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
44     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
45     void OnResourceChanged(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
46         EfficiencyResourcesEventType type);
47     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
48 
49 private:
50     std::mutex subscriberLock_;
51     std::list<sptr<IBackgroundTaskSubscriber>> subscriberList_ {};
52     sptr<ObserverDeathRecipient> deathRecipient_ {nullptr};
53 };
54 
55 class ObserverDeathRecipient final : public IRemoteObject::DeathRecipient {
56 public:
57     DISALLOW_COPY_AND_MOVE(ObserverDeathRecipient);
58     explicit ObserverDeathRecipient();
59     ~ObserverDeathRecipient() override;
60     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
61 };
62 }  // namespace BackgroundTaskMgr
63 }  // namespace OHOS
64 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_RESOURCES_INCLUDE_RESOURCES_SUBSCRIBER_MGR_H