• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TEMP_PERMISSION_OBSERVER_H
17 #define TEMP_PERMISSION_OBSERVER_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <string>
22 
23 #include "access_token.h"
24 #ifdef EVENTHANDLER_ENABLE
25 #include "access_event_handler.h"
26 #endif
27 #include "app_manager_death_callback.h"
28 #include "app_manager_death_recipient.h"
29 #include "app_status_change_callback.h"
30 #include "permission_manager.h"
31 
32 namespace OHOS {
33 namespace Security {
34 namespace AccessToken {
35 class PermissionAppStateObserver : public ApplicationStateObserverStub {
36 public:
37     PermissionAppStateObserver() = default;
38     ~PermissionAppStateObserver() = default;
39 
40     void OnForegroundApplicationChanged(const AppStateData &appStateData) override;
41     void OnProcessDied(const ProcessData &processData) override;
42     DISALLOW_COPY_AND_MOVE(PermissionAppStateObserver);
43 
44 private:
45     std::mutex taskMutex_;
46     std::vector<std::string> taskName_;
47 };
48 
49 class PermissionAppManagerDeathCallback : public AppManagerDeathCallback {
50 public:
51     PermissionAppManagerDeathCallback() = default;
52     ~PermissionAppManagerDeathCallback() = default;
53 
54     void NotifyAppManagerDeath() override;
55     DISALLOW_COPY_AND_MOVE(PermissionAppManagerDeathCallback);
56 };
57 
58 class TempPermissionObserver {
59 public:
60     static TempPermissionObserver& GetInstance();
61     TempPermissionObserver();
62     virtual ~TempPermissionObserver();
63 
64     void OnAppMgrRemoteDiedHandle();
65 
66     void AddTempPermTokenToList(AccessTokenID tokenID, const std::string& permissionName);
67     void DeleteTempPermFromList(AccessTokenID tokenID, const std::string& permissionName);
68     void RevokeAllTempPermission(AccessTokenID tokenID);
69     bool GetPermissionStateFull(AccessTokenID tokenID, std::vector<PermissionStateFull>& permissionStateFullList);
70 
71 #ifdef EVENTHANDLER_ENABLE
72     void InitEventHandler(const std::shared_ptr<AccessEventHandler>& eventHandler);
73 #endif
74     bool DelayRevokePermission(AccessToken::AccessTokenID tokenId, const std::string& taskName);
75     bool CancleTaskOfPermissionRevoking(const std::string& taskName);
76     void RegisterCallback();
77 
78 private:
79 #ifdef EVENTHANDLER_ENABLE
80     std::shared_ptr<AccessEventHandler> eventHandler_;
81 #endif
82 
83     std::mutex tempPermissionMutex_;
84     std::vector<AccessTokenID> tempPermTokenList_;
85 
86     // appState
87     sptr<PermissionAppStateObserver> appStateCallback_ = nullptr;
88 
89     // app manager death
90     std::shared_ptr<PermissionAppManagerDeathCallback> appManagerDeathCallback_ = nullptr;
91     DISALLOW_COPY_AND_MOVE(TempPermissionObserver);
92 };
93 } // namespace AccessToken
94 } // namespace Security
95 } // namespace OHOS
96 #endif // TEMP_PERMISSION_OBSERVER_H
97