Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/real_time_provider.h" |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/time/time.h> |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 22 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 23 | #include "update_engine/common/system_state.h" |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 24 | |
| 25 | using base::Time; |
| 26 | using base::TimeDelta; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 27 | using chromeos_update_engine::SystemState; |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 28 | using std::string; |
| 29 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | namespace chromeos_update_manager { |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 31 | |
| 32 | // A variable returning the current date. |
| 33 | class CurrDateVariable : public Variable<Time> { |
| 34 | public: |
| 35 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 36 | // logic for when it value changes. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 37 | explicit CurrDateVariable(const string& name) |
| 38 | : Variable<Time>(name, TimeDelta::FromHours(1)) {} |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 39 | |
| 40 | protected: |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 41 | virtual const Time* GetValue(TimeDelta /* timeout */, string* /* errmsg */) { |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 42 | Time::Exploded now_exp; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 43 | SystemState::Get()->clock()->GetWallclockTime().LocalExplode(&now_exp); |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 44 | now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0; |
Hidehiko Abe | eab915e | 2018-09-23 16:54:06 +0900 | [diff] [blame] | 45 | Time* now = new Time(); |
| 46 | bool success = Time::FromLocalExploded(now_exp, now); |
| 47 | DCHECK(success); |
| 48 | return now; |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | private: |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(CurrDateVariable); |
| 53 | }; |
| 54 | |
| 55 | // A variable returning the current hour in local time. |
| 56 | class CurrHourVariable : public Variable<int> { |
| 57 | public: |
| 58 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 59 | // logic for when it value changes. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 60 | explicit CurrHourVariable(const string& name) |
| 61 | : Variable<int>(name, TimeDelta::FromMinutes(5)) {} |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 62 | |
| 63 | protected: |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 64 | virtual const int* GetValue(TimeDelta /* timeout */, string* /* errmsg */) { |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 65 | Time::Exploded exploded; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 66 | SystemState::Get()->clock()->GetWallclockTime().LocalExplode(&exploded); |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 67 | return new int(exploded.hour); |
| 68 | } |
| 69 | |
| 70 | private: |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 71 | DISALLOW_COPY_AND_ASSIGN(CurrHourVariable); |
| 72 | }; |
| 73 | |
Adolfo Victoria | 9614350 | 2018-06-13 18:44:49 -0700 | [diff] [blame] | 74 | class CurrMinuteVariable : public Variable<int> { |
| 75 | public: |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 76 | explicit CurrMinuteVariable(const string& name) |
| 77 | : Variable<int>(name, TimeDelta::FromSeconds(15)) {} |
Adolfo Victoria | 9614350 | 2018-06-13 18:44:49 -0700 | [diff] [blame] | 78 | |
| 79 | protected: |
| 80 | virtual const int* GetValue(TimeDelta /* timeout */, string* /* errmsg */) { |
| 81 | Time::Exploded exploded; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 82 | SystemState::Get()->clock()->GetWallclockTime().LocalExplode(&exploded); |
Adolfo Victoria | 9614350 | 2018-06-13 18:44:49 -0700 | [diff] [blame] | 83 | return new int(exploded.minute); |
| 84 | } |
| 85 | |
| 86 | private: |
Adolfo Victoria | 9614350 | 2018-06-13 18:44:49 -0700 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(CurrMinuteVariable); |
| 88 | }; |
| 89 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 90 | bool RealTimeProvider::Init() { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 91 | var_curr_date_.reset(new CurrDateVariable("curr_date")); |
| 92 | var_curr_hour_.reset(new CurrHourVariable("curr_hour")); |
| 93 | var_curr_minute_.reset(new CurrMinuteVariable("curr_minute")); |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 97 | } // namespace chromeos_update_manager |