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.
5 #ifndef CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_
6 #define CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_
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"
23 class DictionaryValue;
29 class TestMobileActivator;
31 // Cellular plan config document.
32 class CellularConfigDocument
33 : public base::RefCountedThreadSafe<CellularConfigDocument> {
35 CellularConfigDocument();
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_; }
43 friend class base::RefCountedThreadSafe<CellularConfigDocument>;
44 typedef std::map<std::string, std::string> ErrorMap;
46 virtual ~CellularConfigDocument();
48 void SetErrorMap(const ErrorMap& map);
49 bool LoadFromFile(const base::FilePath& config_path);
53 base::Lock config_lock_;
55 DISALLOW_COPY_AND_ASSIGN(CellularConfigDocument);
58 // This class performs mobile plan activation process.
60 : public base::SupportsWeakPtr<MobileActivator>,
61 public NetworkStateHandlerObserver {
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
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,
97 // Activation process observer.
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;
108 virtual ~Observer() {}
111 static MobileActivator* GetInstance();
113 // Add/remove activation process observer.
114 void AddObserver(Observer* observer);
115 void RemoveObserver(Observer* observer);
117 // Activation is in process.
118 bool RunningActivation() const;
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);
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;
142 virtual ~MobileActivator();
144 // NetworkStateHandlerObserver overrides.
145 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE;
146 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
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).
164 // Continues activation process. This method is called after we disconnect
165 // due to detected connectivity issue to kick off reconnection.
166 void ContinueConnecting();
168 // Sends message to host registration page with system/user info data.
169 void SendDeviceInfo();
171 // Starts OTASP process.
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();
181 // Called on default network changes to update cellular network activation
183 void RefreshCellularNetworks();
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);
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);
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);
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);
221 // Changes internal state.
222 virtual void ChangeState(const NetworkState* network,
223 PlanActivationState new_state,
224 const 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;
236 static bool ShouldReportDeviceState(std::string* state, std::string* error);
238 // Performs activation state cellular device evaluation.
239 // Returns false if device activation failed. In this case |error|
240 // will contain error message to be reported to Web UI.
241 static bool EvaluateCellularDeviceState(bool* report_status,
244 // Starts the OTASP timeout timer. If the timer fires, we'll force a
245 // disconnect/reconnect cycle on this network.
246 virtual void StartOTASPTimer();
248 // Records information that cellular plan payment has happened.
249 virtual void SignalCellularPlanPayment();
251 // Returns true if cellular plan payment has been recorded recently.
252 virtual bool HasRecentCellularPlanPayment() const;
254 static const char* GetStateDescription(PlanActivationState state);
256 scoped_refptr<CellularConfigDocument> cellular_config_;
257 // Internal handler state.
258 PlanActivationState state_;
259 // MEID of cellular device to activate.
261 // ICCID of the SIM card on cellular device to activate.
263 // Service path of network being activated. Note that the path can change
264 // during the activation process while still representing the same service.
265 std::string service_path_;
266 // Device on which the network service is activated. While the service path
267 // can change during activation due to modem resets, the device path stays
269 std::string device_path_;
270 // Flags that controls if cert_checks needs to be restored
271 // after the activation of cellular network.
272 bool reenable_cert_check_;
273 // True if activation process has been terminated.
275 // True if an asynchronous activation request was dispatched to Shill
276 // but the succcess or failure of the request is yet unknown.
277 bool pending_activation_request_;
278 // Connection retry counter.
279 int connection_retry_count_;
280 // Counters for how many times we've tried each OTASP step.
281 int initial_OTASP_attempts_;
282 int trying_OTASP_attempts_;
283 int final_OTASP_attempts_;
284 // Payment portal reload/reconnect attempt count.
285 int payment_reconnect_count_;
286 // Timer that monitors how long we spend in error-prone states.
287 base::RepeatingTimer<MobileActivator> state_duration_timer_;
289 // State we will return to if we are disconnected.
290 PlanActivationState post_reconnect_state_;
291 // Called to continue the reconnect attempt.
292 base::RepeatingTimer<MobileActivator> continue_reconnect_timer_;
293 // Called when the reconnect attempt times out.
294 base::OneShotTimer<MobileActivator> reconnect_timeout_timer_;
295 // Cellular plan payment time.
296 base::Time cellular_plan_payment_time_;
298 ObserverList<Observer> observers_;
299 base::WeakPtrFactory<MobileActivator> weak_ptr_factory_;
301 DISALLOW_COPY_AND_ASSIGN(MobileActivator);
304 } // namespace chromeos
306 #endif // CHROME_BROWSER_CHROMEOS_MOBILE_MOBILE_ACTIVATOR_H_