| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 local_bytes_used_(0), total_bytes_used_(data_bytes_used) { |
| 53 CHECK_GE(data_bytes_max_, 0); |
| 54 CHECK_GE(data_bytes_used_, 0); |
| 53 } | 55 } |
| 54 | 56 |
| 55 DataPlan::~DataPlan() { | 57 DataPlan::~DataPlan() { |
| 56 } | 58 } |
| 57 | 59 |
| 58 const std::string& DataPlan::GetName() const { | 60 const std::string& DataPlan::GetName() const { |
| 59 return name_; | 61 return name_; |
| 60 } | 62 } |
| 61 | 63 |
| 62 DataPlan::Type DataPlan::GetType() const { | 64 DataPlan::Type DataPlan::GetType() const { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 80 } | 82 } |
| 81 | 83 |
| 82 Bytes DataPlan::GetDataBytesUsed() const { | 84 Bytes DataPlan::GetDataBytesUsed() const { |
| 83 return data_bytes_used_; | 85 return data_bytes_used_; |
| 84 } | 86 } |
| 85 | 87 |
| 86 Bytes DataPlan::GetLocalBytesUsed() const { | 88 Bytes DataPlan::GetLocalBytesUsed() const { |
| 87 return local_bytes_used_; | 89 return local_bytes_used_; |
| 88 } | 90 } |
| 89 | 91 |
| 92 Bytes DataPlan::GetTotalBytesUsed() const { |
| 93 return total_bytes_used_; |
| 94 } |
| 95 |
| 90 void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) { | 96 void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) { |
| 91 CHECK_GE(local_bytes_used, 0); | 97 CHECK_GE(local_bytes_used, 0); |
| 92 local_bytes_used_ = local_bytes_used; | 98 local_bytes_used_ = local_bytes_used; |
| 99 total_bytes_used_ = data_bytes_used_ + local_bytes_used_; |
| 100 if (total_bytes_used_ < 0) { |
| 101 LOG(WARNING) << "SetLocalBytesUsed: overflow detected"; |
| 102 total_bytes_used_ = kint64max; |
| 103 } |
| 93 } | 104 } |
| 94 | 105 |
| 95 bool DataPlan::IsActive() const { | 106 bool DataPlan::IsActive() const { |
| 96 // is the plan current? | 107 // is the plan current? |
| 97 base::Time now = base::Time::Now(); | 108 base::Time now = base::Time::Now(); |
| 98 if (now < start_time_ || now >= end_time_) { | 109 if (now < start_time_ || now >= end_time_) { |
| 99 return false; | 110 return false; |
| 100 } | 111 } |
| 101 // is the plan exhausted? | 112 // is the plan exhausted? |
| 102 if (type_ != kTypeUnlimited && data_bytes_used_ >= data_bytes_max_) { | 113 if (type_ != kTypeUnlimited && total_bytes_used_ >= data_bytes_max_) { |
| 103 return false; | 114 return false; |
| 104 } | 115 } |
| 105 return true; | 116 return true; |
| 106 } | 117 } |
| 107 | 118 |
| 108 DBusDataPlan DataPlan::ToDBusFormat() const { | 119 DBusDataPlan DataPlan::ToDBusFormat() const { |
| 109 DBusDataPlan plan; | 120 DBusDataPlan plan; |
| 110 // Indexing into map w/ nonexistent key causes empty DBus::Variant to be | 121 // Indexing into map w/ nonexistent key causes empty DBus::Variant to be |
| 111 // created and inserted. We then fill in the desired value via its writer | 122 // created and inserted. We then fill in the desired value via its writer |
| 112 // DBus::MessageIter. | 123 // DBus::MessageIter. |
| 113 plan[kCellularDataPlanName].writer().append_string(name_.c_str()); | 124 plan[kCellularDataPlanName].writer().append_string(name_.c_str()); |
| 114 plan[kCellularDataPlanType].writer().append_string( | 125 plan[kCellularDataPlanType].writer().append_string( |
| 115 TypeToLibcrosString(type_)); | 126 TypeToLibcrosString(type_)); |
| 116 plan[kCellularDataPlanUpdateTime].writer().append_int64( | 127 plan[kCellularDataPlanUpdateTime].writer().append_int64( |
| 117 update_time_.ToInternalValue()); | 128 update_time_.ToInternalValue()); |
| 118 plan[kCellularDataPlanStartTime].writer().append_int64( | 129 plan[kCellularDataPlanStartTime].writer().append_int64( |
| 119 start_time_.ToInternalValue()); | 130 start_time_.ToInternalValue()); |
| 120 plan[kCellularDataPlanEndTime].writer().append_int64( | 131 plan[kCellularDataPlanEndTime].writer().append_int64( |
| 121 end_time_.ToInternalValue()); | 132 end_time_.ToInternalValue()); |
| 122 // omit max bytes field for unlimited plans | 133 // omit max bytes field for unlimited plans |
| 123 if (type_ != kTypeUnlimited) { | 134 if (type_ != kTypeUnlimited) { |
| 124 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_); | 135 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_); |
| 125 } | 136 } |
| 126 // send our best estimate of data bytes used | 137 // send our best estimate of data bytes used |
| 127 // this is the baseline, if any, that we received from the carrier usage API | 138 // this is the baseline, if any, that we received from the carrier usage API |
| 128 // plus any local traffic that we've measured since then | 139 // plus any local traffic that we've measured since then |
| 129 Bytes total_bytes_used = data_bytes_used_ + local_bytes_used_; | 140 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used_); |
| 130 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used); | |
| 131 return plan; | 141 return plan; |
| 132 } | 142 } |
| 133 | 143 |
| 134 // static | 144 // static |
| 135 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value, | 145 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value, |
| 136 const Policy *policy) { | 146 const Policy *policy) { |
| 137 CHECK_NOTNULL(value); | 147 CHECK_NOTNULL(value); |
| 138 CHECK_NOTNULL(policy); | 148 CHECK_NOTNULL(policy); |
| 139 std::string last_update_iso8601; | 149 std::string last_update_iso8601; |
| 140 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty, | 150 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty, |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 *out_value = generic_int_out; | 355 *out_value = generic_int_out; |
| 346 return true; | 356 return true; |
| 347 } | 357 } |
| 348 if (dict.GetInteger64(key, out_value)) { | 358 if (dict.GetInteger64(key, out_value)) { |
| 349 return true; | 359 return true; |
| 350 } | 360 } |
| 351 return false; | 361 return false; |
| 352 } | 362 } |
| 353 | 363 |
| 354 } // namespace cashew | 364 } // namespace cashew |
| OLD | NEW |