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/memory/singleton.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/observer_list.h" 15 #include "chrome/browser/chromeos/cros/network_library.h" 16 #include "content/public/browser/browser_thread.h" 17 18 namespace chromeos { 19 20 class TestMobileActivator; 21 22 // Cellular plan config document. 23 class CellularConfigDocument 24 : public base::RefCountedThreadSafe<CellularConfigDocument> { 25 public: 26 CellularConfigDocument(); 27 28 // Return error message for a given code. 29 std::string GetErrorMessage(const std::string& code); 30 void LoadCellularConfigFile(); 31 const std::string& version() { return version_; } 32 33 private: 34 friend class base::RefCountedThreadSafe<CellularConfigDocument>; 35 typedef std::map<std::string, std::string> ErrorMap; 36 37 virtual ~CellularConfigDocument(); 38 39 void SetErrorMap(const ErrorMap& map); 40 bool LoadFromFile(const base::FilePath& config_path); 41 42 std::string version_; 43 ErrorMap error_map_; 44 base::Lock config_lock_; 45 46 DISALLOW_COPY_AND_ASSIGN(CellularConfigDocument); 47 }; 48 49 // This class performs mobile plan activation process. 50 class MobileActivator 51 : public NetworkLibrary::NetworkManagerObserver, 52 public NetworkLibrary::NetworkObserver, 53 public base::SupportsWeakPtr<MobileActivator> { 54 public: 55 // Activation state. 56 enum PlanActivationState { 57 // Activation WebUI page is loading, activation not started. 58 PLAN_ACTIVATION_PAGE_LOADING = -1, 59 // Activation process started. 60 PLAN_ACTIVATION_START = 0, 61 // Initial over the air activation attempt. 62 PLAN_ACTIVATION_TRYING_OTASP = 1, 63 // Performing pre-activation process. 64 PLAN_ACTIVATION_INITIATING_ACTIVATION = 3, 65 // Reconnecting to network. Used for networks activated over cellular 66 // connection. 67 PLAN_ACTIVATION_RECONNECTING = 4, 68 // Passively waiting for a network connection. Used for networks activated 69 // over non-cellular network. 70 PLAN_ACTIVATION_WAITING_FOR_CONNECTION = 5, 71 // Loading payment portal page. 72 PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING = 6, 73 // Showing payment portal page. 74 PLAN_ACTIVATION_SHOWING_PAYMENT = 7, 75 // Decides whether to load the portal again or call us done. 76 PLAN_ACTIVATION_RECONNECTING_PAYMENT = 8, 77 // Delaying activation until payment portal catches up. 78 PLAN_ACTIVATION_DELAY_OTASP = 9, 79 // Starting post-payment activation attempt. 80 PLAN_ACTIVATION_START_OTASP = 10, 81 // Attempting activation. 82 PLAN_ACTIVATION_OTASP = 11, 83 // Finished activation. 84 PLAN_ACTIVATION_DONE = 12, 85 // Error occured during activation process. 86 PLAN_ACTIVATION_ERROR = 0xFF, 87 }; 88 89 // Activation process observer. 90 class Observer { 91 public: 92 // Signals activation |state| change for given |network|. 93 virtual void OnActivationStateChanged( 94 CellularNetwork* network, 95 PlanActivationState state, 96 const std::string& error_description) = 0; 97 98 protected: 99 Observer() {} 100 virtual ~Observer() {} 101 }; 102 103 static MobileActivator* GetInstance(); 104 105 // Add/remove activation process observer. 106 void AddObserver(Observer* observer); 107 void RemoveObserver(Observer* observer); 108 109 // Activation is in process. 110 bool RunningActivation() const; 111 // Activation state. 112 PlanActivationState state() const { return state_; } 113 // Initiates activation process. Can only be called from the UI thread. 114 void InitiateActivation(const std::string& service_path); 115 // Terminates activation process if already started. 116 void TerminateActivation(); 117 // Process portal load attempt status. 118 void OnPortalLoaded(bool success); 119 // Process payment transaction status. 120 void OnSetTransactionStatus(bool success); 121 122 private: 123 friend struct DefaultSingletonTraits<MobileActivator>; 124 friend class TestMobileActivator; 125 FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, BasicFlowForNewDevices); 126 FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, OTASPScheduling); 127 FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, 128 ReconnectOnDisconnectFromPaymentPortal); 129 FRIEND_TEST_ALL_PREFIXES(MobileActivatorTest, StartAtStart); 130 // We reach directly into the activator for testing purposes. 131 friend class MobileActivatorTest; 132 133 MobileActivator(); 134 virtual ~MobileActivator(); 135 136 // NetworkLibrary::NetworkManagerObserver overrides. 137 virtual void OnNetworkManagerChanged(NetworkLibrary* obj) OVERRIDE; 138 // NetworkLibrary::NetworkObserver overrides. 139 virtual void OnNetworkChanged(NetworkLibrary* obj, 140 const Network* network) OVERRIDE; 141 142 // Continue activation after inital setup (config load). 143 void ContinueActivation(); 144 // Handles the signal that the payment portal has finished loading. 145 void HandlePortalLoaded(bool success); 146 // Handles the signal that the user has finished with the portal. 147 void HandleSetTransactionStatus(bool success); 148 // Starts activation. 149 void StartActivation(); 150 // Called after we delay our OTASP (after payment). 151 void RetryOTASP(); 152 // Continues activation process. This method is called after we disconnect 153 // due to detected connectivity issue to kick off reconnection. 154 void ContinueConnecting(); 155 156 // Sends message to host registration page with system/user info data. 157 void SendDeviceInfo(); 158 159 // Starts OTASP process. 160 void StartOTASP(); 161 // Called when an OTASP attempt times out. 162 void HandleOTASPTimeout(); 163 // Forces disconnect / reconnect when we detect portal connectivity issues. 164 void ForceReconnect(CellularNetwork* network, PlanActivationState next_state); 165 // Called when ForceReconnect takes too long to reconnect. 166 void ReconnectTimedOut(); 167 // Verify the state of cellular network and modify internal state. 168 virtual void EvaluateCellularNetwork(CellularNetwork* network); 169 // PickNextState selects the desired state based on the current state of the 170 // modem and the activator. It does not transition to this state however. 171 PlanActivationState PickNextState(CellularNetwork* network, 172 std::string* error_description) const; 173 // One of PickNext*State are called in PickNextState based on whether the 174 // modem is online or not. 175 PlanActivationState PickNextOnlineState(CellularNetwork* network) const; 176 PlanActivationState PickNextOfflineState(CellularNetwork* network) const; 177 // Check the current cellular network for error conditions. 178 bool GotActivationError(CellularNetwork* network, 179 std::string* error) const; 180 // Sends status updates to WebUI page. 181 void UpdatePage(CellularNetwork* network, 182 const std::string& error_description); 183 // Changes internal state. 184 virtual void ChangeState(CellularNetwork* network, 185 PlanActivationState new_state, 186 const std::string& error_description); 187 // Resets network devices after cellular activation process. 188 // |network| should be NULL if the activation process failed. 189 void CompleteActivation(CellularNetwork* network); 190 // Disables SSL certificate revocation checking mechanism. In the case 191 // where captive portal connection is the only one present, such revocation 192 // checks could prevent payment portal page from loading. 193 void DisableCertRevocationChecking(); 194 // Reenables SSL certificate revocation checking mechanism. 195 void ReEnableCertRevocationChecking(); 196 // Return error message for a given code. 197 std::string GetErrorMessage(const std::string& code) const; 198 199 // Converts the currently active CellularNetwork device into a JS object. 200 static void GetDeviceInfo(CellularNetwork* network, 201 DictionaryValue* value); 202 static bool ShouldReportDeviceState(std::string* state, std::string* error); 203 204 // Performs activation state cellular device evaluation. 205 // Returns false if device activation failed. In this case |error| 206 // will contain error message to be reported to Web UI. 207 static bool EvaluateCellularDeviceState(bool* report_status, 208 std::string* state, 209 std::string* error); 210 // Finds cellular network that matches |meid_| or |iccid_|, reattach network 211 // change observer if |reattach_observer| flag is set. 212 virtual CellularNetwork* FindMatchingCellularNetwork(bool reattach_observer); 213 // Starts the OTASP timeout timer. If the timer fires, we'll force a 214 // disconnect/reconnect cycle on this network. 215 virtual void StartOTASPTimer(); 216 217 static const char* GetStateDescription(PlanActivationState state); 218 219 virtual NetworkLibrary* GetNetworkLibrary() const; 220 221 scoped_refptr<CellularConfigDocument> cellular_config_; 222 // Internal handler state. 223 PlanActivationState state_; 224 // MEID of cellular device to activate. 225 std::string meid_; 226 // ICCID of the SIM card on cellular device to activate. 227 std::string iccid_; 228 // Service path of network being activated. Note that the path can change 229 // during the activation process while still representing the same service. 230 std::string service_path_; 231 // Flags that controls if cert_checks needs to be restored 232 // after the activation of cellular network. 233 bool reenable_cert_check_; 234 // True if activation process has been terminated. 235 bool terminated_; 236 // Connection retry counter. 237 int connection_retry_count_; 238 // Counters for how many times we've tried each OTASP step. 239 int initial_OTASP_attempts_; 240 int trying_OTASP_attempts_; 241 int final_OTASP_attempts_; 242 // Payment portal reload/reconnect attempt count. 243 int payment_reconnect_count_; 244 // Timer that monitors how long we spend in error-prone states. 245 base::RepeatingTimer<MobileActivator> state_duration_timer_; 246 247 // State we will return to if we are disconnected. 248 PlanActivationState post_reconnect_state_; 249 // Called to continue the reconnect attempt. 250 base::RepeatingTimer<MobileActivator> continue_reconnect_timer_; 251 // Called when the reconnect attempt times out. 252 base::OneShotTimer<MobileActivator> reconnect_timeout_timer_; 253 254 255 ObserverList<Observer> observers_; 256 257 DISALLOW_COPY_AND_ASSIGN(MobileActivator); 258 }; 259 260 } // namespace chromeos 261 262 #endif // CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_ 263