• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 TIMER_INFO_H
17 #define TIMER_INFO_H
18 
19 #include "timer_manager_interface.h"
20 
21 namespace OHOS {
22 namespace MiscServices {
23 
24 class TimerInfo {
25 public:
26     const std::string name;
27     const uint64_t id;
28     const int type;
29     const std::chrono::milliseconds origWhen;
30     const bool wakeup;
31     const bool autoRestore;
32     const std::function<int32_t (const uint64_t)> callback;
33     const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent;
34     const uint32_t flags;
35     const int uid;
36     const int pid;
37 
38     enum TimerState : int {
39         INIT = 0,
40         ADJUST,
41         PROXY,
42     };
43     std::chrono::milliseconds when;
44     std::chrono::milliseconds windowLength;
45     std::chrono::steady_clock::time_point originWhenElapsed;
46     std::chrono::steady_clock::time_point originMaxWhenElapsed;
47     std::chrono::steady_clock::time_point whenElapsed;
48     std::chrono::steady_clock::time_point maxWhenElapsed;
49     std::chrono::milliseconds repeatInterval;
50     std::chrono::milliseconds offset;
51     std::string bundleName;
52     int state;
53 
54     TimerInfo(std::string name, uint64_t id, int type,
55         std::chrono::milliseconds when,
56         std::chrono::steady_clock::time_point whenElapsed,
57         std::chrono::milliseconds windowLength,
58         std::chrono::steady_clock::time_point maxWhen,
59         std::chrono::milliseconds interval,
60         std::function<int32_t (const uint64_t)> callback,
61         std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
62         uint32_t flags,
63         bool autoRestore,
64         int uid,
65         int pid,
66         const std::string &bundleName);
67     virtual ~TimerInfo() = default;
68     bool operator==(const TimerInfo &other) const;
69     static std::shared_ptr<TimerInfo> CreateTimerInfo(std::string _name, uint64_t _id, int _type,
70         uint64_t _triggerAtTime,
71         int64_t _windowLength,
72         uint64_t _interval,
73         uint32_t _flag,
74         bool _autoRestore,
75         std::function<int32_t (const uint64_t)> _callback,
76         std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> _wantAgent,
77         int _uid,
78         int _pid,
79         const std::string &_bundleName);
80     static std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type);
81     static std::chrono::steady_clock::time_point MaxTriggerTime(std::chrono::steady_clock::time_point now,
82         std::chrono::steady_clock::time_point triggerAtTime,
83         std::chrono::milliseconds interval);
84     bool Matches(const std::string &packageName) const;
85     void CalculateWhenElapsed(std::chrono::steady_clock::time_point nowElapsed);
86     void CalculateOriWhenElapsed();
87     bool UpdateWhenElapsedFromNow(std::chrono::steady_clock::time_point now, std::chrono::nanoseconds offset);
88     bool ProxyTimer(const std::chrono::steady_clock::time_point &now, std::chrono::nanoseconds deltaTime);
89     bool RestoreProxyTimer();
90     bool AdjustTimer(const std::chrono::steady_clock::time_point &now, const uint32_t interval, const uint32_t delta);
91     bool RestoreAdjustTimer();
92 private:
93     bool RestoreTimer();
94 };
95 } // MiscService
96 } // OHOS
97 #endif