• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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         default: {
52             ACCESSTOKEN_LOG_DEBUG(LABEL, "default case, need check AudioListenerStub");
53             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54         }
55     }
56     return NO_ERROR;
57 }
58 
HandleOnForegroundApplicationChanged(MessageParcel & data,MessageParcel & reply)59 int32_t ApplicationStateObserverStub::HandleOnForegroundApplicationChanged(MessageParcel &data, MessageParcel &reply)
60 {
61     std::unique_ptr<AppStateData> processData(data.ReadParcelable<AppStateData>());
62     if (processData == nullptr) {
63         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable failed");
64         return -1;
65     }
66 
67     OnForegroundApplicationChanged(*processData);
68     return NO_ERROR;
69 }
70 
OnForegroundApplicationChanged(const AppStateData & appStateData)71 void ApplicationStateObserverStub::OnForegroundApplicationChanged(const AppStateData& appStateData)
72 {
73     ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(accessTokenId=%{public}d, state=%{public}d)",
74         appStateData.accessTokenId, appStateData.state);
75 
76     uint32_t tokenId = appStateData.accessTokenId;
77 
78     ActiveChangeType status = PERM_INACTIVE;
79     if (appStateData.state == static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND)) {
80         status = PERM_ACTIVE_IN_FOREGROUND;
81     } else if (appStateData.state == static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND)) {
82         status = PERM_ACTIVE_IN_BACKGROUND;
83     }
84     PermissionRecordManager::GetInstance().NotifyAppStateChange(tokenId, status);
85 }
86 } // namespace AccessToken
87 } // namespace Security
88 } // namespace OHOS