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 POWERMGR_RUNNING_LOCK_PROXY_H
17 #define POWERMGR_RUNNING_LOCK_PROXY_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include <iremote_object.h>
23 #include <functional>
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 enum class RunningLockEvent : uint32_t {
28     RUNNINGLOCK_UPDATE = 0,
29     RUNNINGLOCK_PROXY,
30     RUNNINGLOCK_UNPROXY
31 };
32 class RunningLockProxy {
33 public:
34     using WksMap = std::map<int32_t, std::pair<std::string, bool>>;
35     using TokenWorkSourceMap = std::map<const sptr<IRemoteObject>,
36         std::pair<WksMap, int32_t>>;
37     using RunningLockProxyMap = std::map<std::string, TokenWorkSourceMap>;
38     RunningLockProxy() = default;
39     ~RunningLockProxy() = default;
40 
41     void AddRunningLock(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj);
42     void RemoveRunningLock(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj);
43     bool UpdateWorkSource(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj,
44         const std::map<int32_t, std::string>& workSources);
45     bool IncreaseProxyCnt(pid_t pid, pid_t uid);
46     bool DecreaseProxyCnt(pid_t pid, pid_t uid);
47     void Clear();
48     std::string DumpProxyInfo();
49     void ResetRunningLocks();
50     bool UpdateProxyState(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj, bool state);
51 private:
52     std::string AssembleProxyKey(pid_t pid, pid_t uid);
53     void ProxyInner(const sptr<IRemoteObject>& remoteObj, const std::string& bundleNames, RunningLockEvent event);
54     std::string GetRunningLockName(const sptr<IRemoteObject>& remoteObj);
55     std::string MergeBundleName(const WksMap& wksMap);
56     RunningLockProxyMap proxyMap_;
57 };
58 } // namespace PowerMgr
59 } // namespace OHOS
60 #endif // POWERMGR_RUNNING_LOCK_PROXY_H
61 
62