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 BUNDLE_ACTIVE_APP_STATE_OBSERVER_H
17 #define BUNDLE_ACTIVE_APP_STATE_OBSERVER_H
18 
19 #include <memory>
20 #ifdef OS_ACCOUNT_PART_ENABLED
21 #include "os_account_manager.h"
22 #endif // OS_ACCOUNT_PART_ENABLED
23 #include "application_state_observer_stub.h"
24 #include "bundle_active_report_handler.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 using OHOS::AppExecFwk::ApplicationStateObserverStub;
29 using OHOS::AppExecFwk::AppStateData;
30 using OHOS::AppExecFwk::AbilityStateData;
31 using OHOS::AppExecFwk::ProcessData;
32 
33 class BundleActiveAppStateObserver : public ApplicationStateObserverStub {
34 public:
35     void OnAbilityStateChanged(const AbilityStateData &abilityStateData) override;
36     void Init(const std::shared_ptr<BundleActiveReportHandler>& reportHandler);
37 private:
ValidateAppStateData(const AppStateData & appStateData)38     inline bool ValidateAppStateData(const AppStateData &appStateData) const
39     {
40         return appStateData.uid > 0
41             && appStateData.bundleName.size() > 0;
42     }
43 
ValidateAbilityStateData(const AbilityStateData & abilityStateData)44     inline bool ValidateAbilityStateData(const AbilityStateData &abilityStateData) const
45     {
46         return abilityStateData.uid > 0 && abilityStateData.pid >= 0
47             && abilityStateData.bundleName.size() > 0
48             && abilityStateData.abilityName.size() > 0
49             && abilityStateData.token != nullptr;
50     }
51 
ValidateProcessData(const ProcessData & processData)52     inline bool ValidateProcessData(const ProcessData &processData) const
53     {
54         return processData.uid > 0 && processData.pid >= 0
55             && processData.bundleName.size() > 0;
56     }
57     std::shared_ptr<BundleActiveReportHandler> reportHandler_;
58 };
59 }  // namespace DeviceUsageStats
60 }  // namespace OHOS
61 #endif  // BUNDLE_ACTIVE_APP_STATE_OBSERVER_H
62 
63