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 #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_KEY_INFO_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_KEY_INFO_H
18 
19 #include <string>
20 
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
23 class KeyInfo {
24 public:
25     KeyInfo(const std::string& pkg, const int32_t& uid, const int32_t& pid = -1): pkg_(pkg), uid_(uid), pid_(pid) {}
26     ~KeyInfo() = default;
27 
28     inline bool operator<(const KeyInfo &o) const
29     {
30         return (pkg_ < o.GetPkg()) || ((pkg_ == o.GetPkg()) && (uid_ < o.GetUid()));
31     }
32 
IsEqual(const KeyInfo & other)33     inline bool IsEqual(const KeyInfo& other) const
34     {
35         return (other.GetPkg() == pkg_ && other.GetUid() == uid_);
36     }
37 
IsEqual(const std::string & pkg,const int32_t & uid)38     inline bool IsEqual(const std::string& pkg, const int32_t& uid) const
39     {
40         return (pkg == pkg_ && uid == uid_);
41     }
42 
GetPkg()43     inline const std::string& GetPkg() const
44     {
45         return pkg_;
46     }
47 
GetUid()48     inline int32_t GetUid() const
49     {
50         return uid_;
51     }
52 
GetPid()53     inline int32_t GetPid() const
54     {
55         return pid_;
56     }
57 
ToString()58     const std::string ToString() const
59     {
60         return "pkg: " + pkg_ + " uid: " + std::to_string(uid_) + " pid: " + std::to_string(pid_);
61     }
62 
63 private:
64     std::string pkg_ {""};
65     int32_t uid_ {-1};
66     int32_t pid_ {-1};
67 };
68 
69 struct KeyInfoComp {
operatorKeyInfoComp70     bool operator()(const std::shared_ptr<KeyInfo> x, const std::shared_ptr<KeyInfo> y) const
71     {
72         if (x == nullptr || y == nullptr) {
73             return false;
74         }
75         return (x->GetPkg() < y->GetPkg()) || ((x->GetPkg() == y->GetPkg()) && (x->GetUid() < y->GetUid()));
76     }
77 };
78 }  // namespace BackgroundTaskMgr
79 }  // namespace OHOS
80 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_KEY_INFO_H