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