Home | History | Annotate | Download | only in mobile
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_
      6 #define CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/singleton.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/observer_list.h"
     17 #include "base/timer/timer.h"
     18 #include "chromeos/network/network_handler_callbacks.h"
     19 #include "chromeos/network/network_state_handler_observer.h"
     20 #include "content/public/browser/browser_thread.h"
     21 
     22 namespace base {
     23 class DictionaryValue;
     24 }
     25 
     26 namespace chromeos {
     27 
     28 class NetworkState;
     29 class TestMobileActivator;
     30 
     31 // Cellular plan config document.
     32 class CellularConfigDocument
     33     : public base::RefCountedThreadSafe<CellularConfigDocument> {
     34  public:
     35   CellularConfigDocument();
     36 
     37   // Return error message for a given code.
     38   std::string GetErrorMessage(const std::string& code);
     39   void LoadCellularConfigFile();
     40   const std::string& version() { return version_; }
     41 
     42  private:
     43   friend class base::RefCountedThreadSafe<CellularConfigDocument>;
     44   typedef std::map<std::string, std::string> ErrorMap;
     45 
     46   virtual ~CellularConfigDocument();
     47 
     48   void SetErrorMap(const ErrorMap& map);
     49   bool LoadFromFile(const base::FilePath& config_path);
     50 
     51   std::string version_;
     52   ErrorMap error_map_;
     53   base::Lock config_lock_;
     54 
     55   DISALLOW_COPY_AND_ASSIGN(CellularConfigDocument);
     56 };
     57 
     58 // This class performs mobile plan activation process.
     59 class MobileActivator
     60     : public base::SupportsWeakPtr<MobileActivator>,
     61       public NetworkStateHandlerObserver {
     62  public:
     63   // Activation state.
     64   enum PlanActivationState {
     65     // Activation WebUI page is loading, activation not started.
     66     PLAN_ACTIVATION_PAGE_LOADING            = -1,
     67     // Activation process started.
     68     PLAN_ACTIVATION_START                   = 0,
     69     // Initial over the air activation attempt.
     70     PLAN_ACTIVATION_TRYING_OTASP            = 1,
     71     // Performing pre-activation process.
     72     PLAN_ACTIVATION_INITIATING_ACTIVATION   = 3,
     73     // Reconnecting to network. Used for networks activated over cellular
     74     // connection.
     75     PLAN_ACTIVATION_RECONNECTING            = 4,
     76     // Passively waiting for a network connection. Used for networks activated
     77     // over non-cellular network.
     78     PLAN_ACTIVATION_WAITING_FOR_CONNECTION  = 5,
     79     // Loading payment portal page.
     80     PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING  = 6,
     81     // Showing payment portal page.
     82     PLAN_ACTIVATION_SHOWING_PAYMENT         = 7,
     83     // Decides whether to load the portal again or call us done.
     84     PLAN_ACTIVATION_RECONNECTING_PAYMENT    = 8,
     85     // Delaying activation until payment portal catches up.
     86     PLAN_ACTIVATION_DELAY_OTASP             = 9,
     87     // Starting post-payment activation attempt.
     88     PLAN_ACTIVATION_START_OTASP             = 10,
     89     // Attempting activation.
     90     PLAN_ACTIVATION_OTASP                   = 11,
     91     // Finished activation.
     92     PLAN_ACTIVATION_DONE                    = 12,
     93     // Error occured during activation process.
     94     PLAN_ACTIVATION_ERROR                   = 0xFF,
     95   };
     96 
     97   // Activation process observer.
     98   class Observer {
     99    public:
    100     // Signals activation |state| change for given |network|.
    101     virtual void OnActivationStateChanged(
    102         const NetworkState* network,
    103         PlanActivationState state,
    104         const std::string& error_description) = 0;
    105 
    106    protected:
    107     Observer() {}
    108     virtual ~Observer() {}
    109   };
    110 
    111   static MobileActivator* GetInstance();
    112 
    113   // Add/remove activation process observer.
    114   void AddObserver(Observer* observer);
    115   void RemoveObserver(Observer* observer);
    116 
    117   // Activation is in process.
    118   bool RunningActivation() const;
    119   // Activation state.
    120   PlanActivationState state() const { return state_; }
    121   // Initiates activation process.  Can only be called from the UI thread.
    122   void InitiateActivation(const std::string& service_path);
    123   // Terminates activation process if already started.
    124   void TerminateActivation();
    125   // Process portal load attempt status.
    126   void OnPortalLoaded(bool success);
    127   // Process payment transaction status.
    128   void OnSetTransactionStatus(bool success);
    129 
    130  private:
    131   friend struct DefaultSingletonTraits<MobileActivator>;
    132   friend class TestMobileActivator;
    133   FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, BasicFlowForNewDevices);
    134   FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, OTASPScheduling);
    135   FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest,
    136                            ReconnectOnDisconnectFromPaymentPortal);
    137   FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, StartAtStart);
    138   // We reach directly into the activator for testing purposes.
    139   friend class MobileActivatorTest;
    140 
    141   MobileActivator();
    142   virtual ~MobileActivator();
    143 
    144   // NetworkStateHandlerObserver overrides.
    145   virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE;
    146   virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
    147 
    148   // Continue activation after inital setup (config load). Makes an
    149   // asynchronous call to NetworkConfigurationHandler::GetProperties.
    150   void ContinueActivation();
    151   void GetPropertiesAndContinueActivation(
    152       const std::string& service_path,
    153       const base::DictionaryValue& properties);
    154   void GetPropertiesFailure(const std::string& error_name,
    155                             scoped_ptr<base::DictionaryValue> error_data);
    156   // Handles the signal that the payment portal has finished loading.
    157   void HandlePortalLoaded(bool success);
    158   // Handles the signal that the user has finished with the portal.
    159   void HandleSetTransactionStatus(bool success);
    160   // Starts activation.
    161   void StartActivation();
    162   // Called after we delay our OTASP (after payment).
    163   void RetryOTASP();
    164   // Continues activation process. This method is called after we disconnect
    165   // due to detected connectivity issue to kick off reconnection.
    166   void ContinueConnecting();
    167 
    168   // Sends message to host registration page with system/user info data.
    169   void SendDeviceInfo();
    170 
    171   // Starts OTASP process.
    172   void StartOTASP();
    173   // Called when an OTASP attempt times out.
    174   void HandleOTASPTimeout();
    175   // Forces disconnect / reconnect when we detect portal connectivity issues.
    176   void ForceReconnect(const NetworkState* network,
    177                       PlanActivationState next_state);
    178   // Called when ForceReconnect takes too long to reconnect.
    179   void ReconnectTimedOut();
    180 
    181   // Called on default network changes to update cellular network activation
    182   // state.
    183   void RefreshCellularNetworks();
    184 
    185   // Helper to get network state corresponding to service path. Provided
    186   // for easy mocking in unit tests.
    187   virtual const NetworkState* GetNetworkState(const std::string& service_path);
    188 
    189   // Verify the state of cellular network and modify internal state.
    190   virtual void EvaluateCellularNetwork(const NetworkState* network);
    191   // PickNextState selects the desired state based on the current state of the
    192   // modem and the activator.  It does not transition to this state however.
    193   PlanActivationState PickNextState(const NetworkState* network,
    194                                     std::string* error_description) const;
    195   // One of PickNext*State are called in PickNextState based on whether the
    196   // modem is online or not.
    197   PlanActivationState PickNextOnlineState(const NetworkState* network) const;
    198   PlanActivationState PickNextOfflineState(const NetworkState* network) const;
    199   // Check the current cellular network for error conditions.
    200   bool GotActivationError(const NetworkState* network,
    201                           std::string* error) const;
    202   // Sends status updates to WebUI page.
    203   void UpdatePage(const NetworkState* network,
    204                   const std::string& error_description);
    205 
    206   // Callback used to handle an activation error.
    207   void HandleActivationFailure(
    208       const std::string& service_path,
    209       PlanActivationState new_state,
    210       const std::string& error_name,
    211       scoped_ptr<base::DictionaryValue> error_data);
    212 
    213   // Request cellular activation for |network|.
    214   // On success, |success_callback| will be called.
    215   // On failure, |error_callback| will be called.
    216   virtual void RequestCellularActivation(
    217       const NetworkState* network,
    218       const base::Closure& success_callback,
    219       const network_handler::ErrorCallback& error_callback);
    220 
    221   // Changes internal state.
    222   virtual void ChangeState(const NetworkState* network,
    223                            PlanActivationState new_state,
    224                            std::string error_description);
    225   // Resets network devices after cellular activation process.
    226   void CompleteActivation();
    227   // Disables SSL certificate revocation checking mechanism. In the case
    228   // where captive portal connection is the only one present, such revocation
    229   // checks could prevent payment portal page from loading.
    230   void DisableCertRevocationChecking();
    231   // Reenables SSL certificate revocation checking mechanism.
    232   void ReEnableCertRevocationChecking();
    233   // Return error message for a given code.
    234   std::string GetErrorMessage(const std::string& code) const;
    235 
    236   // Performs activation state cellular device evaluation.
    237   // Returns false if device activation failed. In this case |error|
    238   // will contain error message to be reported to Web UI.
    239   static bool EvaluateCellularDeviceState(bool* report_status,
    240                                           std::string* state,
    241                                           std::string* error);
    242   // Starts the OTASP timeout timer.  If the timer fires, we'll force a
    243   // disconnect/reconnect cycle on this network.
    244   virtual void StartOTASPTimer();
    245 
    246   // Records information that cellular plan payment has happened.
    247   virtual void SignalCellularPlanPayment();
    248 
    249   // Returns true if cellular plan payment has been recorded recently.
    250   virtual bool HasRecentCellularPlanPayment() const;
    251 
    252   static const char* GetStateDescription(PlanActivationState state);
    253 
    254   scoped_refptr<CellularConfigDocument> cellular_config_;
    255   // Internal handler state.
    256   PlanActivationState state_;
    257   // MEID of cellular device to activate.
    258   std::string meid_;
    259   // ICCID of the SIM card on cellular device to activate.
    260   std::string iccid_;
    261   // Service path of network being activated. Note that the path can change
    262   // during the activation process while still representing the same service.
    263   std::string service_path_;
    264   // Device on which the network service is activated. While the service path
    265   // can change during activation due to modem resets, the device path stays
    266   // the same.
    267   std::string device_path_;
    268   // Flags that controls if cert_checks needs to be restored
    269   // after the activation of cellular network.
    270   bool reenable_cert_check_;
    271   // True if activation process has been terminated.
    272   bool terminated_;
    273   // True if an asynchronous activation request was dispatched to Shill
    274   // but the succcess or failure of the request is yet unknown.
    275   bool pending_activation_request_;
    276   // Connection retry counter.
    277   int connection_retry_count_;
    278   // Counters for how many times we've tried each OTASP step.
    279   int initial_OTASP_attempts_;
    280   int trying_OTASP_attempts_;
    281   int final_OTASP_attempts_;
    282   // Payment portal reload/reconnect attempt count.
    283   int payment_reconnect_count_;
    284   // Timer that monitors how long we spend in error-prone states.
    285   base::RepeatingTimer<MobileActivator> state_duration_timer_;
    286 
    287   // State we will return to if we are disconnected.
    288   PlanActivationState post_reconnect_state_;
    289   // Called to continue the reconnect attempt.
    290   base::RepeatingTimer<MobileActivator> continue_reconnect_timer_;
    291   // Called when the reconnect attempt times out.
    292   base::OneShotTimer<MobileActivator> reconnect_timeout_timer_;
    293   // Cellular plan payment time.
    294   base::Time cellular_plan_payment_time_;
    295 
    296   ObserverList<Observer> observers_;
    297   base::WeakPtrFactory<MobileActivator> weak_ptr_factory_;
    298 
    299   DISALLOW_COPY_AND_ASSIGN(MobileActivator);
    300 };
    301 
    302 }  // namespace chromeos
    303 
    304 #endif  // CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_
    305