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 | |
| 5 | #include "update_engine/policy_manager/real_time_provider.h" |
| 6 | |
| 7 | #include <base/time.h> |
| 8 | |
| 9 | #include "update_engine/clock_interface.h" |
| 10 | |
| 11 | using base::Time; |
| 12 | using base::TimeDelta; |
| 13 | using chromeos_update_engine::ClockInterface; |
| 14 | using std::string; |
| 15 | |
| 16 | namespace chromeos_policy_manager { |
| 17 | |
| 18 | // A variable returning the current date. |
| 19 | class CurrDateVariable : public Variable<Time> { |
| 20 | public: |
| 21 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 22 | // logic for when it value changes. |
| 23 | CurrDateVariable(const string& name, ClockInterface* clock) |
| 24 | : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {} |
| 25 | |
| 26 | protected: |
| 27 | virtual const Time* GetValue(base::TimeDelta /* timeout */, |
| 28 | string* /* errmsg */) { |
| 29 | Time::Exploded now_exp; |
| 30 | clock_->GetWallclockTime().LocalExplode(&now_exp); |
| 31 | now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0; |
| 32 | return new Time(Time::FromLocalExploded(now_exp)); |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | ClockInterface* clock_; |
| 37 | |
| 38 | DISALLOW_COPY_AND_ASSIGN(CurrDateVariable); |
| 39 | }; |
| 40 | |
| 41 | // A variable returning the current hour in local time. |
| 42 | class CurrHourVariable : public Variable<int> { |
| 43 | public: |
| 44 | // TODO(garnold) Turn this into an async variable with the needed callback |
| 45 | // logic for when it value changes. |
| 46 | CurrHourVariable(const string& name, ClockInterface* clock) |
| 47 | : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {} |
| 48 | |
| 49 | protected: |
| 50 | virtual const int* GetValue(base::TimeDelta /* timeout */, |
| 51 | string* /* errmsg */) { |
| 52 | Time::Exploded exploded; |
| 53 | clock_->GetWallclockTime().LocalExplode(&exploded); |
| 54 | return new int(exploded.hour); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | ClockInterface* clock_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(CurrHourVariable); |
| 61 | }; |
| 62 | |
| 63 | bool RealTimeProvider::DoInit() { |
| 64 | set_var_curr_date(new CurrDateVariable("curr_date", clock_)); |
| 65 | set_var_curr_hour(new CurrHourVariable("curr_hour", clock_)); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | } // namespace chromeos_policy_manager |