Add async support for shill client calls.

Many of the modem manager method invocations performed by the
shill cellular support have been converted to be asynchronous,
based on the dbus-c++ support for asynchronous proxies.
I've also hooked this up with the mechanism that was recently
introduced that allows adaptor methods to defer returning
results to callers of shill methods, so that we now have
an end-to-end asynchronous solution.

Adaptor methods that want to be non-blocking create a Returner
object, which will later be used to send back the result
of the operation. The cellular device code that implements
the method (or the cellular service code, depending on the
method), wraps the Returner in an AsyncCallHandler object,
which is passed to the proxy methods as the "data" argument.
When the reply callback for the method occurs, it can pull
the Returner object out of the AsyncCallHandler and use it
to return the result.

In the case of an operation like Enable that involves multiple
client method invocations, the object that wraps the returner
is a MultiStepAsyncCallHandler, a subclass of AsyncCallHandler,
that also wraps a list of Tasks comprising the compound
operation.

In either case, a callback indicates that the handling of
the method is complete by calling Complete() on the AsyncCallHandler.
This is an overloaded method - without any argument, it returns
a success result to the caller of the shill method. With an
Error argument, it returns the DBus::Error equivalent of that
error to the caller. For a MultiStepAsyncCallHandler, calling
Complete() with no argument removes the next Task from the
list and posts it to the main loop, unless there are no
remaining tasks, in which case it returns the result to
the caller of the original shill method.

I've converted the following operations to work asynchronously
end-to-end:
Enable, Register, EnterPIN, RequirePIN, ChangePIN, UnblockPIN,
Activate.
Connect and Scan have been changed to be asynchronous on the
proxy side, but not yet on the adaptor side. For Enable, all of
the individual proxy operations are now done asynchronously,
except for fetching of individual properties.

I've moved all the ModemProxy and ModemSimpleProxy method
invocations from Cellular into CellularCapability. Now all
operations to the modem are done through a Capability interface.

There is a memory leak issue noted in the file async_call_handler.h

BUG=chromium-os:23433,chromium-os:22732
TEST=unit tests, testing on device

Change-Id: I54254dd36d116a1e9089bc9b1a60fa06a3098bd5
Reviewed-on: https://gerrit.chromium.org/gerrit/12564
Commit-Ready: Eric Shienbrood <ers@chromium.org>
Reviewed-by: Eric Shienbrood <ers@chromium.org>
Tested-by: Eric Shienbrood <ers@chromium.org>
diff --git a/cellular_capability_cdma.cc b/cellular_capability_cdma.cc
index bf03db6..85b9625 100644
--- a/cellular_capability_cdma.cc
+++ b/cellular_capability_cdma.cc
@@ -22,8 +22,9 @@
 
 const char CellularCapabilityCDMA::kPhoneNumber[] = "#777";
 
-CellularCapabilityCDMA::CellularCapabilityCDMA(Cellular *cellular)
-    : CellularCapability(cellular),
+CellularCapabilityCDMA::CellularCapabilityCDMA(Cellular *cellular,
+                                               ProxyFactory *proxy_factory)
+    : CellularCapability(cellular, proxy_factory),
       task_factory_(this),
       activation_state_(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED),
       registration_state_evdo_(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN),
@@ -34,14 +35,31 @@
   store->RegisterConstUint16(flimflam::kPRLVersionProperty, &prl_version_);
 }
 
