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 //
16
17 #include "update_engine/update_manager/real_time_provider.h"
18
19 #include <string>
20
21 #include <base/time/time.h>
22
23 #include "update_engine/common/clock_interface.h"
24
25 using base::Time;
26 using base::TimeDelta;
27 using chromeos_update_engine::ClockInterface;
28 using std::string;
29
30 namespace chromeos_update_manager {
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.
CurrDateVariable(const string & name,ClockInterface * clock)37 CurrDateVariable(const string& name, ClockInterface* clock)
38 : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {}
39
40 protected:
GetValue(TimeDelta,string *)41 virtual const Time* GetValue(TimeDelta /* timeout */, string* /* errmsg */) {
42 Time::Exploded now_exp;
43 clock_->GetWallclockTime().LocalExplode(&now_exp);
44 now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0;
45 Time* now = new Time();
46 bool success = Time::FromLocalExploded(now_exp, now);
47 DCHECK(success);
48 return now;
49 }
50
51 private:
52 ClockInterface* clock_;
53
54 DISALLOW_COPY_AND_ASSIGN(CurrDateVariable);
55 };
56
57 // A variable returning the current hour in local time.
58 class CurrHourVariable : public Variable<int> {
59 public:
60 // TODO(garnold) Turn this into an async variable with the needed callback
61 // logic for when it value changes.
CurrHourVariable(const string & name,ClockInterface * clock)62 CurrHourVariable(const string& name, ClockInterface* clock)
63 : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {}
64
65 protected:
GetValue(TimeDelta,string *)66 virtual const int* GetValue(TimeDelta /* timeout */, string* /* errmsg */) {
67 Time::Exploded exploded;
68 clock_->GetWallclockTime().LocalExplode(&exploded);
69 return new int(exploded.hour);
70 }
71
72 private:
73 ClockInterface* clock_;
74
75 DISALLOW_COPY_AND_ASSIGN(CurrHourVariable);
76 };
77
78 class CurrMinuteVariable : public Variable<int> {
79 public:
CurrMinuteVariable(const string & name,ClockInterface * clock)80 CurrMinuteVariable(const string& name, ClockInterface* clock)
81 : Variable<int>(name, TimeDelta::FromSeconds(15)), clock_(clock) {}
82
83 protected:
GetValue(TimeDelta,string *)84 virtual const int* GetValue(TimeDelta /* timeout */, string* /* errmsg */) {
85 Time::Exploded exploded;
86 clock_->GetWallclockTime().LocalExplode(&exploded);
87 return new int(exploded.minute);
88 }
89
90 private:
91 ClockInterface* clock_;
92
93 DISALLOW_COPY_AND_ASSIGN(CurrMinuteVariable);
94 };
95
Init()96 bool RealTimeProvider::Init() {
97 var_curr_date_.reset(new CurrDateVariable("curr_date", clock_));
98 var_curr_hour_.reset(new CurrHourVariable("curr_hour", clock_));
99 var_curr_minute_.reset(new CurrMinuteVariable("curr_minute", clock_));
100 return true;
101 }
102
103 } // namespace chromeos_update_manager
104