1 /*
2  * Copyright (c) 2022-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 BUNDLE_ACTIVE_REPORT_HANDLER_H
17 #define BUNDLE_ACTIVE_REPORT_HANDLER_H
18 
19 #include "ffrt.h"
20 #include <map>
21 #include <queue>
22 
23 #include "ibundle_active_service.h"
24 #include "bundle_active_core.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 class BundleActiveReportHandler : public std::enable_shared_from_this<BundleActiveReportHandler> {
29 public:
30     BundleActiveReportHandler() = default;
31     ~BundleActiveReportHandler() = default;
32         /**
33      * Process the event. Developers should override this method.
34      *
35      * @param eventId The event id
36      * @param handlerobj The eventobj
37      */
38     void ProcessEvent(const int32_t& eventId, const std::shared_ptr<BundleActiveReportHandlerObject>& handlerobj);
39     void SendEvent(const int32_t& eventId,
40         const std::shared_ptr<BundleActiveReportHandlerObject>& handlerobj, const int64_t& delayTime = 0);
41     void RemoveEvent(const int32_t& eventId);
42     bool HasEvent(const int32_t& eventId);
43     void Init(const std::shared_ptr<BundleActiveCore>& bundleActiveCore);
44     static const int32_t MSG_REPORT_EVENT;
45     static const int32_t MSG_FLUSH_TO_DISK;
46     static const int32_t MSG_REMOVE_USER;
47     static const int32_t MSG_BUNDLE_UNINSTALLED;
48     static const int32_t MSG_SWITCH_USER;
49 
50 private:
51     bool isInited_ = false;
52     ffrt::mutex taskHandlerMutex_;
53     std::shared_ptr<ffrt::queue> ffrtQueue_;
54     std::map<int32_t, std::queue<ffrt::task_handle>> taskHandlerMap_;
55     std::shared_ptr<BundleActiveCore> bundleActiveCore_;
56 };
57 }  // namespace DeviceUsageStats
58 }  // namespace OHOS
59 #endif  // BUNDLE_ACTIVE_REPORT_HANDLER_H
60 
61