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 #include "extension_record.h"
17 #include "preload_uiext_state_observer.h"
18 #include "hilog_tag_wrapper.h"
19 
20 
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 constexpr int32_t APP_STATE_CACHED = 100;
25 }
PreLoadUIExtStateObserver(std::weak_ptr<OHOS::AbilityRuntime::ExtensionRecord> extensionRecord)26 PreLoadUIExtStateObserver::PreLoadUIExtStateObserver(
27     std::weak_ptr<OHOS::AbilityRuntime::ExtensionRecord> extensionRecord) : extensionRecord_(extensionRecord) {}
28 
OnProcessDied(const AppExecFwk::ProcessData & processData)29 void PreLoadUIExtStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
30 {
31     auto diedProcessName = processData.processName;
32     TAG_LOGI(AAFwkTag::ABILITYMGR, "DiedProcessName is %{public}s.", diedProcessName.c_str());
33     auto extensionRecord = extensionRecord_.lock();
34     if (extensionRecord != nullptr) {
35         auto hostPid = extensionRecord->hostPid_;
36         int32_t diedPid = processData.pid;
37         TAG_LOGD(AAFwkTag::ABILITYMGR, "Host pid is %{public}d, died pid is %{public}d.", hostPid, diedPid);
38         if (static_cast<int32_t>(hostPid) != diedPid) {
39             TAG_LOGD(AAFwkTag::ABILITYMGR, "Host pid is not equals to died pid.");
40             return;
41         }
42         extensionRecord->UnloadUIExtensionAbility();
43     } else {
44         TAG_LOGW(AAFwkTag::ABILITYMGR, "extensionRecord null");
45     }
46 }
47 
OnAppCacheStateChanged(const AppExecFwk::AppStateData & appStateData)48 void PreLoadUIExtStateObserver::OnAppCacheStateChanged(const AppExecFwk::AppStateData &appStateData)
49 {
50     auto extensionRecord = extensionRecord_.lock();
51     if (extensionRecord != nullptr) {
52         auto hostPid = extensionRecord->hostPid_;
53         int32_t cachePid = appStateData.pid;
54         if (static_cast<int32_t>(hostPid) != cachePid || appStateData.state != APP_STATE_CACHED) {
55             TAG_LOGI(AAFwkTag::ABILITYMGR, "appStateData.state = %{public}d", appStateData.state);
56             return;
57         }
58         extensionRecord->UnloadUIExtensionAbility();
59     } else {
60         TAG_LOGW(AAFwkTag::ABILITYMGR, "extensionRecord null");
61     }
62 }
63 } // namespace AAFwk
64 } // namespace OHOS