16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// found in the LICENSE file.
46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/bind.h"
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/strings/string16.h"
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
105b892326406927b709cdaf6c384d4ababf456332Ben Murdoch#include "chrome/browser/browser_process.h"
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/chromeos/chromeos_utils.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace {
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)ScreenlockBridge::UserPodCustomIcon GetIconForState(
1834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    EasyUnlockScreenlockStateHandler::State state) {
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  switch (state) {
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH:
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE:
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED:
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED:
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_NEARBY:
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE:
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED:
2734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED;
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING:
2934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER;
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED:
3134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED;
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    default:
3334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return ScreenlockBridge::USER_POD_CUSTOM_ICON_NONE;
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool HardlockOnClick(EasyUnlockScreenlockStateHandler::State state) {
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return state != EasyUnlockScreenlockStateHandler::STATE_INACTIVE;
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)size_t GetTooltipResourceId(EasyUnlockScreenlockStateHandler::State state) {
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  switch (state) {
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH:
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_NO_BLUETOOTH;
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE:
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_NO_PHONE;
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED:
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_NOT_AUTHENTICATED;
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED:
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_LOCKED;
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE:
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_UNLOCKABLE;
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_NEARBY:
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_PHONE_NOT_NEARBY;
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED:
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_INSTRUCTIONS;
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED:
5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_UNSUPPORTED_ANDROID_VERSION;
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    default:
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return 0;
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool TooltipContainsDeviceType(EasyUnlockScreenlockStateHandler::State state) {
6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return state == EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED ||
6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)         state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE ||
6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)         state == EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH ||
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)         state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED;
6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
715b892326406927b709cdaf6c384d4ababf456332Ben Murdochbool IsLocaleEnUS() {
725b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  return g_browser_process->GetApplicationLocale() == "en-US";
735b892326406927b709cdaf6c384d4ababf456332Ben Murdoch}
745b892326406927b709cdaf6c384d4ababf456332Ben Murdoch
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)EasyUnlockScreenlockStateHandler::EasyUnlockScreenlockStateHandler(
796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const std::string& user_email,
8034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    HardlockState initial_hardlock_state,
816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ScreenlockBridge* screenlock_bridge)
826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    : state_(STATE_INACTIVE),
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      user_email_(user_email),
8434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      screenlock_bridge_(screenlock_bridge),
8534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      hardlock_state_(initial_hardlock_state),
865b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      hardlock_ui_shown_(false),
875b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      is_trial_run_(false) {
886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(screenlock_bridge_);
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  screenlock_bridge_->AddObserver(this);
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)EasyUnlockScreenlockStateHandler::~EasyUnlockScreenlockStateHandler() {
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  screenlock_bridge_->RemoveObserver(this);
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Make sure the screenlock state set by this gets cleared.
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ChangeState(STATE_INACTIVE);
966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
985b892326406927b709cdaf6c384d4ababf456332Ben Murdochbool EasyUnlockScreenlockStateHandler::IsActive() const {
995b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  return state_ != STATE_INACTIVE;
1005b892326406927b709cdaf6c384d4ababf456332Ben Murdoch}
1015b892326406927b709cdaf6c384d4ababf456332Ben Murdoch
1026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::ChangeState(State new_state) {
1036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (state_ == new_state)
1046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
1056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  state_ = new_state;
1076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // If lock screen is not active or it forces offline password, just cache the
10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // current state. The screenlock state will get refreshed in |ScreenDidLock|.
11034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (!screenlock_bridge_->IsLocked())
11134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    return;
11234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
11334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  // No hardlock UI for trial run.
1145b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  if (!is_trial_run_ && hardlock_state_ != NO_HARDLOCK) {
11534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    ShowHardlockUI();
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  UpdateScreenlockAuthType();
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
12134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  ScreenlockBridge::UserPodCustomIcon icon = GetIconForState(state_);
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
12334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (icon == ScreenlockBridge::USER_POD_CUSTOM_ICON_NONE) {
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_);
1256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
1266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
1276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
12834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  ScreenlockBridge::UserPodCustomIconOptions icon_options;
12934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  icon_options.SetIcon(icon);
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
13134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  // Don't hardlock on trial run.
1325b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  if (!is_trial_run_ && HardlockOnClick(state_))
13303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    icon_options.SetHardlockOnClick();
13403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1355b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  UpdateTooltipOptions(is_trial_run_, &icon_options);
13603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
137ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  // For states without tooltips, we still need to set an accessibility label.
138ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  if (state_ == EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING) {
139ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    icon_options.SetAriaLabel(
140ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch        l10n_util::GetStringUTF16(IDS_SMART_LOCK_SPINNER_ACCESSIBILITY_LABEL));
141ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  }
142ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch
1436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_,
1446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                                            icon_options);
1456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
1466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
14734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::SetHardlockState(
14834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    HardlockState new_state) {
14934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (hardlock_state_ == new_state)
15034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    return;
15134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
15234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  hardlock_state_ = new_state;
15334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
15434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  // If hardlock_state_ was set to NO_HARDLOCK, this means the screen is about
15534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  // to get unlocked. No need to update it in this case.
15634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (hardlock_state_ != NO_HARDLOCK) {
15734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    hardlock_ui_shown_ = false;
15834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1595b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    RefreshScreenlockState();
16034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  }
16134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)}
16234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
16334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::MaybeShowHardlockUI() {
16434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (hardlock_state_ != NO_HARDLOCK)
16534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    ShowHardlockUI();
16634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)}
16734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1685b892326406927b709cdaf6c384d4ababf456332Ben Murdochvoid EasyUnlockScreenlockStateHandler::SetTrialRun() {
1695b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  if (is_trial_run_)
1705b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    return;
1715b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  is_trial_run_ = true;
1725b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  RefreshScreenlockState();
1735b892326406927b709cdaf6c384d4ababf456332Ben Murdoch}
1745b892326406927b709cdaf6c384d4ababf456332Ben Murdoch
1756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::OnScreenDidLock() {
1765b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  RefreshScreenlockState();
1776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
1786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::OnScreenDidUnlock() {
18034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  hardlock_ui_shown_ = false;
1815b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  is_trial_run_ = false;
1826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
1836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid EasyUnlockScreenlockStateHandler::OnFocusedUserChanged(
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& user_id) {
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1885b892326406927b709cdaf6c384d4ababf456332Ben Murdochvoid EasyUnlockScreenlockStateHandler::RefreshScreenlockState() {
1895b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  State last_state = state_;
1905b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  // This should force updating screenlock state.
1915b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  state_ = STATE_INACTIVE;
1925b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  ChangeState(last_state);
1935b892326406927b709cdaf6c384d4ababf456332Ben Murdoch}
1945b892326406927b709cdaf6c384d4ababf456332Ben Murdoch
19534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::ShowHardlockUI() {
19634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  DCHECK(hardlock_state_ != NO_HARDLOCK);
19734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
19834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (!screenlock_bridge_->IsLocked())
19934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    return;
20034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
201e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  // Do not override online signin.
202e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  const ScreenlockBridge::LockHandler::AuthType existing_auth_type =
203e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      screenlock_bridge_->lock_handler()->GetAuthType(user_email_);
204e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  if (existing_auth_type == ScreenlockBridge::LockHandler::ONLINE_SIGN_IN)
205e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch    return;
206e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch
207e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  if (existing_auth_type != ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) {
20834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    screenlock_bridge_->lock_handler()->SetAuthType(
20934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)        user_email_,
21034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)        ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
21134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)        base::string16());
21234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  }
21334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
21434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (hardlock_state_ == NO_PAIRING) {
21534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_);
21634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    hardlock_ui_shown_ = false;
21734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    return;
21834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  }
21934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
22034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (hardlock_ui_shown_)
22134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    return;
22234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
22334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  ScreenlockBridge::UserPodCustomIconOptions icon_options;
22434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  icon_options.SetIcon(ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED);
22534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
2265b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  // TODO(tbarzic): Remove this condition for M-40.
2275b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  if (IsLocaleEnUS()) {
2285b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    base::string16 tooltip;
2295b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    if (hardlock_state_ == USER_HARDLOCK) {
2305b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      tooltip = l10n_util::GetStringFUTF16(
2315b892326406927b709cdaf6c384d4ababf456332Ben Murdoch          IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_USER, GetDeviceName());
2325b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    } else if (hardlock_state_ == PAIRING_CHANGED) {
2335b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      tooltip = l10n_util::GetStringUTF16(
2345b892326406927b709cdaf6c384d4ababf456332Ben Murdoch          IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_PAIRING_CHANGED);
235e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch    } else if (hardlock_state_ == PAIRING_ADDED) {
236e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      tooltip = l10n_util::GetStringUTF16(
237e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch          IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_PAIRING_ADDED);
2385b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    } else {
2395b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      LOG(ERROR) << "Unknown hardlock state " << hardlock_state_;
2405b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    }
2415b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    icon_options.SetTooltip(tooltip, true /* autoshow */);
24234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  }
24334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
24434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_,
24534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)                                                            icon_options);
24634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  hardlock_ui_shown_ = true;
24734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)}
24834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
2496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::UpdateTooltipOptions(
25003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool trial_run,
2516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ScreenlockBridge::UserPodCustomIconOptions* icon_options) {
2526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  size_t resource_id = 0;
2536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::string16 device_name;
25403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (trial_run && state_ == STATE_AUTHENTICATED) {
2555b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    // TODO(tbarzic): Remove this condition for M-40 branch.
2565b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    if (IsLocaleEnUS())
2575b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_INITIAL_AUTHENTICATED;
2585b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    else
2595b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_TUTORIAL;
2606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else {
2616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    resource_id = GetTooltipResourceId(state_);
26203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (TooltipContainsDeviceType(state_))
2636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      device_name = GetDeviceName();
2646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!resource_id)
2676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::string16 tooltip;
2706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (device_name.empty()) {
2716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    tooltip = l10n_util::GetStringUTF16(resource_id);
2726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  } else {
2736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    tooltip = l10n_util::GetStringFUTF16(resource_id, device_name);
2746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (tooltip.empty())
2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2795b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  icon_options->SetTooltip(tooltip, trial_run /* autoshow tooltip */);
2806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)base::string16 EasyUnlockScreenlockStateHandler::GetDeviceName() {
2836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_CHROMEOS)
2846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return chromeos::GetChromeDeviceType();
2856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else
2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // TODO(tbarzic): Figure out the name for non Chrome OS case.
2876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return base::ASCIIToUTF16("Chrome");
2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void EasyUnlockScreenlockStateHandler::UpdateScreenlockAuthType() {
2925b892326406927b709cdaf6c384d4ababf456332Ben Murdoch  if (!is_trial_run_ && hardlock_state_ != NO_HARDLOCK)
29303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return;
29403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
295e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  // Do not override online signin.
296e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  const ScreenlockBridge::LockHandler::AuthType existing_auth_type =
297e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      screenlock_bridge_->lock_handler()->GetAuthType(user_email_);
298e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  if (existing_auth_type == ScreenlockBridge::LockHandler::ONLINE_SIGN_IN)
299e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch    return;
300e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch
3016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (state_ == STATE_AUTHENTICATED) {
302e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch    if (existing_auth_type != ScreenlockBridge::LockHandler::USER_CLICK) {
3035b892326406927b709cdaf6c384d4ababf456332Ben Murdoch      screenlock_bridge_->lock_handler()->SetAuthType(
3045b892326406927b709cdaf6c384d4ababf456332Ben Murdoch          user_email_,
3055b892326406927b709cdaf6c384d4ababf456332Ben Murdoch          ScreenlockBridge::LockHandler::USER_CLICK,
3065b892326406927b709cdaf6c384d4ababf456332Ben Murdoch          l10n_util::GetStringUTF16(
3075b892326406927b709cdaf6c384d4ababf456332Ben Murdoch              IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE));
3085b892326406927b709cdaf6c384d4ababf456332Ben Murdoch    }
309e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  } else if (existing_auth_type !=
310e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch             ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) {
3116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    screenlock_bridge_->lock_handler()->SetAuthType(
3126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        user_email_,
3136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
3146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        base::string16());
3156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
3166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
317