• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 REQUEST_APPLICATION_STATE_OBSERVER_H
17 #define REQUEST_APPLICATION_STATE_OBSERVER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <map>
22 #include <string>
23 
24 #include "application_state_observer_stub.h"
25 #include "c_string_wrapper.h"
26 
27 namespace OHOS::Request {
28 class ApplicationStateObserver {
29 public:
30     ~ApplicationStateObserver();
31     using RegCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid)>;
32     using ProcessCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid, CStringWrapper bundleName)>;
33     static ApplicationStateObserver &GetInstance();
34     bool RegisterAppStateChanged(RegCallBack &&callback);
35     void RegisterProcessDied(ProcessCallBack &&callback);
36 
37 public:
38     class AppProcessState : public AppExecFwk::ApplicationStateObserverStub {
39     public:
AppProcessState(ApplicationStateObserver & appStateObserver)40         explicit AppProcessState(ApplicationStateObserver &appStateObserver) : appStateObserver_(appStateObserver)
41         {
42         }
43         ~AppProcessState() override = default;
44         void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override;
45         void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override;
46         void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override;
47         void OnProcessCreated(const AppExecFwk::ProcessData &processData) override;
48         void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
49 
50     public:
51         void RunAppStateCallback(int32_t uid, int32_t state, int32_t pid);
52         void RunProcessDiedCallback(int32_t uid, int32_t state, int32_t pid, const std::string &bundleName);
53         ApplicationStateObserver &appStateObserver_;
54     };
55     ApplicationStateObserver();
56     RegCallBack appStateCallback_ = nullptr;
57     ProcessCallBack processCallback_ = nullptr;
58 };
59 } // namespace OHOS::Request
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 typedef void (*APPStateCallback)(int32_t, int32_t, int32_t);
66 typedef void (*ProcessStateCallback)(int32_t, int32_t, int32_t, CStringWrapper);
67 void RegisterAPPStateCallback(APPStateCallback fun);
68 void RegisterProcessDiedCallback(ProcessStateCallback fun);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #endif // REQUEST_APPLICATION_STATE_OBSERVER_H
75