• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "app_status_change_callback.h"
17 #include "accesstoken_log.h"
18 #include "permission_record_manager.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25     LOG_CORE, SECURITY_DOMAIN_PRIVACY, "ApplicationStateObserverStub"
26 };
27 }
28 
ApplicationStateObserverStub()29 ApplicationStateObserverStub::ApplicationStateObserverStub()
30 {
31     ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub Instance create");
32 }
33 
~ApplicationStateObserverStub()34 ApplicationStateObserverStub::~ApplicationStateObserverStub()
35 {
36     ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub Instance destroy");
37 }
38 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t ApplicationStateObserverStub::OnRemoteRequest(
40     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42     if (data.ReadInterfaceToken() != GetDescriptor()) {
43         ACCESSTOKEN_LOG_INFO(LABEL, "ApplicationStateObserverStub: ReadInterfaceToken failed");
44         return -1;
45     }
46     switch (static_cast<IApplicationStateObserver::Message>(code)) {
47         case IApplicationStateObserver::Message::TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED: {
48             HandleOnForegroundApplicationChanged(data, reply);
49             return NO_ERROR;
50         }
51         case IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED: {
52             HandleOnProcessDied(data, reply);
53             return NO_ERROR;
54         }
55         default: {
56             ACCESSTOKEN_LOG_DEBUG(LABEL, "default case, need check AudioListenerStub");
57             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58         }
59     }
60     return NO_ERROR;
61 }
62 
HandleOnForegroundApplicationChanged(MessageParcel & data,MessageParcel & reply)63 int32_t ApplicationStateObserverStub::HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply)
64 {
65     std::unique_ptr<AppStateData> processData(data.ReadParcelable<AppStateData>());
66     if (processData == nullptr) {
67         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
68         return -1;
69     }
70 
71     OnForegroundApplicationChanged(*processData);
72     return NO_ERROR;
73 }
74 
HandleOnProcessDied(MessageParcel & data,MessageParcel & reply)75 int32_t ApplicationStateObserverStub::HandleOnProcessDied(MessageParcel &data, MessageParcel &reply)
76 {
77     std::unique_ptr<ProcessData> processData(data.ReadParcelable<ProcessData>());
78     if (processData == nullptr) {
79         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
80         return -1;
81     }
82 
83     OnProcessDied(*processData);
84     return NO_ERROR;
85 }
86 
OnForegroundApplicationChanged(const AppStateData & appStateData)87 void ApplicationStateObserverStub::OnForegroundApplicationChanged(const AppStateData& appStateData)
88 {
89     ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(accessTokenId=%{public}d, state=%{public}d)",
90         appStateData.accessTokenId, appStateData.state);
91 
92     uint32_t tokenId = appStateData.accessTokenId;
93 
94     ActiveChangeType status = PERM_INACTIVE;
95     if (appStateData.state == static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND)) {
96         status = PERM_ACTIVE_IN_FOREGROUND;
97     } else if (appStateData.state == static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND)) {
98         status = PERM_ACTIVE_IN_BACKGROUND;
99     }
100     PermissionRecordManager::GetInstance().NotifyAppStateChange(tokenId, status);
101 }
102 } // namespace AccessToken
103 } // namespace Security
104 } // namespace OHOS
105