1 /*
2  * Copyright (C) 2021-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_H
17 #define POWERMGR_RUNNING_LOCK_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include <iremote_object.h>
23 
24 #include "ipower_mgr.h"
25 #include "running_lock_info.h"
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 class RunningLock {
30 public:
31     RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type);
32     ~RunningLock();
33     DISALLOW_COPY_AND_MOVE(RunningLock);
34 
35     PowerErrors Init();
36     PowerErrors Recover(const wptr<IPowerMgr>& proxy);
37     bool IsUsed();
38 
39     ErrCode UpdateWorkSource(const std::vector<int32_t>& workSources);
40 
41     /**
42      * Acquires a runninglock.
43      * The parameter of last called will replace that of the previous called, and the effective parameter will
44      * be that of the last called. Eg : If call Lock(n) first then Lock(), a lasting lock will be hold, and
45      * need UnLock() to be called to release it. If else call Lock() first and then Lock(n), the parameter 'n'
46      * takes effect and it will be released in n ms automatiocly.
47      * @param timeOutMs timeOutMs this runninglock will be released in timeOutMs milliseconds. If it is called without
48      *                  parameter or parameter is 0 a lasting runninglock will be hold.
49      */
50     ErrCode Lock(int32_t timeOutMs = -1);
51 
52     /**
53      * Release the runninglock, no matter how many times Lock() was called.
54 	 * The release can be done by calling UnLock() only once.
55      */
56     ErrCode UnLock();
57 
58     static constexpr uint32_t MAX_NAME_LEN = 256;
59     static constexpr uint32_t CREATE_WITH_SCREEN_ON = 0x10000000;
60 
61 private:
62     PowerErrors Create();
63     void Release();
64     std::string GetBundleNameByUid(const int32_t uid);
65     std::mutex mutex_;
66     RunningLockInfo runningLockInfo_;
67     sptr<IRemoteObject> token_;
68     wptr<IPowerMgr> proxy_;
69 };
70 } // namespace PowerMgr
71 } // namespace OHOS
72 #endif // POWERMGR_RUNNING_LOCK_H
73