• 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 #include "ability_state_observer.h"
17 
18 #include "distributed_sched_service.h"
19 #include "dsched_collab_manager.h"
20 #include "dtbschedmgr_log.h"
21 #ifdef EFFICIENCY_MANAGER_ENABLE
22 #include "res_type.h"
23 #include "res_sched_client.h"
24 #endif
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 const std::string TAG = "AbilityLifecycleObserver";
30 const std::string PID_KEY = "pid";
31 const std::string UID_KEY = "uid";
32 const std::string BUNDLE_NAME_KEY = "bundleName";
33 const std::string COMPONENT_TYPE_KEY = "componentType";
34 const std::string DEVICE_TYPE_KEY = "deviceType";
35 const std::string CHANGE_TYPE_KEY = "changeType";
36 constexpr int32_t DISTRIBUTED_COMPONENT_ADD = 1;
37 constexpr int32_t DISTRIBUTED_COMPONENT_REMOVE = 2;
38 }
39 
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)40 void AbilityLifecycleObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData)
41 {
42     HILOGI("called.");
43 }
44 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)45 void AbilityLifecycleObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData& abilityStateData)
46 {
47     HILOGI("called, abilityState: %{public}d", abilityStateData.abilityState);
48 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
49      if (abilityStateData.abilityState == static_cast<int32_t>(SliteAbilityState::STATE_BACKGROUND)) {
50         std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasksInfos;
51         int32_t result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasksInfos);
52         if (result != ERR_OK) {
53             HILOGE("failed to GetContinuousTaskApps, err: %{public}d", result);
54             return;
55         }
56         for (auto continuousTasksInfo : continuousTasksInfos) {
57             if (continuousTasksInfo == nullptr) {
58                 continue;
59             }
60             if (continuousTasksInfo->GetCreatorPid() == abilityStateData.pid) {
61                 HILOGW("called, find the pid in continuous tasks");
62                 ReportDistributedComponentChange(abilityStateData, DISTRIBUTED_COMPONENT_ADD);
63                 return;
64             }
65         }
66         HILOGW("called, can't find the pid in continuous tasks");
67         DSchedCollabManager::GetInstance().ReleaseAbilityLink(abilityStateData.bundleName, abilityStateData.pid);
68     } else if (abilityStateData.abilityState == static_cast<int32_t>(SliteAbilityState::STATE_FOREGROUND) ||
69         abilityStateData.abilityState == static_cast<int32_t>(SliteAbilityState::STATE_INITIAL)) {
70         ReportDistributedComponentChange(abilityStateData, DISTRIBUTED_COMPONENT_REMOVE);
71         DSchedCollabManager::GetInstance().CancleReleaseAbilityLink(abilityStateData.bundleName, abilityStateData.pid);
72     }
73 #else
74     HILOGE("failed to check continuousTaskApps");
75 #endif
76     HILOGI("end.");
77 }
78 
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & extensionStateData)79 void AbilityLifecycleObserver::OnExtensionStateChanged(const AppExecFwk::AbilityStateData& extensionStateData)
80 {
81     HILOGI("called.");
82 }
83 
OnProcessCreated(const AppExecFwk::ProcessData & processData)84 void AbilityLifecycleObserver::OnProcessCreated(const AppExecFwk::ProcessData& processData)
85 {
86     HILOGI("called.");
87 }
88 
OnProcessDied(const AppExecFwk::ProcessData & processData)89 void AbilityLifecycleObserver::OnProcessDied(const AppExecFwk::ProcessData& processData)
90 {
91     HILOGI("called, bundleName: %{public}s", processData.bundleName.c_str());
92     DSchedCollabManager::GetInstance().NotifyAbilityDied(processData.bundleName, processData.pid);
93     HILOGI("end.");
94 }
95 
ReportDistributedComponentChange(const AppExecFwk::AbilityStateData & abilityStateData,int32_t changeType)96 void AbilityLifecycleObserver::ReportDistributedComponentChange(const AppExecFwk::AbilityStateData& abilityStateData,
97     int32_t changeType)
98 {
99     HILOGI("called.");
100 #ifdef EFFICIENCY_MANAGER_ENABLE
101     std::unordered_map<std::string, std::string> payload;
102     payload[PID_KEY] = std::to_string(abilityStateData.pid);
103     payload[UID_KEY] = std::to_string(abilityStateData.uid);
104     payload[BUNDLE_NAME_KEY] = abilityStateData.bundleName;
105     payload[COMPONENT_TYPE_KEY] = std::to_string(IDistributedSched::CONNECT);
106     payload[DEVICE_TYPE_KEY] = std::to_string(IDistributedSched::CALLER);
107     payload[CHANGE_TYPE_KEY] = std::to_string(changeType);
108     uint32_t type = ResourceSchedule::ResType::RES_TYPE_CONTINUOUS_TASK;
109     HILOGI("begin report data.");
110     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, 0, payload);
111 #endif
112 }
113 } // namespace DistributedSchedule
114 } // namespace OHOS
115