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

Side by Side Diff: src/service.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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/service.h" 5 #include "src/service.h"
6 6
7 #include <glog/logging.h> 7 #include <glog/logging.h>
8 8
9 #include "src/data_plan_provider.h" 9 #include "src/data_plan_provider.h"
10 #include "src/device.h" 10 #include "src/device.h"
11 #include "src/policy.h" 11 #include "src/policy.h"
12 #include "src/service_manager.h" 12 #include "src/service_manager.h"
13 13
14 namespace cashew { 14 namespace cashew {
15 15
16 // Flimflam Service D-Bus identifiers 16 // Flimflam Service D-Bus identifiers
17 static const char *kFlimflamServiceName = "org.chromium.flimflam"; 17 static const char *kFlimflamServiceName = "org.chromium.flimflam";
18 18
19 // Flimflam Service property names 19 // Flimflam Service property names
20 static const char *kFlimflamServiceConnectivityStateProperty =
21 "ConnectivityState";
20 static const char *kFlimflamServiceDeviceProperty = "Device"; 22 static const char *kFlimflamServiceDeviceProperty = "Device";
21 static const char *kFlimflamServiceStateProperty = "State"; 23 static const char *kFlimflamServiceStateProperty = "State";
22 static const char *kFlimflamServiceTypeProperty = "Type"; 24 static const char *kFlimflamServiceTypeProperty = "Type";
23 static const char *kFlimflamServiceUsageUrlProperty = "Cellular.UsageUrl"; 25 static const char *kFlimflamServiceUsageUrlProperty = "Cellular.UsageUrl";
24 26
27 // Flimflam Service on-the-wire ConnectivityState values
28 static const char *kFlimflamServiceConnectivityStateUnknown = "unknown";
29 static const char *kFlimflamServiceConnectivityStateRestricted = "restricted";
30 static const char *kFlimflamServiceConnectivityStateUnrestricted =
31 "unrestricted";
32 static const char *kFlimflamServiceConnectivityStateNone = "none";
33
25 // Flimflam Service on-the-wire State values 34 // Flimflam Service on-the-wire State values
26 static const char *kFlimflamServiceStateIdle = "idle"; 35 static const char *kFlimflamServiceStateIdle = "idle";
27 static const char *kFlimflamServiceStateCarrier = "carrier"; 36 static const char *kFlimflamServiceStateCarrier = "carrier";
28 static const char *kFlimflamServiceStateAssociation = "association"; 37 static const char *kFlimflamServiceStateAssociation = "association";
29 static const char *kFlimflamServiceStateConfiguration = "configuration"; 38 static const char *kFlimflamServiceStateConfiguration = "configuration";
30 static const char *kFlimflamServiceStateReady = "ready"; 39 static const char *kFlimflamServiceStateReady = "ready";
31 static const char *kFlimflamServiceStateDisconnect = "disconnect"; 40 static const char *kFlimflamServiceStateDisconnect = "disconnect";
32 static const char *kFlimflamServiceStateFailure = "failure"; 41 static const char *kFlimflamServiceStateFailure = "failure";
33 static const char *kFlimflamServiceStateActivationFailure = 42 static const char *kFlimflamServiceStateActivationFailure =
34 "activation-failure"; 43 "activation-failure";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 75
67 Service::Service(ServiceManager * const parent, 76 Service::Service(ServiceManager * const parent,
68 DBus::Connection& connection, // NOLINT 77 DBus::Connection& connection, // NOLINT
69 const DBus::Path& path) 78 const DBus::Path& path)
70 : DBus::ObjectProxy(connection, path, kFlimflamServiceName), 79 : DBus::ObjectProxy(connection, path, kFlimflamServiceName),
71 parent_(CHECK_NOTNULL(parent)), connection_(connection), path_(path), 80 parent_(CHECK_NOTNULL(parent)), connection_(connection), path_(path),
72 state_(kStateUnknown), type_(kTypeUnknown), device_(NULL), 81 state_(kStateUnknown), type_(kTypeUnknown), device_(NULL),
73 provider_(NULL), request_in_progress_(false), 82 provider_(NULL), request_in_progress_(false),
74 update_timeout_source_(NULL), policy_(NULL), 83 update_timeout_source_(NULL), policy_(NULL),
75 is_default_service_(false), get_properties_source_id_(0), 84 is_default_service_(false), get_properties_source_id_(0),
76 retrying_get_properties_(false) { 85 retrying_get_properties_(false),
86 connectivity_state_(kConnectivityStateUnknown) {
77 // schedule a GetProperties() call to our Flimflam service path to init state 87 // schedule a GetProperties() call to our Flimflam service path to init state
78 // we'll keep trying periodically until we succeed 88 // we'll keep trying periodically until we succeed
79 // we'll subsequently update this state by monitoring PropertyChanged signals 89 // we'll subsequently update this state by monitoring PropertyChanged signals
80 get_properties_source_id_ = 90 get_properties_source_id_ =
81 g_idle_add(StaticGetServicePropertiesCallback, this); 91 g_idle_add(StaticGetServicePropertiesCallback, this);
82 if (get_properties_source_id_ == 0) { 92 if (get_properties_source_id_ == 0) {
83 LOG(ERROR) << path_ << ": ctor: g_idle_add failed"; 93 LOG(ERROR) << path_ << ": ctor: g_idle_add failed";
84 } 94 }
85 } 95 }
86 96
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // cross-check with parent's idea of default technology 160 // cross-check with parent's idea of default technology
151 if (parent_->GetDefaultTechnology() != kTypeCellular) { 161 if (parent_->GetDefaultTechnology() != kTypeCellular) {
152 DLOG(WARNING) << path_ << ": IsDefaultService: " 162 DLOG(WARNING) << path_ << ": IsDefaultService: "
153 << "service manager doesn't think default technology is cellular"; 163 << "service manager doesn't think default technology is cellular";
154 } 164 }
155 return true; 165 return true;
156 } 166 }
157 return false; 167 return false;
158 } 168 }
159 169
170 Service::ConnectivityState Service::GetConnectivityState() const {
171 return connectivity_state_;
172 }
173
160 // Flimflam Service D-Bus Proxy methods 174 // Flimflam Service D-Bus Proxy methods
161 175
162 void Service::PropertyChanged(const std::string& property_name, 176 void Service::PropertyChanged(const std::string& property_name,
163 const DBus::Variant& new_value) { 177 const DBus::Variant& new_value) {
164 DLOG(INFO) << path_ << ": PropertyChanged: property_name = " << property_name; 178 DLOG(INFO) << path_ << ": PropertyChanged: property_name = " << property_name;
165 if (property_name == kFlimflamServiceDeviceProperty) { 179 if (property_name == kFlimflamServiceDeviceProperty) {
166 OnDeviceUpdate(new_value.reader().get_path()); 180 OnDeviceUpdate(new_value.reader().get_path());
167 } else if (property_name == kFlimflamServiceStateProperty) { 181 } else if (property_name == kFlimflamServiceStateProperty) {
168 OnStateUpdate(new_value.reader().get_string()); 182 OnStateUpdate(new_value.reader().get_string());
169 } else if (property_name == kFlimflamServiceTypeProperty) { 183 } else if (property_name == kFlimflamServiceTypeProperty) {
170 OnTypeUpdate(new_value.reader().get_string()); 184 OnTypeUpdate(new_value.reader().get_string());
171 } else if (property_name == kFlimflamServiceUsageUrlProperty) { 185 } else if (property_name == kFlimflamServiceUsageUrlProperty) {
172 OnUsageUrlUpdate(new_value.reader().get_string()); 186 OnUsageUrlUpdate(new_value.reader().get_string());
187 } else if (property_name == kFlimflamServiceConnectivityStateProperty) {
188 OnConnectivityStateUpdate(new_value.reader().get_string());
173 } else { 189 } else {
174 // we don't care about this property 190 // we don't care about this property
175 } 191 }
176 } 192 }
177 193
178 // Device methods 194 // Device methods
179 195
180 void Service::OnCarrierUpdate(const std::string& carrier) { 196 void Service::OnCarrierUpdate(const std::string& carrier) {
181 DLOG(INFO) << path_ << ": OnCarrierUpdate: carrier = " << carrier; 197 DLOG(INFO) << path_ << ": OnCarrierUpdate: carrier = " << carrier;
182 // NOTE: device_ can be NULL here because we may still be in Device ctor 198 DCHECK(device_ != NULL);
183 DCHECK(device_ == NULL || carrier == device_->GetCarrier()); 199 DCHECK(carrier == device_->GetCarrier());
184 200
185 // get rid of state associated with old carrier 201 // get rid of state associated with old carrier
186 if (provider_ != NULL) { 202 if (provider_ != NULL) {
187 DCHECK(carrier != provider_->GetCarrier()); 203 DCHECK(carrier != provider_->GetCarrier());
188 } 204 }
189 DeleteCarrierState(); 205 DeleteCarrierState();
190 206
191 // if we don't have new carrier info, we can't do anything now 207 // if we don't have new carrier info, we can't do anything now
192 if (carrier == Device::kCarrierUnknown) { 208 if (carrier.empty()) {
193 return; 209 return;
194 } 210 }
195 211
196 // create state for new carrier 212 // create state for new carrier
197 DLOG(INFO) << path_ << ": OnCarrierUpdate: creating data plan provider"; 213 DLOG(INFO) << path_ << ": OnCarrierUpdate: creating data plan provider";
198 provider_ = new(std::nothrow) DataPlanProvider(carrier); 214 provider_ = new(std::nothrow) DataPlanProvider(carrier);
199 if (provider_ == NULL) { 215 if (provider_ == NULL) {
200 LOG(ERROR) << path_ 216 LOG(ERROR) << path_
201 << ": OnCarrierUpdate: could not create data plan provider"; 217 << ": OnCarrierUpdate: could not create data plan provider";
202 return; 218 return;
(...skipping 10 matching lines...) Expand all
213 229
214 // if we already have usage url, set new provider in motion 230 // if we already have usage url, set new provider in motion
215 if (!usage_url_.empty()) { 231 if (!usage_url_.empty()) {
216 provider_->SetUsageUrl(usage_url_); 232 provider_->SetUsageUrl(usage_url_);
217 ReconsiderSendingUsageRequests(); 233 ReconsiderSendingUsageRequests();
218 } else { 234 } else {
219 // we'll do this later in OnUsageUrlUpdate 235 // we'll do this later in OnUsageUrlUpdate
220 } 236 }
221 } 237 }
222 238
239 void Service::OnByteCounterUpdate(uint64 rx_bytes, uint64 tx_bytes) {
240 DCHECK(device_ != NULL);
241 DCHECK(device_->ByteCounterRunning());
242 DLOG(INFO) << path_ << ": OnByteCounterUpdate: rx_bytes = " << rx_bytes
243 << ", tx_bytes = " << tx_bytes;
244 DataPlan *active_plan = DataPlan::GetActivePlan(data_plans_);
245 if (active_plan == NULL) {
246 DLOG(WARNING) << path_ << ": OnByteCounterUpdate: no active plan";
247 return;
248 }
249 Bytes local_bytes_used = rx_bytes + tx_bytes;
250 DLOG(INFO) << path_ << ": OnByteCounterUpdate: local_bytes_used = "
251 << local_bytes_used;
252 // try to detect two error conditions:
253 // (1) overflow of the unsigned addition above prior to the assignment
254 // (2) no overflow, but result has high order bit set and so is interpreted as
255 // a negative number when assigned to |local_bytes_used|
256 if (local_bytes_used < 0 ||
257 static_cast<uint64>(local_bytes_used) < rx_bytes ||
258 static_cast<uint64>(local_bytes_used) < tx_bytes) {
259 LOG(WARNING) << path_ << ": OnByteCounterUpdate: overflow detected";
260 return;
261 }
262 active_plan->SetLocalBytesUsed(local_bytes_used);
263 DLOG(INFO) << path_
264 << ": OnByteCounterUpdate: updated plan state: data bytes used = "
265 << active_plan->GetDataBytesUsed() << ", local byes used = "
266 << active_plan->GetLocalBytesUsed();
267 MaybeEmitDataPlansUpdate();
268 }
269
223 // DataPlanProviderDelegate methods 270 // DataPlanProviderDelegate methods
224 271
225 void Service::OnRequestComplete(const DataPlanProvider *provider, 272 void Service::OnRequestComplete(const DataPlanProvider *provider,
226 bool successful, 273 bool successful,
227 const Value *parsed_usage_update) { 274 const Value *parsed_usage_update) {
228 DCHECK(provider != NULL); 275 DCHECK(provider != NULL);
229 DCHECK(!successful || parsed_usage_update != NULL); 276 DCHECK(!successful || parsed_usage_update != NULL);
230 if (provider != provider_) { 277 if (provider != provider_) {
231 DLOG(WARNING) << path_ << ": OnRequestComplete: wrong provider"; 278 DLOG(WARNING) << path_ << ": OnRequestComplete: wrong provider";
232 return; 279 return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 } 322 }
276 if (status != kCrosUsageStatusOk) { 323 if (status != kCrosUsageStatusOk) {
277 LOG(WARNING) << path_ << ": OnRequestComplete: status: " << status; 324 LOG(WARNING) << path_ << ": OnRequestComplete: status: " << status;
278 OnCrosUsageErrorResult(status); 325 OnCrosUsageErrorResult(status);
279 return; 326 return;
280 } 327 }
281 328
282 bool restricted = false; 329 bool restricted = false;
283 if (root->GetBoolean(kCrosUsageRestrictedProperty, &restricted)) { 330 if (root->GetBoolean(kCrosUsageRestrictedProperty, &restricted)) {
284 DLOG(INFO) << path_ << ": OnRequestComplete: restricted = " << restricted; 331 DLOG(INFO) << path_ << ": OnRequestComplete: restricted = " << restricted;
332 // TODO(vlaviano): this should probably be combined with flimflam's
333 // ConnectivityState property to arrive at a conclusion.
334
285 } else { 335 } else {
286 DLOG(INFO) << path_ << ": OnRequestComplete: no restricted property"; 336 DLOG(INFO) << path_ << ": OnRequestComplete: no restricted property";
287 // restricted property is optional 337 // restricted property is optional
288 } 338 }
289 339
290 ListValue *plans = NULL; // list to which this points is owned by dictionary 340 ListValue *plans = NULL; // list to which this points is owned by dictionary
291 if (!root->GetList(kCrosUsagePlansProperty, &plans)) { 341 if (!root->GetList(kCrosUsagePlansProperty, &plans)) {
292 LOG(WARNING) << path_ << ": OnRequestComplete: no plans property"; 342 LOG(WARNING) << path_ << ": OnRequestComplete: no plans property";
293 return; 343 return;
294 } 344 }
(...skipping 20 matching lines...) Expand all
315 } 365 }
316 DLOG(INFO) << path_ << ": OnRequestComplete: converted plan[" << i << "]"; 366 DLOG(INFO) << path_ << ": OnRequestComplete: converted plan[" << i << "]";
317 new_plans.push_back(plan); 367 new_plans.push_back(plan);
318 } 368 }
319 369
320 // store new info, causing it to be used for subsequent updates to clients 370 // store new info, causing it to be used for subsequent updates to clients
321 DataPlanList old_plans = data_plans_; 371 DataPlanList old_plans = data_plans_;
322 data_plans_ = new_plans; 372 data_plans_ = new_plans;
323 LOG(INFO) << path_ << ": OnRequestComplete: updated data plans"; 373 LOG(INFO) << path_ << ": OnRequestComplete: updated data plans";
324 374
325 // consult policy to determine if we should send an unsolicited update 375 MaybeEmitDataPlansUpdate();
326 if (policy_->ShouldEmitDataPlansUpdate(old_plans, new_plans)) {
327 DLOG(INFO) << path_ << ": OnRequestComplete: sending update";
328 parent_->EmitDataPlansUpdate(*this);
329 } else {
330 DLOG(INFO) << path_ << ": OnRequestComplete: not sending update";
331 }
332 376
333 DLOG(INFO) << path_ << ": OnRequestComplete: deleting old data plans"; 377 DLOG(INFO) << path_ << ": OnRequestComplete: deleting old data plans";
334 DeleteDataPlans(&old_plans); 378 DeleteDataPlans(&old_plans);
379
380 // start local byte counter so that we can stop hitting usage API
381 // we'll stop the counter when we disconnect
382 // we don't bother if carrier told us that there are no active data plans
383 DataPlan *active_plan = DataPlan::GetActivePlan(data_plans_);
384 if (active_plan == NULL) {
385 DLOG(INFO) << path_
386 << ": OnRequestComplete: no active plans, not starting byte counter";
387 return;
388 }
389 DCHECK(device_ != NULL);
390 DCHECK(!device_->ByteCounterRunning());
391 if (!device_->StartByteCounter()) {
392 LOG(WARNING) << path_
393 << ": OnRequestComplete: could not start byte counter";
394 // we'll keep the update timer running and try again later
395 }
396 DLOG(INFO) << path_ << ": OnRequestComplete: started byte counter";
397 ReconsiderSendingUsageRequests();
335 } 398 }
336 399
337 // Service Manager methods 400 // Service Manager methods
338 401
339 void Service::OnDefaultServiceUpdate(bool is_default_service) { 402 void Service::OnDefaultServiceUpdate(bool is_default_service) {
340 DCHECK(is_default_service_ == !is_default_service); 403 DCHECK(is_default_service_ == !is_default_service);
341 DLOG(INFO) << path_ << ": OnDefaultServiceUpdate: is_default_service = " 404 DLOG(INFO) << path_ << ": OnDefaultServiceUpdate: is_default_service = "
342 << is_default_service; 405 << is_default_service;
343 is_default_service_ = is_default_service; 406 is_default_service_ = is_default_service;
344 if (is_default_service_) { 407 if (is_default_service_) {
(...skipping 30 matching lines...) Expand all
375 bool Service::RetryingGetProperties() const { 438 bool Service::RetryingGetProperties() const {
376 return retrying_get_properties_; 439 return retrying_get_properties_;
377 } 440 }
378 441
379 void Service::OnRetryingGetProperties(bool retrying) { 442 void Service::OnRetryingGetProperties(bool retrying) {
380 retrying_get_properties_ = retrying; 443 retrying_get_properties_ = retrying;
381 } 444 }
382 445
383 // Private methods 446 // Private methods
384 447
385 Service::State Service::StateFromString(const std::string& state) const { 448 // static
449 Service::ConnectivityState Service::ConnectivityStateFromString(
450 const std::string& connectivity_state) {
451 if (connectivity_state == kFlimflamServiceConnectivityStateUnknown) {
452 return kConnectivityStateUnknown;
453 }
454 if (connectivity_state == kFlimflamServiceConnectivityStateRestricted) {
455 return kConnectivityStateRestricted;
456 }
457 if (connectivity_state == kFlimflamServiceConnectivityStateUnrestricted) {
458 return kConnectivityStateUnrestricted;
459 }
460 if (connectivity_state == kFlimflamServiceConnectivityStateNone) {
461 return kConnectivityStateNone;
462 }
463 return kConnectivityStateUnknown;
464 }
465
466 // static
467 Service::State Service::StateFromString(const std::string& state) {
386 if (state == kFlimflamServiceStateIdle) { 468 if (state == kFlimflamServiceStateIdle) {
387 return kStateIdle; 469 return kStateIdle;
388 } 470 }
389 if (state == kFlimflamServiceStateCarrier) { 471 if (state == kFlimflamServiceStateCarrier) {
390 return kStateCarrier; 472 return kStateCarrier;
391 } 473 }
392 if (state == kFlimflamServiceStateAssociation) { 474 if (state == kFlimflamServiceStateAssociation) {
393 return kStateAssociation; 475 return kStateAssociation;
394 } 476 }
395 if (state == kFlimflamServiceStateConfiguration) { 477 if (state == kFlimflamServiceStateConfiguration) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 device_ = new(std::nothrow) Device(this, connection_, device_path); 513 device_ = new(std::nothrow) Device(this, connection_, device_path);
432 if (device_ == NULL) { 514 if (device_ == NULL) {
433 LOG(ERROR) << path_ << ": OnDeviceUpdate: couldn't create device for " 515 LOG(ERROR) << path_ << ": OnDeviceUpdate: couldn't create device for "
434 << device_path; 516 << device_path;
435 return; 517 return;
436 } 518 }
437 DLOG(INFO) << path_ << ": OnDeviceUpdate: created device for " 519 DLOG(INFO) << path_ << ": OnDeviceUpdate: created device for "
438 << device_path; 520 << device_path;
439 } 521 }
440 522
523 void Service::OnConnectivityStateUpdate(const std::string& connectivity_state) {
524 DLOG(INFO) << path_ << ": OnConnectivityStateUpdate: connectivity_state = "
525 << connectivity_state;
526 Service::ConnectivityState new_connectivity_state =
527 ConnectivityStateFromString(connectivity_state);
528 if (connectivity_state_ == new_connectivity_state) {
529 return;
530 }
531 connectivity_state_ = new_connectivity_state;
532 ReconsiderSendingUsageRequests();
533 }
534
441 void Service::OnStateUpdate(const std::string& state) { 535 void Service::OnStateUpdate(const std::string& state) {
442 DLOG(INFO) << path_ << ": OnStateUpdate: state = " << state; 536 DLOG(INFO) << path_ << ": OnStateUpdate: state = " << state;
443 Service::State old_state = state_; 537 Service::State old_state = state_;
444 state_ = StateFromString(state); 538 state_ = StateFromString(state);
445 if (old_state != kStateReady && IsConnected()) { 539 if (old_state != kStateReady && IsConnected()) {
446 OnConnected(); 540 OnConnected();
447 } else if (old_state == kStateReady && !IsConnected()) { 541 } else if (old_state == kStateReady && !IsConnected()) {
448 OnDisconnected(); 542 OnDisconnected();
449 } 543 }
450 } 544 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 return true; 651 return true;
558 } 652 }
559 it = properties.find(kFlimflamServiceUsageUrlProperty); 653 it = properties.find(kFlimflamServiceUsageUrlProperty);
560 if (it != properties.end()) { 654 if (it != properties.end()) {
561 const DBus::Variant& value = static_cast<DBus::Variant>(it->second); 655 const DBus::Variant& value = static_cast<DBus::Variant>(it->second);
562 OnUsageUrlUpdate(value.reader().get_string()); 656 OnUsageUrlUpdate(value.reader().get_string());
563 } else { 657 } else {
564 DLOG(WARNING) << path_ 658 DLOG(WARNING) << path_
565 << ": GetServiceProperties: no Cellular.UsageUrl property"; 659 << ": GetServiceProperties: no Cellular.UsageUrl property";
566 } 660 }
661 // flimflam docs re: ConnectivityState: "This state is currently only
662 // computed for services of type Cellular"
663 it = properties.find(kFlimflamServiceConnectivityStateProperty);
664 if (it != properties.end()) {
665 const DBus::Variant& value = static_cast<DBus::Variant>(it->second);
666 OnConnectivityStateUpdate(value.reader().get_string());
667 } else {
668 DLOG(WARNING) << path_
669 << ": GetServiceProperties: no ConnectivityState property";
670 }
567 return true; 671 return true;
568 } 672 }
569 673
570 void Service::DeleteDataPlans(DataPlanList *data_plans) { 674 void Service::DeleteDataPlans(DataPlanList *data_plans) {
571 DCHECK(data_plans != NULL); 675 DCHECK(data_plans != NULL);
572 while (!data_plans->empty()) { 676 while (!data_plans->empty()) {
573 DataPlan *plan = *data_plans->begin(); 677 DataPlan *plan = *data_plans->begin();
574 DCHECK(plan != NULL); 678 DCHECK(plan != NULL);
575 DLOG(INFO) << path_ << ": DeleteDataPlans: deleting plan: " 679 DLOG(INFO) << path_ << ": DeleteDataPlans: deleting plan: "
576 << plan->GetName(); 680 << plan->GetName();
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 819
716 void Service::OnConnected() { 820 void Service::OnConnected() {
717 DLOG(INFO) << path_ << ": OnConnected"; 821 DLOG(INFO) << path_ << ": OnConnected";
718 DCHECK(IsConnected()); 822 DCHECK(IsConnected());
719 ReconsiderSendingUsageRequests(); 823 ReconsiderSendingUsageRequests();
720 } 824 }
721 825
722 void Service::OnDisconnected() { 826 void Service::OnDisconnected() {
723 DLOG(INFO) << path_ << ": OnDisconnected"; 827 DLOG(INFO) << path_ << ": OnDisconnected";
724 DCHECK(!IsConnected()); 828 DCHECK(!IsConnected());
829 // stop byte counter
830 // we'll issue a new usage API request when we reconnect
831 if (device_ != NULL) {
832 device_->StopByteCounter();
833 }
725 ReconsiderSendingUsageRequests(); 834 ReconsiderSendingUsageRequests();
726 } 835 }
727 836
728 bool Service::IsSendingUsageRequests() const { 837 bool Service::IsSendingUsageRequests() const {
729 return request_in_progress_ || update_timeout_source_ != NULL; 838 return request_in_progress_ || update_timeout_source_ != NULL;
730 } 839 }
731 840
732 // NOTE: we centralize our decisionmaking here to avoid insanity and just call 841 // NOTE: we centralize our decisionmaking here to avoid insanity and just call
733 // ReconsiderSendingUsageRequests when anything changes 842 // ReconsiderSendingUsageRequests when anything changes
734 bool Service::ShouldSendUsageRequests() const { 843 bool Service::ShouldSendUsageRequests() const {
735 if (provider_ == NULL) { 844 if (provider_ == NULL) {
736 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: no provider"; 845 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: no provider";
737 DCHECK(policy_ == NULL); 846 DCHECK(policy_ == NULL);
738 return false; 847 return false;
739 } 848 }
740 DCHECK(device_ == NULL || device_->GetCarrier() != Device::kCarrierUnknown); 849 DCHECK(device_ != NULL);
850 DCHECK(!device_->GetCarrier().empty());
741 DCHECK(policy_ != NULL); 851 DCHECK(policy_ != NULL);
742 if (usage_url_.empty()) { 852 if (usage_url_.empty()) {
743 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: no usage url"; 853 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: no usage url";
744 return false; 854 return false;
745 } 855 }
746 if (ServiceManager::IsOfflineConnectivityState( 856 if (ServiceManager::IsOfflineConnectivityState(
747 parent_->GetConnectivityState())) { 857 parent_->GetConnectivityState())) {
748 DLOG(INFO) << path_ 858 DLOG(INFO) << path_
749 << ": ShouldSendUsageRequests: no: flimflam is offline"; 859 << ": ShouldSendUsageRequests: no: flimflam is offline";
750 return false; 860 return false;
751 } 861 }
752 if (policy_->UsageRequestsMustBeSentOTA()) { 862 if (!IsConnected()) {
753 if (!IsConnected()) { 863 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: not connected";
754 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: " 864 return false;
755 << "ota-only policy and not connected"; 865 }
756 return false; 866 if (!IsDefaultService()) {
757 } 867 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: not default service";
758 if (!IsDefaultService()) { 868 return false;
759 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: " 869 }
760 << "ota-only policy and not default service"; 870 // TODO(vlaviano): this is a blunt way to ensure that we don't count local
761 return false; 871 // traffic if we're already in the restricted pool when cashew starts
762 } 872 if (GetConnectivityState() == kConnectivityStateRestricted) {
873 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: no: in restricted pool";
874 return false;
875 }
876 if (device_->ByteCounterRunning()) {
877 DLOG(INFO) << path_
878 << ": ShouldSendUsageRequests: no: local byte counter is running";
879 return false;
763 } 880 }
764 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: yes"; 881 DLOG(INFO) << path_ << ": ShouldSendUsageRequests: yes";
765 return true; 882 return true;
766 } 883 }
767 884
768 void Service::StopSendingUsageRequests() { 885 void Service::StopSendingUsageRequests() {
769 DLOG(INFO) << path_ << ": StopSendingUsageRequests"; 886 DLOG(INFO) << path_ << ": StopSendingUsageRequests";
770 DestroyUpdateTimer(); 887 DestroyUpdateTimer();
771 CancelPendingRequests(); 888 CancelPendingRequests();
772 } 889 }
773 890
774 void Service::StartSendingUsageRequests() { 891 void Service::StartSendingUsageRequests() {
775 DLOG(INFO) << path_ << ": StartSendingUsageRequests"; 892 DLOG(INFO) << path_ << ": StartSendingUsageRequests";
776 DCHECK(!IsSendingUsageRequests()); 893 DCHECK(!IsSendingUsageRequests());
777 RequestUsageUpdate(); 894 RequestUsageUpdate();
778 CreateUpdateTimer(); 895 CreateUpdateTimer();
779 } 896 }
780 897
781 void Service::ReconsiderSendingUsageRequests() { 898 void Service::ReconsiderSendingUsageRequests() {
782 DLOG(INFO) << path_ << ": ReconsiderSendingUsageRequests"; 899 DLOG(INFO) << path_ << ": ReconsiderSendingUsageRequests";
783 if (!IsSendingUsageRequests() && ShouldSendUsageRequests()) { 900 if (!IsSendingUsageRequests() && ShouldSendUsageRequests()) {
784 StartSendingUsageRequests(); 901 StartSendingUsageRequests();
785 } else if (IsSendingUsageRequests() && !ShouldSendUsageRequests()) { 902 } else if (IsSendingUsageRequests() && !ShouldSendUsageRequests()) {
786 StopSendingUsageRequests(); 903 StopSendingUsageRequests();
787 } 904 }
788 } 905 }
789 906
907 void Service::MaybeEmitDataPlansUpdate() {
908 DCHECK(policy_ != NULL);
909 if (policy_->ShouldEmitDataPlansUpdate(data_plans_)) {
910 DLOG(INFO) << path_ << ": MaybeEmitDataPlansUpdate: sending update";
911 parent_->EmitDataPlansUpdate(*this);
912 } else {
913 DLOG(INFO) << path_ << ": MaybeEmitDataPlansUpdate: not sending update";
914 }
915 }
916
790 } // namespace cashew 917 } // namespace cashew
OLDNEW
« no previous file with comments | « src/service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698