-void CellularCapabilityCDMA::OnDeviceStarted() {
-  VLOG(2) << __func__;
+
+void CellularCapabilityCDMA::StartModem()
+{
+  CellularCapability::StartModem();
   proxy_.reset(proxy_factory()->CreateModemCDMAProxy(
       this, cellular()->dbus_path(), cellular()->dbus_owner()));
+
+  MultiStepAsyncCallHandler *call_handler =
+      new MultiStepAsyncCallHandler(cellular()->dispatcher());
+  call_handler->AddTask(task_factory_.NewRunnableMethod(
+      &CellularCapabilityCDMA::EnableModem, call_handler));
+  call_handler->AddTask(task_factory_.NewRunnableMethod(
+      &CellularCapabilityCDMA::GetModemStatus, call_handler));
+  call_handler->AddTask(task_factory_.NewRunnableMethod(
+      &CellularCapabilityCDMA::GetMEID, call_handler));
+  call_handler->AddTask(task_factory_.NewRunnableMethod(
+      &CellularCapabilityCDMA::GetModemInfo, call_handler));
+  call_handler->AddTask(task_factory_.NewRunnableMethod(
+      &CellularCapabilityCDMA::GetRegistrationState, call_handler));
+  call_handler->PostNextTask();
 }
 
-void CellularCapabilityCDMA::OnDeviceStopped() {
+void CellularCapabilityCDMA::StopModem() {
   VLOG(2) << __func__;
+  CellularCapability::StopModem();
   proxy_.reset();
 }
 
@@ -73,39 +91,23 @@
 
 void CellularCapabilityCDMA::SetupConnectProperties(
     DBusPropertiesMap *properties) {
-  (*properties)[Cellular::kConnectPropertyPhoneNumber].writer().append_string(
+  (*properties)[kConnectPropertyPhoneNumber].writer().append_string(
       kPhoneNumber);
 }
 
-void CellularCapabilityCDMA::Activate(const string &carrier, Error *error) {
+void CellularCapabilityCDMA::Activate(const string &carrier,
+                                      AsyncCallHandler *call_handler) {
   VLOG(2) << __func__ << "(" << carrier << ")";
   if (cellular()->state() != Cellular::kStateEnabled &&
       cellular()->state() != Cellular::kStateRegistered) {
-    Error::PopulateAndLog(error, Error::kInvalidArguments,
+    Error error;
+    Error::PopulateAndLog(&error, Error::kInvalidArguments,
                           "Unable to activate in " +
                           Cellular::GetStateString(cellular()->state()));
+    CompleteOperation(call_handler, error);
     return;
   }
-  // Defer because we may be in a dbus-c++ callback.
-  dispatcher()->PostTask(
-      task_factory_.NewRunnableMethod(
-          &CellularCapabilityCDMA::ActivateTask, carrier));
-}
-
-void CellularCapabilityCDMA::ActivateTask(const string &carrier) {
-  VLOG(2) << __func__ << "(" << carrier << ")";
-  if (cellular()->state() != Cellular::kStateEnabled &&
-      cellular()->state() != Cellular::kStateRegistered) {
-    LOG(ERROR) << "Unable to activate in "
-               << Cellular::GetStateString(cellular()->state());
-    return;
-  }
-  // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
-  uint32 status = proxy_->Activate(carrier);
-  if (status == MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR) {
-    activation_state_ = MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING;
-  }
-  HandleNewActivationState(status);
+  proxy_->Activate(carrier, call_handler, kTimeoutActivate);
 }
 
 void CellularCapabilityCDMA::HandleNewActivationState(uint32 error) {
@@ -152,16 +154,17 @@
   }
 }
 
-void CellularCapabilityCDMA::GetIdentifiers() {
+void CellularCapabilityCDMA::GetMEID(AsyncCallHandler *call_handler) {
   VLOG(2) << __func__;
-  if (cellular()->meid().empty()) {
+  if (meid_.empty()) {
     // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
-    cellular()->set_meid(proxy_->MEID());
-    VLOG(2) << "MEID: " << cellular()->meid();
+    meid_ = proxy_->MEID();
+    VLOG(2) << "MEID: " << meid_;
   }
+  CompleteOperation(call_handler);
 }
 
-void CellularCapabilityCDMA::GetProperties() {
+void CellularCapabilityCDMA::GetProperties(AsyncCallHandler */*call_handler*/) {
   VLOG(2) << __func__;
   // No properties.
 }
@@ -207,20 +210,22 @@
   cellular()->HandleNewSignalQuality(strength);
 }
 
-void CellularCapabilityCDMA::GetRegistrationState() {
+void CellularCapabilityCDMA::GetRegistrationState(
+    AsyncCallHandler *call_handler) {
   VLOG(2) << __func__;
-  // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
+  // TODO(petkov) Switch to asynchronous calls (crosbug.com/17583).
   proxy_->GetRegistrationState(
       &registration_state_1x_, &registration_state_evdo_);
   VLOG(2) << "CDMA Registration: 1x(" << registration_state_1x_
           << ") EVDO(" << registration_state_evdo_ << ")";
   cellular()->HandleNewRegistrationState();
+  CompleteOperation(call_handler);
 }
 
 string CellularCapabilityCDMA::CreateFriendlyServiceName() {
   VLOG(2) << __func__;
-  if (!cellular()->carrier().empty()) {
-    return cellular()->carrier();
+  if (!carrier_.empty()) {
+    return carrier_;
   }
   return base::StringPrintf("CDMANetwork%u", friendly_service_name_id_++);
 }
@@ -232,19 +237,30 @@
   }
 }
 
+void CellularCapabilityCDMA::OnActivateCallback(
+    uint32 status,
+    const Error &error,
+    AsyncCallHandler *call_handler) {
+  if (error.IsFailure()) {
+    CompleteOperation(call_handler, error);
+    return;
+  }
+
+  if (status == MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR) {
+    activation_state_ = MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING;
+  }
+  HandleNewActivationState(status);
+  CompleteOperation(call_handler);
+}
+
 void CellularCapabilityCDMA::OnCDMAActivationStateChanged(
     uint32 activation_state,
     uint32 activation_error,
     const DBusPropertiesMap &status_changes) {
   VLOG(2) << __func__;
   string mdn;
-  if (DBusProperties::GetString(status_changes, "mdn", &mdn)) {
-    cellular()->set_mdn(mdn);
-  }
-  string min;
-  if (DBusProperties::GetString(status_changes, "min", &min)) {
-    cellular()->set_min(min);
-  }
+  DBusProperties::GetString(status_changes, "mdn", &mdn_);
+  DBusProperties::GetString(status_changes, "min", &min_);
   if (DBusProperties::GetString(status_changes, "payment_url", &payment_url_) &&
       cellular()->service().get()) {
     cellular()->service()->set_payment_url(payment_url_);