1 /*
2  * Copyright (c) 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 #ifndef DLP_PERMISSION_SERVICE_SERVICES_DLP_PERMISSION_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
17 #define DLP_PERMISSION_SERVICE_SERVICES_DLP_PERMISSION_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
18 
19 #include <unordered_map>
20 #include <mutex>
21 #include "application_state_observer_stub.h"
22 #include "app_mgr_proxy.h"
23 #include "dlp_sandbox_info.h"
24 #include "iremote_object.h"
25 #include "retention_file_manager.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace DlpPermission {
30 using OHOS::AppExecFwk::RunningProcessInfo;
31 class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
32 public:
33     explicit AppStateObserver();
34     virtual ~AppStateObserver();
35 
36     void OnProcessDied(const AppExecFwk::ProcessData& processData) override;
37     int32_t QueryDlpFileCopyableByTokenId(bool& copyable, uint32_t tokenId);
38     int32_t QueryDlpFileAccessByUid(DLPFileAccess& dlpFileAccess, int32_t uid);
39     int32_t IsInDlpSandbox(bool& inSandbox, int32_t uid);
40     void AddDlpSandboxInfo(const DlpSandboxInfo& appInfo);
41     uint32_t EraseDlpSandboxInfo(int uid);
42     bool CheckSandboxInfo(const std::string& bundleName, int32_t appIndex, int32_t userId);
43     void DumpSandbox(int fd);
44     void ExitSaAfterAllDlpManagerDie();
45     void GetOpeningReadOnlySandbox(const std::string& bundleName, int32_t userId, int32_t& appIndex);
46     void AddCallbackListener(int32_t pid);
47     bool RemoveCallbackListener(int32_t pid);
48     bool CallbackListenerEmpty();
49     bool GetSandboxInfo(int32_t uid, DlpSandboxInfo& appInfo);
50     void UpdatReadFlag(int32_t uid);
51     bool GetOpeningSandboxInfo(const std::string& bundleName, const std::string& uri,
52         int32_t userId, SandboxInfo& sandboxInfo);
53     void SetAppProxy(const sptr<AppExecFwk::AppMgrProxy>& appProxy);
54 private:
55     void UninstallDlpSandbox(DlpSandboxInfo& appInfo);
56     void UninstallAllDlpSandboxForUser(int32_t userId);
57     void UninstallAllDlpSandbox();
58 
59     void EraseUserId(int32_t userId);
60     void AddUserId(int32_t userId);
61 
62     void AddSandboxInfo(const DlpSandboxInfo& appInfo);
63     void EraseSandboxInfo(int32_t uid);
64 
65     void AddUidWithTokenId(uint32_t tokenId, int32_t uid);
66     bool GetUidByTokenId(uint32_t tokenId, int32_t& uid);
67     void EraseUidTokenIdMap(uint32_t tokenId);
68     bool GetRunningProcessesInfo(std::vector<RunningProcessInfo>& infoVec);
69 
70     std::unordered_map<uint32_t, int32_t> tokenIdToUidMap_;
71     std::mutex tokenIdToUidMapLock_;
72     std::unordered_map<int32_t, DlpSandboxInfo> sandboxInfo_;
73     std::mutex sandboxInfoLock_;
74     std::set<int32_t> userIdList_;
75     std::mutex userIdListLock_;
76     std::map<int32_t, int32_t> callbackList_;
77     std::mutex callbackListLock_;
78     sptr<AppExecFwk::AppMgrProxy> appProxy_ = nullptr;
79 };
80 }  // namespace DlpPermission
81 }  // namespace Security
82 }  // namespace OHOS
83 
84 #endif  // DLP_PERMISSION_SERVICE_SERVICES_DLP_PERMISSION_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
85