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_gsm.h b/cellular_capability_gsm.h
index fe1001e..d19738d 100644
--- a/cellular_capability_gsm.h
+++ b/cellular_capability_gsm.h
@@ -26,39 +26,41 @@
   static const char kPropertyUnlockRequired[];
   static const char kPropertyUnlockRetries[];
 
-  CellularCapabilityGSM(Cellular *cellular);
+  CellularCapabilityGSM(Cellular *cellular, ProxyFactory *proxy_factory);
 
   // Inherited from CellularCapability.
-  virtual void OnDeviceStarted();
-  virtual void OnDeviceStopped();
+  virtual void StartModem();
+  virtual void StopModem();
   virtual void OnServiceCreated();
   virtual void UpdateStatus(const DBusPropertiesMap &properties);
   virtual void SetupConnectProperties(DBusPropertiesMap *properties);
   virtual void GetSignalQuality();
-  virtual void GetRegistrationState();
-  virtual void GetProperties();
-  virtual void Register();
-  virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
+  virtual void GetRegistrationState(AsyncCallHandler *call_handler);
+  virtual void GetProperties(AsyncCallHandler *call_handler);
+  virtual void GetIMEI(AsyncCallHandler *call_handler);
+  virtual void GetIMSI(AsyncCallHandler *call_handler);
+  virtual void GetSPN(AsyncCallHandler *call_handler);
+  virtual void GetMSISDN(AsyncCallHandler *call_handler);
+  virtual void Register(AsyncCallHandler *call_handler);
+  virtual void RegisterOnNetwork(const std::string &network_id,
+                                 AsyncCallHandler *call_handler);
   virtual bool IsRegistered();
   virtual std::string CreateFriendlyServiceName();
   virtual void RequirePIN(
-      const std::string &pin, bool require, ReturnerInterface *returner);
-  virtual void EnterPIN(const std::string &pin, ReturnerInterface *returner);
+      const std::string &pin, bool require, AsyncCallHandler *call_handler);
+  virtual void EnterPIN(const std::string &pin, AsyncCallHandler *call_handler);
   virtual void UnblockPIN(const std::string &unblock_code,
                           const std::string &pin,
-                          ReturnerInterface *returner);
+                          AsyncCallHandler *call_handler);
   virtual void ChangePIN(const std::string &old_pin,
                          const std::string &new_pin,
-                         ReturnerInterface *returner);
-  virtual void Scan(Error *error);
+                         AsyncCallHandler *call_handler);
+  virtual void Scan(AsyncCallHandler *call_handler);
   virtual std::string GetNetworkTechnologyString() const;
   virtual std::string GetRoamingStateString() const;
   virtual void OnModemManagerPropertiesChanged(
       const DBusPropertiesMap &properties);
 
-  // Obtains the IMEI, IMSI, SPN and MSISDN.
-  virtual void GetIdentifiers();
-
   const std::string &spn() const { return spn_; }
   const std::string &sim_lock_type() const {
     return sim_lock_status_.lock_type;
@@ -66,8 +68,17 @@
   uint32 sim_lock_retries_left() const { return sim_lock_status_.retries_left; }
 
  private:
+  friend class CellularTest;
   friend class CellularCapabilityGSMTest;
   FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
+  FRIEND_TEST(CellularCapabilityGSMTest, GetIMEI);
+  FRIEND_TEST(CellularCapabilityGSMTest, GetIMSI);
+  FRIEND_TEST(CellularCapabilityGSMTest, GetMSISDN);
+  FRIEND_TEST(CellularCapabilityGSMTest, GetSPN);
+  FRIEND_TEST(CellularCapabilityGSMTest, RequirePIN);
+  FRIEND_TEST(CellularCapabilityGSMTest, EnterPIN);
+  FRIEND_TEST(CellularCapabilityGSMTest, UnblockPIN);
+  FRIEND_TEST(CellularCapabilityGSMTest, ChangePIN);
   FRIEND_TEST(CellularCapabilityGSMTest, InitAPNList);
   FRIEND_TEST(CellularCapabilityGSMTest, ParseScanResult);
   FRIEND_TEST(CellularCapabilityGSMTest, ParseScanResultProviderLookup);
@@ -76,6 +87,7 @@
   FRIEND_TEST(CellularCapabilityGSMTest, SetAccessTechnology);
   FRIEND_TEST(CellularCapabilityGSMTest, SetHomeProvider);
   FRIEND_TEST(CellularCapabilityGSMTest, UpdateOperatorInfo);
+  FRIEND_TEST(CellularCapabilityGSMTest, GetRegistrationState);
 
   struct SimLockStatus {
    public:
@@ -96,18 +108,6 @@
   static const char kPhoneNumber[];
   static const char kPropertyAccessTechnology[];
 
-  void RegisterOnNetworkTask(const std::string &network_id);
-  void RequirePINTask(
-      const std::string &pin, bool require, ReturnerInterface *returner);
-  void EnterPINTask(const std::string &pin, ReturnerInterface *returner);
-  void UnblockPINTask(const std::string &unblock_code,
-                      const std::string &pin,
-                      ReturnerInterface *returner);
-  void ChangePINTask(const std::string &old_pin,
-                     const std::string &new_pin,
-                     ReturnerInterface *returner);
-  void ScanTask();
-
   void SetAccessTechnology(uint32 access_technology);
 
   // Sets the upper level information about the home cellular provider from the
@@ -124,8 +124,7 @@
   // Initializes the |apn_list_| property based on the current |home_provider_|.
   void InitAPNList();
 
-  Stringmap ParseScanResult(
-      const ModemGSMNetworkProxyInterface::ScanResult &result);
+  Stringmap ParseScanResult(const GSMScanResult &result);
 
   StrIntPair SimLockStatusToProperty(Error *error);
 
@@ -138,8 +137,31 @@
   virtual void OnGSMNetworkModeChanged(uint32 mode);
   virtual void OnGSMRegistrationInfoChanged(uint32 status,
                                             const std::string &operator_code,
-                                            const std::string &operator_name);
+                                            const std::string &operator_name,
+                                            const Error &error,
+                                            AsyncCallHandler *call_handler);
   virtual void OnGSMSignalQualityChanged(uint32 quality);
+  //
+  // Method callbacks inherited from ModemGSMNetworkProxyDelegate.
+  virtual void OnRegisterCallback(const Error &error,
+                                  AsyncCallHandler *call_handler);
+  virtual void OnGetIMEICallback(const std::string &imei,
+                                 const Error &error,
+                                 AsyncCallHandler *call_handler);
+  virtual void OnGetIMSICallback(const std::string &imsi,
+                                 const Error &error,
+                                 AsyncCallHandler *call_handler);
+  virtual void OnGetSPNCallback(const std::string &spn,
+                                const Error &error,
+                                AsyncCallHandler *call_handler);
+  virtual void OnGetMSISDNCallback(const std::string &msisdn,
+                                   const Error &error,
+                                   AsyncCallHandler *call_handler);
+  virtual void OnPINOperationCallback(const Error &error,
+                                      AsyncCallHandler *call_handler);
+  virtual void OnScanCallback(const GSMScanResults &results,
+                              const Error &error,
+                              AsyncCallHandler *call_handler);
 
   scoped_ptr<ModemGSMCardProxyInterface> card_proxy_;
   scoped_ptr<ModemGSMNetworkProxyInterface> network_proxy_;
@@ -151,6 +173,7 @@
   Cellular::Operator serving_operator_;
   std::string spn_;
   mobile_provider *home_provider_;
+  std::string desired_network_;
 
   // Properties.
   std::string selected_network_;