OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TODO(vlaviano): update accessor style (GetFoo/SetFoo -> foo) | 5 // TODO(vlaviano): update accessor style (GetFoo/SetFoo -> foo) |
6 | 6 |
7 #ifndef SRC_DATA_PLAN_H_ | 7 #ifndef SRC_DATA_PLAN_H_ |
8 #define SRC_DATA_PLAN_H_ | 8 #define SRC_DATA_PLAN_H_ |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include <base/basictypes.h> // NOLINT | 14 #include <base/basictypes.h> // NOLINT |
15 #include <base/time.h> // NOLINT | 15 #include <base/time.h> // NOLINT |
16 #include <dbus-c++/dbus.h> // NOLINT | 16 #include <dbus-c++/dbus.h> // NOLINT |
17 | 17 |
18 #include "src/values.h" | 18 #include "src/values.h" |
19 | 19 |
20 namespace cashew { | 20 namespace cashew { |
21 | 21 |
22 class DataPlan; | 22 class DataPlan; |
| 23 class Device; |
23 class Policy; | 24 class Policy; |
| 25 class Service; |
| 26 class ServiceManager; |
24 | 27 |
25 // byte count | 28 // byte count |
26 // TODO(vlaviano): this should be a uint64, but libcros/Chrome expects this | 29 // TODO(vlaviano): this should be a uint64, but libcros/Chrome expects this |
27 typedef int64 Bytes; | 30 typedef int64 ByteCount; |
28 | 31 |
29 // List of DataPlan objects | 32 // List of DataPlan objects |
30 // TODO(vlaviano): consider promoting this from a typedef to a class | 33 // TODO(vlaviano): consider promoting this from a typedef to a class |
31 typedef std::vector<DataPlan*> DataPlanList; | 34 typedef std::vector<DataPlan*> DataPlanList; |
32 | 35 |
33 // DataPlan object formatted for D-Bus (using DBus::Variants to wrap fields) | 36 // DataPlan object formatted for D-Bus (using DBus::Variants to wrap fields) |
34 typedef std::map<std::string, DBus::Variant> DBusDataPlan; | 37 typedef std::map<std::string, DBus::Variant> DBusDataPlan; |
35 | 38 |
36 // List of DataPlan objects formatted for D-Bus | 39 // List of DataPlan objects formatted for D-Bus |
37 typedef std::vector<DBusDataPlan> DBusDataPlanList; | 40 typedef std::vector<DBusDataPlan> DBusDataPlanList; |
38 | 41 |
39 // Represents a data plan associated with a cellular service | 42 // Represents a data plan associated with a cellular service |
40 class DataPlan { | 43 class DataPlan { |
41 public: | 44 public: |
42 // valid type values for this plan | 45 // valid type values for this plan |
43 typedef enum { | 46 typedef enum { |
44 kTypeUnlimited = 0, | 47 kTypeUnlimited = 0, |
45 kTypeMeteredFree, | 48 kTypeMeteredFree, |
46 kTypeMeteredPaid, | 49 kTypeMeteredPaid, |
47 } Type; | 50 } Type; |
48 | 51 |
49 // |data_bytes_max| must be set to 0 for unlimited plans | 52 // |data_bytes_max| must be set to 0 for unlimited plans |
50 DataPlan(const std::string& name, DataPlan::Type type, | 53 DataPlan(const std::string& name, DataPlan::Type type, |
51 base::Time update_time, base::Time start_time, | 54 base::Time update_time, base::Time start_time, |
52 base::Time end_time, Bytes data_bytes_max, Bytes data_bytes_used); | 55 base::Time end_time, ByteCount data_bytes_max, |
| 56 ByteCount data_bytes_used); |
53 virtual ~DataPlan(); | 57 virtual ~DataPlan(); |
54 | 58 |
55 // get plan name | 59 // get plan name |
56 virtual const std::string& GetName() const; | 60 virtual const std::string& GetName() const; |
57 | 61 |
58 // get plan type | 62 // get plan type |
59 virtual Type GetType() const; | 63 virtual Type GetType() const; |
60 | 64 |
61 // get last update time | 65 // get last update time |
62 virtual base::Time GetUpdateTime() const; | 66 virtual base::Time GetUpdateTime() const; |
63 | 67 |
64 // get plan start time | 68 // get plan start time |
65 virtual base::Time GetStartTime() const; | 69 virtual base::Time GetStartTime() const; |
66 | 70 |
67 // get plan end time | 71 // get plan end time |
68 virtual base::Time GetEndTime() const; | 72 virtual base::Time GetEndTime() const; |
69 | 73 |
70 // get plan max bytes | 74 // get plan max bytes |
71 virtual Bytes GetDataBytesMax() const; | 75 virtual ByteCount GetDataBytesMax() const; |
72 | 76 |
73 // get plan used bytes | 77 // get plan used bytes |
74 virtual Bytes GetDataBytesUsed() const; | 78 virtual ByteCount GetDataBytesUsed() const; |
75 | 79 |
76 // get local used bytes | 80 // get local used bytes |
77 virtual Bytes GetLocalBytesUsed() const; | 81 virtual ByteCount GetLocalBytesUsed() const; |
78 | 82 |
79 // set local used bytes | 83 // set local used bytes |
80 // must be >= 0 | 84 // must be >= 0 |
81 virtual void SetLocalBytesUsed(Bytes local_bytes_used); | 85 virtual void SetLocalBytesUsed(ByteCount local_bytes_used); |
82 | 86 |
83 // get total used bytes | 87 // get total used bytes |
84 virtual Bytes GetTotalBytesUsed() const; | 88 virtual ByteCount GetTotalBytesUsed() const; |
85 | 89 |
86 // is this an active plan? | 90 // is this an active plan? |
87 // we define active to mean that the plan is current and not exhausted | 91 // we define active to mean that the plan is current and not exhausted |
88 virtual bool IsActive() const; | 92 virtual bool IsActive() const; |
89 | 93 |
90 // return a{sv} representation for D-Bus | 94 // return a{sv} representation for D-Bus |
91 virtual DBusDataPlan ToDBusFormat() const; | 95 virtual DBusDataPlan ToDBusFormat() const; |
92 | 96 |
93 // construct DataPlan from DictionaryValue | 97 // construct DataPlan from DictionaryValue |
94 static DataPlan* FromDictionaryValue(const DictionaryValue *value, | 98 static DataPlan* FromDictionaryValue(const DictionaryValue *value, |
95 const Policy *policy); | 99 const Policy *policy); |
96 | 100 |
97 // convert ISO 8601 time value to base::Time | 101 // convert ISO 8601 time value to base::Time |
98 // returns true on success and false on failure | 102 // returns true on success and false on failure |
99 // |time_out| must not be NULL and is set on a successful return | 103 // |time_out| must not be NULL and is set on a successful return |
100 static bool TimeFromIso8601(const std::string& time_8601, | 104 static bool TimeFromIso8601(const std::string& time_8601, |
101 base::Time *time_out, | 105 base::Time *time_out, |
102 bool zoneless_strings_are_local); | 106 bool zoneless_strings_are_local); |
103 | 107 |
104 // returns first active plan in |data_plans| | 108 // returns first active plan in |data_plans| |
105 // returns NULL if list is empty or contains no active plans | 109 // returns NULL if list is empty or contains no active plans |
106 static DataPlan* GetActivePlan(const DataPlanList& data_plans); | 110 static DataPlan* GetActivePlan(const DataPlanList& data_plans); |
107 | 111 |
| 112 // event handler for |service's| local byte counter on |device| |
| 113 // updates active plans in |data_plans| based on the |rx_bytes| and |
| 114 // |tx_bytes| values gathered during the most recent sample interval |
| 115 // notifies |service_manager| if an active plan is completely consumed |
| 116 // returns true if any plans were updated and false otherwise |
| 117 // |data_plans|, |service|, |service_manager| and |device| must not be NULL |
| 118 static bool OnByteCounterUpdate(DataPlanList *data_plans, Service *service, |
| 119 ServiceManager *service_manager, |
| 120 Device *device, uint64 rx_bytes, |
| 121 uint64 tx_bytes); |
| 122 |
108 private: | 123 private: |
109 // human-readable plan name | 124 // human-readable plan name |
110 const std::string name_; | 125 const std::string name_; |
111 | 126 |
112 // plan type | 127 // plan type |
113 const Type type_; | 128 const Type type_; |
114 | 129 |
115 // last update time (i.e., how fresh is plan data) | 130 // last update time (i.e., how fresh is plan data) |
116 // this refers to carrier API data, not local measurements | 131 // this refers to carrier API data, not local measurements |
117 base::Time update_time_; | 132 base::Time update_time_; |
118 | 133 |
119 // plan start time | 134 // plan start time |
120 const base::Time start_time_; | 135 const base::Time start_time_; |
121 | 136 |
122 // plan end time | 137 // plan end time |
123 const base::Time end_time_; | 138 const base::Time end_time_; |
124 | 139 |
125 // max bytes available during plan period | 140 // max bytes available during plan period |
126 // irrelevant for plans of type kTypeUnlimited | 141 // irrelevant for plans of type kTypeUnlimited |
127 const Bytes data_bytes_max_; | 142 const ByteCount data_bytes_max_; |
128 | 143 |
129 // bytes consumed so far during plan period | 144 // bytes consumed so far during plan period |
130 // this measurement comes from the carrier API | 145 // this measurement comes from the carrier API |
131 Bytes data_bytes_used_; | 146 ByteCount data_bytes_used_; |
132 | 147 |
133 // local measurement of bytes used since we received |data_bytes_used_| | 148 // local measurement of bytes used since we received |data_bytes_used_| |
134 // baseline from the carrier API | 149 // baseline from the carrier API |
135 Bytes local_bytes_used_; | 150 ByteCount local_bytes_used_; |
136 | 151 |
137 // our best estimate of total bytes used for this plan | 152 // our best estimate of total bytes used for this plan |
138 // carrier baseline (|data_bytes_used_|) + | 153 // carrier baseline (|data_bytes_used_|) + |
139 // locally measured traffic (|local_bytes_used_|) | 154 // locally measured traffic (|local_bytes_used_|) |
140 Bytes total_bytes_used_; | 155 ByteCount total_bytes_used_; |
141 | 156 |
142 // convert plan type enum value to string formatted for libcros | 157 // convert plan type enum value to string formatted for libcros |
143 const char* TypeToLibcrosString(Type type) const; | 158 const char* TypeToLibcrosString(Type type) const; |
144 | 159 |
145 // convert string formatted for Cros Usage API to plan type enum value | 160 // convert string formatted for Cros Usage API to plan type enum value |
146 // returns true on success and false on failure | 161 // returns true on success and false on failure |
147 // |type_out| must not be NULL and is set on a successful return | 162 // |type_out| must not be NULL and is set on a successful return |
148 static bool CrosUsageStringToType(const std::string& type_string, | 163 static bool CrosUsageStringToType(const std::string& type_string, |
149 Type *type_out); | 164 Type *type_out); |
150 | 165 |
151 // converts the value associated with |key| in |dict| to an int64 | 166 // converts the value associated with |key| in |dict| to an int64 |
152 // returns true on success and false on failure | 167 // returns true on success and false on failure |
153 // |out_value| must not be NULL and is set on a successful return | 168 // |out_value| must not be NULL and is set on a successful return |
154 static bool GetInt64FromDictionary(const DictionaryValue& dict, | 169 static bool GetInt64FromDictionary(const DictionaryValue& dict, |
155 const std::string& key, | 170 const std::string& key, |
156 int64* out_value); | 171 int64* out_value); |
157 | 172 |
158 DISALLOW_COPY_AND_ASSIGN(DataPlan); | 173 DISALLOW_COPY_AND_ASSIGN(DataPlan); |
159 }; | 174 }; |
160 | 175 |
161 } // namespace cashew | 176 } // namespace cashew |
162 | 177 |
163 #endif // SRC_DATA_PLAN_H_ | 178 #endif // SRC_DATA_PLAN_H_ |
OLD | NEW |