1 /*
2  * Copyright (C) 2024 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 OHOS_REQUEST_RUNNING_TASK_COUNT_H
17 #define OHOS_REQUEST_RUNNING_TASK_COUNT_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <vector>
22 
23 #include "../../interfaces/inner_kits/running_count/include/running_task_count.h"
24 #include "iremote_proxy.h"
25 #include "js_common.h"
26 #include "log.h"
27 #include "notify_interface.h"
28 #include "peer_holder.h"
29 #include "request_service_interface.h"
30 
31 namespace OHOS::Request {
32 class FwkRunningTaskCountManager;
33 class FwkIRunningTaskObserver {
34 public:
35     std::shared_ptr<IRunningTaskObserver> GetInnerObserver();
36     void UpdateRunningTaskCount();
37 
38 public:
39     ~FwkIRunningTaskObserver() = default;
40     FwkIRunningTaskObserver(std::shared_ptr<IRunningTaskObserver> ob);
41 
42 private:
43     std::shared_ptr<IRunningTaskObserver> pInnerOb_;
44 };
45 
46 class FwkRunningTaskCountManager {
47 public:
48     static std::unique_ptr<FwkRunningTaskCountManager> &GetInstance();
49     int32_t GetCount();
50     void SetCount(int runCount);
51     void AttachObserver(std::shared_ptr<IRunningTaskObserver> ob);
52     void DetachObserver(std::shared_ptr<IRunningTaskObserver> ob);
53     void NotifyAllObservers();
54     bool HasObserver();
55     bool SaIsOnline();
56     void SetSaStatus(bool isOnline);
57 
58     ~FwkRunningTaskCountManager() = default;
59     FwkRunningTaskCountManager(const FwkRunningTaskCountManager &) = delete;
60     FwkRunningTaskCountManager(FwkRunningTaskCountManager &&) = delete;
61     FwkRunningTaskCountManager &operator=(const FwkRunningTaskCountManager &) = delete;
62 
63 private:
64     FwkRunningTaskCountManager() = default;
65     std::atomic<bool> saIsOnline_ = false;
66     int count_ = 0;
67     std::mutex observersLock_;
68     std::mutex countLock_;
69     std::vector<std::shared_ptr<FwkIRunningTaskObserver>> observers_;
70 };
71 
72 } // namespace OHOS::Request
73 
74 #endif // OHOS_REQUEST_RUNNING_TASK_COUNT_H