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