Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/chromeos/mobile/mobile_activator.cc

Issue 10456045: Refactored mobile activation engine outside of WebUI handler in order to expose its state to other … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/chromeos/mobile_setup_ui.h" 5 #include "chrome/browser/chromeos/mobile/mobile_activator.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/file_util.h" 13 #include "base/file_util.h"
14 #include "base/json/json_reader.h" 14 #include "base/json/json_reader.h"
15 #include "base/json/json_writer.h"
16 #include "base/logging.h" 15 #include "base/logging.h"
17 #include "base/memory/ref_counted_memory.h" 16 #include "base/memory/ref_counted_memory.h"
18 #include "base/message_loop.h" 17 #include "base/message_loop.h"
19 #include "base/metrics/histogram.h" 18 #include "base/metrics/histogram.h"
19 #include "base/observer_list_threadsafe.h"
20 #include "base/string_piece.h" 20 #include "base/string_piece.h"
21 #include "base/string_util.h" 21 #include "base/string_util.h"
22 #include "base/timer.h" 22 #include "base/timer.h"
23 #include "base/utf_string_conversions.h" 23 #include "base/utf_string_conversions.h"
24 #include "base/values.h" 24 #include "base/values.h"
25 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chromeos/cros/cros_library.h" 26 #include "chrome/browser/chromeos/cros/cros_library.h"
27 #include "chrome/browser/chromeos/cros/network_library.h" 27 #include "chrome/browser/chromeos/cros/network_library.h"
28 #include "chrome/browser/prefs/pref_service.h" 28 #include "chrome/browser/prefs/pref_service.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/ui/browser_list.h"
31 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
32 #include "chrome/common/jstemplate_builder.h"
33 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
34 #include "chrome/common/render_messages.h"
35 #include "chrome/common/url_constants.h"
36 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/render_view_host_observer.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/browser/web_ui.h"
40 #include "content/public/browser/web_ui_message_handler.h"
41 #include "googleurl/src/gurl.h"
42 #include "grit/browser_resources.h"
43 #include "grit/chromium_strings.h"
44 #include "grit/generated_resources.h"
45 #include "grit/locale_settings.h"
46 #include "ui/base/l10n/l10n_util.h"
47 #include "ui/base/layout.h"
48 #include "ui/base/resource/resource_bundle.h"
49 31
50 using content::BrowserThread; 32 using content::BrowserThread;
51 using content::RenderViewHost;
52 using content::WebContents;
53 using content::WebUIMessageHandler;
54 33
55 namespace { 34 namespace {
56 35
57 // Host page JS API function names.
58 const char kJsApiStartActivation[] = "startActivation";
59 const char kJsApiSetTransactionStatus[] = "setTransactionStatus";
60 const char kJsApiPaymentPortalLoad[] = "paymentPortalLoad";
61 const char kJsApiResultOK[] = "ok";
62
63 const char kJsDeviceStatusChangedCallback[] =
64 "mobile.MobileSetup.deviceStateChanged";
65 const char kJsPortalFrameLoadFailedCallback[] =
66 "mobile.MobileSetup.portalFrameLoadError";
67 const char kJsPortalFrameLoadCompletedCallback[] =
68 "mobile.MobileSetup.portalFrameLoadCompleted";
69
70 // Error codes matching codes defined in the cellular config file.
71 const char kErrorDefault[] = "default";
72 const char kErrorBadConnectionPartial[] = "bad_connection_partial";
73 const char kErrorBadConnectionActivated[] = "bad_connection_activated";
74 const char kErrorRoamingOnConnection[] = "roaming_connection";
75 const char kErrorNoEVDO[] = "no_evdo";
76 const char kErrorRoamingActivation[] = "roaming_activation";
77 const char kErrorRoamingPartiallyActivated[] = "roaming_partially_activated";
78 const char kErrorNoService[] = "no_service";
79 const char kErrorDisabled[] = "disabled";
80 const char kErrorNoDevice[] = "no_device";
81 const char kFailedPaymentError[] = "failed_payment";
82 const char kFailedConnectivity[] = "connectivity";
83 const char kErrorAlreadyRunning[] = "already_running";
84
85 // Cellular configuration file path. 36 // Cellular configuration file path.
86 const char kCellularConfigPath[] = 37 const char kCellularConfigPath[] =
87 "/usr/share/chromeos-assets/mobile/mobile_config.json"; 38 "/usr/share/chromeos-assets/mobile/mobile_config.json";
88 39
89 // Cellular config file field names. 40 // Cellular config file field names.
90 const char kVersionField[] = "version"; 41 const char kVersionField[] = "version";
91 const char kErrorsField[] = "errors"; 42 const char kErrorsField[] = "errors";
92 43
93 // Number of times we will retry to restart the activation process in case 44 // Number of times we will retry to restart the activation process in case
94 // there is no connectivity in the restricted pool. 45 // there is no connectivity in the restricted pool.
(...skipping 12 matching lines...) Expand all
107 const int kConnectionTimeoutSeconds = 45; 58 const int kConnectionTimeoutSeconds = 45;
108 // Reconnect delay. 59 // Reconnect delay.
109 const int kReconnectDelayMS = 3000; 60 const int kReconnectDelayMS = 3000;
110 // Reconnect timer delay. 61 // Reconnect timer delay.
111 const int kReconnectTimerDelayMS = 5000; 62 const int kReconnectTimerDelayMS = 5000;
112 // Reconnect delay after previous failure. 63 // Reconnect delay after previous failure.
113 const int kFailedReconnectDelayMS = 10000; 64 const int kFailedReconnectDelayMS = 10000;
114 // Retry delay after failed OTASP attempt. 65 // Retry delay after failed OTASP attempt.
115 const int kOTASPRetryDelay = 20000; 66 const int kOTASPRetryDelay = 20000;
116 67
117 chromeos::CellularNetwork* GetCellularNetwork() { 68 // Error codes matching codes defined in the cellular config file.
118 chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> 69 const char kErrorDefault[] = "default";
119 GetNetworkLibrary(); 70 const char kErrorBadConnectionPartial[] = "bad_connection_partial";
120 if (lib->cellular_networks().begin() != lib->cellular_networks().end()) { 71 const char kErrorBadConnectionActivated[] = "bad_connection_activated";
121 return *(lib->cellular_networks().begin()); 72 const char kErrorRoamingOnConnection[] = "roaming_connection";
73 const char kErrorNoEVDO[] = "no_evdo";
74 const char kErrorRoamingActivation[] = "roaming_activation";
75 const char kErrorRoamingPartiallyActivated[] = "roaming_partially_activated";
76 const char kErrorNoService[] = "no_service";
77 const char kErrorDisabled[] = "disabled";
78 const char kErrorNoDevice[] = "no_device";
79 const char kFailedPaymentError[] = "failed_payment";
80 const char kFailedConnectivity[] = "connectivity";
81 const char kErrorAlreadyRunning[] = "already_running";
rkc 2012/06/05 21:48:03 Since the 'already_running' logic has replaced wit
zel 2012/06/05 23:02:35 Done.
82
83 } // namespace
84
85 namespace chromeos {
86
87 CellularNetwork* FindCellularNetwork(
rkc 2012/06/05 21:48:03 This function is uncessary. It can be replaced in
zel 2012/06/05 23:02:35 Removed.
88 const std::string& service_path) {
89 NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
90 for (CellularNetworkVector::const_iterator it =
91 lib->cellular_networks().begin();
92 it != lib->cellular_networks().end(); ++it) {
93 if (service_path == (*it)->service_path())
94 return *it;
122 } 95 }
123 return NULL; 96 return NULL;
124 } 97 }
125 98
126 chromeos::CellularNetwork* GetCellularNetwork(
127 const std::string& service_path) {
128 return chromeos::CrosLibrary::Get()->
129 GetNetworkLibrary()->FindCellularNetworkByPath(service_path);
130 }
131
132 } // namespace
133
134 // Observes IPC messages from the rederer and notifies JS if frame loading error
135 // appears.
136 class PortalFrameLoadObserver : public content::RenderViewHostObserver {
137 public:
138 PortalFrameLoadObserver(const base::WeakPtr<MobileSetupUI>& parent,
139 RenderViewHost* host)
140 : content::RenderViewHostObserver(host), parent_(parent) {
141 Send(new ChromeViewMsg_StartFrameSniffer(routing_id(),
142 UTF8ToUTF16("paymentForm")));
143 }
144
145 // IPC::Channel::Listener implementation.
146 virtual bool OnMessageReceived(const IPC::Message& message) {
147 bool handled = true;
148 IPC_BEGIN_MESSAGE_MAP(PortalFrameLoadObserver, message)
149 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FrameLoadingError, OnFrameLoadError)
150 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FrameLoadingCompleted,
151 OnFrameLoadCompleted)
152 IPC_MESSAGE_UNHANDLED(handled = false)
153 IPC_END_MESSAGE_MAP()
154 return handled;
155 }
156
157 private:
158 void OnFrameLoadError(int error) {
159 if (!parent_.get())
160 return;
161
162 base::FundamentalValue result_value(error);
163 parent_->web_ui()->CallJavascriptFunction(kJsPortalFrameLoadFailedCallback,
164 result_value);
165 }
166
167 void OnFrameLoadCompleted() {
168 if (!parent_.get())
169 return;
170
171 parent_->web_ui()->CallJavascriptFunction(
172 kJsPortalFrameLoadCompletedCallback);
173 }
174
175 base::WeakPtr<MobileSetupUI> parent_;
176 DISALLOW_COPY_AND_ASSIGN(PortalFrameLoadObserver);
177 };
178
179 class CellularConfigDocument
180 : public base::RefCountedThreadSafe<CellularConfigDocument> {
181 public:
182 CellularConfigDocument() {}
183
184 // Return error message for a given code.
185 std::string GetErrorMessage(const std::string& code);
186 void LoadCellularConfigFile();
187 const std::string& version() { return version_; }
188
189 private:
190 typedef std::map<std::string, std::string> ErrorMap;
191
192 void SetErrorMap(const ErrorMap& map);
193 bool LoadFromFile(const FilePath& config_path);
194
195 std::string version_;
196 ErrorMap error_map_;
197 base::Lock config_lock_;
198
199 DISALLOW_COPY_AND_ASSIGN(CellularConfigDocument);
200 };
201
202 class MobileSetupUIHTMLSource : public ChromeURLDataManager::DataSource {
203 public:
204 explicit MobileSetupUIHTMLSource(const std::string& service_path);
205
206 // Called when the network layer has requested a resource underneath
207 // the path we registered.
208 virtual void StartDataRequest(const std::string& path,
209 bool is_incognito,
210 int request_id);
211 virtual std::string GetMimeType(const std::string&) const {
212 return "text/html";
213 }
214
215 private:
216 virtual ~MobileSetupUIHTMLSource() {}
217
218 std::string service_path_;
219 DISALLOW_COPY_AND_ASSIGN(MobileSetupUIHTMLSource);
220 };
221
222 // The handler for Javascript messages related to the "register" view.
223 class MobileSetupHandler
224 : public WebUIMessageHandler,
225 public chromeos::NetworkLibrary::NetworkManagerObserver,
226 public chromeos::NetworkLibrary::NetworkObserver,
227 public base::SupportsWeakPtr<MobileSetupHandler> {
228 public:
229 explicit MobileSetupHandler(const std::string& service_path);
230 virtual ~MobileSetupHandler();
231
232 // Init work after Attach.
233 void StartActivationOnUIThread();
234
235 // WebUIMessageHandler implementation.
236 virtual void RegisterMessages() OVERRIDE;
237
238 // NetworkLibrary::NetworkManagerObserver implementation.
239 virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj) OVERRIDE;
240 // NetworkLibrary::NetworkObserver implementation.
241 virtual void OnNetworkChanged(chromeos::NetworkLibrary* obj,
242 const chromeos::Network* network) OVERRIDE;
243
244 private:
245 typedef enum PlanActivationState {
246 PLAN_ACTIVATION_PAGE_LOADING = -1,
247 PLAN_ACTIVATION_START = 0,
248 PLAN_ACTIVATION_TRYING_OTASP = 1,
249 PLAN_ACTIVATION_RECONNECTING_OTASP_TRY = 2,
250 PLAN_ACTIVATION_INITIATING_ACTIVATION = 3,
251 PLAN_ACTIVATION_RECONNECTING = 4,
252 PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING = 5,
253 PLAN_ACTIVATION_SHOWING_PAYMENT = 6,
254 PLAN_ACTIVATION_RECONNECTING_PAYMENT = 7,
255 PLAN_ACTIVATION_DELAY_OTASP = 8,
256 PLAN_ACTIVATION_START_OTASP = 9,
257 PLAN_ACTIVATION_OTASP = 10,
258 PLAN_ACTIVATION_RECONNECTING_OTASP = 11,
259 PLAN_ACTIVATION_DONE = 12,
260 PLAN_ACTIVATION_ERROR = 0xFF,
261 } PlanActivationState;
262
263 // Handlers for JS WebUI messages.
264 void HandleSetTransactionStatus(const ListValue* args);
265 void HandleStartActivation(const ListValue* args);
266 void HandlePaymentPortalLoad(const ListValue* args);
267 void SetTransactionStatus(const std::string& status);
268 // Starts activation.
269 void StartActivation();
270 // Retried OTASP.
271 void RetryOTASP();
272 // Continues activation process. This method is called after we disconnect
273 // due to detected connectivity issue to kick off reconnection.
274 void ContinueConnecting(int delay);
275
276 // Sends message to host registration page with system/user info data.
277 void SendDeviceInfo();
278
279 // Callback for when |reconnect_timer_| fires.
280 void ReconnectTimerFired();
281 // Starts OTASP process.
282 void StartOTASP();
283 // Checks if we need to reconnect due to failed connection attempt.
284 bool NeedsReconnecting(chromeos::CellularNetwork* network,
285 PlanActivationState* new_state,
286 std::string* error_description);
287 // Disconnect from network.
288 void DisconnectFromNetwork(chromeos::CellularNetwork* network);
289 // Connects to cellular network, resets connection timer.
290 bool ConnectToNetwork(chromeos::CellularNetwork* network, int delay);
291 // Forces disconnect / reconnect when we detect portal connectivity issues.
292 void ForceReconnect(chromeos::CellularNetwork* network, int delay);
293 // Reports connection timeout.
294 bool ConnectionTimeout();
295 // Verify the state of cellular network and modify internal state.
296 void EvaluateCellularNetwork(chromeos::CellularNetwork* network);
297 // Check the current cellular network for error conditions.
298 bool GotActivationError(chromeos::CellularNetwork* network,
299 std::string* error);
300 // Sends status updates to WebUI page.
301 void UpdatePage(chromeos::CellularNetwork* network,
302 const std::string& error_description);
303 // Changes internal state.
304 void ChangeState(chromeos::CellularNetwork* network,
305 PlanActivationState new_state,
306 const std::string& error_description);
307 // Prepares network devices for cellular activation process.
308 void SetupActivationProcess(chromeos::CellularNetwork* network);
309 // Disables ethernet and wifi newtorks since they interefere with
310 // detection of restricted pool on cellular side.
311 void DisableOtherNetworks();
312 // Resets network devices after cellular activation process.
313 // |network| should be NULL if the activation process failed.
314 void CompleteActivation(chromeos::CellularNetwork* network);
315 // Control routines for handling other types of connections during
316 // cellular activation.
317 void ReEnableOtherConnections();
318 // Return error message for a given code.
319 std::string GetErrorMessage(const std::string& code);
320
321 // Converts the currently active CellularNetwork device into a JS object.
322 static void GetDeviceInfo(chromeos::CellularNetwork* network,
323 DictionaryValue* value);
324 static bool ShouldReportDeviceState(std::string* state, std::string* error);
325
326 // Performs activation state cellular device evaluation.
327 // Returns false if device activation failed. In this case |error|
328 // will contain error message to be reported to Web UI.
329 static bool EvaluateCellularDeviceState(bool* report_status,
330 std::string* state,
331 std::string* error);
332
333 // Returns next reconnection state based on the current activation phase.
334 static PlanActivationState GetNextReconnectState(PlanActivationState state);
335 static const char* GetStateDescription(PlanActivationState state);
336
337 scoped_refptr<CellularConfigDocument> cellular_config_;
338 // Internal handler state.
339 PlanActivationState state_;
340 std::string service_path_;
341 // Flags that control if wifi and ethernet connection needs to be restored
342 // after the activation of cellular network.
343 bool reenable_wifi_;
344 bool reenable_ethernet_;
345 bool reenable_cert_check_;
346 bool evaluating_;
347 // True if we think that another tab is already running activation.
348 bool already_running_;
349 // Connection retry counter.
350 int connection_retry_count_;
351 // Post payment reconnect wait counters.
352 int reconnect_wait_count_;
353 // Payment portal reload/reconnect attempt count.
354 int payment_reconnect_count_;
355 // Activation retry attempt count;
356 int activation_attempt_;
357 // Connection start time.
358 base::Time connection_start_time_;
359 // Timer that monitors reconnection timeouts.
360 base::RepeatingTimer<MobileSetupHandler> reconnect_timer_;
361
362 DISALLOW_COPY_AND_ASSIGN(MobileSetupHandler);
363 };
364
365 //////////////////////////////////////////////////////////////////////////////// 99 ////////////////////////////////////////////////////////////////////////////////
366 // 100 //
367 // CellularConfigDocument 101 // CellularConfigDocument
368 // 102 //
369 //////////////////////////////////////////////////////////////////////////////// 103 ////////////////////////////////////////////////////////////////////////////////
104 CellularConfigDocument::CellularConfigDocument() {
105 }
106
107 CellularConfigDocument::~CellularConfigDocument() {
108 }
370 109
371 std::string CellularConfigDocument::GetErrorMessage(const std::string& code) { 110 std::string CellularConfigDocument::GetErrorMessage(const std::string& code) {
372 base::AutoLock create(config_lock_); 111 base::AutoLock create(config_lock_);
373 ErrorMap::iterator iter = error_map_.find(code); 112 ErrorMap::iterator iter = error_map_.find(code);
374 if (iter == error_map_.end()) 113 if (iter == error_map_.end())
375 return code; 114 return code;
376 return iter->second; 115 return iter->second;
377 } 116 }
378 117
379 void CellularConfigDocument::LoadCellularConfigFile() { 118 void CellularConfigDocument::LoadCellularConfigFile() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 168
430 void CellularConfigDocument::SetErrorMap( 169 void CellularConfigDocument::SetErrorMap(
431 const ErrorMap& map) { 170 const ErrorMap& map) {
432 base::AutoLock create(config_lock_); 171 base::AutoLock create(config_lock_);
433 error_map_.clear(); 172 error_map_.clear();
434 error_map_.insert(map.begin(), map.end()); 173 error_map_.insert(map.begin(), map.end());
435 } 174 }
436 175
437 //////////////////////////////////////////////////////////////////////////////// 176 ////////////////////////////////////////////////////////////////////////////////
438 // 177 //
439 // MobileSetupUIHTMLSource 178 // MobileActivator
440 // 179 //
441 //////////////////////////////////////////////////////////////////////////////// 180 ////////////////////////////////////////////////////////////////////////////////
442 181 MobileActivator::MobileActivator()
443 MobileSetupUIHTMLSource::MobileSetupUIHTMLSource(
444 const std::string& service_path)
445 : DataSource(chrome::kChromeUIMobileSetupHost, MessageLoop::current()),
446 service_path_(service_path) {
447 }
448
449 void MobileSetupUIHTMLSource::StartDataRequest(const std::string& path,
450 bool is_incognito,
451 int request_id) {
452 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_);
453 // If we are activating, shutting down, or logging in, |network| may not
454 // be available.
455 if (!network || !network->SupportsActivation()) {
456 scoped_refptr<base::RefCountedBytes> html_bytes(new base::RefCountedBytes);
457 SendResponse(request_id, html_bytes);
458 return;
459 }
460 DictionaryValue strings;
461 strings.SetString("title", l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE));
462 strings.SetString("connecting_header",
463 l10n_util::GetStringFUTF16(IDS_MOBILE_CONNECTING_HEADER,
464 network ? UTF8ToUTF16(network->name()) : string16()));
465 strings.SetString("error_header",
466 l10n_util::GetStringUTF16(IDS_MOBILE_ERROR_HEADER));
467 strings.SetString("activating_header",
468 l10n_util::GetStringUTF16(IDS_MOBILE_ACTIVATING_HEADER));
469 strings.SetString("completed_header",
470 l10n_util::GetStringUTF16(IDS_MOBILE_COMPLETED_HEADER));
471 strings.SetString("please_wait",
472 l10n_util::GetStringUTF16(IDS_MOBILE_PLEASE_WAIT));
473 strings.SetString("completed_text",
474 l10n_util::GetStringUTF16(IDS_MOBILE_COMPLETED_TEXT));
475 strings.SetString("close_button",
476 l10n_util::GetStringUTF16(IDS_CLOSE));
477 strings.SetString("cancel_button",
478 l10n_util::GetStringUTF16(IDS_CANCEL));
479 strings.SetString("ok_button",
480 l10n_util::GetStringUTF16(IDS_OK));
481 SetFontAndTextDirection(&strings);
482
483 static const base::StringPiece html(
484 ResourceBundle::GetSharedInstance().GetRawDataResource(
485 IDR_MOBILE_SETUP_PAGE_HTML, ui::SCALE_FACTOR_NONE));
486
487 std::string full_html = jstemplate_builder::GetI18nTemplateHtml(html,
488 &strings);
489
490 SendResponse(request_id, base::RefCountedString::TakeString(&full_html));
491 }
492
493 ////////////////////////////////////////////////////////////////////////////////
494 //
495 // MobileSetupHandler
496 //
497 ////////////////////////////////////////////////////////////////////////////////
498 MobileSetupHandler::MobileSetupHandler(const std::string& service_path)
499 : cellular_config_(new CellularConfigDocument()), 182 : cellular_config_(new CellularConfigDocument()),
500 state_(PLAN_ACTIVATION_PAGE_LOADING), 183 state_(PLAN_ACTIVATION_PAGE_LOADING),
501 service_path_(service_path),
502 reenable_wifi_(false), 184 reenable_wifi_(false),
503 reenable_ethernet_(false), 185 reenable_ethernet_(false),
504 reenable_cert_check_(false), 186 reenable_cert_check_(false),
505 evaluating_(false), 187 evaluating_(false),
506 already_running_(false), 188 terminated_(true),
507 connection_retry_count_(0), 189 connection_retry_count_(0),
508 reconnect_wait_count_(0), 190 reconnect_wait_count_(0),
509 payment_reconnect_count_(0), 191 payment_reconnect_count_(0),
510 activation_attempt_(0) { 192 activation_attempt_(0) {
511 } 193 }
512 194
513 MobileSetupHandler::~MobileSetupHandler() { 195 MobileActivator::~MobileActivator() {
196 TerminateActivation();
197 }
198
199 MobileActivator* MobileActivator::GetInstance() {
200 return Singleton<MobileActivator>::get();
201 }
202
203 void MobileActivator::TerminateActivation() {
rkc 2012/06/05 21:48:03 This class is a singleton, so the destructor will
zel 2012/06/05 23:02:35 TerminateActivation() is called when user closes t
514 reconnect_timer_.Stop(); 204 reconnect_timer_.Stop();
515 chromeos::NetworkLibrary* lib = 205 NetworkLibrary* lib =
516 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); 206 CrosLibrary::Get()->GetNetworkLibrary();
517 lib->RemoveNetworkManagerObserver(this); 207 lib->RemoveNetworkManagerObserver(this);
518 lib->RemoveObserverForAllNetworks(this); 208 lib->RemoveObserverForAllNetworks(this);
519 if (lib->IsLocked()) 209 if (lib->IsLocked())
520 lib->Unlock(); 210 lib->Unlock();
521 ReEnableOtherConnections(); 211 ReEnableOtherConnections();
212 meid_.clear();
213 service_path_.clear();
214 state_ = PLAN_ACTIVATION_PAGE_LOADING;
215 reconnect_wait_count_ = 0;
216 evaluating_ = false;
217 reenable_wifi_ = false;
218 reenable_ethernet_ = false;
219 reenable_cert_check_ = false;
220 terminated_ = true;
221 cellular_config_ = NULL;
522 } 222 }
523 223
524 void MobileSetupHandler::RegisterMessages() { 224 void MobileActivator::OnNetworkManagerChanged(NetworkLibrary* cros) {
525 web_ui()->RegisterMessageCallback(kJsApiStartActivation,
526 base::Bind(&MobileSetupHandler::HandleStartActivation,
527 base::Unretained(this)));
528 web_ui()->RegisterMessageCallback(kJsApiSetTransactionStatus,
529 base::Bind(&MobileSetupHandler::HandleSetTransactionStatus,
530 base::Unretained(this)));
531 web_ui()->RegisterMessageCallback(kJsApiPaymentPortalLoad,
532 base::Bind(&MobileSetupHandler::HandlePaymentPortalLoad,
533 base::Unretained(this)));
534 }
535
536 void MobileSetupHandler::OnNetworkManagerChanged(
537 chromeos::NetworkLibrary* cros) {
538 if (state_ == PLAN_ACTIVATION_PAGE_LOADING) 225 if (state_ == PLAN_ACTIVATION_PAGE_LOADING)
539 return; 226 return;
540 // Note that even though we get here when the service has 227 // Note that even though we get here when the service has
541 // reappeared after disappearing earlier in the activation 228 // reappeared after disappearing earlier in the activation
542 // process, there's no need to re-establish the NetworkObserver, 229 // process, there's no need to re-establish the NetworkObserver,
543 // because the service path remains the same. 230 // because the service path remains the same.
rkc 2012/06/05 21:48:03 Fix outdated/now incorrect comment.
zel 2012/06/05 23:02:35 Done.
544 EvaluateCellularNetwork(GetCellularNetwork(service_path_)); 231 EvaluateCellularNetwork(FindCellularNetworkByMeid(meid_, true));
545 } 232 }
546 233
547 void MobileSetupHandler::OnNetworkChanged(chromeos::NetworkLibrary* cros, 234 void MobileActivator::OnNetworkChanged(NetworkLibrary* cros,
548 const chromeos::Network* network) { 235 const Network* network) {
549 if (state_ == PLAN_ACTIVATION_PAGE_LOADING) 236 if (state_ == PLAN_ACTIVATION_PAGE_LOADING)
550 return; 237 return;
551 DCHECK(network && network->type() == chromeos::TYPE_CELLULAR); 238
552 EvaluateCellularNetwork(GetCellularNetwork(network->service_path())); 239 if (!network || network->type() != TYPE_CELLULAR) {
240 NOTREACHED();
241 return;
242 }
243
244 EvaluateCellularNetwork(
245 static_cast<CellularNetwork*>(const_cast<Network*>(network)));
553 } 246 }
554 247
555 void MobileSetupHandler::HandleStartActivation(const ListValue* args) { 248 void MobileActivator::AddObserver(MobileActivator::Observer* observer) {
249 observers_.AddObserver(observer);
250 }
251
252 void MobileActivator::RemoveObserver(MobileActivator::Observer* observer) {
253 observers_.RemoveObserver(observer);
254 }
255
256 void MobileActivator::InitiateActivation(const std::string& service_path) {
257 CellularNetwork* network = FindCellularNetwork(service_path);
258 if (!network) {
259 LOG(ERROR) << "Cellular service can't be found: " << service_path;
260 return;
261 }
262
263 NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
264 const chromeos::NetworkDevice* device =
265 lib->FindNetworkDeviceByPath(network->device_path());
266 if (!device) {
267 LOG(ERROR) << "Cellular device can't be found: " << network->device_path();
268 return;
269 }
270
271 terminated_ = false;
272 meid_ = device->meid();
273 service_path_ = service_path;
556 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, 274 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
557 base::Bind(&CellularConfigDocument::LoadCellularConfigFile, 275 base::Bind(&CellularConfigDocument::LoadCellularConfigFile,
558 cellular_config_.get()), 276 cellular_config_.get()),
559 base::Bind(&MobileSetupHandler::StartActivationOnUIThread, AsWeakPtr())); 277 base::Bind(&MobileActivator::ContinueActivation,
278 base::Unretained(this)));
560 } 279 }
561 280
562 void MobileSetupHandler::HandleSetTransactionStatus(const ListValue* args) { 281 void MobileActivator::ContinueActivation() {
563 const size_t kSetTransactionStatusParamCount = 1; 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
564 if (args->GetSize() != kSetTransactionStatusParamCount) 283 CellularNetwork* network = FindCellularNetworkByMeid(meid_, false);
284 if (!network || !network->SupportsActivation())
rkc 2012/06/05 21:48:03 A NULL check for network is already being done abo
zel 2012/06/05 23:02:35 In theory FindCellularNetworkByMeid() can return N
565 return; 285 return;
566 // Get change callback function name. 286
567 std::string status; 287 DCHECK(!CrosLibrary::Get()->GetNetworkLibrary()->IsLocked());
568 if (!args->GetString(0, &status)) 288 SetupActivationProcess(network);
569 return; 289
570 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 290 StartActivation();
571 base::Bind(&MobileSetupHandler::SetTransactionStatus, AsWeakPtr(),
572 status));
573 } 291 }
574 292
575 void MobileSetupHandler::HandlePaymentPortalLoad(const ListValue* args) { 293 void MobileActivator::OnSetTransactionStatus(bool success) {
576 const size_t kPaymentPortalLoadParamCount = 1; 294 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
577 if (args->GetSize() != kPaymentPortalLoadParamCount) 295 base::Bind(&MobileActivator::SetTransactionStatus,
578 return; 296 base::Unretained(this),
579 // Get change callback function name. 297 success));
580 std::string result; 298 }
581 if (!args->GetString(0, &result)) 299
582 return; 300 void MobileActivator::OnPortalLoaded(bool success) {
583 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_); 301 CellularNetwork* network = FindCellularNetworkByMeid(meid_, true);
584 if (!network) { 302 if (!network) {
585 ChangeState(NULL, PLAN_ACTIVATION_ERROR, 303 ChangeState(NULL, PLAN_ACTIVATION_ERROR,
586 GetErrorMessage(kErrorNoService)); 304 GetErrorMessage(kErrorNoService));
587 return; 305 return;
588 } 306 }
589 if (state_ == PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING || 307 if (state_ == PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING ||
590 state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) { 308 state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) {
591 if (LowerCaseEqualsASCII(result, kJsApiResultOK)) { 309 if (success) {
592 payment_reconnect_count_ = 0; 310 payment_reconnect_count_ = 0;
593 ChangeState(network, PLAN_ACTIVATION_SHOWING_PAYMENT, std::string()); 311 ChangeState(network, PLAN_ACTIVATION_SHOWING_PAYMENT, std::string());
594 } else { 312 } else {
595 payment_reconnect_count_++; 313 payment_reconnect_count_++;
596 if (payment_reconnect_count_ > kMaxPortalReconnectCount) { 314 if (payment_reconnect_count_ > kMaxPortalReconnectCount) {
597 ChangeState(NULL, PLAN_ACTIVATION_ERROR, 315 ChangeState(NULL, PLAN_ACTIVATION_ERROR,
598 GetErrorMessage(kErrorNoService)); 316 GetErrorMessage(kErrorNoService));
599 return; 317 return;
600 } 318 }
601 // Disconnect now, this should force reconnection and we will retry to 319 // Disconnect now, this should force reconnection and we will retry to
602 // load the frame containing payment portal again. 320 // load the frame containing payment portal again.
603 DisconnectFromNetwork(network); 321 DisconnectFromNetwork(network);
604 } 322 }
605 } else { 323 } else {
606 NOTREACHED() << "Called paymentPortalLoad while in unexpected state: " 324 NOTREACHED() << "Called paymentPortalLoad while in unexpected state: "
607 << GetStateDescription(state_); 325 << GetStateDescription(state_);
608 } 326 }
609 } 327 }
610 328
611 void MobileSetupHandler::StartActivation() { 329 CellularNetwork* MobileActivator::FindCellularNetworkByMeid(
330 const std::string& meid, bool reattach_observer) {
331 NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
332 for (CellularNetworkVector::const_iterator it =
333 lib->cellular_networks().begin();
334 it != lib->cellular_networks().end(); ++it) {
335 const chromeos::NetworkDevice* device =
336 lib->FindNetworkDeviceByPath((*it)->device_path());
337 if (device && meid == device->meid()) {
338 CellularNetwork* network = *it;
339 // If service path has changed, reattach the event observer for this
340 // network service.
341 if (reattach_observer && service_path_ != network->service_path()) {
342 lib->RemoveObserverForAllNetworks(this);
343 lib->AddNetworkObserver(network->service_path(), this);
344 service_path_ = network->service_path();
345 }
346 return network;
347 }
348 }
349 return NULL;
350 }
351
352 void MobileActivator::StartActivation() {
612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
613 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupStart", 1); 354 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupStart", 1);
614 chromeos::NetworkLibrary* lib = 355 NetworkLibrary* lib =
615 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); 356 CrosLibrary::Get()->GetNetworkLibrary();
616 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_); 357 CellularNetwork* network = FindCellularNetworkByMeid(meid_, true);
617 // Check if we can start activation process. 358 // Check if we can start activation process.
618 if (!network || already_running_) { 359 if (!network) {
619 std::string error; 360 std::string error;
620 if (already_running_) 361 if (!lib->cellular_available())
621 error = kErrorAlreadyRunning;
622 else if (!lib->cellular_available())
623 error = kErrorNoDevice; 362 error = kErrorNoDevice;
624 else if (!lib->cellular_enabled()) 363 else if (!lib->cellular_enabled())
625 error = kErrorDisabled; 364 error = kErrorDisabled;
626 else 365 else
627 error = kErrorNoService; 366 error = kErrorNoService;
628 ChangeState(NULL, PLAN_ACTIVATION_ERROR, GetErrorMessage(error)); 367 ChangeState(NULL, PLAN_ACTIVATION_ERROR, GetErrorMessage(error));
629 return; 368 return;
630 } 369 }
631 370
632 // Start monitoring network property changes. 371 // Start monitoring network property changes.
633 lib->AddNetworkManagerObserver(this); 372 lib->AddNetworkManagerObserver(this);
634 lib->RemoveObserverForAllNetworks(this); 373 lib->RemoveObserverForAllNetworks(this);
635 lib->AddNetworkObserver(network->service_path(), this); 374 lib->AddNetworkObserver(network->service_path(), this);
636 // Try to start with OTASP immediately if we have received payment recently. 375 // Try to start with OTASP immediately if we have received payment recently.
637 state_ = lib->HasRecentCellularPlanPayment() ? 376 state_ = lib->HasRecentCellularPlanPayment() ?
638 PLAN_ACTIVATION_START_OTASP : 377 PLAN_ACTIVATION_START_OTASP :
639 PLAN_ACTIVATION_START; 378 PLAN_ACTIVATION_START;
640 EvaluateCellularNetwork(network); 379 EvaluateCellularNetwork(network);
641 } 380 }
642 381
643 void MobileSetupHandler::RetryOTASP() { 382 void MobileActivator::RetryOTASP() {
644 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
645 DCHECK(state_ == PLAN_ACTIVATION_DELAY_OTASP); 384 DCHECK(state_ == PLAN_ACTIVATION_DELAY_OTASP);
646 StartOTASP(); 385 StartOTASP();
647 } 386 }
648 387
649 void MobileSetupHandler::ContinueConnecting(int delay) { 388 void MobileActivator::ContinueConnecting(int delay) {
650 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
651 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_); 390 CellularNetwork* network = FindCellularNetworkByMeid(meid_, true);
652 if (network && network->connecting_or_connected()) { 391 if (network && network->connecting_or_connected()) {
653 EvaluateCellularNetwork(network); 392 EvaluateCellularNetwork(network);
654 } else { 393 } else {
655 ConnectToNetwork(network, delay); 394 ConnectToNetwork(network, delay);
656 } 395 }
657 } 396 }
658 397
659 void MobileSetupHandler::SetTransactionStatus(const std::string& status) { 398 void MobileActivator::SetTransactionStatus(bool success) {
660 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
661 // The payment is received, try to reconnect and check the status all over 400 // The payment is received, try to reconnect and check the status all over
662 // again. 401 // again.
663 if (LowerCaseEqualsASCII(status, kJsApiResultOK) && 402 if (success && state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) {
664 state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) { 403 NetworkLibrary* lib =
665 chromeos::NetworkLibrary* lib = 404 CrosLibrary::Get()->GetNetworkLibrary();
666 chromeos::CrosLibrary::Get()->GetNetworkLibrary();
667 lib->SignalCellularPlanPayment(); 405 lib->SignalCellularPlanPayment();
668 UMA_HISTOGRAM_COUNTS("Cellular.PaymentReceived", 1); 406 UMA_HISTOGRAM_COUNTS("Cellular.PaymentReceived", 1);
669 StartOTASP(); 407 StartOTASP();
670 } else { 408 } else {
671 UMA_HISTOGRAM_COUNTS("Cellular.PaymentFailed", 1); 409 UMA_HISTOGRAM_COUNTS("Cellular.PaymentFailed", 1);
672 } 410 }
673 } 411 }
674 412
675 void MobileSetupHandler::StartOTASP() { 413 void MobileActivator::StartOTASP() {
676 state_ = PLAN_ACTIVATION_START_OTASP; 414 state_ = PLAN_ACTIVATION_START_OTASP;
677 chromeos::CellularNetwork* network = GetCellularNetwork(); 415 CellularNetwork* network = FindCellularNetworkByMeid(meid_, true);
678 if (network && 416 if (network &&
679 network->connected() && 417 network->connected() &&
680 network->activation_state() == chromeos::ACTIVATION_STATE_ACTIVATED) { 418 network->activation_state() == ACTIVATION_STATE_ACTIVATED) {
681 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> 419 CrosLibrary::Get()->GetNetworkLibrary()->
682 DisconnectFromNetwork(network); 420 DisconnectFromNetwork(network);
683 } else { 421 } else {
684 EvaluateCellularNetwork(network); 422 EvaluateCellularNetwork(network);
685 } 423 }
686 } 424 }
687 425
688 void MobileSetupHandler::ReconnectTimerFired() { 426 void MobileActivator::ReconnectTimerFired() {
689 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
690 // Permit network connection changes only in reconnecting states. 428 // Permit network connection changes only in reconnecting states.
691 if (state_ != PLAN_ACTIVATION_RECONNECTING_OTASP_TRY && 429 if (state_ != PLAN_ACTIVATION_RECONNECTING_OTASP_TRY &&
692 state_ != PLAN_ACTIVATION_RECONNECTING && 430 state_ != PLAN_ACTIVATION_RECONNECTING &&
693 state_ != PLAN_ACTIVATION_RECONNECTING_PAYMENT && 431 state_ != PLAN_ACTIVATION_RECONNECTING_PAYMENT &&
694 state_ != PLAN_ACTIVATION_RECONNECTING_OTASP) 432 state_ != PLAN_ACTIVATION_RECONNECTING_OTASP)
695 return; 433 return;
696 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_); 434 CellularNetwork* network = FindCellularNetworkByMeid(meid_, true);
697 if (!network) { 435 if (!network) {
698 // No service, try again since this is probably just transient condition. 436 // No service, try again since this is probably just transient condition.
699 LOG(WARNING) << "Service not present at reconnect attempt."; 437 LOG(WARNING) << "Service not present at reconnect attempt.";
700 } 438 }
701 EvaluateCellularNetwork(network); 439 EvaluateCellularNetwork(network);
702 } 440 }
703 441
704 void MobileSetupHandler::DisconnectFromNetwork( 442 void MobileActivator::DisconnectFromNetwork(CellularNetwork* network) {
705 chromeos::CellularNetwork* network) {
706 DCHECK(network); 443 DCHECK(network);
707 LOG(INFO) << "Disconnecting from: " << network->service_path(); 444 LOG(INFO) << "Disconnecting from: " << network->service_path();
708 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> 445 CrosLibrary::Get()->GetNetworkLibrary()->
709 DisconnectFromNetwork(network); 446 DisconnectFromNetwork(network);
710 // Disconnect will force networks to be reevaluated, so 447 // Disconnect will force networks to be reevaluated, so
711 // we don't want to continue processing on this path anymore. 448 // we don't want to continue processing on this path anymore.
712 evaluating_ = false; 449 evaluating_ = false;
713 } 450 }
714 451
715 bool MobileSetupHandler::NeedsReconnecting( 452 bool MobileActivator::NeedsReconnecting(CellularNetwork* network,
716 chromeos::CellularNetwork* network, 453 PlanActivationState* new_state,
717 PlanActivationState* new_state, 454 std::string* error_description) {
718 std::string* error_description) {
719 DCHECK(network); 455 DCHECK(network);
720 if (!network->failed() && !ConnectionTimeout()) 456 if (!network->failed() && !ConnectionTimeout())
721 return false; 457 return false;
722 458
723 // Try to reconnect again if reconnect failed, or if for some 459 // Try to reconnect again if reconnect failed, or if for some
724 // reasons we are still not connected after 45 seconds. 460 // reasons we are still not connected after 45 seconds.
725 int max_retries = (state_ == PLAN_ACTIVATION_RECONNECTING_OTASP) ? 461 int max_retries = (state_ == PLAN_ACTIVATION_RECONNECTING_OTASP) ?
726 kMaxConnectionRetryOTASP : kMaxConnectionRetry; 462 kMaxConnectionRetryOTASP : kMaxConnectionRetry;
727 if (connection_retry_count_ < max_retries) { 463 if (connection_retry_count_ < max_retries) {
728 UMA_HISTOGRAM_COUNTS("Cellular.ConnectionRetry", 1); 464 UMA_HISTOGRAM_COUNTS("Cellular.ConnectionRetry", 1);
729 ConnectToNetwork(network, kFailedReconnectDelayMS); 465 ConnectToNetwork(network, kFailedReconnectDelayMS);
730 return true; 466 return true;
731 } 467 }
732 // We simply can't connect anymore after all these tries. 468 // We simply can't connect anymore after all these tries.
733 UMA_HISTOGRAM_COUNTS("Cellular.ConnectionFailed", 1); 469 UMA_HISTOGRAM_COUNTS("Cellular.ConnectionFailed", 1);
734 *new_state = PLAN_ACTIVATION_ERROR; 470 *new_state = PLAN_ACTIVATION_ERROR;
735 *error_description = GetErrorMessage(kFailedConnectivity); 471 *error_description = GetErrorMessage(kFailedConnectivity);
736 return false; 472 return false;
737 } 473 }
738 474
739 bool MobileSetupHandler::ConnectToNetwork( 475 bool MobileActivator::ConnectToNetwork(CellularNetwork* network, int delay) {
740 chromeos::CellularNetwork* network,
741 int delay) {
742 if (network && network->connecting_or_connected()) 476 if (network && network->connecting_or_connected())
743 return true; 477 return true;
744 // Permit network connection changes only in reconnecting states. 478 // Permit network connection changes only in reconnecting states.
745 if (state_ != PLAN_ACTIVATION_RECONNECTING_OTASP_TRY && 479 if (state_ != PLAN_ACTIVATION_RECONNECTING_OTASP_TRY &&
746 state_ != PLAN_ACTIVATION_RECONNECTING && 480 state_ != PLAN_ACTIVATION_RECONNECTING &&
747 state_ != PLAN_ACTIVATION_RECONNECTING_PAYMENT && 481 state_ != PLAN_ACTIVATION_RECONNECTING_PAYMENT &&
748 state_ != PLAN_ACTIVATION_RECONNECTING_OTASP) return false; 482 state_ != PLAN_ACTIVATION_RECONNECTING_OTASP) return false;
749 if (network) 483 if (network)
750 LOG(INFO) << "Connecting to: " << network->service_path(); 484 LOG(INFO) << "Connecting to: " << network->service_path();
751 connection_retry_count_++; 485 connection_retry_count_++;
752 connection_start_time_ = base::Time::Now(); 486 connection_start_time_ = base::Time::Now();
753 if (!network) { 487 if (!network) {
754 LOG(WARNING) << "Connect failed." 488 LOG(WARNING) << "Connect failed."
755 << (network ? network->service_path().c_str() : "no service"); 489 << (network ? network->service_path().c_str() : "no service");
756 // If we coudn't connect during reconnection phase, try to reconnect 490 // If we coudn't connect during reconnection phase, try to reconnect
757 // with a delay (and try to reconnect if needed). 491 // with a delay (and try to reconnect if needed).
758 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, 492 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
759 base::Bind(&MobileSetupHandler::ContinueConnecting, AsWeakPtr(), delay), 493 base::Bind(&MobileActivator::ContinueConnecting,
494 base::Unretained(this), delay),
rkc 2012/06/05 21:48:03 Though this class is Singleton, it will be destruc
zel 2012/06/05 23:02:35 Done.
760 base::TimeDelta::FromMilliseconds(delay)); 495 base::TimeDelta::FromMilliseconds(delay));
761 return false; 496 return false;
762 } 497 }
763 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> 498 CrosLibrary::Get()->GetNetworkLibrary()->
764 ConnectToCellularNetwork(network); 499 ConnectToCellularNetwork(network);
765 return true; 500 return true;
766 } 501 }
767 502
768 void MobileSetupHandler::ForceReconnect( 503 void MobileActivator::ForceReconnect(CellularNetwork* network, int delay) {
769 chromeos::CellularNetwork* network,
770 int delay) {
771 DCHECK(network); 504 DCHECK(network);
772 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetry", 1); 505 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetry", 1);
773 // Reset reconnect metrics. 506 // Reset reconnect metrics.
774 connection_retry_count_ = 0; 507 connection_retry_count_ = 0;
775 connection_start_time_ = base::Time(); 508 connection_start_time_ = base::Time();
776 // First, disconnect... 509 // First, disconnect...
777 LOG(INFO) << "Disconnecting from " << network->service_path(); 510 LOG(INFO) << "Disconnecting from " << network->service_path();
778 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> 511 CrosLibrary::Get()->GetNetworkLibrary()->
779 DisconnectFromNetwork(network); 512 DisconnectFromNetwork(network);
780 // Check the network state 3s after we disconnect to make sure. 513 // Check the network state 3s after we disconnect to make sure.
781 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, 514 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
782 base::Bind(&MobileSetupHandler::ContinueConnecting, AsWeakPtr(), delay), 515 base::Bind(&MobileActivator::ContinueConnecting,
516 base::Unretained(this),
rkc 2012/06/05 21:48:03 Same as above.
517 delay),
783 base::TimeDelta::FromMilliseconds(delay)); 518 base::TimeDelta::FromMilliseconds(delay));
784 } 519 }
785 520
786 bool MobileSetupHandler::ConnectionTimeout() { 521 bool MobileActivator::ConnectionTimeout() {
787 return (base::Time::Now() - 522 return (base::Time::Now() -
788 connection_start_time_).InSeconds() > kConnectionTimeoutSeconds; 523 connection_start_time_).InSeconds() > kConnectionTimeoutSeconds;
789 } 524 }
790 525
791 void MobileSetupHandler::EvaluateCellularNetwork( 526 void MobileActivator::EvaluateCellularNetwork(CellularNetwork* network) {
792 chromeos::CellularNetwork* network) { 527 if (terminated_)
793 if (!web_ui())
794 return; 528 return;
795 529
796 PlanActivationState new_state = state_; 530 PlanActivationState new_state = state_;
797 if (!network) { 531 if (!network) {
798 LOG(WARNING) << "Cellular service lost"; 532 LOG(WARNING) << "Cellular service lost";
799 if (state_ == PLAN_ACTIVATION_RECONNECTING_OTASP_TRY || 533 if (state_ == PLAN_ACTIVATION_RECONNECTING_OTASP_TRY ||
800 state_ == PLAN_ACTIVATION_RECONNECTING || 534 state_ == PLAN_ACTIVATION_RECONNECTING ||
801 state_ == PLAN_ACTIVATION_RECONNECTING_PAYMENT || 535 state_ == PLAN_ACTIVATION_RECONNECTING_PAYMENT ||
802 state_ == PLAN_ACTIVATION_RECONNECTING_OTASP) { 536 state_ == PLAN_ACTIVATION_RECONNECTING_OTASP) {
803 // This might be the legit case when service is lost after activation. 537 // This might be the legit case when service is lost after activation.
(...skipping 12 matching lines...) Expand all
816 std::string error_description; 550 std::string error_description;
817 551
818 LOG(WARNING) << "Cellular:\n service=" << network->GetStateString().c_str() 552 LOG(WARNING) << "Cellular:\n service=" << network->GetStateString().c_str()
819 << "\n ui=" << GetStateDescription(state_) 553 << "\n ui=" << GetStateDescription(state_)
820 << "\n activation=" << network->GetActivationStateString().c_str() 554 << "\n activation=" << network->GetActivationStateString().c_str()
821 << "\n error=" << network->GetErrorString().c_str() 555 << "\n error=" << network->GetErrorString().c_str()
822 << "\n setvice_path=" << network->service_path().c_str(); 556 << "\n setvice_path=" << network->service_path().c_str();
823 switch (state_) { 557 switch (state_) {
824 case PLAN_ACTIVATION_START: { 558 case PLAN_ACTIVATION_START: {
825 switch (network->activation_state()) { 559 switch (network->activation_state()) {
826 case chromeos::ACTIVATION_STATE_ACTIVATED: { 560 case ACTIVATION_STATE_ACTIVATED: {
827 if (network->disconnected()) { 561 if (network->disconnected()) {
828 new_state = PLAN_ACTIVATION_RECONNECTING; 562 new_state = PLAN_ACTIVATION_RECONNECTING;
829 } else if (network->connected()) { 563 } else if (network->connected()) {
830 if (network->restricted_pool()) { 564 if (network->restricted_pool()) {
831 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 565 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
832 } else { 566 } else {
833 new_state = PLAN_ACTIVATION_DONE; 567 new_state = PLAN_ACTIVATION_DONE;
834 } 568 }
835 } 569 }
836 break; 570 break;
837 } 571 }
838 default: { 572 default: {
839 if (network->disconnected() || 573 if (network->disconnected() ||
840 network->state() == chromeos::STATE_ACTIVATION_FAILURE) { 574 network->state() == STATE_ACTIVATION_FAILURE) {
841 new_state = (network->activation_state() == 575 new_state = (network->activation_state() ==
842 chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED) ? 576 ACTIVATION_STATE_PARTIALLY_ACTIVATED) ?
843 PLAN_ACTIVATION_TRYING_OTASP : 577 PLAN_ACTIVATION_TRYING_OTASP :
844 PLAN_ACTIVATION_INITIATING_ACTIVATION; 578 PLAN_ACTIVATION_INITIATING_ACTIVATION;
845 } else if (network->connected()) { 579 } else if (network->connected()) {
846 DisconnectFromNetwork(network); 580 DisconnectFromNetwork(network);
847 return; 581 return;
848 } 582 }
849 break; 583 break;
850 } 584 }
851 } 585 }
852 break; 586 break;
853 } 587 }
854 case PLAN_ACTIVATION_START_OTASP: { 588 case PLAN_ACTIVATION_START_OTASP: {
855 switch (network->activation_state()) { 589 switch (network->activation_state()) {
856 case chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED: { 590 case ACTIVATION_STATE_PARTIALLY_ACTIVATED: {
857 if (network->disconnected()) { 591 if (network->disconnected()) {
858 new_state = PLAN_ACTIVATION_OTASP; 592 new_state = PLAN_ACTIVATION_OTASP;
859 } else if (network->connected()) { 593 } else if (network->connected()) {
860 DisconnectFromNetwork(network); 594 DisconnectFromNetwork(network);
861 return; 595 return;
862 } 596 }
863 break; 597 break;
864 } 598 }
865 case chromeos::ACTIVATION_STATE_ACTIVATED: 599 case ACTIVATION_STATE_ACTIVATED:
866 new_state = PLAN_ACTIVATION_RECONNECTING_OTASP; 600 new_state = PLAN_ACTIVATION_RECONNECTING_OTASP;
867 break; 601 break;
868 default: { 602 default: {
869 LOG(WARNING) << "Unexpected activation state for device " 603 LOG(WARNING) << "Unexpected activation state for device "
870 << network->service_path().c_str(); 604 << network->service_path().c_str();
871 break; 605 break;
872 } 606 }
873 } 607 }
874 break; 608 break;
875 } 609 }
876 case PLAN_ACTIVATION_DELAY_OTASP: 610 case PLAN_ACTIVATION_DELAY_OTASP:
877 // Just ignore any changes until the OTASP retry timer kicks in. 611 // Just ignore any changes until the OTASP retry timer kicks in.
878 evaluating_ = false; 612 evaluating_ = false;
879 return; 613 return;
880 case PLAN_ACTIVATION_INITIATING_ACTIVATION: { 614 case PLAN_ACTIVATION_INITIATING_ACTIVATION: {
881 switch (network->activation_state()) { 615 switch (network->activation_state()) {
882 case chromeos::ACTIVATION_STATE_ACTIVATED: 616 case ACTIVATION_STATE_ACTIVATED:
883 case chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED: 617 case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
884 new_state = PLAN_ACTIVATION_START; 618 new_state = PLAN_ACTIVATION_START;
885 break; 619 break;
886 case chromeos::ACTIVATION_STATE_NOT_ACTIVATED: 620 case ACTIVATION_STATE_NOT_ACTIVATED:
887 case chromeos::ACTIVATION_STATE_ACTIVATING: 621 case ACTIVATION_STATE_ACTIVATING:
888 // Wait in this state until activation state changes. 622 // Wait in this state until activation state changes.
889 break; 623 break;
890 default: 624 default:
891 break; 625 break;
892 } 626 }
893 break; 627 break;
894 } 628 }
895 case PLAN_ACTIVATION_OTASP: 629 case PLAN_ACTIVATION_OTASP:
896 case PLAN_ACTIVATION_TRYING_OTASP: { 630 case PLAN_ACTIVATION_TRYING_OTASP: {
897 switch (network->activation_state()) { 631 switch (network->activation_state()) {
898 case chromeos::ACTIVATION_STATE_ACTIVATED: 632 case ACTIVATION_STATE_ACTIVATED:
899 if (network->disconnected()) { 633 if (network->disconnected()) {
900 new_state = GetNextReconnectState(state_); 634 new_state = GetNextReconnectState(state_);
901 } else if (network->connected()) { 635 } else if (network->connected()) {
902 if (network->restricted_pool()) { 636 if (network->restricted_pool()) {
903 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 637 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
904 } else { 638 } else {
905 new_state = PLAN_ACTIVATION_DONE; 639 new_state = PLAN_ACTIVATION_DONE;
906 } 640 }
907 } 641 }
908 break; 642 break;
909 case chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED: 643 case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
910 if (network->connected()) { 644 if (network->connected()) {
911 if (network->restricted_pool()) 645 if (network->restricted_pool())
912 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 646 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
913 } else { 647 } else {
914 new_state = GetNextReconnectState(state_); 648 new_state = GetNextReconnectState(state_);
915 } 649 }
916 break; 650 break;
917 case chromeos::ACTIVATION_STATE_NOT_ACTIVATED: 651 case ACTIVATION_STATE_NOT_ACTIVATED:
918 case chromeos::ACTIVATION_STATE_ACTIVATING: 652 case ACTIVATION_STATE_ACTIVATING:
919 // Wait in this state until activation state changes. 653 // Wait in this state until activation state changes.
920 break; 654 break;
921 default: 655 default:
922 break; 656 break;
923 } 657 }
924 break; 658 break;
925 } 659 }
926 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY: 660 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY:
927 case PLAN_ACTIVATION_RECONNECTING_PAYMENT: 661 case PLAN_ACTIVATION_RECONNECTING_PAYMENT:
928 case PLAN_ACTIVATION_RECONNECTING: { 662 case PLAN_ACTIVATION_RECONNECTING: {
929 if (network->connected()) { 663 if (network->connected()) {
930 // Make sure other networks are not interfering with our detection of 664 // Make sure other networks are not interfering with our detection of
931 // restricted pool. 665 // restricted pool.
932 DisableOtherNetworks(); 666 DisableOtherNetworks();
933 // Wait until the service shows up and gets activated. 667 // Wait until the service shows up and gets activated.
934 switch (network->activation_state()) { 668 switch (network->activation_state()) {
935 case chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED: 669 case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
936 case chromeos::ACTIVATION_STATE_ACTIVATED: 670 case ACTIVATION_STATE_ACTIVATED:
937 if (network->restricted_pool()) { 671 if (network->restricted_pool()) {
938 if (network->error() == chromeos::ERROR_DNS_LOOKUP_FAILED) { 672 if (network->error() == ERROR_DNS_LOOKUP_FAILED) {
939 LOG(WARNING) << "No connectivity for device " 673 LOG(WARNING) << "No connectivity for device "
940 << network->service_path().c_str(); 674 << network->service_path().c_str();
941 // If we are connected but there is no connectivity at all, 675 // If we are connected but there is no connectivity at all,
942 // restart the whole process again. 676 // restart the whole process again.
943 if (activation_attempt_ < kMaxActivationAttempt) { 677 if (activation_attempt_ < kMaxActivationAttempt) {
944 activation_attempt_++; 678 activation_attempt_++;
945 LOG(WARNING) << "Reconnect attempt #" 679 LOG(WARNING) << "Reconnect attempt #"
946 << activation_attempt_; 680 << activation_attempt_;
947 ForceReconnect(network, kFailedReconnectDelayMS); 681 ForceReconnect(network, kFailedReconnectDelayMS);
948 evaluating_ = false; 682 evaluating_ = false;
949 return; 683 return;
950 } else { 684 } else {
951 new_state = PLAN_ACTIVATION_ERROR; 685 new_state = PLAN_ACTIVATION_ERROR;
952 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetryFailure", 1); 686 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetryFailure", 1);
953 error_description = GetErrorMessage(kFailedConnectivity); 687 error_description = GetErrorMessage(kFailedConnectivity);
954 } 688 }
955 } else { 689 } else {
956 // If we have already received payment, don't show the payment 690 // If we have already received payment, don't show the payment
957 // page again. We should try to reconnect after some 691 // page again. We should try to reconnect after some
958 // time instead. 692 // time instead.
959 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 693 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
960 } 694 }
961 } else if (network->activation_state() == 695 } else if (network->activation_state() ==
962 chromeos::ACTIVATION_STATE_ACTIVATED) { 696 ACTIVATION_STATE_ACTIVATED) {
963 new_state = PLAN_ACTIVATION_DONE; 697 new_state = PLAN_ACTIVATION_DONE;
964 } 698 }
965 break; 699 break;
966 default: 700 default:
967 break; 701 break;
968 } 702 }
969 } else if (NeedsReconnecting(network, &new_state, &error_description)) { 703 } else if (NeedsReconnecting(network, &new_state, &error_description)) {
970 evaluating_ = false; 704 evaluating_ = false;
971 return; 705 return;
972 } 706 }
973 break; 707 break;
974 } 708 }
975 case PLAN_ACTIVATION_RECONNECTING_OTASP: { 709 case PLAN_ACTIVATION_RECONNECTING_OTASP: {
976 if (network->connected()) { 710 if (network->connected()) {
977 // Make sure other networks are not interfering with our detection of 711 // Make sure other networks are not interfering with our detection of
978 // restricted pool. 712 // restricted pool.
979 DisableOtherNetworks(); 713 DisableOtherNetworks();
980 // Wait until the service shows up and gets activated. 714 // Wait until the service shows up and gets activated.
981 switch (network->activation_state()) { 715 switch (network->activation_state()) {
982 case chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED: 716 case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
983 case chromeos::ACTIVATION_STATE_ACTIVATED: 717 case ACTIVATION_STATE_ACTIVATED:
984 if (network->restricted_pool()) { 718 if (network->restricted_pool()) {
985 LOG(WARNING) << "Still no connectivity after OTASP for device " 719 LOG(WARNING) << "Still no connectivity after OTASP for device "
986 << network->service_path().c_str(); 720 << network->service_path().c_str();
987 // If we have already received payment, don't show the payment 721 // If we have already received payment, don't show the payment
988 // page again. We should try to reconnect after some time instead. 722 // page again. We should try to reconnect after some time instead.
989 if (reconnect_wait_count_ < kMaxReconnectAttemptOTASP) { 723 if (reconnect_wait_count_ < kMaxReconnectAttemptOTASP) {
990 reconnect_wait_count_++; 724 reconnect_wait_count_++;
991 LOG(WARNING) << "OTASP reconnect attempt #" 725 LOG(WARNING) << "OTASP reconnect attempt #"
992 << reconnect_wait_count_; 726 << reconnect_wait_count_;
993 ForceReconnect(network, kPostPaymentReconnectDelayMS); 727 ForceReconnect(network, kPostPaymentReconnectDelayMS);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 case PLAN_ACTIVATION_ERROR: 759 case PLAN_ACTIVATION_ERROR:
1026 break; 760 break;
1027 } 761 }
1028 762
1029 if (new_state != PLAN_ACTIVATION_ERROR && 763 if (new_state != PLAN_ACTIVATION_ERROR &&
1030 GotActivationError(network, &error_description)) { 764 GotActivationError(network, &error_description)) {
1031 // Check for this special case when we try to do activate partially 765 // Check for this special case when we try to do activate partially
1032 // activated device. If that attempt failed, try to disconnect to clear the 766 // activated device. If that attempt failed, try to disconnect to clear the
1033 // state and reconnect again. 767 // state and reconnect again.
1034 if ((network->activation_state() == 768 if ((network->activation_state() ==
1035 chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED || 769 ACTIVATION_STATE_PARTIALLY_ACTIVATED ||
1036 network->activation_state() == chromeos::ACTIVATION_STATE_ACTIVATING) && 770 network->activation_state() == ACTIVATION_STATE_ACTIVATING) &&
1037 (network->error() == chromeos::ERROR_NO_ERROR || 771 (network->error() == ERROR_NO_ERROR ||
1038 network->error() == chromeos::ERROR_OTASP_FAILED) && 772 network->error() == ERROR_OTASP_FAILED) &&
1039 network->state() == chromeos::STATE_ACTIVATION_FAILURE) { 773 network->state() == STATE_ACTIVATION_FAILURE) {
1040 LOG(WARNING) << "Activation failure detected " 774 LOG(WARNING) << "Activation failure detected "
1041 << network->service_path().c_str(); 775 << network->service_path().c_str();
1042 switch (state_) { 776 switch (state_) {
1043 case PLAN_ACTIVATION_OTASP: 777 case PLAN_ACTIVATION_OTASP:
1044 case PLAN_ACTIVATION_RECONNECTING_OTASP: 778 case PLAN_ACTIVATION_RECONNECTING_OTASP:
1045 new_state = PLAN_ACTIVATION_DELAY_OTASP; 779 new_state = PLAN_ACTIVATION_DELAY_OTASP;
1046 break; 780 break;
1047 case PLAN_ACTIVATION_TRYING_OTASP: 781 case PLAN_ACTIVATION_TRYING_OTASP:
1048 new_state = PLAN_ACTIVATION_RECONNECTING_OTASP_TRY; 782 new_state = PLAN_ACTIVATION_RECONNECTING_OTASP_TRY;
1049 break; 783 break;
(...skipping 21 matching lines...) Expand all
1071 } 805 }
1072 } 806 }
1073 807
1074 if (new_state == PLAN_ACTIVATION_ERROR && !error_description.length()) 808 if (new_state == PLAN_ACTIVATION_ERROR && !error_description.length())
1075 error_description = GetErrorMessage(kErrorDefault); 809 error_description = GetErrorMessage(kErrorDefault);
1076 810
1077 ChangeState(network, new_state, error_description); 811 ChangeState(network, new_state, error_description);
1078 evaluating_ = false; 812 evaluating_ = false;
1079 } 813 }
1080 814
1081 MobileSetupHandler::PlanActivationState 815 MobileActivator::PlanActivationState MobileActivator::GetNextReconnectState(
1082 MobileSetupHandler::GetNextReconnectState( 816 MobileActivator::PlanActivationState state) {
1083 MobileSetupHandler::PlanActivationState state) {
1084 switch (state) { 817 switch (state) {
1085 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 818 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
1086 return PLAN_ACTIVATION_RECONNECTING; 819 return PLAN_ACTIVATION_RECONNECTING;
1087 case PLAN_ACTIVATION_OTASP: 820 case PLAN_ACTIVATION_OTASP:
1088 return PLAN_ACTIVATION_RECONNECTING_OTASP; 821 return PLAN_ACTIVATION_RECONNECTING_OTASP;
1089 case PLAN_ACTIVATION_TRYING_OTASP: 822 case PLAN_ACTIVATION_TRYING_OTASP:
1090 return PLAN_ACTIVATION_RECONNECTING_OTASP_TRY; 823 return PLAN_ACTIVATION_RECONNECTING_OTASP_TRY;
1091 default: 824 default:
1092 return PLAN_ACTIVATION_RECONNECTING; 825 return PLAN_ACTIVATION_RECONNECTING;
1093 } 826 }
1094 } 827 }
1095 828
1096 // Debugging helper function, will take it out at the end. 829 // Debugging helper function, will take it out at the end.
1097 const char* MobileSetupHandler::GetStateDescription( 830 const char* MobileActivator::GetStateDescription(PlanActivationState state) {
1098 PlanActivationState state) {
1099 switch (state) { 831 switch (state) {
1100 case PLAN_ACTIVATION_PAGE_LOADING: 832 case PLAN_ACTIVATION_PAGE_LOADING:
1101 return "PAGE_LOADING"; 833 return "PAGE_LOADING";
1102 case PLAN_ACTIVATION_START: 834 case PLAN_ACTIVATION_START:
1103 return "ACTIVATION_START"; 835 return "ACTIVATION_START";
1104 case PLAN_ACTIVATION_TRYING_OTASP: 836 case PLAN_ACTIVATION_TRYING_OTASP:
1105 return "TRYING_OTASP"; 837 return "TRYING_OTASP";
1106 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY: 838 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY:
1107 return "RECONNECTING_OTASP_TRY"; 839 return "RECONNECTING_OTASP_TRY";
1108 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 840 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
(...skipping 16 matching lines...) Expand all
1125 return "RECONNECTING_OTASP"; 857 return "RECONNECTING_OTASP";
1126 case PLAN_ACTIVATION_DONE: 858 case PLAN_ACTIVATION_DONE:
1127 return "DONE"; 859 return "DONE";
1128 case PLAN_ACTIVATION_ERROR: 860 case PLAN_ACTIVATION_ERROR:
1129 return "ERROR"; 861 return "ERROR";
1130 } 862 }
1131 return "UNKNOWN"; 863 return "UNKNOWN";
1132 } 864 }
1133 865
1134 866
1135 void MobileSetupHandler::CompleteActivation( 867 void MobileActivator::CompleteActivation(
1136 chromeos::CellularNetwork* network) { 868 CellularNetwork* network) {
1137 // Remove observers, we are done with this page. 869 // Remove observers, we are done with this page.
1138 chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> 870 NetworkLibrary* lib = CrosLibrary::Get()->
1139 GetNetworkLibrary(); 871 GetNetworkLibrary();
1140 lib->RemoveNetworkManagerObserver(this); 872 lib->RemoveNetworkManagerObserver(this);
1141 lib->RemoveObserverForAllNetworks(this); 873 lib->RemoveObserverForAllNetworks(this);
1142 if (lib->IsLocked()) 874 if (lib->IsLocked())
1143 lib->Unlock(); 875 lib->Unlock();
1144 // If we have successfully activated the connection, set autoconnect flag. 876 // If we have successfully activated the connection, set autoconnect flag.
1145 if (network) 877 if (network)
1146 network->SetAutoConnect(true); 878 network->SetAutoConnect(true);
1147 // Reactivate other types of connections if we have 879 // Reactivate other types of connections if we have
1148 // shut them down previously. 880 // shut them down previously.
1149 ReEnableOtherConnections(); 881 ReEnableOtherConnections();
1150 } 882 }
1151 883
1152 void MobileSetupHandler::UpdatePage( 884 bool MobileActivator::RunningActivation() const {
1153 chromeos::CellularNetwork* network, 885 return !(state_ == PLAN_ACTIVATION_DONE ||
1154 const std::string& error_description) { 886 state_ == PLAN_ACTIVATION_ERROR ||
1155 DictionaryValue device_dict; 887 state_ == PLAN_ACTIVATION_PAGE_LOADING);
1156 if (network)
1157 GetDeviceInfo(network, &device_dict);
1158 device_dict.SetInteger("state", state_);
1159 if (error_description.length())
1160 device_dict.SetString("error", error_description);
1161 web_ui()->CallJavascriptFunction(
1162 kJsDeviceStatusChangedCallback, device_dict);
1163 } 888 }
1164 889
1165 890 void MobileActivator::ChangeState(CellularNetwork* network,
1166 void MobileSetupHandler::ChangeState(chromeos::CellularNetwork* network, 891 PlanActivationState new_state,
1167 PlanActivationState new_state, 892 const std::string& error_description) {
1168 const std::string& error_description) {
1169 static bool first_time = true; 893 static bool first_time = true;
1170 if (state_ == new_state && !first_time) 894 if (state_ == new_state && !first_time)
1171 return; 895 return;
1172 LOG(WARNING) << "Activation state flip old = " 896 LOG(WARNING) << "Activation state flip old = "
1173 << GetStateDescription(state_) 897 << GetStateDescription(state_)
1174 << ", new = " << GetStateDescription(new_state); 898 << ", new = " << GetStateDescription(new_state);
1175 first_time = false; 899 first_time = false;
1176 900
1177 // Pick action that should happen on leaving the old state. 901 // Pick action that should happen on leaving the old state.
1178 switch (state_) { 902 switch (state_) {
1179 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY: 903 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY:
1180 case PLAN_ACTIVATION_RECONNECTING: 904 case PLAN_ACTIVATION_RECONNECTING:
1181 case PLAN_ACTIVATION_RECONNECTING_OTASP: 905 case PLAN_ACTIVATION_RECONNECTING_OTASP:
1182 if (reconnect_timer_.IsRunning()) { 906 if (reconnect_timer_.IsRunning()) {
1183 reconnect_timer_.Stop(); 907 reconnect_timer_.Stop();
1184 } 908 }
1185 break; 909 break;
1186 default: 910 default:
1187 break; 911 break;
1188 } 912 }
1189 state_ = new_state; 913 state_ = new_state;
1190 914
1191 // Signal to JS layer that the state is changing. 915 // Signal to observers layer that the state is changing.
1192 UpdatePage(network, error_description); 916 FOR_EACH_OBSERVER(Observer, observers_,
917 OnActivationStateChanged(network, state_, error_description));
1193 918
1194 // Pick action that should happen on entering the new state. 919 // Pick action that should happen on entering the new state.
1195 switch (new_state) { 920 switch (new_state) {
1196 case PLAN_ACTIVATION_START: 921 case PLAN_ACTIVATION_START:
1197 break; 922 break;
1198 case PLAN_ACTIVATION_DELAY_OTASP: { 923 case PLAN_ACTIVATION_DELAY_OTASP: {
1199 UMA_HISTOGRAM_COUNTS("Cellular.RetryOTASP", 1); 924 UMA_HISTOGRAM_COUNTS("Cellular.RetryOTASP", 1);
1200 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, 925 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
1201 base::Bind(&MobileSetupHandler::RetryOTASP, AsWeakPtr()), 926 base::Bind(&MobileActivator::RetryOTASP, base::Unretained(this)),
1202 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay)); 927 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay));
1203 break; 928 break;
1204 } 929 }
1205 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 930 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
1206 case PLAN_ACTIVATION_TRYING_OTASP: 931 case PLAN_ACTIVATION_TRYING_OTASP:
1207 case PLAN_ACTIVATION_OTASP: 932 case PLAN_ACTIVATION_OTASP:
1208 DCHECK(network); 933 DCHECK(network);
1209 LOG(WARNING) << "Activating service " << network->service_path().c_str(); 934 LOG(WARNING) << "Activating service " << network->service_path().c_str();
1210 UMA_HISTOGRAM_COUNTS("Cellular.ActivationTry", 1); 935 UMA_HISTOGRAM_COUNTS("Cellular.ActivationTry", 1);
1211 if (!network->StartActivation()) { 936 if (!network->StartActivation()) {
1212 UMA_HISTOGRAM_COUNTS("Cellular.ActivationFailure", 1); 937 UMA_HISTOGRAM_COUNTS("Cellular.ActivationFailure", 1);
1213 if (new_state == PLAN_ACTIVATION_OTASP) { 938 if (new_state == PLAN_ACTIVATION_OTASP) {
1214 ChangeState(network, PLAN_ACTIVATION_DELAY_OTASP, std::string()); 939 ChangeState(network, PLAN_ACTIVATION_DELAY_OTASP, std::string());
1215 } else { 940 } else {
1216 ChangeState(network, PLAN_ACTIVATION_ERROR, 941 ChangeState(network, PLAN_ACTIVATION_ERROR,
1217 GetErrorMessage(kFailedConnectivity)); 942 GetErrorMessage(kFailedConnectivity));
1218 } 943 }
1219 } 944 }
1220 break; 945 break;
1221 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY: 946 case PLAN_ACTIVATION_RECONNECTING_OTASP_TRY:
1222 case PLAN_ACTIVATION_RECONNECTING: 947 case PLAN_ACTIVATION_RECONNECTING:
1223 case PLAN_ACTIVATION_RECONNECTING_PAYMENT: 948 case PLAN_ACTIVATION_RECONNECTING_PAYMENT:
1224 case PLAN_ACTIVATION_RECONNECTING_OTASP: { 949 case PLAN_ACTIVATION_RECONNECTING_OTASP: {
1225 // Start reconnect timer. This will ensure that we are not left in 950 // Start reconnect timer. This will ensure that we are not left in
1226 // limbo by the network library. 951 // limbo by the network library.
1227 if (!reconnect_timer_.IsRunning()) { 952 if (!reconnect_timer_.IsRunning()) {
1228 reconnect_timer_.Start( 953 reconnect_timer_.Start(
1229 FROM_HERE, 954 FROM_HERE,
1230 base::TimeDelta::FromMilliseconds(kReconnectTimerDelayMS), 955 base::TimeDelta::FromMilliseconds(kReconnectTimerDelayMS),
1231 this, &MobileSetupHandler::ReconnectTimerFired); 956 this, &MobileActivator::ReconnectTimerFired);
1232 } 957 }
1233 // Reset connection metrics and try to connect. 958 // Reset connection metrics and try to connect.
1234 reconnect_wait_count_ = 0; 959 reconnect_wait_count_ = 0;
1235 connection_retry_count_ = 0; 960 connection_retry_count_ = 0;
1236 connection_start_time_ = base::Time::Now(); 961 connection_start_time_ = base::Time::Now();
1237 ConnectToNetwork(network, kReconnectDelayMS); 962 ConnectToNetwork(network, kReconnectDelayMS);
1238 break; 963 break;
1239 } 964 }
1240 case PLAN_ACTIVATION_PAGE_LOADING: 965 case PLAN_ACTIVATION_PAGE_LOADING:
1241 return; 966 return;
1242 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING: 967 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING:
1243 case PLAN_ACTIVATION_SHOWING_PAYMENT: 968 case PLAN_ACTIVATION_SHOWING_PAYMENT:
1244 // Fix for fix SSL for the walled gardens where cert chain verification 969 // Fix for fix SSL for the walled gardens where cert chain verification
1245 // might not work. 970 // might not work.
1246 break; 971 break;
1247 case PLAN_ACTIVATION_DONE: 972 case PLAN_ACTIVATION_DONE:
1248 DCHECK(network); 973 DCHECK(network);
1249 CompleteActivation(network); 974 CompleteActivation(network);
1250 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupSucceeded", 1); 975 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupSucceeded", 1);
1251 break; 976 break;
1252 case PLAN_ACTIVATION_ERROR: 977 case PLAN_ACTIVATION_ERROR:
1253 CompleteActivation(NULL); 978 CompleteActivation(NULL);
1254 UMA_HISTOGRAM_COUNTS("Cellular.PlanFailed", 1); 979 UMA_HISTOGRAM_COUNTS("Cellular.PlanFailed", 1);
1255 break; 980 break;
1256 default: 981 default:
1257 break; 982 break;
1258 } 983 }
1259 } 984 }
1260 985
1261 void MobileSetupHandler::ReEnableOtherConnections() { 986 void MobileActivator::ReEnableOtherConnections() {
1262 chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> 987 NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
1263 GetNetworkLibrary();
1264 if (reenable_ethernet_) { 988 if (reenable_ethernet_) {
1265 reenable_ethernet_ = false; 989 reenable_ethernet_ = false;
1266 lib->EnableEthernetNetworkDevice(true); 990 lib->EnableEthernetNetworkDevice(true);
1267 } 991 }
1268 if (reenable_wifi_) { 992 if (reenable_wifi_) {
1269 reenable_wifi_ = false; 993 reenable_wifi_ = false;
1270 lib->EnableWifiNetworkDevice(true); 994 lib->EnableWifiNetworkDevice(true);
1271 } 995 }
1272 996
1273 PrefService* prefs = g_browser_process->local_state(); 997 PrefService* prefs = g_browser_process->local_state();
1274 if (reenable_cert_check_) { 998 if (reenable_cert_check_) {
1275 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled, 999 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled,
1276 true); 1000 true);
1277 reenable_cert_check_ = false; 1001 reenable_cert_check_ = false;
1278 } 1002 }
1279 } 1003 }
1280 1004
1281 void MobileSetupHandler::SetupActivationProcess( 1005 void MobileActivator::SetupActivationProcess(CellularNetwork* network) {
1282 chromeos::CellularNetwork* network) {
1283 if (!network) 1006 if (!network)
1284 return; 1007 return;
1285 1008
1286 // Disable SSL cert checks since we will be doing this in 1009 // Disable SSL cert checks since we will be doing this in
1287 // restricted pool. 1010 // restricted pool.
1288 PrefService* prefs = g_browser_process->local_state(); 1011 PrefService* prefs = g_browser_process->local_state();
1289 if (!reenable_cert_check_ && 1012 if (!reenable_cert_check_ &&
1290 prefs->GetBoolean( 1013 prefs->GetBoolean(
1291 prefs::kCertRevocationCheckingEnabled)) { 1014 prefs::kCertRevocationCheckingEnabled)) {
1292 reenable_cert_check_ = true; 1015 reenable_cert_check_ = true;
1293 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled, false); 1016 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled, false);
1294 } 1017 }
1295 1018
1296 chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> 1019 NetworkLibrary* lib = CrosLibrary::Get()->
1297 GetNetworkLibrary(); 1020 GetNetworkLibrary();
1298 // Disable autoconnect to cellular network. 1021 // Disable autoconnect to cellular network.
1299 network->SetAutoConnect(false); 1022 network->SetAutoConnect(false);
1300 1023
1301 // Prevent any other network interference. 1024 // Prevent any other network interference.
1302 DisableOtherNetworks(); 1025 DisableOtherNetworks();
1303 lib->Lock(); 1026 lib->Lock();
1304 } 1027 }
1305 1028
1306 void MobileSetupHandler::DisableOtherNetworks() { 1029 void MobileActivator::DisableOtherNetworks() {
1307 chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> 1030 NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
1308 GetNetworkLibrary();
1309 // Disable ethernet and wifi. 1031 // Disable ethernet and wifi.
1310 if (lib->ethernet_enabled()) { 1032 if (lib->ethernet_enabled()) {
1311 reenable_ethernet_ = true; 1033 reenable_ethernet_ = true;
1312 lib->EnableEthernetNetworkDevice(false); 1034 lib->EnableEthernetNetworkDevice(false);
1313 } 1035 }
1314 if (lib->wifi_enabled()) { 1036 if (lib->wifi_enabled()) {
1315 reenable_wifi_ = true; 1037 reenable_wifi_ = true;
1316 lib->EnableWifiNetworkDevice(false); 1038 lib->EnableWifiNetworkDevice(false);
1317 } 1039 }
1318 } 1040 }
1319 1041
1320 bool MobileSetupHandler::GotActivationError( 1042 bool MobileActivator::GotActivationError(
1321 chromeos::CellularNetwork* network, std::string* error) { 1043 CellularNetwork* network, std::string* error) {
1322 DCHECK(network); 1044 DCHECK(network);
1323 bool got_error = false; 1045 bool got_error = false;
1324 const char* error_code = kErrorDefault; 1046 const char* error_code = kErrorDefault;
1325 1047
1326 // This is the magic for detection of errors in during activation process. 1048 // This is the magic for detection of errors in during activation process.
1327 if (network->state() == chromeos::STATE_FAILURE && 1049 if (network->state() == STATE_FAILURE &&
1328 network->error() == chromeos::ERROR_AAA_FAILED) { 1050 network->error() == ERROR_AAA_FAILED) {
1329 if (network->activation_state() == 1051 if (network->activation_state() ==
1330 chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED) { 1052 ACTIVATION_STATE_PARTIALLY_ACTIVATED) {
1331 error_code = kErrorBadConnectionPartial; 1053 error_code = kErrorBadConnectionPartial;
1332 } else if (network->activation_state() == 1054 } else if (network->activation_state() ==
1333 chromeos::ACTIVATION_STATE_ACTIVATED) { 1055 ACTIVATION_STATE_ACTIVATED) {
1334 if (network->roaming_state() == chromeos::ROAMING_STATE_HOME) { 1056 if (network->roaming_state() == ROAMING_STATE_HOME) {
1335 error_code = kErrorBadConnectionActivated; 1057 error_code = kErrorBadConnectionActivated;
1336 } else if (network->roaming_state() == chromeos::ROAMING_STATE_ROAMING) { 1058 } else if (network->roaming_state() == ROAMING_STATE_ROAMING) {
1337 error_code = kErrorRoamingOnConnection; 1059 error_code = kErrorRoamingOnConnection;
1338 } 1060 }
1339 } 1061 }
1340 got_error = true; 1062 got_error = true;
1341 } else if (network->state() == chromeos::STATE_ACTIVATION_FAILURE) { 1063 } else if (network->state() == STATE_ACTIVATION_FAILURE) {
1342 if (network->error() == chromeos::ERROR_NEED_EVDO) { 1064 if (network->error() == ERROR_NEED_EVDO) {
1343 if (network->activation_state() == 1065 if (network->activation_state() ==
1344 chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED) 1066 ACTIVATION_STATE_PARTIALLY_ACTIVATED)
1345 error_code = kErrorNoEVDO; 1067 error_code = kErrorNoEVDO;
1346 } else if (network->error() == chromeos::ERROR_NEED_HOME_NETWORK) { 1068 } else if (network->error() == ERROR_NEED_HOME_NETWORK) {
1347 if (network->activation_state() == 1069 if (network->activation_state() ==
1348 chromeos::ACTIVATION_STATE_NOT_ACTIVATED) { 1070 ACTIVATION_STATE_NOT_ACTIVATED) {
1349 error_code = kErrorRoamingActivation; 1071 error_code = kErrorRoamingActivation;
1350 } else if (network->activation_state() == 1072 } else if (network->activation_state() ==
1351 chromeos::ACTIVATION_STATE_PARTIALLY_ACTIVATED) { 1073 ACTIVATION_STATE_PARTIALLY_ACTIVATED) {
1352 error_code = kErrorRoamingPartiallyActivated; 1074 error_code = kErrorRoamingPartiallyActivated;
1353 } 1075 }
1354 } 1076 }
1355 got_error = true; 1077 got_error = true;
1356 } 1078 }
1357 1079
1358 if (got_error) 1080 if (got_error)
1359 *error = GetErrorMessage(error_code); 1081 *error = GetErrorMessage(error_code);
1360 1082
1361 return got_error; 1083 return got_error;
1362 } 1084 }
1363 1085
1364 void MobileSetupHandler::GetDeviceInfo(chromeos::CellularNetwork* network, 1086 void MobileActivator::GetDeviceInfo(CellularNetwork* network,
1365 DictionaryValue* value) { 1087 DictionaryValue* value) {
1366 DCHECK(network); 1088 DCHECK(network);
1367 chromeos::NetworkLibrary* cros = 1089 NetworkLibrary* cros =
1368 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); 1090 CrosLibrary::Get()->GetNetworkLibrary();
1369 if (!cros) 1091 if (!cros)
1370 return; 1092 return;
1371 value->SetString("carrier", network->name()); 1093 value->SetString("carrier", network->name());
1372 value->SetString("payment_url", network->payment_url()); 1094 value->SetString("payment_url", network->payment_url());
1373 if (network->using_post() && network->post_data().length()) 1095 if (network->using_post() && network->post_data().length())
1374 value->SetString("post_data", network->post_data()); 1096 value->SetString("post_data", network->post_data());
1375 1097
1376 const chromeos::NetworkDevice* device = 1098 const NetworkDevice* device =
1377 cros->FindNetworkDeviceByPath(network->device_path()); 1099 cros->FindNetworkDeviceByPath(network->device_path());
1378 if (device) { 1100 if (device) {
1379 value->SetString("MEID", device->meid()); 1101 value->SetString("MEID", device->meid());
1380 value->SetString("IMEI", device->imei()); 1102 value->SetString("IMEI", device->imei());
1381 value->SetString("MDN", device->mdn()); 1103 value->SetString("MDN", device->mdn());
1382 } 1104 }
1383 } 1105 }
1384 1106
1385 std::string MobileSetupHandler::GetErrorMessage(const std::string& code) { 1107 std::string MobileActivator::GetErrorMessage(const std::string& code) {
1386 return cellular_config_->GetErrorMessage(code); 1108 return cellular_config_->GetErrorMessage(code);
1387 } 1109 }
1388 1110
1389 void MobileSetupHandler::StartActivationOnUIThread() { 1111 } // namespace chromeos
1390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1391 chromeos::CellularNetwork* network = GetCellularNetwork(service_path_);
1392 if (!network || !network->SupportsActivation())
1393 return;
1394
1395 if (!chromeos::CrosLibrary::Get()->GetNetworkLibrary()->IsLocked())
1396 SetupActivationProcess(network);
1397 else
1398 already_running_ = true;
1399
1400 StartActivation();
1401 }
1402
1403 ////////////////////////////////////////////////////////////////////////////////
1404 //
1405 // MobileSetupUI
1406 //
1407 ////////////////////////////////////////////////////////////////////////////////
1408
1409 MobileSetupUI::MobileSetupUI(content::WebUI* web_ui)
1410 : WebUIController(web_ui) {
1411 chromeos::CellularNetwork* network = GetCellularNetwork();
1412 std::string service_path = network ? network->service_path() : std::string();
1413 web_ui->AddMessageHandler(new MobileSetupHandler(service_path));
1414 MobileSetupUIHTMLSource* html_source =
1415 new MobileSetupUIHTMLSource(service_path);
1416
1417 // Set up the chrome://mobilesetup/ source.
1418 Profile* profile = Profile::FromWebUI(web_ui);
1419 ChromeURLDataManager::AddDataSource(profile, html_source);
1420 }
1421
1422 void MobileSetupUI::RenderViewCreated(RenderViewHost* host) {
1423 // Destroyed by the corresponding RenderViewHost
1424 new PortalFrameLoadObserver(AsWeakPtr(), host);
1425 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698