Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #include "update_engine/update_manager/real_time_provider.h" |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 9 | #include <base/time/time.h> |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 10 | |
| 11 | #include "update_engine/clock_interface.h" |
| 12 | |
| 13 | using base::Time; |
| 14 | using base::TimeDelta; |
| 15 | using chromeos_update_engine::ClockInterface; |
| 16 | using std::string; |
| 17 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 18 | namespace chromeos_update_manager { |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 19 | |
| 20 | // A variable returning the current date. |
| 21 | class CurrDateVariable : public Variable<Time> { |
| 22 | public: |
| 23 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 24 | // logic for when it value changes. |
| 25 | CurrDateVariable(const string& name, ClockInterface* clock) |
| 26 | : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {} |
| 27 | |
| 28 | protected: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 29 | virtual const Time* GetValue(TimeDelta /* timeout */, |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 30 | string* /* errmsg */) { |
| 31 | Time::Exploded now_exp; |
| 32 | clock_->GetWallclockTime().LocalExplode(&now_exp); |
| 33 | now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0; |
| 34 | return new Time(Time::FromLocalExploded(now_exp)); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | ClockInterface* clock_; |
| 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(CurrDateVariable); |
| 41 | }; |
| 42 | |
| 43 | // A variable returning the current hour in local time. |
| 44 | class CurrHourVariable : public Variable<int> { |
| 45 | public: |
| 46 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 47 | // logic for when it value changes. |
| 48 | CurrHourVariable(const string& name, ClockInterface* clock) |
| 49 | : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {} |
| 50 | |
| 51 | protected: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 52 | virtual const int* GetValue(TimeDelta /* timeout */, |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 53 | string* /* errmsg */) { |
| 54 | Time::Exploded exploded; |
| 55 | clock_->GetWallclockTime().LocalExplode(&exploded); |
| 56 | return new int(exploded.hour); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | ClockInterface* clock_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(CurrHourVariable); |
| 63 | }; |
| 64 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 65 | bool RealTimeProvider::Init() { |
David Zeuthen | 21716e2 | 2014-04-23 15:42:05 -0700 | [diff] [blame] | 66 | var_curr_date_.reset(new CurrDateVariable("curr_date", clock_)); |
| 67 | var_curr_hour_.reset(new CurrHourVariable("curr_hour", clock_)); |
Gilad Arnold | 78a7811 | 2014-03-13 14:58:06 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 71 | } // namespace chromeos_update_manager |