| 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 #include "src/data_plan.h" | 5 #include "src/data_plan.h" |
| 6 | 6 |
| 7 #include <time.h> | 7 #include <time.h> |
| 8 | 8 |
| 9 #include <glog/logging.h> | 9 #include <glog/logging.h> |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 static const char *kCrosUsageDataPlanTypeUnlimited = "UNLIMITED"; | 41 static const char *kCrosUsageDataPlanTypeUnlimited = "UNLIMITED"; |
| 42 static const char *kCrosUsageDataPlanTypeMeteredFree = "METERED_FREE"; | 42 static const char *kCrosUsageDataPlanTypeMeteredFree = "METERED_FREE"; |
| 43 static const char *kCrosUsageDataPlanTypeMeteredPaid = "METERED_PAID"; | 43 static const char *kCrosUsageDataPlanTypeMeteredPaid = "METERED_PAID"; |
| 44 | 44 |
| 45 DataPlan::DataPlan(const std::string& name, DataPlan::Type type, | 45 DataPlan::DataPlan(const std::string& name, DataPlan::Type type, |
| 46 base::Time update_time, base::Time start_time, | 46 base::Time update_time, base::Time start_time, |
| 47 base::Time end_time, Bytes data_bytes_max, | 47 base::Time end_time, Bytes data_bytes_max, |
| 48 Bytes data_bytes_used) | 48 Bytes data_bytes_used) |
| 49 : name_(name), type_(type), update_time_(update_time), | 49 : name_(name), type_(type), update_time_(update_time), |
| 50 start_time_(start_time), end_time_(end_time), | 50 start_time_(start_time), end_time_(end_time), |
| 51 data_bytes_max_(data_bytes_max), data_bytes_used_(data_bytes_used) { | 51 data_bytes_max_(data_bytes_max), data_bytes_used_(data_bytes_used), |
| 52 local_bytes_used_(0) { |
| 52 } | 53 } |
| 53 | 54 |
| 54 DataPlan::~DataPlan() { | 55 DataPlan::~DataPlan() { |
| 55 } | 56 } |
| 56 | 57 |
| 57 const std::string& DataPlan::GetName() const { | 58 const std::string& DataPlan::GetName() const { |
| 58 return name_; | 59 return name_; |
| 59 } | 60 } |
| 60 | 61 |
| 61 DataPlan::Type DataPlan::GetType() const { | 62 DataPlan::Type DataPlan::GetType() const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 75 } | 76 } |
| 76 | 77 |
| 77 Bytes DataPlan::GetDataBytesMax() const { | 78 Bytes DataPlan::GetDataBytesMax() const { |
| 78 return data_bytes_max_; | 79 return data_bytes_max_; |
| 79 } | 80 } |
| 80 | 81 |
| 81 Bytes DataPlan::GetDataBytesUsed() const { | 82 Bytes DataPlan::GetDataBytesUsed() const { |
| 82 return data_bytes_used_; | 83 return data_bytes_used_; |
| 83 } | 84 } |
| 84 | 85 |
| 86 Bytes DataPlan::GetLocalBytesUsed() const { |
| 87 return local_bytes_used_; |
| 88 } |
| 89 |
| 90 void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) { |
| 91 CHECK_GE(local_bytes_used, 0); |
| 92 local_bytes_used_ = local_bytes_used; |
| 93 } |
| 94 |
| 95 bool DataPlan::IsActive() const { |
| 96 // is the plan current? |
| 97 base::Time now = base::Time::Now(); |
| 98 if (now < start_time_ || now >= end_time_) { |
| 99 return false; |
| 100 } |
| 101 // is the plan exhausted? |
| 102 if (type_ != kTypeUnlimited && data_bytes_used_ >= data_bytes_max_) { |
| 103 return false; |
| 104 } |
| 105 return true; |
| 106 } |
| 107 |
| 85 DBusDataPlan DataPlan::ToDBusFormat() const { | 108 DBusDataPlan DataPlan::ToDBusFormat() const { |
| 86 DBusDataPlan plan; | 109 DBusDataPlan plan; |
| 87 // Indexing into map w/ nonexistent key causes empty DBus::Variant to be | 110 // Indexing into map w/ nonexistent key causes empty DBus::Variant to be |
| 88 // created and inserted. We then fill in the desired value via its writer | 111 // created and inserted. We then fill in the desired value via its writer |
| 89 // DBus::MessageIter. | 112 // DBus::MessageIter. |
| 90 plan[kCellularDataPlanName].writer().append_string(name_.c_str()); | 113 plan[kCellularDataPlanName].writer().append_string(name_.c_str()); |
| 91 plan[kCellularDataPlanType].writer().append_string( | 114 plan[kCellularDataPlanType].writer().append_string( |
| 92 TypeToLibcrosString(type_)); | 115 TypeToLibcrosString(type_)); |
| 93 plan[kCellularDataPlanUpdateTime].writer().append_int64( | 116 plan[kCellularDataPlanUpdateTime].writer().append_int64( |
| 94 update_time_.ToInternalValue()); | 117 update_time_.ToInternalValue()); |
| 95 plan[kCellularDataPlanStartTime].writer().append_int64( | 118 plan[kCellularDataPlanStartTime].writer().append_int64( |
| 96 start_time_.ToInternalValue()); | 119 start_time_.ToInternalValue()); |
| 97 plan[kCellularDataPlanEndTime].writer().append_int64( | 120 plan[kCellularDataPlanEndTime].writer().append_int64( |
| 98 end_time_.ToInternalValue()); | 121 end_time_.ToInternalValue()); |
| 99 // omit max bytes field for unlimited plans | 122 // omit max bytes field for unlimited plans |
| 100 if (type_ != kTypeUnlimited) { | 123 if (type_ != kTypeUnlimited) { |
| 101 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_); | 124 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_); |
| 102 } | 125 } |
| 103 // always send used bytes field, even if we had to assume a value of 0 | 126 // send our best estimate of data bytes used |
| 104 // because this is an unlimited plan and the property was absent in the | 127 // this is the baseline, if any, that we received from the carrier usage API |
| 105 // usage API replies that we received | 128 // plus any local traffic that we've measured since then |
| 106 // TODO(vlaviano): we'll be able to do better when we have local counters | 129 Bytes total_bytes_used = data_bytes_used_ + local_bytes_used_; |
| 107 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(data_bytes_used_); | 130 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used); |
| 108 return plan; | 131 return plan; |
| 109 } | 132 } |
| 110 | 133 |
| 111 // static | 134 // static |
| 112 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value, | 135 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value, |
| 113 const Policy *policy) { | 136 const Policy *policy) { |
| 114 CHECK_NOTNULL(value); | 137 CHECK_NOTNULL(value); |
| 115 CHECK_NOTNULL(policy); | 138 CHECK_NOTNULL(policy); |
| 116 std::string last_update_iso8601; | 139 std::string last_update_iso8601; |
| 117 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty, | 140 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 DLOG(WARNING) << "TimeFromIso8601: mktime failed"; | 284 DLOG(WARNING) << "TimeFromIso8601: mktime failed"; |
| 262 return false; | 285 return false; |
| 263 } | 286 } |
| 264 } | 287 } |
| 265 | 288 |
| 266 // convert from time_t to base::Time | 289 // convert from time_t to base::Time |
| 267 *time_out = base::Time::FromTimeT(tt); | 290 *time_out = base::Time::FromTimeT(tt); |
| 268 return true; | 291 return true; |
| 269 } | 292 } |
| 270 | 293 |
| 294 // static |
| 295 DataPlan* DataPlan::GetActivePlan(const DataPlanList& data_plans) { |
| 296 DataPlanList::const_iterator it; |
| 297 for (it = data_plans.begin(); it != data_plans.end(); ++it) { |
| 298 DataPlan *plan = *it; |
| 299 DCHECK(plan != NULL); |
| 300 if (plan->IsActive()) { |
| 301 return plan; |
| 302 } |
| 303 } |
| 304 return NULL; |
| 305 } |
| 306 |
| 271 // private methods | 307 // private methods |
| 272 | 308 |
| 273 const char* DataPlan::TypeToLibcrosString(DataPlan::Type type) const { | 309 const char* DataPlan::TypeToLibcrosString(DataPlan::Type type) const { |
| 274 switch (type) { | 310 switch (type) { |
| 275 case kTypeUnlimited: | 311 case kTypeUnlimited: |
| 276 return kCellularDataPlanTypeUnlimited; | 312 return kCellularDataPlanTypeUnlimited; |
| 277 case kTypeMeteredFree: | 313 case kTypeMeteredFree: |
| 278 return kCellularDataPlanTypeMeteredFree; | 314 return kCellularDataPlanTypeMeteredFree; |
| 279 case kTypeMeteredPaid: | 315 case kTypeMeteredPaid: |
| 280 return kCellularDataPlanTypeMeteredPaid; | 316 return kCellularDataPlanTypeMeteredPaid; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 309 *out_value = generic_int_out; | 345 *out_value = generic_int_out; |
| 310 return true; | 346 return true; |
| 311 } | 347 } |
| 312 if (dict.GetInteger64(key, out_value)) { | 348 if (dict.GetInteger64(key, out_value)) { |
| 313 return true; | 349 return true; |
| 314 } | 350 } |
| 315 return false; | 351 return false; |
| 316 } | 352 } |
| 317 | 353 |
| 318 } // namespace cashew | 354 } // namespace cashew |
| OLD | NEW |