Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: src/data_plan.cc

Issue 5321001: cashew: don't propagate inactive plans to clients (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cashew.git@master
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
rtc 2010/11/23 04:38:58 I'm wondering if it would be better to have unit t
Vince Laviano 2010/11/23 05:11:04 Removed asserts that test values hardcoded in memb
55 CHECK_GE(local_bytes_used_, 0);
56 CHECK_GE(total_bytes_used_, 0);
53 } 57 }
54 58
55 DataPlan::~DataPlan() { 59 DataPlan::~DataPlan() {
56 } 60 }
57 61
58 const std::string& DataPlan::GetName() const { 62 const std::string& DataPlan::GetName() const {
59 return name_; 63 return name_;
60 } 64 }
61 65
62 DataPlan::Type DataPlan::GetType() const { 66 DataPlan::Type DataPlan::GetType() const {
(...skipping 17 matching lines...) Expand all
80 } 84 }
81 85
82 Bytes DataPlan::GetDataBytesUsed() const { 86 Bytes DataPlan::GetDataBytesUsed() const {
83 return data_bytes_used_; 87 return data_bytes_used_;
84 } 88 }
85 89
86 Bytes DataPlan::GetLocalBytesUsed() const { 90 Bytes DataPlan::GetLocalBytesUsed() const {
87 return local_bytes_used_; 91 return local_bytes_used_;
88 } 92 }
89 93
94 Bytes DataPlan::GetTotalBytesUsed() const {
95 return total_bytes_used_;
96 }
97
90 void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) { 98 void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) {
91 CHECK_GE(local_bytes_used, 0); 99 CHECK_GE(local_bytes_used, 0);
92 local_bytes_used_ = local_bytes_used; 100 local_bytes_used_ = local_bytes_used;
101 total_bytes_used_ = data_bytes_used_ + local_bytes_used_;
102 if (total_bytes_used_ < 0) {
103 LOG(WARNING) << "SetLocalBytesUsed: overflow detected";
104 total_bytes_used_ = kint64max;
105 }
93 } 106 }
94 107
95 bool DataPlan::IsActive() const { 108 bool DataPlan::IsActive() const {
96 // is the plan current? 109 // is the plan current?
97 base::Time now = base::Time::Now(); 110 base::Time now = base::Time::Now();
98 if (now < start_time_ || now >= end_time_) { 111 if (now < start_time_ || now >= end_time_) {
99 return false; 112 return false;
100 } 113 }
101 // is the plan exhausted? 114 // is the plan exhausted?
102 if (type_ != kTypeUnlimited && data_bytes_used_ >= data_bytes_max_) { 115 if (type_ != kTypeUnlimited && total_bytes_used_ >= data_bytes_max_) {
103 return false; 116 return false;
104 } 117 }
105 return true; 118 return true;
106 } 119 }
107 120
108 DBusDataPlan DataPlan::ToDBusFormat() const { 121 DBusDataPlan DataPlan::ToDBusFormat() const {
109 DBusDataPlan plan; 122 DBusDataPlan plan;
110 // Indexing into map w/ nonexistent key causes empty DBus::Variant to be 123 // 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 124 // created and inserted. We then fill in the desired value via its writer
112 // DBus::MessageIter. 125 // DBus::MessageIter.
113 plan[kCellularDataPlanName].writer().append_string(name_.c_str()); 126 plan[kCellularDataPlanName].writer().append_string(name_.c_str());
114 plan[kCellularDataPlanType].writer().append_string( 127 plan[kCellularDataPlanType].writer().append_string(
115 TypeToLibcrosString(type_)); 128 TypeToLibcrosString(type_));
116 plan[kCellularDataPlanUpdateTime].writer().append_int64( 129 plan[kCellularDataPlanUpdateTime].writer().append_int64(
117 update_time_.ToInternalValue()); 130 update_time_.ToInternalValue());
118 plan[kCellularDataPlanStartTime].writer().append_int64( 131 plan[kCellularDataPlanStartTime].writer().append_int64(
119 start_time_.ToInternalValue()); 132 start_time_.ToInternalValue());
120 plan[kCellularDataPlanEndTime].writer().append_int64( 133 plan[kCellularDataPlanEndTime].writer().append_int64(
121 end_time_.ToInternalValue()); 134 end_time_.ToInternalValue());
122 // omit max bytes field for unlimited plans 135 // omit max bytes field for unlimited plans
123 if (type_ != kTypeUnlimited) { 136 if (type_ != kTypeUnlimited) {
124 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_); 137 plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_);
125 } 138 }
126 // send our best estimate of data bytes used 139 // send our best estimate of data bytes used
127 // this is the baseline, if any, that we received from the carrier usage API 140 // 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 141 // plus any local traffic that we've measured since then
129 Bytes total_bytes_used = data_bytes_used_ + local_bytes_used_; 142 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used_);
130 plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used);
131 return plan; 143 return plan;
132 } 144 }
133 145
134 // static 146 // static
135 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value, 147 DataPlan* DataPlan::FromDictionaryValue(const DictionaryValue *value,
136 const Policy *policy) { 148 const Policy *policy) {
137 CHECK_NOTNULL(value); 149 CHECK_NOTNULL(value);
138 CHECK_NOTNULL(policy); 150 CHECK_NOTNULL(policy);
139 std::string last_update_iso8601; 151 std::string last_update_iso8601;
140 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty, 152 if (!value->GetString(kCrosUsageDataPlanLastUpdateProperty,
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 *out_value = generic_int_out; 357 *out_value = generic_int_out;
346 return true; 358 return true;
347 } 359 }
348 if (dict.GetInteger64(key, out_value)) { 360 if (dict.GetInteger64(key, out_value)) {
349 return true; 361 return true;
350 } 362 }
351 return false; 363 return false;
352 } 364 }
353 365
354 } // namespace cashew 366 } // namespace cashew
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698