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 #ifdef STORAGE_SERVICE_MEDIA_FUSE
16 #include "observer/appstate_observer.h"
17
18 #include "singleton.h"
19 #include "storage_service_constant.h"
20 #include "user/mount_manager.h"
21
22 namespace OHOS {
23 namespace StorageDaemon {
24 using namespace std;
25
26 constexpr int32_t BASE_USER_RANGE = 200000;
27
SubscribeAppState(const std::vector<std::string> & bundleNameList)28 void AppStateObserverManager::SubscribeAppState(const std::vector<std::string> &bundleNameList)
29 {
30 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
31 if (appMgrClient == nullptr) {
32 LOGE("appMgrClient_ is nullptr");
33 return;
34 }
35 if (appStateObserver_ != nullptr) {
36 LOGI("appStateObserver has been registed");
37 return;
38 }
39 appStateObserver_ = sptr<AppStateObserver>(new (std::nothrow) AppStateObserver());
40 if (appStateObserver_ == nullptr) {
41 LOGI("get appStateObserver failed");
42 return;
43 }
44
45 int32_t result = appMgrClient->RegisterApplicationStateObserver(appStateObserver_, bundleNameList);
46 if (result != E_OK) {
47 LOGE("RegistApplicationStateObserver failed result = %{public}d", result);
48 appStateObserver_ = nullptr;
49 }
50 }
51
UnSubscribeAppState()52 void AppStateObserverManager::UnSubscribeAppState()
53 {
54 if (appStateObserver_ == nullptr) {
55 LOGI("appStateObserver is nullptr");
56 return;
57 }
58
59 auto appMgrClient = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance();
60 if (appMgrClient == nullptr) {
61 LOGE("appMgrClient_ is nullptr");
62 return;
63 }
64 int32_t result = appMgrClient->UnregisterApplicationStateObserver(appStateObserver_);
65 if (result != E_OK) {
66 LOGE("UnregisterApplicationStateObserver failed result = %{public}d", result);
67 }
68 appStateObserver_ = nullptr;
69 }
70
GetInstance()71 AppStateObserverManager &AppStateObserverManager::GetInstance()
72 {
73 static AppStateObserverManager instance;
74 return instance;
75 }
76
OnAppStopped(const AppStateData & appStateData)77 void AppStateObserver::OnAppStopped(const AppStateData &appStateData)
78 {
79 if (appStateData.bundleName == MEDIALIBRARY_NAME) {
80 LOGI("StorageDaemon OnAppStopped start %{public}d", appStateData.uid);
81 int32_t userId = appStateData.uid / BASE_USER_RANGE;
82 list<string> killList = { MEDIA_FUSE_EL2 };
83 MountManager::GetInstance()->FindAndKillProcessWithoutRadar(userId, killList);
84 }
85 }
86 } // namespace StorageDaemon
87 } // namespace OHOS
88 #endif // STORAGE_SERVICE_MEDIA_FUSE