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

Unified Diff: src/data_plan.cc

Issue 5180003: cashew: add local byte counters (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cashew.git@master
Patch Set: Address jglasgow and zelidrag (offline) code review comments Created 10 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/data_plan.h ('k') | src/default_policy.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/data_plan.cc
diff --git a/src/data_plan.cc b/src/data_plan.cc
index 1b2c5cf0ce948690855c59c53bbfa8cc495607c0..9512acb7ac337f69d3f0680e15fd6063a3e979a5 100644
--- a/src/data_plan.cc
+++ b/src/data_plan.cc
@@ -48,7 +48,8 @@ DataPlan::DataPlan(const std::string& name, DataPlan::Type type,
Bytes data_bytes_used)
: name_(name), type_(type), update_time_(update_time),
start_time_(start_time), end_time_(end_time),
- data_bytes_max_(data_bytes_max), data_bytes_used_(data_bytes_used) {
+ data_bytes_max_(data_bytes_max), data_bytes_used_(data_bytes_used),
+ local_bytes_used_(0) {
}
DataPlan::~DataPlan() {
@@ -82,6 +83,28 @@ Bytes DataPlan::GetDataBytesUsed() const {
return data_bytes_used_;
}
+Bytes DataPlan::GetLocalBytesUsed() const {
+ return local_bytes_used_;
+}
+
+void DataPlan::SetLocalBytesUsed(Bytes local_bytes_used) {
+ CHECK_GE(local_bytes_used, 0);
+ local_bytes_used_ = local_bytes_used;
+}
+
+bool DataPlan::IsActive() const {
+ // is the plan current?
+ base::Time now = base::Time::Now();
+ if (now < start_time_ || now >= end_time_) {
+ return false;
+ }
+ // is the plan exhausted?
+ if (type_ != kTypeUnlimited && data_bytes_used_ >= data_bytes_max_) {
+ return false;
+ }
+ return true;
+}
+
DBusDataPlan DataPlan::ToDBusFormat() const {
DBusDataPlan plan;
// Indexing into map w/ nonexistent key causes empty DBus::Variant to be
@@ -100,11 +123,11 @@ DBusDataPlan DataPlan::ToDBusFormat() const {
if (type_ != kTypeUnlimited) {
plan[kCellularDataPlanDataBytesMax].writer().append_int64(data_bytes_max_);
}
- // always send used bytes field, even if we had to assume a value of 0
- // because this is an unlimited plan and the property was absent in the
- // usage API replies that we received
- // TODO(vlaviano): we'll be able to do better when we have local counters
- plan[kCellularDataPlanDataBytesUsed].writer().append_int64(data_bytes_used_);
+ // send our best estimate of data bytes used
+ // this is the baseline, if any, that we received from the carrier usage API
+ // plus any local traffic that we've measured since then
+ Bytes total_bytes_used = data_bytes_used_ + local_bytes_used_;
+ plan[kCellularDataPlanDataBytesUsed].writer().append_int64(total_bytes_used);
return plan;
}
@@ -268,6 +291,19 @@ bool DataPlan::TimeFromIso8601(const std::string& time_8601,
return true;
}
+// static
+DataPlan* DataPlan::GetActivePlan(const DataPlanList& data_plans) {
+ DataPlanList::const_iterator it;
+ for (it = data_plans.begin(); it != data_plans.end(); ++it) {
+ DataPlan *plan = *it;
+ DCHECK(plan != NULL);
+ if (plan->IsActive()) {
+ return plan;
+ }
+ }
+ return NULL;
+}
+
// private methods
const char* DataPlan::TypeToLibcrosString(DataPlan::Type type) const {
« no previous file with comments | « src/data_plan.h ('k') | src/default_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698