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

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

Issue 22611005: Switch over MobileActivator to use Network*Handler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ws Created 7 years, 3 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/chromeos/mobile/mobile_activator.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 "ash/system/chromeos/network/network_connect.h" 11 #include "ash/system/chromeos/network/network_connect.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
16 #include "base/location.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/memory/ref_counted_memory.h" 18 #include "base/memory/ref_counted_memory.h"
18 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
19 #include "base/metrics/histogram.h" 20 #include "base/metrics/histogram.h"
20 #include "base/observer_list_threadsafe.h" 21 #include "base/observer_list_threadsafe.h"
21 #include "base/prefs/pref_service.h" 22 #include "base/prefs/pref_service.h"
22 #include "base/strings/string_piece.h" 23 #include "base/strings/string_piece.h"
23 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
25 #include "base/timer/timer.h" 26 #include "base/timer/timer.h"
26 #include "base/values.h" 27 #include "base/values.h"
27 #include "chrome/browser/browser_process.h" 28 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/chromeos/cros/network_library.h"
29 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
30 #include "chromeos/network/device_state.h"
31 #include "chromeos/network/network_activation_handler.h"
32 #include "chromeos/network/network_configuration_handler.h"
33 #include "chromeos/network/network_connection_handler.h"
34 #include "chromeos/network/network_event_log.h"
35 #include "chromeos/network/network_handler_callbacks.h"
36 #include "chromeos/network/network_state.h"
37 #include "chromeos/network/network_state_handler.h"
30 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
39 #include "third_party/cros_system_api/dbus/service_constants.h"
31 40
32 using content::BrowserThread; 41 using content::BrowserThread;
33 42
34 namespace { 43 namespace {
35 44
36 // Cellular configuration file path. 45 // Cellular configuration file path.
37 const char kCellularConfigPath[] = 46 const char kCellularConfigPath[] =
38 "/usr/share/chromeos-assets/mobile/mobile_config.json"; 47 "/usr/share/chromeos-assets/mobile/mobile_config.json";
39 48
40 // Cellular config file field names. 49 // Cellular config file field names.
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 //////////////////////////////////////////////////////////////////////////////// 153 ////////////////////////////////////////////////////////////////////////////////
145 // 154 //
146 // MobileActivator 155 // MobileActivator
147 // 156 //
148 //////////////////////////////////////////////////////////////////////////////// 157 ////////////////////////////////////////////////////////////////////////////////
149 MobileActivator::MobileActivator() 158 MobileActivator::MobileActivator()
150 : cellular_config_(new CellularConfigDocument()), 159 : cellular_config_(new CellularConfigDocument()),
151 state_(PLAN_ACTIVATION_PAGE_LOADING), 160 state_(PLAN_ACTIVATION_PAGE_LOADING),
152 reenable_cert_check_(false), 161 reenable_cert_check_(false),
153 terminated_(true), 162 terminated_(true),
163 pending_activation_request_(false),
154 connection_retry_count_(0), 164 connection_retry_count_(0),
165 initial_OTASP_attempts_(0),
166 trying_OTASP_attempts_(0),
167 final_OTASP_attempts_(0),
155 payment_reconnect_count_(0) { 168 payment_reconnect_count_(0) {
156 } 169 }
157 170
158 MobileActivator::~MobileActivator() { 171 MobileActivator::~MobileActivator() {
159 TerminateActivation(); 172 TerminateActivation();
160 } 173 }
161 174
162 MobileActivator* MobileActivator::GetInstance() { 175 MobileActivator* MobileActivator::GetInstance() {
163 return Singleton<MobileActivator>::get(); 176 return Singleton<MobileActivator>::get();
164 } 177 }
165 178
166 void MobileActivator::TerminateActivation() { 179 void MobileActivator::TerminateActivation() {
167 // We're exiting; don't continue with termination.
168 if (!NetworkLibrary::Get())
169 return;
170
171 state_duration_timer_.Stop(); 180 state_duration_timer_.Stop();
172 continue_reconnect_timer_.Stop(); 181 continue_reconnect_timer_.Stop();
173 reconnect_timeout_timer_.Stop(); 182 reconnect_timeout_timer_.Stop();
174 183
175 NetworkLibrary* lib = GetNetworkLibrary(); 184 if (NetworkHandler::IsInitialized())
176 lib->RemoveNetworkManagerObserver(this); 185 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
177 lib->RemoveObserverForAllNetworks(this); 186 FROM_HERE);
178 ReEnableCertRevocationChecking(); 187 ReEnableCertRevocationChecking();
179 meid_.clear(); 188 meid_.clear();
180 iccid_.clear(); 189 iccid_.clear();
181 service_path_.clear(); 190 service_path_.clear();
191 device_path_.clear();
182 state_ = PLAN_ACTIVATION_PAGE_LOADING; 192 state_ = PLAN_ACTIVATION_PAGE_LOADING;
183 reenable_cert_check_ = false; 193 reenable_cert_check_ = false;
184 terminated_ = true; 194 terminated_ = true;
185 // Release the previous cellular config and setup a new empty one. 195 // Release the previous cellular config and setup a new empty one.
186 cellular_config_ = new CellularConfigDocument(); 196 cellular_config_ = new CellularConfigDocument();
187 } 197 }
188 198
189 void MobileActivator::OnNetworkManagerChanged(NetworkLibrary* cros) { 199 void MobileActivator::DefaultNetworkChanged(const NetworkState* network) {
190 if (state_ == PLAN_ACTIVATION_PAGE_LOADING || 200 RefreshCellularNetworks();
191 state_ == PLAN_ACTIVATION_DONE || 201 }
192 state_ == PLAN_ACTIVATION_ERROR) { 202
203 void MobileActivator::NetworkPropertiesUpdated(const NetworkState* network) {
204 if (state_ == PLAN_ACTIVATION_PAGE_LOADING)
205 return;
206
207 if (!network || network->type() != flimflam::kTypeCellular)
208 return;
209
210 const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
211 GetDeviceState(network->device_path());
212 if (!device) {
213 LOG(ERROR) << "Cellular device can't be found: " << network->device_path();
214 return;
215 }
216 if (network->device_path() != device_path_) {
217 LOG(WARNING) << "Ignoring property update for cellular service "
218 << network->path()
219 << " on unknown device " << network->device_path()
220 << " (Stored device path = " << device_path_ << ")";
193 return; 221 return;
194 } 222 }
195 223
196 CellularNetwork* network = FindMatchingCellularNetwork(true); 224 // A modem reset leads to a new service path. Since we have verified that we
197 if (network && network->activate_over_non_cellular_network()) { 225 // are a cellular service on a still valid stored device path, update it.
198 bool waiting = (state_ == PLAN_ACTIVATION_WAITING_FOR_CONNECTION); 226 service_path_ = network->path();
199 bool is_online =
200 cros->connected_network() && cros->connected_network()->online();
201 if (waiting && is_online) {
202 ChangeState(network, post_reconnect_state_, "");
203 } else if (!waiting && !is_online) {
204 ChangeState(network, PLAN_ACTIVATION_WAITING_FOR_CONNECTION, "");
205 }
206 }
207 227
208 EvaluateCellularNetwork(network); 228 EvaluateCellularNetwork(network);
209 } 229 }
210 230
211 void MobileActivator::OnNetworkChanged(NetworkLibrary* cros,
212 const Network* network) {
213 if (state_ == PLAN_ACTIVATION_PAGE_LOADING)
214 return;
215
216 if (!network || network->type() != TYPE_CELLULAR) {
217 NOTREACHED();
218 return;
219 }
220
221 EvaluateCellularNetwork(
222 static_cast<CellularNetwork*>(const_cast<Network*>(network)));
223 }
224
225 void MobileActivator::AddObserver(MobileActivator::Observer* observer) { 231 void MobileActivator::AddObserver(MobileActivator::Observer* observer) {
226 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI)); 232 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
227 observers_.AddObserver(observer); 233 observers_.AddObserver(observer);
228 } 234 }
229 235
230 void MobileActivator::RemoveObserver(MobileActivator::Observer* observer) { 236 void MobileActivator::RemoveObserver(MobileActivator::Observer* observer) {
231 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI)); 237 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
232 observers_.RemoveObserver(observer); 238 observers_.RemoveObserver(observer);
233 } 239 }
234 240
235 void MobileActivator::InitiateActivation(const std::string& service_path) { 241 void MobileActivator::InitiateActivation(const std::string& service_path) {
236 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI)); 242 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::UI));
237 NetworkLibrary* lib = GetNetworkLibrary(); 243 const NetworkState* network = GetNetworkState(service_path);
238 CellularNetwork* network = lib->FindCellularNetworkByPath(service_path);
239 if (!network) { 244 if (!network) {
240 LOG(ERROR) << "Cellular service can't be found: " << service_path; 245 LOG(ERROR) << "Cellular service can't be found: " << service_path;
241 return; 246 return;
242 } 247 }
243 248
244 const chromeos::NetworkDevice* device = 249 const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
245 lib->FindNetworkDeviceByPath(network->device_path()); 250 GetDeviceState(network->device_path());
246 if (!device) { 251 if (!device) {
247 LOG(ERROR) << "Cellular device can't be found: " << network->device_path(); 252 LOG(ERROR) << "Cellular device can't be found: " << network->device_path();
248 return; 253 return;
249 } 254 }
250 255
251 terminated_ = false; 256 terminated_ = false;
252 meid_ = device->meid(); 257 meid_ = device->meid();
253 iccid_ = device->iccid(); 258 iccid_ = device->iccid();
254 service_path_ = service_path; 259 service_path_ = service_path;
260 device_path_ = network->device_path();
255 261
256 ChangeState(network, PLAN_ACTIVATION_PAGE_LOADING, ""); 262 ChangeState(network, PLAN_ACTIVATION_PAGE_LOADING, "");
257 263
258 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, 264 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
259 base::Bind(&CellularConfigDocument::LoadCellularConfigFile, 265 base::Bind(&CellularConfigDocument::LoadCellularConfigFile,
260 cellular_config_.get()), 266 cellular_config_.get()),
261 base::Bind(&MobileActivator::ContinueActivation, AsWeakPtr())); 267 base::Bind(&MobileActivator::ContinueActivation, AsWeakPtr()));
262 } 268 }
263 269
264 void MobileActivator::ContinueActivation() { 270 void MobileActivator::ContinueActivation() {
265 CellularNetwork* network = FindMatchingCellularNetwork(false); 271 const NetworkState* network = GetNetworkState(service_path_);
266 if (!network || !network->SupportsActivation()) 272 if (!network ||
273 (network->payment_url().empty() && network->usage_url().empty()))
267 return; 274 return;
268 275
269 DisableCertRevocationChecking(); 276 DisableCertRevocationChecking();
270 // We want shill to connect us after activations.
271 network->SetAutoConnect(true);
272 277
278 // We want shill to connect us after activations, so enable autoconnect.
279 DictionaryValue auto_connect_property;
280 auto_connect_property.SetBoolean(flimflam::kAutoConnectProperty, true);
281 NetworkHandler::Get()->network_configuration_handler()->SetProperties(
282 network->path(),
283 auto_connect_property,
284 base::Bind(&base::DoNothing),
285 network_handler::ErrorCallback());
273 StartActivation(); 286 StartActivation();
274 } 287 }
275 288
276 void MobileActivator::OnSetTransactionStatus(bool success) { 289 void MobileActivator::OnSetTransactionStatus(bool success) {
277 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 290 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
278 base::Bind(&MobileActivator::HandleSetTransactionStatus, 291 base::Bind(&MobileActivator::HandleSetTransactionStatus,
279 AsWeakPtr(), success)); 292 AsWeakPtr(), success));
280 } 293 }
281 294
282 void MobileActivator::HandleSetTransactionStatus(bool success) { 295 void MobileActivator::HandleSetTransactionStatus(bool success) {
283 // The payment is received, try to reconnect and check the status all over 296 // The payment is received, try to reconnect and check the status all over
284 // again. 297 // again.
285 if (success && state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) { 298 if (success && state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) {
286 NetworkLibrary* lib = GetNetworkLibrary(); 299 SignalCellularPlanPayment();
287 lib->SignalCellularPlanPayment();
288 UMA_HISTOGRAM_COUNTS("Cellular.PaymentReceived", 1); 300 UMA_HISTOGRAM_COUNTS("Cellular.PaymentReceived", 1);
289 CellularNetwork* network = FindMatchingCellularNetwork(true); 301 const NetworkState* network = GetNetworkState(service_path_);
290 if (network && network->activate_over_non_cellular_network()) { 302 if (network && network->activate_over_non_cellular_networks()) {
291 state_ = PLAN_ACTIVATION_DONE; 303 state_ = PLAN_ACTIVATION_DONE;
292 network->CompleteActivation(); 304 NetworkHandler::Get()->network_activation_handler()->
305 CompleteActivation(network->path(),
306 base::Bind(&base::DoNothing),
307 network_handler::ErrorCallback());
293 } else { 308 } else {
294 StartOTASP(); 309 StartOTASP();
295 } 310 }
296 } else { 311 } else {
297 UMA_HISTOGRAM_COUNTS("Cellular.PaymentFailed", 1); 312 UMA_HISTOGRAM_COUNTS("Cellular.PaymentFailed", 1);
298 } 313 }
299 } 314 }
300 315
301 void MobileActivator::OnPortalLoaded(bool success) { 316 void MobileActivator::OnPortalLoaded(bool success) {
302 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 317 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
303 base::Bind(&MobileActivator::HandlePortalLoaded, 318 base::Bind(&MobileActivator::HandlePortalLoaded,
304 AsWeakPtr(), success)); 319 AsWeakPtr(), success));
305 } 320 }
306 321
307 void MobileActivator::HandlePortalLoaded(bool success) { 322 void MobileActivator::HandlePortalLoaded(bool success) {
308 CellularNetwork* network = FindMatchingCellularNetwork(true); 323 const NetworkState* network = GetNetworkState(service_path_);
309 if (!network) { 324 if (!network) {
310 ChangeState(NULL, PLAN_ACTIVATION_ERROR, 325 ChangeState(NULL, PLAN_ACTIVATION_ERROR,
311 GetErrorMessage(kErrorNoService)); 326 GetErrorMessage(kErrorNoService));
312 return; 327 return;
313 } 328 }
314 if (state_ == PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING || 329 if (state_ == PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING ||
315 state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) { 330 state_ == PLAN_ACTIVATION_SHOWING_PAYMENT) {
316 if (success) { 331 if (success) {
317 payment_reconnect_count_ = 0; 332 payment_reconnect_count_ = 0;
318 ChangeState(network, PLAN_ACTIVATION_SHOWING_PAYMENT, std::string()); 333 ChangeState(network, PLAN_ACTIVATION_SHOWING_PAYMENT, std::string());
319 } else { 334 } else {
320 // There is no point in forcing reconnecting the cellular network if the 335 // There is no point in forcing reconnecting the cellular network if the
321 // activation should not be done over it. 336 // activation should not be done over it.
322 if (network->activate_over_non_cellular_network()) 337 if (network->activate_over_non_cellular_networks())
323 return; 338 return;
324 339
325 payment_reconnect_count_++; 340 payment_reconnect_count_++;
326 if (payment_reconnect_count_ > kMaxPortalReconnectCount) { 341 if (payment_reconnect_count_ > kMaxPortalReconnectCount) {
327 ChangeState(NULL, PLAN_ACTIVATION_ERROR, 342 ChangeState(NULL, PLAN_ACTIVATION_ERROR,
328 GetErrorMessage(kErrorNoService)); 343 GetErrorMessage(kErrorNoService));
329 return; 344 return;
330 } 345 }
331 346
332 // Reconnect and try and load the frame again. 347 // Reconnect and try and load the frame again.
333 ChangeState(network, 348 ChangeState(network,
334 PLAN_ACTIVATION_RECONNECTING, 349 PLAN_ACTIVATION_RECONNECTING,
335 GetErrorMessage(kFailedPaymentError)); 350 GetErrorMessage(kFailedPaymentError));
336 } 351 }
337 } else { 352 } else {
338 NOTREACHED() << "Called paymentPortalLoad while in unexpected state: " 353 NOTREACHED() << "Called paymentPortalLoad while in unexpected state: "
339 << GetStateDescription(state_); 354 << GetStateDescription(state_);
340 } 355 }
341 } 356 }
342 357
343 CellularNetwork* MobileActivator::FindMatchingCellularNetwork(
344 bool reattach_observer) {
345 NetworkLibrary* lib = GetNetworkLibrary();
346 for (CellularNetworkVector::const_iterator it =
347 lib->cellular_networks().begin();
348 it != lib->cellular_networks().end(); ++it) {
349 const chromeos::NetworkDevice* device =
350 lib->FindNetworkDeviceByPath((*it)->device_path());
351 if (device && ((!meid_.empty() && meid_ == device->meid()) ||
352 (!iccid_.empty() && iccid_ == device->iccid()))) {
353 CellularNetwork* network = *it;
354 // If service path has changed, reattach the event observer for this
355 // network service.
356 if (reattach_observer && service_path_ != network->service_path()) {
357 lib->RemoveObserverForAllNetworks(this);
358 lib->AddNetworkObserver(network->service_path(), this);
359 service_path_ = network->service_path();
360 }
361 return network;
362 }
363 }
364 return NULL;
365 }
366
367 void MobileActivator::StartOTASPTimer() { 358 void MobileActivator::StartOTASPTimer() {
359 pending_activation_request_ = false;
368 state_duration_timer_.Start( 360 state_duration_timer_.Start(
369 FROM_HERE, 361 FROM_HERE,
370 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay), 362 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay),
371 this, &MobileActivator::HandleOTASPTimeout); 363 this, &MobileActivator::HandleOTASPTimeout);
372 } 364 }
373 365
374 void MobileActivator::StartActivation() { 366 void MobileActivator::StartActivation() {
375 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupStart", 1); 367 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupStart", 1);
376 NetworkLibrary* lib = GetNetworkLibrary(); 368 const NetworkState* network = GetNetworkState(service_path_);
377 CellularNetwork* network = FindMatchingCellularNetwork(true);
378 // Check if we can start activation process. 369 // Check if we can start activation process.
379 if (!network) { 370 if (!network) {
371 NetworkStateHandler::TechnologyState technology_state =
372 NetworkHandler::Get()->network_state_handler()->GetTechnologyState(
373 flimflam::kTypeCellular);
380 std::string error; 374 std::string error;
381 if (!lib->cellular_available()) 375 if (technology_state == NetworkStateHandler::TECHNOLOGY_UNAVAILABLE) {
382 error = kErrorNoDevice; 376 error = kErrorNoDevice;
383 else if (!lib->cellular_enabled()) 377 } else if (technology_state != NetworkStateHandler::TECHNOLOGY_ENABLED) {
384 error = kErrorDisabled; 378 error = kErrorDisabled;
385 else 379 } else {
386 error = kErrorNoService; 380 error = kErrorNoService;
381 }
387 ChangeState(NULL, PLAN_ACTIVATION_ERROR, GetErrorMessage(error)); 382 ChangeState(NULL, PLAN_ACTIVATION_ERROR, GetErrorMessage(error));
388 return; 383 return;
389 } 384 }
390 385
391 // Start monitoring network property changes. 386 // Start monitoring network property changes.
392 lib->AddNetworkManagerObserver(this); 387 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
393 if (network->activate_over_non_cellular_network()) { 388 if (network->activate_over_non_cellular_networks()) {
394 // Fast forward to payment portal loading if the activation is performed 389 // Fast forward to payment portal loading if the activation is performed
395 // over a non-cellular network. 390 // over a non-cellular network.
396 ChangeState(network, 391 ChangeState(
397 (network->activation_state() == ACTIVATION_STATE_ACTIVATED) ? 392 network,
398 PLAN_ACTIVATION_DONE : 393 (network->activation_state() == flimflam::kActivationStateActivated) ?
399 PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING, 394 PLAN_ACTIVATION_DONE :
400 ""); 395 PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING,
396 "");
401 // Verify that there is no need to wait for the connection. This will also 397 // Verify that there is no need to wait for the connection. This will also
402 // evaluate the network. 398 // evaluate the network.
403 OnNetworkManagerChanged(lib); 399 RefreshCellularNetworks();
404 return; 400 return;
405 } 401 }
406 402
407 if (lib->HasRecentCellularPlanPayment() && 403 if (HasRecentCellularPlanPayment() &&
408 network->activation_state() == ACTIVATION_STATE_PARTIALLY_ACTIVATED) { 404 (network->activation_state() ==
405 flimflam::kActivationStatePartiallyActivated)) {
409 // Try to start with OTASP immediately if we have received payment recently. 406 // Try to start with OTASP immediately if we have received payment recently.
410 state_ = PLAN_ACTIVATION_START_OTASP; 407 state_ = PLAN_ACTIVATION_START_OTASP;
411 } else { 408 } else {
412 state_ = PLAN_ACTIVATION_START; 409 state_ = PLAN_ACTIVATION_START;
413 } 410 }
414 411
415 EvaluateCellularNetwork(network); 412 EvaluateCellularNetwork(network);
416 } 413 }
417 414
418 void MobileActivator::RetryOTASP() { 415 void MobileActivator::RetryOTASP() {
419 DCHECK(state_ == PLAN_ACTIVATION_DELAY_OTASP); 416 DCHECK(state_ == PLAN_ACTIVATION_DELAY_OTASP);
420 StartOTASP(); 417 StartOTASP();
421 } 418 }
422 419
423 void MobileActivator::StartOTASP() { 420 void MobileActivator::StartOTASP() {
424 CellularNetwork* network = FindMatchingCellularNetwork(true); 421 const NetworkState* network = GetNetworkState(service_path_);
425 ChangeState(network, PLAN_ACTIVATION_START_OTASP, std::string()); 422 ChangeState(network, PLAN_ACTIVATION_START_OTASP, std::string());
426 EvaluateCellularNetwork(network); 423 EvaluateCellularNetwork(network);
427 } 424 }
428 425
429 void MobileActivator::HandleOTASPTimeout() { 426 void MobileActivator::HandleOTASPTimeout() {
430 LOG(WARNING) << "OTASP seems to be taking too long."; 427 LOG(WARNING) << "OTASP seems to be taking too long.";
431 CellularNetwork* network = FindMatchingCellularNetwork(true); 428 const NetworkState* network = GetNetworkState(service_path_);
432 // We're here because one of OTASP steps is taking too long to complete. 429 // We're here because one of OTASP steps is taking too long to complete.
433 // Usually, this means something bad has happened below us. 430 // Usually, this means something bad has happened below us.
434 if (state_ == PLAN_ACTIVATION_INITIATING_ACTIVATION) { 431 if (state_ == PLAN_ACTIVATION_INITIATING_ACTIVATION) {
435 ++initial_OTASP_attempts_; 432 ++initial_OTASP_attempts_;
436 if (initial_OTASP_attempts_ <= kMaxOTASPTries) { 433 if (initial_OTASP_attempts_ <= kMaxOTASPTries) {
437 ChangeState(network, 434 ChangeState(network,
438 PLAN_ACTIVATION_RECONNECTING, 435 PLAN_ACTIVATION_RECONNECTING,
439 GetErrorMessage(kErrorDefault)); 436 GetErrorMessage(kErrorDefault));
440 return; 437 return;
441 } 438 }
(...skipping 16 matching lines...) Expand all
458 } 455 }
459 } else { 456 } else {
460 LOG(ERROR) << "OTASP timed out from a non-OTASP wait state?"; 457 LOG(ERROR) << "OTASP timed out from a non-OTASP wait state?";
461 } 458 }
462 LOG(ERROR) << "OTASP failed too many times; aborting."; 459 LOG(ERROR) << "OTASP failed too many times; aborting.";
463 ChangeState(network, 460 ChangeState(network,
464 PLAN_ACTIVATION_ERROR, 461 PLAN_ACTIVATION_ERROR,
465 GetErrorMessage(kErrorDefault)); 462 GetErrorMessage(kErrorDefault));
466 } 463 }
467 464
468 void MobileActivator::ForceReconnect(CellularNetwork* network, 465 void MobileActivator::ForceReconnect(const NetworkState* network,
469 PlanActivationState next_state) { 466 PlanActivationState next_state) {
470 DCHECK(network); 467 DCHECK(network);
471 // Store away our next destination for when we complete. 468 // Store away our next destination for when we complete.
472 post_reconnect_state_ = next_state; 469 post_reconnect_state_ = next_state;
473 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetry", 1); 470 UMA_HISTOGRAM_COUNTS("Cellular.ActivationRetry", 1);
474 // First, disconnect... 471 // First, disconnect...
475 LOG(INFO) << "Disconnecting from " << network->service_path(); 472 LOG(INFO) << "Disconnecting from " << network->path();
476 // Explicit service Disconnect()s disable autoconnect on the service until 473 // Explicit service Disconnect()s disable autoconnect on the service until
477 // Connect() is called on the service again. Hence this dance to explicitly 474 // Connect() is called on the service again. Hence this dance to explicitly
478 // call Connect(). 475 // call Connect().
479 GetNetworkLibrary()->DisconnectFromNetwork(network); 476 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
477 network->path(),
478 base::Bind(&base::DoNothing),
479 network_handler::ErrorCallback());
480 // Keep trying to connect until told otherwise. 480 // Keep trying to connect until told otherwise.
481 continue_reconnect_timer_.Stop(); 481 continue_reconnect_timer_.Stop();
482 continue_reconnect_timer_.Start( 482 continue_reconnect_timer_.Start(
483 FROM_HERE, 483 FROM_HERE,
484 base::TimeDelta::FromMilliseconds(kReconnectDelayMS), 484 base::TimeDelta::FromMilliseconds(kReconnectDelayMS),
485 this, &MobileActivator::ContinueConnecting); 485 this, &MobileActivator::ContinueConnecting);
486 // If we don't ever connect again, we're going to call this a failure. 486 // If we don't ever connect again, we're going to call this a failure.
487 reconnect_timeout_timer_.Stop(); 487 reconnect_timeout_timer_.Stop();
488 reconnect_timeout_timer_.Start( 488 reconnect_timeout_timer_.Start(
489 FROM_HERE, 489 FROM_HERE,
490 base::TimeDelta::FromMilliseconds(kMaxReconnectTime), 490 base::TimeDelta::FromMilliseconds(kMaxReconnectTime),
491 this, &MobileActivator::ReconnectTimedOut); 491 this, &MobileActivator::ReconnectTimedOut);
492 } 492 }
493 493
494 void MobileActivator::ReconnectTimedOut() { 494 void MobileActivator::ReconnectTimedOut() {
495 LOG(ERROR) << "Ending activation attempt after failing to reconnect."; 495 LOG(ERROR) << "Ending activation attempt after failing to reconnect.";
496 ChangeState(FindMatchingCellularNetwork(true), 496 const NetworkState* network = GetNetworkState(service_path_);
497 ChangeState(network,
497 PLAN_ACTIVATION_ERROR, 498 PLAN_ACTIVATION_ERROR,
498 GetErrorMessage(kFailedConnectivity)); 499 GetErrorMessage(kFailedConnectivity));
499 } 500 }
500 501
501 void MobileActivator::ContinueConnecting() { 502 void MobileActivator::ContinueConnecting() {
502 CellularNetwork* network = FindMatchingCellularNetwork(true); 503 const NetworkState* network = GetNetworkState(service_path_);
503 if (network && network->connected()) { 504 if (network && network->IsConnectedState()) {
504 if (network->restricted_pool() && 505 if (network->connection_state() == flimflam::kStatePortal &&
505 network->error() == ERROR_DNS_LOOKUP_FAILED) { 506 network->error() == flimflam::kErrorDNSLookupFailed) {
506 // It isn't an error to be in a restricted pool, but if DNS doesn't work, 507 // It isn't an error to be in a restricted pool, but if DNS doesn't work,
507 // then we're not getting traffic through at all. Just disconnect and 508 // then we're not getting traffic through at all. Just disconnect and
508 // try again. 509 // try again.
509 GetNetworkLibrary()->DisconnectFromNetwork(network); 510 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
511 network->path(),
512 base::Bind(&base::DoNothing),
513 network_handler::ErrorCallback());
510 return; 514 return;
511 } 515 }
512 // Stop this callback 516 // Stop this callback
513 continue_reconnect_timer_.Stop(); 517 continue_reconnect_timer_.Stop();
514 EvaluateCellularNetwork(network); 518 EvaluateCellularNetwork(network);
515 } else { 519 } else {
516 LOG(WARNING) << "Connect failed, will try again in a little bit."; 520 LOG(WARNING) << "Connect failed, will try again in a little bit.";
517 if (network) { 521 if (network) {
518 LOG(INFO) << "Connecting to: " << network->service_path(); 522 LOG(INFO) << "Connecting to: " << network->path();
519 ash::network_connect::ConnectToNetwork( 523 ash::network_connect::ConnectToNetwork(
520 network->service_path(), NULL /* no parent window */); 524 network->path(), NULL /* no parent window */);
521 } 525 }
522 } 526 }
523 } 527 }
524 528
525 void MobileActivator::EvaluateCellularNetwork(CellularNetwork* network) { 529 void MobileActivator::RefreshCellularNetworks() {
530 if (state_ == PLAN_ACTIVATION_PAGE_LOADING ||
531 state_ == PLAN_ACTIVATION_DONE ||
532 state_ == PLAN_ACTIVATION_ERROR) {
533 return;
534 }
535
536 NetworkStateHandler* nsh = NetworkHandler::Get()->network_state_handler();
537 const NetworkState* network = GetNetworkState(service_path_);
538 if (network && network->activate_over_non_cellular_networks()) {
539 bool waiting = (state_ == PLAN_ACTIVATION_WAITING_FOR_CONNECTION);
540 bool is_online = nsh->DefaultNetwork() &&
541 nsh->DefaultNetwork()->connection_state() == flimflam::kStateOnline;
542 if (waiting && is_online) {
543 ChangeState(network, post_reconnect_state_, "");
544 } else if (!waiting && !is_online) {
545 ChangeState(network, PLAN_ACTIVATION_WAITING_FOR_CONNECTION, "");
546 }
547 }
548
549 EvaluateCellularNetwork(network);
550 }
551
552 const NetworkState* MobileActivator::GetNetworkState(
553 const std::string& service_path) {
554 return NetworkHandler::Get()->network_state_handler()->GetNetworkState(
555 service_path);
556 }
557
558 void MobileActivator::EvaluateCellularNetwork(const NetworkState* network) {
526 if (terminated_) { 559 if (terminated_) {
527 LOG(ERROR) << "Tried to run MobileActivator state machine while " 560 LOG(ERROR) << "Tried to run MobileActivator state machine while "
528 << "terminated."; 561 << "terminated.";
529 return; 562 return;
530 } 563 }
531 564
532 if (!network) { 565 if (!network) {
533 LOG(WARNING) << "Cellular service lost"; 566 LOG(WARNING) << "Cellular service lost";
534 return; 567 return;
535 } 568 }
536 569
537 LOG(WARNING) << "Cellular:\n service=" << network->GetStateString() 570 LOG(WARNING) << "Cellular:\n service state=" << network->connection_state()
538 << "\n ui=" << GetStateDescription(state_) 571 << "\n ui=" << GetStateDescription(state_)
539 << "\n activation=" << network->GetActivationStateString() 572 << "\n activation=" << network->activation_state()
540 << "\n error=" << network->GetErrorString() 573 << "\n error=" << network->error()
541 << "\n setvice_path=" << network->service_path() 574 << "\n setvice_path=" << network->path()
542 << "\n connected=" << network->connected(); 575 << "\n connected=" << network->IsConnectedState();
543 576
544 // If the network is activated over non cellular network, the activator state 577 // If the network is activated over non cellular network, the activator state
545 // does not depend on the network's own state. 578 // does not depend on the network's own state.
546 if (network->activate_over_non_cellular_network()) 579 if (network->activate_over_non_cellular_networks())
547 return; 580 return;
548 581
549 std::string error_description; 582 std::string error_description;
550 PlanActivationState new_state = PickNextState(network, &error_description); 583 PlanActivationState new_state = PickNextState(network, &error_description);
551 584
552 ChangeState(network, new_state, error_description); 585 ChangeState(network, new_state, error_description);
553 } 586 }
554 587
555 MobileActivator::PlanActivationState MobileActivator::PickNextState( 588 MobileActivator::PlanActivationState MobileActivator::PickNextState(
556 CellularNetwork* network, std::string* error_description) const { 589 const NetworkState* network, std::string* error_description) const {
557 PlanActivationState new_state = state_; 590 PlanActivationState new_state = state_;
558 if (!network->connected()) 591 if (!network->IsConnectedState())
559 new_state = PickNextOfflineState(network); 592 new_state = PickNextOfflineState(network);
560 else 593 else
561 new_state = PickNextOnlineState(network); 594 new_state = PickNextOnlineState(network);
562 if (new_state != PLAN_ACTIVATION_ERROR && 595 if (new_state != PLAN_ACTIVATION_ERROR &&
563 GotActivationError(network, error_description)) { 596 GotActivationError(network, error_description)) {
564 // Check for this special case when we try to do activate partially 597 // Check for this special case when we try to do activate partially
565 // activated device. If that attempt failed, try to disconnect to clear the 598 // activated device. If that attempt failed, try to disconnect to clear the
566 // state and reconnect again. 599 // state and reconnect again.
567 if ((network->activation_state() == ACTIVATION_STATE_PARTIALLY_ACTIVATED || 600 const std::string& activation = network->activation_state();
568 network->activation_state() == ACTIVATION_STATE_ACTIVATING) && 601 if ((activation == flimflam::kActivationStatePartiallyActivated ||
569 (network->error() == ERROR_NO_ERROR || 602 activation == flimflam::kActivationStateActivating) &&
570 network->error() == ERROR_OTASP_FAILED) && 603 (network->error().empty() ||
571 network->state() == STATE_ACTIVATION_FAILURE) { 604 network->error() == flimflam::kErrorOtaspFailed) &&
572 LOG(WARNING) << "Activation failure detected " 605 network->connection_state() == flimflam::kStateActivationFailure) {
573 << network->service_path(); 606 NET_LOG_EVENT("Activation failure detected ", network->path());
574 switch (state_) { 607 switch (state_) {
575 case PLAN_ACTIVATION_OTASP: 608 case PLAN_ACTIVATION_OTASP:
576 new_state = PLAN_ACTIVATION_DELAY_OTASP; 609 new_state = PLAN_ACTIVATION_DELAY_OTASP;
577 break; 610 break;
578 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 611 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
579 case PLAN_ACTIVATION_TRYING_OTASP: 612 case PLAN_ACTIVATION_TRYING_OTASP:
580 new_state = PLAN_ACTIVATION_START; 613 new_state = PLAN_ACTIVATION_START;
581 break; 614 break;
582 case PLAN_ACTIVATION_START: 615 case PLAN_ACTIVATION_START:
583 // We are just starting, so this must be previous activation attempt 616 // We are just starting, so this must be previous activation attempt
584 // failure. 617 // failure.
585 new_state = PLAN_ACTIVATION_TRYING_OTASP; 618 new_state = PLAN_ACTIVATION_TRYING_OTASP;
586 break; 619 break;
587 case PLAN_ACTIVATION_DELAY_OTASP: 620 case PLAN_ACTIVATION_DELAY_OTASP:
588 new_state = state_; 621 new_state = state_;
589 break; 622 break;
590 default: 623 default:
591 new_state = PLAN_ACTIVATION_ERROR; 624 new_state = PLAN_ACTIVATION_ERROR;
592 break; 625 break;
593 } 626 }
594 } else { 627 } else {
595 LOG(WARNING) << "Unexpected activation failure for " 628 LOG(WARNING) << "Unexpected activation failure for " << network->path();
596 << network->service_path();
597 new_state = PLAN_ACTIVATION_ERROR; 629 new_state = PLAN_ACTIVATION_ERROR;
598 } 630 }
599 } 631 }
600 632
601 if (new_state == PLAN_ACTIVATION_ERROR && !error_description->length()) 633 if (new_state == PLAN_ACTIVATION_ERROR && !error_description->length())
602 *error_description = GetErrorMessage(kErrorDefault); 634 *error_description = GetErrorMessage(kErrorDefault);
603 return new_state; 635 return new_state;
604 } 636 }
605 637
606 MobileActivator::PlanActivationState MobileActivator::PickNextOfflineState( 638 MobileActivator::PlanActivationState MobileActivator::PickNextOfflineState(
607 CellularNetwork* network) const { 639 const NetworkState* network) const {
608 PlanActivationState new_state = state_; 640 PlanActivationState new_state = state_;
641 const std::string& activation = network->activation_state();
609 switch (state_) { 642 switch (state_) {
610 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING: 643 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING:
611 case PLAN_ACTIVATION_SHOWING_PAYMENT: 644 case PLAN_ACTIVATION_SHOWING_PAYMENT:
612 if (!network->activate_over_non_cellular_network()) 645 if (!network->activate_over_non_cellular_networks())
613 new_state = PLAN_ACTIVATION_RECONNECTING; 646 new_state = PLAN_ACTIVATION_RECONNECTING;
614 break; 647 break;
615 case PLAN_ACTIVATION_START: 648 case PLAN_ACTIVATION_START:
616 switch (network->activation_state()) { 649 if (activation == flimflam::kActivationStateActivated) {
617 case ACTIVATION_STATE_ACTIVATED: 650 if (network->connection_state() == flimflam::kStatePortal)
618 if (network->restricted_pool()) 651 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
619 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 652 else
620 else 653 new_state = PLAN_ACTIVATION_DONE;
621 new_state = PLAN_ACTIVATION_DONE; 654 } else if (activation == flimflam::kActivationStatePartiallyActivated) {
622 break; 655 new_state = PLAN_ACTIVATION_TRYING_OTASP;
623 case ACTIVATION_STATE_PARTIALLY_ACTIVATED: 656 } else {
624 new_state = PLAN_ACTIVATION_TRYING_OTASP; 657 new_state = PLAN_ACTIVATION_INITIATING_ACTIVATION;
625 break;
626 default:
627 new_state = PLAN_ACTIVATION_INITIATING_ACTIVATION;
628 break;
629 } 658 }
630 break; 659 break;
631 default: { 660 default:
632 LOG(INFO) << "Waiting for cellular service to connect."; 661 LOG(INFO) << "Waiting for cellular service to connect.";
633 } break; 662 break;
634 } 663 }
635 return new_state; 664 return new_state;
636 } 665 }
637 666
638 MobileActivator::PlanActivationState MobileActivator::PickNextOnlineState( 667 MobileActivator::PlanActivationState MobileActivator::PickNextOnlineState(
639 CellularNetwork* network) const { 668 const NetworkState* network) const {
640 PlanActivationState new_state = state_; 669 PlanActivationState new_state = state_;
670 const std::string& activation = network->activation_state();
641 switch (state_) { 671 switch (state_) {
642 case PLAN_ACTIVATION_START: 672 case PLAN_ACTIVATION_START:
643 switch (network->activation_state()) { 673 if (activation == flimflam::kActivationStateActivated) {
644 case ACTIVATION_STATE_ACTIVATED: 674 if (network->connection_state() == flimflam::kStatePortal)
645 if (network->restricted_pool()) 675 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
646 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 676 else
647 else 677 new_state = PLAN_ACTIVATION_DONE;
648 new_state = PLAN_ACTIVATION_DONE; 678 } else if (activation == flimflam::kActivationStatePartiallyActivated) {
649 break; 679 new_state = PLAN_ACTIVATION_TRYING_OTASP;
650 case ACTIVATION_STATE_PARTIALLY_ACTIVATED: 680 } else {
651 new_state = PLAN_ACTIVATION_TRYING_OTASP; 681 new_state = PLAN_ACTIVATION_INITIATING_ACTIVATION;
652 break;
653 default:
654 new_state = PLAN_ACTIVATION_INITIATING_ACTIVATION;
655 break;
656 } 682 }
657 break; 683 break;
658 case PLAN_ACTIVATION_START_OTASP: { 684 case PLAN_ACTIVATION_START_OTASP: {
659 switch (network->activation_state()) { 685 if (activation == flimflam::kActivationStatePartiallyActivated) {
660 case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
661 new_state = PLAN_ACTIVATION_OTASP; 686 new_state = PLAN_ACTIVATION_OTASP;
662 break; 687 } else if (activation == flimflam::kActivationStateActivated) {
663 case ACTIVATION_STATE_ACTIVATED: 688 new_state = PLAN_ACTIVATION_RECONNECTING;
664 new_state = PLAN_ACTIVATION_RECONNECTING; 689 } else {
665 break; 690 LOG(WARNING) << "Unexpected activation state for device "
666 default: { 691 << network->path();
667 LOG(WARNING) << "Unexpected activation state for device "
668 << network->service_path();
669 break;
670 }
671 } 692 }
672 break; 693 break;
673 } 694 }
674 case PLAN_ACTIVATION_DELAY_OTASP: 695 case PLAN_ACTIVATION_DELAY_OTASP:
675 // Just ignore any changes until the OTASP retry timer kicks in. 696 // Just ignore any changes until the OTASP retry timer kicks in.
676 break; 697 break;
677 case PLAN_ACTIVATION_INITIATING_ACTIVATION: { 698 case PLAN_ACTIVATION_INITIATING_ACTIVATION: {
678 switch (network->activation_state()) { 699 if (pending_activation_request_) {
679 case ACTIVATION_STATE_ACTIVATED: 700 LOG(INFO) << "Waiting for pending activation attempt to finish";
680 case ACTIVATION_STATE_PARTIALLY_ACTIVATED: 701 } else if (activation == flimflam::kActivationStateActivated ||
681 new_state = PLAN_ACTIVATION_START; 702 activation == flimflam::kActivationStatePartiallyActivated) {
682 break; 703 new_state = PLAN_ACTIVATION_START;
683 case ACTIVATION_STATE_NOT_ACTIVATED: 704 } else if (activation == flimflam::kActivationStateNotActivated ||
684 case ACTIVATION_STATE_ACTIVATING: 705 activation == flimflam::kActivationStateActivating) {
685 // Wait in this state until activation state changes. 706 // Wait in this state until activation state changes.
686 break; 707 } else {
687 default: 708 LOG(WARNING) << "Unknown transition";
688 LOG(WARNING) << "Unknown transition";
689 break;
690 } 709 }
691 break; 710 break;
692 } 711 }
693 case PLAN_ACTIVATION_OTASP: 712 case PLAN_ACTIVATION_OTASP:
694 case PLAN_ACTIVATION_TRYING_OTASP: 713 case PLAN_ACTIVATION_TRYING_OTASP:
695 if (network->activation_state() == ACTIVATION_STATE_NOT_ACTIVATED || 714 if (pending_activation_request_) {
696 network->activation_state() == ACTIVATION_STATE_ACTIVATING) { 715 LOG(INFO) << "Waiting for pending activation attempt to finish";
716 } else if (activation == flimflam::kActivationStateNotActivated ||
717 activation == flimflam::kActivationStateActivating) {
697 LOG(INFO) << "Waiting for the OTASP to finish and the service to " 718 LOG(INFO) << "Waiting for the OTASP to finish and the service to "
698 << "come back online"; 719 << "come back online";
699 } else if (network->activation_state() == ACTIVATION_STATE_ACTIVATED) { 720 } else if (activation == flimflam::kActivationStateActivated) {
700 new_state = PLAN_ACTIVATION_DONE; 721 new_state = PLAN_ACTIVATION_DONE;
701 } else { 722 } else {
702 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 723 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
703 } 724 }
704 break; 725 break;
705 case PLAN_ACTIVATION_RECONNECTING_PAYMENT: 726 case PLAN_ACTIVATION_RECONNECTING_PAYMENT:
706 if (!network->restricted_pool() && 727 if (network->connection_state() != flimflam::kStatePortal &&
707 network->activation_state() == ACTIVATION_STATE_ACTIVATED) 728 activation == flimflam::kActivationStateActivated)
708 // We're not portalled, and we're already activated, so we're online! 729 // We're not portalled, and we're already activated, so we're online!
709 new_state = PLAN_ACTIVATION_DONE; 730 new_state = PLAN_ACTIVATION_DONE;
710 else 731 else
711 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING; 732 new_state = PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING;
712 break; 733 break;
713 // Initial state 734 // Initial state
714 case PLAN_ACTIVATION_PAGE_LOADING: 735 case PLAN_ACTIVATION_PAGE_LOADING:
715 break; 736 break;
716 // Just ignore all signals until the site confirms payment. 737 // Just ignore all signals until the site confirms payment.
717 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING: 738 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING:
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 return "ERROR"; 781 return "ERROR";
761 case PLAN_ACTIVATION_RECONNECTING: 782 case PLAN_ACTIVATION_RECONNECTING:
762 return "RECONNECTING"; 783 return "RECONNECTING";
763 case PLAN_ACTIVATION_WAITING_FOR_CONNECTION: 784 case PLAN_ACTIVATION_WAITING_FOR_CONNECTION:
764 return "WAITING FOR CONNECTION"; 785 return "WAITING FOR CONNECTION";
765 } 786 }
766 return "UNKNOWN"; 787 return "UNKNOWN";
767 } 788 }
768 789
769 790
770 void MobileActivator::CompleteActivation( 791 void MobileActivator::CompleteActivation() {
771 CellularNetwork* network) {
772 // Remove observers, we are done with this page. 792 // Remove observers, we are done with this page.
773 NetworkLibrary* lib = GetNetworkLibrary(); 793 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
774 lib->RemoveNetworkManagerObserver(this); 794 this, FROM_HERE);
775 lib->RemoveObserverForAllNetworks(this); 795
776 // Reactivate other types of connections if we have 796 // Reactivate other types of connections if we have
777 // shut them down previously. 797 // shut them down previously.
778 ReEnableCertRevocationChecking(); 798 ReEnableCertRevocationChecking();
779 } 799 }
780 800
781 bool MobileActivator::RunningActivation() const { 801 bool MobileActivator::RunningActivation() const {
782 return !(state_ == PLAN_ACTIVATION_DONE || 802 return !(state_ == PLAN_ACTIVATION_DONE ||
783 state_ == PLAN_ACTIVATION_ERROR || 803 state_ == PLAN_ACTIVATION_ERROR ||
784 state_ == PLAN_ACTIVATION_PAGE_LOADING); 804 state_ == PLAN_ACTIVATION_PAGE_LOADING);
785 } 805 }
786 806
787 void MobileActivator::ChangeState(CellularNetwork* network, 807 void MobileActivator::HandleActivationFailure(
808 const std::string& service_path,
809 PlanActivationState new_state,
810 const std::string& error_name,
811 scoped_ptr<base::DictionaryValue> error_data) {
812 pending_activation_request_ = false;
813 const NetworkState* network = GetNetworkState(service_path);
814 if (!network) {
815 NET_LOG_ERROR("Cellular service no longer exists", service_path);
816 return;
817 }
818 UMA_HISTOGRAM_COUNTS("Cellular.ActivationFailure", 1);
819 NET_LOG_ERROR("Failed to call Activate() on service", service_path);
820 if (new_state == PLAN_ACTIVATION_OTASP) {
821 ChangeState(network, PLAN_ACTIVATION_DELAY_OTASP, std::string());
822 } else {
823 ChangeState(network,
824 PLAN_ACTIVATION_ERROR,
825 GetErrorMessage(kFailedConnectivity));
826 }
827 }
828
829 void MobileActivator::RequestCellularActivation(
830 const NetworkState* network,
831 const base::Closure& success_callback,
832 const network_handler::ErrorCallback& error_callback) {
833 DCHECK(network);
834 NET_LOG_EVENT("Activating cellular service", network->path());
835 UMA_HISTOGRAM_COUNTS("Cellular.ActivationTry", 1);
836 pending_activation_request_ = true;
837 NetworkHandler::Get()->network_activation_handler()->
838 Activate(network->path(),
839 "", // carrier
840 success_callback,
841 error_callback);
842 }
843
844 void MobileActivator::ChangeState(const NetworkState* network,
788 PlanActivationState new_state, 845 PlanActivationState new_state,
789 const std::string& error_description) { 846 const std::string& error_description) {
790 static bool first_time = true; 847 static bool first_time = true;
791 LOG(INFO) << "Activation state flip old = " 848 LOG(INFO) << "Activation state flip old = "
792 << GetStateDescription(state_) 849 << GetStateDescription(state_)
793 << ", new = " << GetStateDescription(new_state); 850 << ", new = " << GetStateDescription(new_state);
794 if (state_ == new_state && !first_time) 851 if (state_ == new_state && !first_time)
795 return; 852 return;
796 first_time = false; 853 first_time = false;
797 LOG(INFO) << "Transitioning..."; 854 LOG(INFO) << "Transitioning...";
(...skipping 17 matching lines...) Expand all
815 UMA_HISTOGRAM_COUNTS("Cellular.RetryOTASP", 1); 872 UMA_HISTOGRAM_COUNTS("Cellular.RetryOTASP", 1);
816 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, 873 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
817 base::Bind(&MobileActivator::RetryOTASP, AsWeakPtr()), 874 base::Bind(&MobileActivator::RetryOTASP, AsWeakPtr()),
818 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay)); 875 base::TimeDelta::FromMilliseconds(kOTASPRetryDelay));
819 break; 876 break;
820 } 877 }
821 case PLAN_ACTIVATION_START_OTASP: 878 case PLAN_ACTIVATION_START_OTASP:
822 break; 879 break;
823 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 880 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
824 case PLAN_ACTIVATION_TRYING_OTASP: 881 case PLAN_ACTIVATION_TRYING_OTASP:
825 case PLAN_ACTIVATION_OTASP: 882 case PLAN_ACTIVATION_OTASP: {
826 DCHECK(network); 883 DCHECK(network);
827 LOG(WARNING) << "Activating service " << network->service_path(); 884 network_handler::ErrorCallback on_activation_error =
828 UMA_HISTOGRAM_COUNTS("Cellular.ActivationTry", 1); 885 base::Bind(&MobileActivator::HandleActivationFailure, AsWeakPtr(),
829 if (!network->StartActivation()) { 886 network->path(),
830 UMA_HISTOGRAM_COUNTS("Cellular.ActivationFailure", 1); 887 new_state);
831 LOG(ERROR) << "Failed to call Activate() on service in shill."; 888 RequestCellularActivation(
832 if (new_state == PLAN_ACTIVATION_OTASP) { 889 network,
833 ChangeState(network, PLAN_ACTIVATION_DELAY_OTASP, std::string()); 890 base::Bind(&MobileActivator::StartOTASPTimer, AsWeakPtr()),
834 } else { 891 on_activation_error);
835 ChangeState(network,
836 PLAN_ACTIVATION_ERROR,
837 GetErrorMessage(kFailedConnectivity));
838 }
839 } else {
840 StartOTASPTimer();
841 } 892 }
842 break; 893 break;
843 case PLAN_ACTIVATION_PAGE_LOADING: 894 case PLAN_ACTIVATION_PAGE_LOADING:
844 return; 895 return;
845 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING: 896 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING:
846 case PLAN_ACTIVATION_SHOWING_PAYMENT: 897 case PLAN_ACTIVATION_SHOWING_PAYMENT:
847 case PLAN_ACTIVATION_RECONNECTING_PAYMENT: 898 case PLAN_ACTIVATION_RECONNECTING_PAYMENT:
848 // Fix for fix SSL for the walled gardens where cert chain verification 899 // Fix for fix SSL for the walled gardens where cert chain verification
849 // might not work. 900 // might not work.
850 break; 901 break;
851 case PLAN_ACTIVATION_WAITING_FOR_CONNECTION: 902 case PLAN_ACTIVATION_WAITING_FOR_CONNECTION:
852 post_reconnect_state_ = old_state; 903 post_reconnect_state_ = old_state;
853 break; 904 break;
854 case PLAN_ACTIVATION_RECONNECTING: { 905 case PLAN_ACTIVATION_RECONNECTING: {
855 PlanActivationState next_state = old_state; 906 PlanActivationState next_state = old_state;
856 // Pick where we want to return to after we reconnect. 907 // Pick where we want to return to after we reconnect.
857 switch (old_state) { 908 switch (old_state) {
858 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING: 909 case PLAN_ACTIVATION_PAYMENT_PORTAL_LOADING:
859 case PLAN_ACTIVATION_SHOWING_PAYMENT: 910 case PLAN_ACTIVATION_SHOWING_PAYMENT:
860 // We decide here what to do next based on the state of the modem. 911 // We decide here what to do next based on the state of the modem.
861 next_state = PLAN_ACTIVATION_RECONNECTING_PAYMENT; 912 next_state = PLAN_ACTIVATION_RECONNECTING_PAYMENT;
862 break; 913 break;
863 case PLAN_ACTIVATION_INITIATING_ACTIVATION: 914 case PLAN_ACTIVATION_INITIATING_ACTIVATION:
864 case PLAN_ACTIVATION_TRYING_OTASP: 915 case PLAN_ACTIVATION_TRYING_OTASP:
865 next_state = PLAN_ACTIVATION_START; 916 next_state = PLAN_ACTIVATION_START;
866 break; 917 break;
867 case PLAN_ACTIVATION_START_OTASP: 918 case PLAN_ACTIVATION_START_OTASP:
868 case PLAN_ACTIVATION_OTASP: 919 case PLAN_ACTIVATION_OTASP:
869 if (!network || !network->connected()) { 920 if (!network || !network->IsConnectedState()) {
870 next_state = PLAN_ACTIVATION_START_OTASP; 921 next_state = PLAN_ACTIVATION_START_OTASP;
871 } else { 922 } else {
872 // We're online, which means we've conspired with 923 // We're online, which means we've conspired with
873 // PickNextOnlineState to reconnect after activation (that's the 924 // PickNextOnlineState to reconnect after activation (that's the
874 // only way we see this transition). Thus, after we reconnect, we 925 // only way we see this transition). Thus, after we reconnect, we
875 // should be done. 926 // should be done.
876 next_state = PLAN_ACTIVATION_DONE; 927 next_state = PLAN_ACTIVATION_DONE;
877 } 928 }
878 break; 929 break;
879 default: 930 default:
880 LOG(ERROR) << "Transitioned to RECONNECTING from an unexpected " 931 LOG(ERROR) << "Transitioned to RECONNECTING from an unexpected "
881 << "state."; 932 << "state.";
882 break; 933 break;
883 } 934 }
884 ForceReconnect(network, next_state); 935 ForceReconnect(network, next_state);
885 break; 936 break;
886 } 937 }
887 case PLAN_ACTIVATION_DONE: 938 case PLAN_ACTIVATION_DONE:
888 DCHECK(network); 939 DCHECK(network);
889 CompleteActivation(network); 940 CompleteActivation();
890 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupSucceeded", 1); 941 UMA_HISTOGRAM_COUNTS("Cellular.MobileSetupSucceeded", 1);
891 break; 942 break;
892 case PLAN_ACTIVATION_ERROR: 943 case PLAN_ACTIVATION_ERROR:
893 CompleteActivation(NULL); 944 CompleteActivation();
894 UMA_HISTOGRAM_COUNTS("Cellular.PlanFailed", 1); 945 UMA_HISTOGRAM_COUNTS("Cellular.PlanFailed", 1);
895 break; 946 break;
896 default: 947 default:
897 break; 948 break;
898 } 949 }
899 } 950 }
900 951
901 void MobileActivator::ReEnableCertRevocationChecking() { 952 void MobileActivator::ReEnableCertRevocationChecking() {
902 PrefService* prefs = g_browser_process->local_state(); 953 PrefService* prefs = g_browser_process->local_state();
903 if (reenable_cert_check_) { 954 if (reenable_cert_check_) {
(...skipping 10 matching lines...) Expand all
914 PrefService* prefs = g_browser_process->local_state(); 965 PrefService* prefs = g_browser_process->local_state();
915 if (!reenable_cert_check_ && 966 if (!reenable_cert_check_ &&
916 prefs->GetBoolean( 967 prefs->GetBoolean(
917 prefs::kCertRevocationCheckingEnabled)) { 968 prefs::kCertRevocationCheckingEnabled)) {
918 reenable_cert_check_ = true; 969 reenable_cert_check_ = true;
919 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled, false); 970 prefs->SetBoolean(prefs::kCertRevocationCheckingEnabled, false);
920 } 971 }
921 } 972 }
922 973
923 bool MobileActivator::GotActivationError( 974 bool MobileActivator::GotActivationError(
924 CellularNetwork* network, std::string* error) const { 975 const NetworkState* network, std::string* error) const {
925 DCHECK(network); 976 DCHECK(network);
926 bool got_error = false; 977 bool got_error = false;
927 const char* error_code = kErrorDefault; 978 const char* error_code = kErrorDefault;
979 const std::string& activation = network->activation_state();
928 980
929 // This is the magic for detection of errors in during activation process. 981 // This is the magic for detection of errors in during activation process.
930 if (network->state() == STATE_FAILURE && 982 if (network->connection_state() == flimflam::kStateFailure &&
931 network->error() == ERROR_AAA_FAILED) { 983 network->error() == flimflam::kErrorAaaFailed) {
932 if (network->activation_state() == 984 if (activation == flimflam::kActivationStatePartiallyActivated) {
933 ACTIVATION_STATE_PARTIALLY_ACTIVATED) {
934 error_code = kErrorBadConnectionPartial; 985 error_code = kErrorBadConnectionPartial;
935 } else if (network->activation_state() == ACTIVATION_STATE_ACTIVATED) { 986 } else if (activation == flimflam::kActivationStateActivated) {
936 if (network->roaming_state() == ROAMING_STATE_HOME) 987 if (network->roaming() == flimflam::kRoamingStateHome)
937 error_code = kErrorBadConnectionActivated; 988 error_code = kErrorBadConnectionActivated;
938 else if (network->roaming_state() == ROAMING_STATE_ROAMING) 989 else if (network->roaming() == flimflam::kRoamingStateRoaming)
939 error_code = kErrorRoamingOnConnection; 990 error_code = kErrorRoamingOnConnection;
940 } 991 }
941 got_error = true; 992 got_error = true;
942 } else if (network->state() == STATE_ACTIVATION_FAILURE) { 993 } else if (network->connection_state() == flimflam::kStateActivationFailure) {
943 if (network->error() == ERROR_NEED_EVDO) { 994 if (network->error() == flimflam::kErrorNeedEvdo) {
944 if (network->activation_state() == ACTIVATION_STATE_PARTIALLY_ACTIVATED) 995 if (activation == flimflam::kActivationStatePartiallyActivated)
945 error_code = kErrorNoEVDO; 996 error_code = kErrorNoEVDO;
946 } else if (network->error() == ERROR_NEED_HOME_NETWORK) { 997 } else if (network->error() == flimflam::kErrorNeedHomeNetwork) {
947 if (network->activation_state() == ACTIVATION_STATE_NOT_ACTIVATED) { 998 if (activation == flimflam::kActivationStateNotActivated) {
948 error_code = kErrorRoamingActivation; 999 error_code = kErrorRoamingActivation;
949 } else if (network->activation_state() == 1000 } else if (activation == flimflam::kActivationStatePartiallyActivated) {
950 ACTIVATION_STATE_PARTIALLY_ACTIVATED) {
951 error_code = kErrorRoamingPartiallyActivated; 1001 error_code = kErrorRoamingPartiallyActivated;
952 } 1002 }
953 } 1003 }
954 got_error = true; 1004 got_error = true;
955 } 1005 }
956 1006
957 if (got_error) 1007 if (got_error)
958 *error = GetErrorMessage(error_code); 1008 *error = GetErrorMessage(error_code);
959 1009
960 return got_error; 1010 return got_error;
961 } 1011 }
962 1012
963 void MobileActivator::GetDeviceInfo(CellularNetwork* network,
964 DictionaryValue* value) {
965 DCHECK(network);
966 NetworkLibrary* cros = NetworkLibrary::Get();
967 if (!cros)
968 return;
969 value->SetString("carrier", network->name());
970 value->SetString("payment_url", network->payment_url());
971 if (network->using_post() && network->post_data().length())
972 value->SetString("post_data", network->post_data());
973
974 const NetworkDevice* device =
975 cros->FindNetworkDeviceByPath(network->device_path());
976 if (device) {
977 value->SetString("MEID", device->meid());
978 value->SetString("IMEI", device->imei());
979 value->SetString("MDN", device->mdn());
980 }
981 }
982
983 std::string MobileActivator::GetErrorMessage(const std::string& code) const { 1013 std::string MobileActivator::GetErrorMessage(const std::string& code) const {
984 return cellular_config_->GetErrorMessage(code); 1014 return cellular_config_->GetErrorMessage(code);
985 } 1015 }
986 1016
987 NetworkLibrary* MobileActivator::GetNetworkLibrary() const { 1017 void MobileActivator::SignalCellularPlanPayment() {
988 return NetworkLibrary::Get(); 1018 DCHECK(!HasRecentCellularPlanPayment());
1019 cellular_plan_payment_time_ = base::Time::Now();
1020 }
1021
1022 bool MobileActivator::HasRecentCellularPlanPayment() const {
1023 const int kRecentPlanPaymentHours = 6;
1024 return (base::Time::Now() -
1025 cellular_plan_payment_time_).InHours() < kRecentPlanPaymentHours;
989 } 1026 }
990 1027
991 } // namespace chromeos 1028 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/mobile/mobile_activator.h ('k') | chrome/browser/chromeos/mobile/mobile_activator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698