1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/chromeos/sim_unlock_ui.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/strings/string_piece.h"
18 #include "base/values.h"
19 #include "chrome/browser/chromeos/sim_dialog_delegate.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/url_constants.h"
22 #include "chromeos/network/device_state.h"
23 #include "chromeos/network/network_device_handler.h"
24 #include "chromeos/network/network_event_log.h"
25 #include "chromeos/network/network_state_handler.h"
26 #include "chromeos/network/network_state_handler_observer.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/url_data_source.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_ui.h"
31 #include "content/public/browser/web_ui_message_handler.h"
32 #include "grit/browser_resources.h"
33 #include "grit/generated_resources.h"
34 #include "third_party/cros_system_api/dbus/service_constants.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/webui/jstemplate_builder.h"
38 #include "ui/base/webui/web_ui_util.h"
40 using content::BrowserThread;
41 using content::WebContents;
42 using content::WebUIMessageHandler;
46 // JS API callbacks names.
47 const char kJsApiChangePinCode[] = "changePinCode";
48 const char kJsApiEnterPinCode[] = "enterPinCode";
49 const char kJsApiEnterPukCode[] = "enterPukCode";
50 const char kJsApiProceedToPukInput[] = "proceedToPukInput";
51 const char kJsApiSimStatusInitialize[] = "simStatusInitialize";
53 // Page JS API function names.
54 const char kJsApiSimStatusChanged[] = "mobile.SimUnlock.simStateChanged";
56 // SIM state variables which are passed to the page.
57 const char kState[] = "state";
58 const char kError[] = "error";
59 const char kTriesLeft[] = "tries";
61 // Error constants, passed to the page.
62 const char kErrorPin[] = "incorrectPin";
63 const char kErrorOk[] = "ok";
65 chromeos::NetworkDeviceHandler* GetNetworkDeviceHandler() {
66 return chromeos::NetworkHandler::Get()->network_device_handler();
69 chromeos::NetworkStateHandler* GetNetworkStateHandler() {
70 return chromeos::NetworkHandler::Get()->network_state_handler();
77 class SimUnlockUIHTMLSource : public content::URLDataSource {
79 SimUnlockUIHTMLSource();
81 // content::URLDataSource implementation.
82 virtual std::string GetSource() const OVERRIDE;
83 virtual void StartDataRequest(
84 const std::string& path,
85 int render_process_id,
87 const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
88 virtual std::string GetMimeType(const std::string&) const OVERRIDE {
91 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE {
96 virtual ~SimUnlockUIHTMLSource() {}
98 std::string service_path_;
99 DISALLOW_COPY_AND_ASSIGN(SimUnlockUIHTMLSource);
102 // The handler for Javascript messages related to the "sim-unlock" view.
103 class SimUnlockHandler : public WebUIMessageHandler,
104 public base::SupportsWeakPtr<SimUnlockHandler>,
105 public NetworkStateHandlerObserver {
108 virtual ~SimUnlockHandler();
110 // WebUIMessageHandler implementation.
111 virtual void RegisterMessages() OVERRIDE;
113 // NetworkStateHandlerObserver implementation.
114 virtual void DeviceListChanged() OVERRIDE;
117 // Should keep this state enum in sync with similar one in JS code.
118 // SIM_NOT_LOCKED_ASK_PIN - SIM card is not locked but we ask user
119 // for PIN input because PinRequired preference change was requested.
120 // SIM_NOT_LOCKED_CHANGE_PIN - SIM card is not locked, ask user for old PIN
121 // and new PIN to change it.
122 typedef enum SimUnlockState {
123 SIM_UNLOCK_LOADING = -1,
124 SIM_ABSENT_NOT_LOCKED = 0,
125 SIM_NOT_LOCKED_ASK_PIN = 1,
126 SIM_NOT_LOCKED_CHANGE_PIN = 2,
128 SIM_LOCKED_NO_PIN_TRIES_LEFT = 4,
130 SIM_LOCKED_NO_PUK_TRIES_LEFT = 6,
134 // Type of the SIM unlock code.
140 enum PinOperationError {
142 PIN_ERROR_UNKNOWN = 1,
143 PIN_ERROR_INCORRECT_CODE = 2,
144 PIN_ERROR_BLOCKED = 3
147 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
149 explicit TaskProxy(const base::WeakPtr<SimUnlockHandler>& handler)
154 TaskProxy(const base::WeakPtr<SimUnlockHandler>& handler,
155 const std::string& code,
156 SimUnlockCode code_type)
159 code_type_(code_type) {
162 void HandleEnterCode() {
164 handler_->EnterCode(code_, code_type_);
167 void HandleInitialize() {
169 handler_->InitializeSimStatus();
172 void HandleProceedToPukInput() {
174 handler_->ProceedToPukInput();
178 friend class base::RefCountedThreadSafe<TaskProxy>;
182 base::WeakPtr<SimUnlockHandler> handler_;
184 // Pending code input (PIN/PUK).
187 // Pending code type.
188 SimUnlockCode code_type_;
190 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
193 // Returns the cellular device that this dialog currently corresponds to.
194 const DeviceState* GetCellularDevice();
196 // Pass PIN/PUK code to shill and check status.
197 void EnterCode(const std::string& code, SimUnlockCode code_type);
199 // Methods to invoke shill PIN/PUK D-Bus operations.
200 void ChangeRequirePin(bool require_pin, const std::string& pin);
201 void EnterPin(const std::string& pin);
202 void ChangePin(const std::string& old_pin, const std::string& new_pin);
203 void UnblockPin(const std::string& puk, const std::string& new_pin);
204 void PinOperationSuccessCallback(const std::string& operation_name);
205 void PinOperationErrorCallback(const std::string& operation_name,
206 const std::string& error_name,
207 scoped_ptr<base::DictionaryValue> error_data);
209 // Called when an asynchronous PIN operation has completed.
210 void OnPinOperationCompleted(PinOperationError error);
212 // Single handler for PIN/PUK code operations.
213 void HandleEnterCode(SimUnlockCode code_type, const std::string& code);
215 // Handlers for JS WebUI messages.
216 void HandleChangePinCode(const base::ListValue* args);
217 void HandleEnterPinCode(const base::ListValue* args);
218 void HandleEnterPukCode(const base::ListValue* args);
219 void HandleProceedToPukInput(const base::ListValue* args);
220 void HandleSimStatusInitialize(const base::ListValue* args);
222 // Initialize current SIM card status, passes that to page.
223 void InitializeSimStatus();
225 // Checks whether SIM card is in PUK locked state and proceeds to PUK input.
226 void ProceedToPukInput();
228 // Processes current SIM card state and update internal state/page.
229 void ProcessSimCardState(const DeviceState* cellular);
231 // Updates page with the current state/SIM card info/error.
232 void UpdatePage(const DeviceState* cellular, const std::string& error_msg);
234 // Dialog internal state.
235 SimUnlockState state_;
237 // Path of the Cellular device that we monitor property updates from.
238 std::string cellular_device_path_;
240 // Type of the dialog: generic unlock/change pin/change PinRequire.
241 SimDialogDelegate::SimDialogMode dialog_mode_;
243 // New PIN value for the case when we unblock SIM card or change PIN.
244 std::string new_pin_;
246 // The initial lock type value, used to observe changes to lock status;
247 std::string sim_lock_type_;
249 // True if there's a pending PIN operation.
250 // That means that SIM lock state change will be received 2 times:
251 // OnNetworkDeviceSimLockChanged and OnPinOperationCompleted.
252 // First one should be ignored.
253 bool pending_pin_operation_;
255 base::WeakPtrFactory<SimUnlockHandler> weak_ptr_factory_;
257 DISALLOW_COPY_AND_ASSIGN(SimUnlockHandler);
260 // SimUnlockUIHTMLSource -------------------------------------------------------
262 SimUnlockUIHTMLSource::SimUnlockUIHTMLSource() {
265 std::string SimUnlockUIHTMLSource::GetSource() const {
266 return chrome::kChromeUISimUnlockHost;
269 void SimUnlockUIHTMLSource::StartDataRequest(
270 const std::string& path,
271 int render_process_id,
273 const content::URLDataSource::GotDataCallback& callback) {
274 base::DictionaryValue strings;
275 strings.SetString("title",
276 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PIN_TITLE));
277 strings.SetString("ok", l10n_util::GetStringUTF16(IDS_OK));
278 strings.SetString("enterPinTitle",
279 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PIN_TITLE));
280 strings.SetString("enterPinMessage",
281 l10n_util::GetStringUTF16(IDS_SIM_ENTER_PIN_MESSAGE));
282 strings.SetString("enterPinTriesMessage",
283 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PIN_TRIES_MESSAGE));
284 strings.SetString("incorrectPinTriesMessage",
285 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_INCORRECT_PIN_TRIES_MESSAGE));
286 strings.SetString("incorrectPinTitle",
287 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_INCORRECT_PIN_TITLE));
288 // TODO(nkostylev): Pass carrier name if we know that.
289 strings.SetString("noPinTriesLeft", l10n_util::GetStringFUTF16(
290 IDS_SIM_UNLOCK_NO_PIN_TRIES_LEFT_MESSAGE,
291 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_DEFAULT_CARRIER)));
292 strings.SetString("enterPukButton",
293 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PUK_BUTTON));
294 strings.SetString("enterPukTitle",
295 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PUK_TITLE));
296 strings.SetString("enterPukWarning",
297 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_ENTER_PUK_WARNING));
298 // TODO(nkostylev): Pass carrier name if we know that.
299 strings.SetString("enterPukMessage", l10n_util::GetStringFUTF16(
300 IDS_SIM_UNLOCK_ENTER_PUK_MESSAGE,
301 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_DEFAULT_CARRIER)));
302 strings.SetString("choosePinTitle",
303 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_CHOOSE_PIN_TITLE));
304 strings.SetString("choosePinMessage",
305 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_CHOOSE_PIN_MESSAGE));
306 strings.SetString("newPin", l10n_util::GetStringUTF16(
307 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_NEW_PIN));
308 strings.SetString("retypeNewPin", l10n_util::GetStringUTF16(
309 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_RETYPE_PIN));
310 strings.SetString("pinsDontMatchMessage", l10n_util::GetStringUTF16(
311 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_PINS_DONT_MATCH_ERROR));
312 strings.SetString("noPukTriesLeft",
313 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_NO_PUK_TRIES_LEFT_MESSAGE));
314 strings.SetString("simDisabledTitle",
315 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_SIM_DISABLED_TITLE));
316 strings.SetString("simDisabledMessage",
317 l10n_util::GetStringUTF16(IDS_SIM_UNLOCK_SIM_DISABLED_MESSAGE));
319 strings.SetString("changePinTitle", l10n_util::GetStringUTF16(
320 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_TITLE));
321 strings.SetString("changePinMessage", l10n_util::GetStringUTF16(
322 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_MESSAGE));
323 strings.SetString("oldPin", l10n_util::GetStringUTF16(
324 IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_OLD_PIN));
326 webui::SetFontAndTextDirection(&strings);
328 static const base::StringPiece html(
329 ResourceBundle::GetSharedInstance().GetRawDataResource(
330 IDR_SIM_UNLOCK_HTML));
332 std::string full_html = webui::GetI18nTemplateHtml(html, &strings);
334 callback.Run(base::RefCountedString::TakeString(&full_html));
337 // SimUnlockHandler ------------------------------------------------------------
339 SimUnlockHandler::SimUnlockHandler()
340 : state_(SIM_UNLOCK_LOADING),
341 dialog_mode_(SimDialogDelegate::SIM_DIALOG_UNLOCK),
342 pending_pin_operation_(false),
343 weak_ptr_factory_(this) {
344 if (GetNetworkStateHandler()
345 ->GetTechnologyState(NetworkTypePattern::Cellular()) !=
346 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE)
347 GetNetworkStateHandler()->AddObserver(this, FROM_HERE);
350 SimUnlockHandler::~SimUnlockHandler() {
351 GetNetworkStateHandler()->RemoveObserver(this, FROM_HERE);
354 void SimUnlockHandler::RegisterMessages() {
355 web_ui()->RegisterMessageCallback(kJsApiChangePinCode,
356 base::Bind(&SimUnlockHandler::HandleChangePinCode,
357 base::Unretained(this)));
358 web_ui()->RegisterMessageCallback(kJsApiEnterPinCode,
359 base::Bind(&SimUnlockHandler::HandleEnterPinCode,
360 base::Unretained(this)));
361 web_ui()->RegisterMessageCallback(kJsApiEnterPukCode,
362 base::Bind(&SimUnlockHandler::HandleEnterPukCode,
363 base::Unretained(this)));
364 web_ui()->RegisterMessageCallback(kJsApiProceedToPukInput,
365 base::Bind(&SimUnlockHandler::HandleProceedToPukInput,
366 base::Unretained(this)));
367 web_ui()->RegisterMessageCallback(kJsApiSimStatusInitialize,
368 base::Bind(&SimUnlockHandler::HandleSimStatusInitialize,
369 base::Unretained(this)));
372 void SimUnlockHandler::DeviceListChanged() {
373 const DeviceState* cellular_device = GetCellularDevice();
374 if (!cellular_device) {
375 LOG(WARNING) << "Cellular device with path '" << cellular_device_path_
377 ProcessSimCardState(NULL);
381 // Process the SIM card state only if the lock state changed.
382 if (cellular_device->sim_lock_type() == sim_lock_type_)
385 sim_lock_type_ = cellular_device->sim_lock_type();
386 uint32 retries_left = cellular_device->sim_retries_left();
387 VLOG(1) << "OnNetworkDeviceSimLockChanged, lock: " << sim_lock_type_
388 << ", retries: " << retries_left;
389 // There's a pending PIN operation.
390 // Wait for it to finish and refresh state then.
391 if (!pending_pin_operation_)
392 ProcessSimCardState(cellular_device);
395 void SimUnlockHandler::OnPinOperationCompleted(PinOperationError error) {
396 pending_pin_operation_ = false;
397 VLOG(1) << "OnPinOperationCompleted, error: " << error;
398 const DeviceState* cellular = GetCellularDevice();
400 VLOG(1) << "Cellular device disappeared. Dismissing dialog.";
401 ProcessSimCardState(NULL);
404 if (state_ == SIM_NOT_LOCKED_ASK_PIN && error == PIN_ERROR_NONE) {
405 CHECK(dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON ||
406 dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_OFF);
407 // Dialog will close itself.
408 state_ = SIM_ABSENT_NOT_LOCKED;
409 } else if (state_ == SIM_NOT_LOCKED_CHANGE_PIN && error == PIN_ERROR_NONE) {
410 CHECK(dialog_mode_ == SimDialogDelegate::SIM_DIALOG_CHANGE_PIN);
411 // Dialog will close itself.
412 state_ = SIM_ABSENT_NOT_LOCKED;
414 // If previous EnterPIN was last PIN attempt and SIMLock state was already
415 // processed by OnNetworkDeviceChanged, let dialog stay on
416 // NO_PIN_RETRIES_LEFT step.
417 if (!(state_ == SIM_LOCKED_NO_PIN_TRIES_LEFT && error == PIN_ERROR_BLOCKED))
418 ProcessSimCardState(cellular);
421 const DeviceState* SimUnlockHandler::GetCellularDevice() {
422 return GetNetworkStateHandler()->GetDeviceState(cellular_device_path_);
425 void SimUnlockHandler::EnterCode(const std::string& code,
426 SimUnlockCode code_type) {
427 DCHECK_CURRENTLY_ON(BrowserThread::UI);
429 pending_pin_operation_ = true;
433 if (dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON ||
434 dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_OFF) {
435 if (!sim_lock_type_.empty()) {
436 // If SIM is locked/absent, change RequirePin UI is not accessible.
438 "Changing RequirePin pref on locked / uninitialized SIM.";
441 dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON,
443 } else if (dialog_mode_ == SimDialogDelegate::SIM_DIALOG_CHANGE_PIN) {
444 if (!sim_lock_type_.empty()) {
445 // If SIM is locked/absent, changing PIN UI is not accessible.
446 NOTREACHED() << "Changing PIN on locked / uninitialized SIM.";
448 ChangePin(code, new_pin_);
454 DCHECK(!new_pin_.empty());
455 UnblockPin(code, new_pin_);
460 void SimUnlockHandler::ChangeRequirePin(bool require_pin,
461 const std::string& pin) {
462 const DeviceState* cellular = GetCellularDevice();
464 NOTREACHED() << "Calling RequirePin method w/o cellular device.";
467 std::string operation_name = "ChangeRequirePin";
468 NET_LOG_USER(operation_name, cellular->path());
469 GetNetworkDeviceHandler()->RequirePin(
473 base::Bind(&SimUnlockHandler::PinOperationSuccessCallback,
474 weak_ptr_factory_.GetWeakPtr(),
476 base::Bind(&SimUnlockHandler::PinOperationErrorCallback,
477 weak_ptr_factory_.GetWeakPtr(),
481 void SimUnlockHandler::EnterPin(const std::string& pin) {
482 const DeviceState* cellular = GetCellularDevice();
484 NOTREACHED() << "Calling RequirePin method w/o cellular device.";
487 std::string operation_name = "EnterPin";
488 NET_LOG_USER(operation_name, cellular->path());
489 GetNetworkDeviceHandler()->EnterPin(
492 base::Bind(&SimUnlockHandler::PinOperationSuccessCallback,
493 weak_ptr_factory_.GetWeakPtr(),
495 base::Bind(&SimUnlockHandler::PinOperationErrorCallback,
496 weak_ptr_factory_.GetWeakPtr(),
500 void SimUnlockHandler::ChangePin(const std::string& old_pin,
501 const std::string& new_pin) {
502 const DeviceState* cellular = GetCellularDevice();
504 NOTREACHED() << "Calling RequirePin method w/o cellular device.";
507 std::string operation_name = "ChangePin";
508 NET_LOG_USER(operation_name, cellular->path());
509 GetNetworkDeviceHandler()->ChangePin(
513 base::Bind(&SimUnlockHandler::PinOperationSuccessCallback,
514 weak_ptr_factory_.GetWeakPtr(),
516 base::Bind(&SimUnlockHandler::PinOperationErrorCallback,
517 weak_ptr_factory_.GetWeakPtr(),
521 void SimUnlockHandler::UnblockPin(const std::string& puk,
522 const std::string& new_pin) {
523 const DeviceState* cellular = GetCellularDevice();
525 NOTREACHED() << "Calling RequirePin method w/o cellular device.";
528 std::string operation_name = "UnblockPin";
529 NET_LOG_USER(operation_name, cellular->path());
530 GetNetworkDeviceHandler()->UnblockPin(
534 base::Bind(&SimUnlockHandler::PinOperationSuccessCallback,
535 weak_ptr_factory_.GetWeakPtr(),
537 base::Bind(&SimUnlockHandler::PinOperationErrorCallback,
538 weak_ptr_factory_.GetWeakPtr(),
542 void SimUnlockHandler::PinOperationSuccessCallback(
543 const std::string& operation_name) {
544 NET_LOG_DEBUG("Pin operation successful.", operation_name);
545 OnPinOperationCompleted(PIN_ERROR_NONE);
548 void SimUnlockHandler::PinOperationErrorCallback(
549 const std::string& operation_name,
550 const std::string& error_name,
551 scoped_ptr<base::DictionaryValue> error_data) {
552 NET_LOG_ERROR("Pin operation failed: " + error_name, operation_name);
553 PinOperationError pin_error;
554 if (error_name == NetworkDeviceHandler::kErrorIncorrectPin ||
555 error_name == NetworkDeviceHandler::kErrorPinRequired)
556 pin_error = PIN_ERROR_INCORRECT_CODE;
557 else if (error_name == NetworkDeviceHandler::kErrorPinBlocked)
558 pin_error = PIN_ERROR_BLOCKED;
560 pin_error = PIN_ERROR_UNKNOWN;
561 OnPinOperationCompleted(pin_error);
564 void SimUnlockHandler::HandleChangePinCode(const base::ListValue* args) {
565 const size_t kChangePinParamCount = 2;
568 if (args->GetSize() != kChangePinParamCount ||
569 !args->GetString(0, &pin) ||
570 !args->GetString(1, &new_pin)) {
575 HandleEnterCode(CODE_PIN, pin);
578 void SimUnlockHandler::HandleEnterCode(SimUnlockCode code_type,
579 const std::string& code) {
580 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), code, code_type);
581 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
582 base::Bind(&TaskProxy::HandleEnterCode, task.get()));
585 void SimUnlockHandler::HandleEnterPinCode(const base::ListValue* args) {
586 const size_t kEnterPinParamCount = 1;
588 if (args->GetSize() != kEnterPinParamCount || !args->GetString(0, &pin)) {
592 HandleEnterCode(CODE_PIN, pin);
595 void SimUnlockHandler::HandleEnterPukCode(const base::ListValue* args) {
596 const size_t kEnterPukParamCount = 2;
599 if (args->GetSize() != kEnterPukParamCount ||
600 !args->GetString(0, &puk) ||
601 !args->GetString(1, &new_pin)) {
606 HandleEnterCode(CODE_PUK, puk);
609 void SimUnlockHandler::HandleProceedToPukInput(const base::ListValue* args) {
610 const size_t kProceedToPukInputParamCount = 0;
611 if (args->GetSize() != kProceedToPukInputParamCount) {
615 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr());
616 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
617 base::Bind(&TaskProxy::HandleProceedToPukInput, task.get()));
620 void SimUnlockHandler::HandleSimStatusInitialize(const base::ListValue* args) {
621 const size_t kSimStatusInitializeParamCount = 1;
623 if (args->GetSize() != kSimStatusInitializeParamCount ||
624 !args->GetDouble(0, &mode)) {
628 dialog_mode_ = static_cast<SimDialogDelegate::SimDialogMode>(mode);
629 VLOG(1) << "Initializing SIM dialog in mode: " << dialog_mode_;
630 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr());
631 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
632 base::Bind(&TaskProxy::HandleInitialize, task.get()));
635 void SimUnlockHandler::InitializeSimStatus() {
636 DCHECK_CURRENTLY_ON(BrowserThread::UI);
637 // TODO(armansito): For now, we're initializing the device path to the first
638 // available cellular device. We should try to obtain a specific device here,
639 // as there can be multiple cellular devices present.
640 const DeviceState* cellular_device =
641 GetNetworkStateHandler()
642 ->GetDeviceStateByType(NetworkTypePattern::Cellular());
643 if (cellular_device) {
644 cellular_device_path_ = cellular_device->path();
645 sim_lock_type_ = cellular_device->sim_lock_type();
647 ProcessSimCardState(cellular_device);
650 void SimUnlockHandler::ProceedToPukInput() {
651 DCHECK_CURRENTLY_ON(BrowserThread::UI);
652 ProcessSimCardState(GetCellularDevice());
655 void SimUnlockHandler::ProcessSimCardState(
656 const DeviceState* cellular) {
657 std::string error_msg;
659 uint32 retries_left = cellular->sim_retries_left();
660 VLOG(1) << "Current state: " << state_ << " lock_type: " << sim_lock_type_
661 << " retries: " << retries_left;
663 case SIM_UNLOCK_LOADING:
664 if (sim_lock_type_ == shill::kSIMLockPin) {
665 state_ = SIM_LOCKED_PIN;
666 } else if (sim_lock_type_ == shill::kSIMLockPuk) {
667 if (retries_left > 0)
668 state_ = SIM_LOCKED_PUK;
670 state_ = SIM_DISABLED;
671 } else if (sim_lock_type_.empty()) {
672 if (dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON ||
673 dialog_mode_ == SimDialogDelegate::SIM_DIALOG_SET_LOCK_OFF) {
674 state_ = SIM_NOT_LOCKED_ASK_PIN;
675 } else if (dialog_mode_ == SimDialogDelegate::SIM_DIALOG_CHANGE_PIN) {
676 state_ = SIM_NOT_LOCKED_CHANGE_PIN;
678 state_ = SIM_ABSENT_NOT_LOCKED;
681 // SIM_UNKNOWN: when SIM status is not initialized (should not happen,
682 // since this UI is accessible when SIM is initialized)
683 // or SIM card is absent. In latter case just close dialog.
684 state_ = SIM_ABSENT_NOT_LOCKED;
687 case SIM_ABSENT_NOT_LOCKED:
688 // Dialog will close itself in this case.
690 case SIM_NOT_LOCKED_ASK_PIN:
691 case SIM_NOT_LOCKED_CHANGE_PIN:
692 // We always start in these states when SIM is unlocked.
693 // So if we get here while still being UNLOCKED,
694 // that means entered PIN was incorrect.
695 if (sim_lock_type_.empty()) {
696 error_msg = kErrorPin;
697 } else if (sim_lock_type_ == shill::kSIMLockPuk) {
698 state_ = SIM_LOCKED_NO_PIN_TRIES_LEFT;
701 << "Change PIN / Set lock mode with unexpected SIM lock state";
702 state_ = SIM_ABSENT_NOT_LOCKED;
706 if (sim_lock_type_ == shill::kSIMLockPuk) {
707 state_ = SIM_LOCKED_NO_PIN_TRIES_LEFT;
708 } else if (sim_lock_type_ == shill::kSIMLockPin) {
709 // Still locked with PIN.
710 error_msg = kErrorPin;
712 state_ = SIM_ABSENT_NOT_LOCKED;
715 case SIM_LOCKED_NO_PIN_TRIES_LEFT:
716 // Proceed user to PUK input.
717 state_ = SIM_LOCKED_PUK;
720 if (sim_lock_type_ != shill::kSIMLockPin &&
721 sim_lock_type_ != shill::kSIMLockPuk) {
722 state_ = SIM_ABSENT_NOT_LOCKED;
723 } else if (retries_left == 0) {
724 state_ = SIM_LOCKED_NO_PUK_TRIES_LEFT;
726 // Otherwise SIM card is still locked with PUK code.
727 // Dialog will display enter PUK screen with an updated retries count.
729 case SIM_LOCKED_NO_PUK_TRIES_LEFT:
731 // User will close dialog manually.
735 VLOG(1) << "Cellular device is absent.";
736 // No cellular device, should close dialog.
737 state_ = SIM_ABSENT_NOT_LOCKED;
739 VLOG(1) << "New state: " << state_;
740 UpdatePage(cellular, error_msg);
743 void SimUnlockHandler::UpdatePage(const DeviceState* cellular,
744 const std::string& error_msg) {
745 base::DictionaryValue sim_dict;
747 sim_dict.SetInteger(kTriesLeft, cellular->sim_retries_left());
748 sim_dict.SetInteger(kState, state_);
749 if (!error_msg.empty())
750 sim_dict.SetString(kError, error_msg);
752 sim_dict.SetString(kError, kErrorOk);
753 web_ui()->CallJavascriptFunction(kJsApiSimStatusChanged, sim_dict);
756 // SimUnlockUI -----------------------------------------------------------------
758 SimUnlockUI::SimUnlockUI(content::WebUI* web_ui) : WebUIController(web_ui) {
759 SimUnlockHandler* handler = new SimUnlockHandler();
760 web_ui->AddMessageHandler(handler);
761 SimUnlockUIHTMLSource* html_source = new SimUnlockUIHTMLSource();
763 // Set up the chrome://sim-unlock/ source.
764 Profile* profile = Profile::FromWebUI(web_ui);
765 content::URLDataSource::Add(profile, html_source);
768 } // namespace chromeos