OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/signin/easy_unlock_screenlock_state_handler.h" | 5 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/chromeos/chromeos_utils.h" | 11 #include "chrome/browser/chromeos/chromeos_utils.h" |
12 #include "chrome/common/pref_names.h" | |
13 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
14 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 ScreenlockBridge::UserPodCustomIcon GetIconForState( | 17 ScreenlockBridge::UserPodCustomIcon GetIconForState( |
19 EasyUnlockScreenlockStateHandler::State state) { | 18 EasyUnlockScreenlockStateHandler::State state) { |
20 switch (state) { | 19 switch (state) { |
21 case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH: | 20 case EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH: |
22 case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE: | 21 case EasyUnlockScreenlockStateHandler::STATE_NO_PHONE: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 bool TooltipContainsDeviceType(EasyUnlockScreenlockStateHandler::State state) { | 64 bool TooltipContainsDeviceType(EasyUnlockScreenlockStateHandler::State state) { |
66 return state == EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED || | 65 return state == EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED || |
67 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE || | 66 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE || |
68 state == EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH || | 67 state == EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH || |
69 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED; | 68 state == EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED; |
70 } | 69 } |
71 | 70 |
71 bool IsLocaleEnUS() { | |
72 return g_browser_process->GetApplicationLocale() == "en-US"; | |
73 } | |
74 | |
72 } // namespace | 75 } // namespace |
73 | 76 |
74 | 77 |
75 EasyUnlockScreenlockStateHandler::EasyUnlockScreenlockStateHandler( | 78 EasyUnlockScreenlockStateHandler::EasyUnlockScreenlockStateHandler( |
76 const std::string& user_email, | 79 const std::string& user_email, |
77 HardlockState initial_hardlock_state, | 80 HardlockState initial_hardlock_state, |
78 PrefService* pref_service, | |
79 ScreenlockBridge* screenlock_bridge) | 81 ScreenlockBridge* screenlock_bridge) |
80 : state_(STATE_INACTIVE), | 82 : state_(STATE_INACTIVE), |
81 user_email_(user_email), | 83 user_email_(user_email), |
82 pref_service_(pref_service), | |
83 screenlock_bridge_(screenlock_bridge), | 84 screenlock_bridge_(screenlock_bridge), |
84 hardlock_state_(initial_hardlock_state), | 85 hardlock_state_(initial_hardlock_state), |
85 hardlock_ui_shown_(false) { | 86 hardlock_ui_shown_(false), |
87 is_trial_run_(false) { | |
86 DCHECK(screenlock_bridge_); | 88 DCHECK(screenlock_bridge_); |
87 screenlock_bridge_->AddObserver(this); | 89 screenlock_bridge_->AddObserver(this); |
88 } | 90 } |
89 | 91 |
90 EasyUnlockScreenlockStateHandler::~EasyUnlockScreenlockStateHandler() { | 92 EasyUnlockScreenlockStateHandler::~EasyUnlockScreenlockStateHandler() { |
91 screenlock_bridge_->RemoveObserver(this); | 93 screenlock_bridge_->RemoveObserver(this); |
92 // Make sure the screenlock state set by this gets cleared. | 94 // Make sure the screenlock state set by this gets cleared. |
93 ChangeState(STATE_INACTIVE); | 95 ChangeState(STATE_INACTIVE); |
94 } | 96 } |
95 | 97 |
96 void EasyUnlockScreenlockStateHandler::ChangeState(State new_state) { | 98 void EasyUnlockScreenlockStateHandler::ChangeState(State new_state) { |
97 if (state_ == new_state) | 99 if (state_ == new_state) |
98 return; | 100 return; |
99 | 101 |
100 state_ = new_state; | 102 state_ = new_state; |
101 | 103 |
102 // If lock screen is not active or it forces offline password, just cache the | 104 // If lock screen is not active or it forces offline password, just cache the |
103 // current state. The screenlock state will get refreshed in |ScreenDidLock|. | 105 // current state. The screenlock state will get refreshed in |ScreenDidLock|. |
104 if (!screenlock_bridge_->IsLocked()) | 106 if (!screenlock_bridge_->IsLocked()) |
105 return; | 107 return; |
106 | 108 |
107 const bool trial_run = IsTrialRun(); | |
108 | |
109 // No hardlock UI for trial run. | 109 // No hardlock UI for trial run. |
110 if (!trial_run && hardlock_state_ != NO_HARDLOCK) { | 110 if (!is_trial_run_ && hardlock_state_ != NO_HARDLOCK) { |
111 ShowHardlockUI(); | 111 ShowHardlockUI(); |
112 return; | 112 return; |
113 } | 113 } |
114 | 114 |
115 UpdateScreenlockAuthType(); | 115 UpdateScreenlockAuthType(); |
116 | 116 |
117 ScreenlockBridge::UserPodCustomIcon icon = GetIconForState(state_); | 117 ScreenlockBridge::UserPodCustomIcon icon = GetIconForState(state_); |
118 | 118 |
119 if (icon == ScreenlockBridge::USER_POD_CUSTOM_ICON_NONE) { | 119 if (icon == ScreenlockBridge::USER_POD_CUSTOM_ICON_NONE) { |
120 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); | 120 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 ScreenlockBridge::UserPodCustomIconOptions icon_options; | 124 ScreenlockBridge::UserPodCustomIconOptions icon_options; |
125 icon_options.SetIcon(icon); | 125 icon_options.SetIcon(icon); |
126 | 126 |
127 // Don't hardlock on trial run. | 127 // Don't hardlock on trial run. |
128 if (!trial_run && HardlockOnClick(state_)) | 128 if (!is_trial_run_ && HardlockOnClick(state_)) |
129 icon_options.SetHardlockOnClick(); | 129 icon_options.SetHardlockOnClick(); |
130 | 130 |
131 UpdateTooltipOptions(trial_run, &icon_options); | 131 UpdateTooltipOptions(is_trial_run_, &icon_options); |
132 | 132 |
133 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, | 133 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, |
134 icon_options); | 134 icon_options); |
135 } | 135 } |
136 | 136 |
137 void EasyUnlockScreenlockStateHandler::SetHardlockState( | 137 void EasyUnlockScreenlockStateHandler::SetHardlockState( |
138 HardlockState new_state) { | 138 HardlockState new_state) { |
139 if (hardlock_state_ == new_state) | 139 if (hardlock_state_ == new_state) |
140 return; | 140 return; |
141 | 141 |
142 hardlock_state_ = new_state; | 142 hardlock_state_ = new_state; |
143 | 143 |
144 // If hardlock_state_ was set to NO_HARDLOCK, this means the screen is about | 144 // If hardlock_state_ was set to NO_HARDLOCK, this means the screen is about |
145 // to get unlocked. No need to update it in this case. | 145 // to get unlocked. No need to update it in this case. |
146 if (hardlock_state_ != NO_HARDLOCK) { | 146 if (hardlock_state_ != NO_HARDLOCK) { |
147 hardlock_ui_shown_ = false; | 147 hardlock_ui_shown_ = false; |
148 | 148 |
149 State last_state = state_; | 149 RefreshScreenlockState(); |
150 // This should force updating screenlock state. | |
151 state_ = STATE_INACTIVE; | |
152 ChangeState(last_state); | |
153 } | 150 } |
154 } | 151 } |
155 | 152 |
156 void EasyUnlockScreenlockStateHandler::MaybeShowHardlockUI() { | 153 void EasyUnlockScreenlockStateHandler::MaybeShowHardlockUI() { |
157 if (hardlock_state_ != NO_HARDLOCK) | 154 if (hardlock_state_ != NO_HARDLOCK) |
158 ShowHardlockUI(); | 155 ShowHardlockUI(); |
159 } | 156 } |
160 | 157 |
158 void EasyUnlockScreenlockStateHandler::SetTrialRun() { | |
159 if (is_trial_run_) | |
160 return; | |
161 is_trial_run_ = true; | |
162 RefreshScreenlockState(); | |
163 } | |
164 | |
161 void EasyUnlockScreenlockStateHandler::OnScreenDidLock() { | 165 void EasyUnlockScreenlockStateHandler::OnScreenDidLock() { |
166 RefreshScreenlockState(); | |
167 } | |
168 | |
169 void EasyUnlockScreenlockStateHandler::OnScreenDidUnlock() { | |
170 hardlock_ui_shown_ = false; | |
171 is_trial_run_ = false; | |
172 } | |
173 | |
174 void EasyUnlockScreenlockStateHandler::OnFocusedUserChanged( | |
175 const std::string& user_id) { | |
176 } | |
177 | |
178 void EasyUnlockScreenlockStateHandler::RefreshScreenlockState() { | |
162 State last_state = state_; | 179 State last_state = state_; |
163 // This should force updating screenlock state. | 180 // This should force updating screenlock state. |
164 state_ = STATE_INACTIVE; | 181 state_ = STATE_INACTIVE; |
165 ChangeState(last_state); | 182 ChangeState(last_state); |
166 } | 183 } |
167 | 184 |
168 void EasyUnlockScreenlockStateHandler::OnScreenDidUnlock() { | |
169 hardlock_ui_shown_ = false; | |
170 if (state_ != STATE_INACTIVE) | |
171 MarkTrialRunComplete(); | |
172 } | |
173 | |
174 void EasyUnlockScreenlockStateHandler::OnFocusedUserChanged( | |
175 const std::string& user_id) { | |
176 } | |
177 | |
178 void EasyUnlockScreenlockStateHandler::ShowHardlockUI() { | 185 void EasyUnlockScreenlockStateHandler::ShowHardlockUI() { |
179 DCHECK(hardlock_state_ != NO_HARDLOCK); | 186 DCHECK(hardlock_state_ != NO_HARDLOCK); |
180 | 187 |
181 if (!screenlock_bridge_->IsLocked()) | 188 if (!screenlock_bridge_->IsLocked()) |
182 return; | 189 return; |
183 | 190 |
184 if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != | 191 if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != |
185 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { | 192 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { |
186 screenlock_bridge_->lock_handler()->SetAuthType( | 193 screenlock_bridge_->lock_handler()->SetAuthType( |
187 user_email_, | 194 user_email_, |
188 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, | 195 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, |
189 base::string16()); | 196 base::string16()); |
190 } | 197 } |
191 | 198 |
192 if (hardlock_state_ == NO_PAIRING) { | 199 if (hardlock_state_ == NO_PAIRING) { |
193 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); | 200 screenlock_bridge_->lock_handler()->HideUserPodCustomIcon(user_email_); |
194 hardlock_ui_shown_ = false; | 201 hardlock_ui_shown_ = false; |
195 return; | 202 return; |
196 } | 203 } |
197 | 204 |
198 if (hardlock_ui_shown_) | 205 if (hardlock_ui_shown_) |
199 return; | 206 return; |
200 | 207 |
201 ScreenlockBridge::UserPodCustomIconOptions icon_options; | 208 ScreenlockBridge::UserPodCustomIconOptions icon_options; |
202 icon_options.SetIcon(ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED); | 209 icon_options.SetIcon(ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED); |
203 | 210 |
204 base::string16 tooltip; | 211 // TODO(tbarzic): Remove this condition for M-40. |
205 if (hardlock_state_ == USER_HARDLOCK) { | 212 if (IsLocaleEnUS()) { |
206 tooltip = l10n_util::GetStringFUTF16( | 213 base::string16 tooltip; |
207 IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_USER, GetDeviceName()); | 214 if (hardlock_state_ == USER_HARDLOCK) { |
208 } else if (hardlock_state_ == PAIRING_CHANGED) { | 215 tooltip = l10n_util::GetStringFUTF16( |
209 tooltip = l10n_util::GetStringUTF16( | 216 IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_USER, GetDeviceName()); |
210 IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_PAIRING_CHANGED); | 217 } else if (hardlock_state_ == PAIRING_CHANGED) { |
211 } else { | 218 tooltip = l10n_util::GetStringUTF16( |
212 LOG(ERROR) << "Unknown hardlock state " << hardlock_state_; | 219 IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_HARDLOCK_PAIRING_CHANGED); |
220 } else { | |
221 LOG(ERROR) << "Unknown hardlock state " << hardlock_state_; | |
222 } | |
223 icon_options.SetTooltip(tooltip, true /* autoshow */); | |
213 } | 224 } |
214 icon_options.SetTooltip(tooltip, false /* don't autoshow */); | |
215 | 225 |
216 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, | 226 screenlock_bridge_->lock_handler()->ShowUserPodCustomIcon(user_email_, |
217 icon_options); | 227 icon_options); |
218 hardlock_ui_shown_ = true; | 228 hardlock_ui_shown_ = true; |
219 } | 229 } |
220 | 230 |
221 void EasyUnlockScreenlockStateHandler::UpdateTooltipOptions( | 231 void EasyUnlockScreenlockStateHandler::UpdateTooltipOptions( |
222 bool trial_run, | 232 bool trial_run, |
223 ScreenlockBridge::UserPodCustomIconOptions* icon_options) { | 233 ScreenlockBridge::UserPodCustomIconOptions* icon_options) { |
224 size_t resource_id = 0; | 234 size_t resource_id = 0; |
225 base::string16 device_name; | 235 base::string16 device_name; |
226 if (trial_run && state_ == STATE_AUTHENTICATED) { | 236 if (trial_run && state_ == STATE_AUTHENTICATED) { |
227 resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_TUTORIAL; | 237 // TODO(tbarzic): Remove this condition for M-40 branch. |
238 if (IsLocaleEnUS()) | |
239 resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_INITIAL_AUTHENTICATED; | |
240 else | |
241 resource_id = IDS_EASY_UNLOCK_SCREENLOCK_TOOLTIP_TUTORIAL; | |
228 } else { | 242 } else { |
229 resource_id = GetTooltipResourceId(state_); | 243 resource_id = GetTooltipResourceId(state_); |
230 if (TooltipContainsDeviceType(state_)) | 244 if (TooltipContainsDeviceType(state_)) |
231 device_name = GetDeviceName(); | 245 device_name = GetDeviceName(); |
232 } | 246 } |
233 | 247 |
234 if (!resource_id) | 248 if (!resource_id) |
235 return; | 249 return; |
236 | 250 |
237 base::string16 tooltip; | 251 base::string16 tooltip; |
238 if (device_name.empty()) { | 252 if (device_name.empty()) { |
239 tooltip = l10n_util::GetStringUTF16(resource_id); | 253 tooltip = l10n_util::GetStringUTF16(resource_id); |
240 } else { | 254 } else { |
241 tooltip = l10n_util::GetStringFUTF16(resource_id, device_name); | 255 tooltip = l10n_util::GetStringFUTF16(resource_id, device_name); |
242 } | 256 } |
243 | 257 |
244 if (tooltip.empty()) | 258 if (tooltip.empty()) |
245 return; | 259 return; |
246 | 260 |
247 icon_options->SetTooltip( | 261 icon_options->SetTooltip(tooltip, trial_run /* autoshow tooltip */); |
248 tooltip, | |
249 state_ == STATE_AUTHENTICATED && trial_run /* autoshow tooltip */); | |
250 } | |
251 | |
252 bool EasyUnlockScreenlockStateHandler::IsTrialRun() { | |
253 return pref_service_ && | |
254 pref_service_->GetBoolean(prefs::kEasyUnlockShowTutorial); | |
255 } | |
256 | |
257 void EasyUnlockScreenlockStateHandler::MarkTrialRunComplete() { | |
258 if (!pref_service_) | |
259 return; | |
260 pref_service_->SetBoolean(prefs::kEasyUnlockShowTutorial, false); | |
261 } | 262 } |
262 | 263 |
263 base::string16 EasyUnlockScreenlockStateHandler::GetDeviceName() { | 264 base::string16 EasyUnlockScreenlockStateHandler::GetDeviceName() { |
264 #if defined(OS_CHROMEOS) | 265 #if defined(OS_CHROMEOS) |
265 return chromeos::GetChromeDeviceType(); | 266 return chromeos::GetChromeDeviceType(); |
266 #else | 267 #else |
267 // TODO(tbarzic): Figure out the name for non Chrome OS case. | 268 // TODO(tbarzic): Figure out the name for non Chrome OS case. |
268 return base::ASCIIToUTF16("Chrome"); | 269 return base::ASCIIToUTF16("Chrome"); |
269 #endif | 270 #endif |
270 } | 271 } |
271 | 272 |
272 void EasyUnlockScreenlockStateHandler::UpdateScreenlockAuthType() { | 273 void EasyUnlockScreenlockStateHandler::UpdateScreenlockAuthType() { |
273 if (!IsTrialRun() && hardlock_state_ != NO_HARDLOCK) | 274 if (!is_trial_run_ && hardlock_state_ != NO_HARDLOCK) |
274 return; | 275 return; |
275 | 276 |
276 if (state_ == STATE_AUTHENTICATED) { | 277 if (state_ == STATE_AUTHENTICATED) { |
277 screenlock_bridge_->lock_handler()->SetAuthType( | 278 if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != |
278 user_email_, | 279 ScreenlockBridge::LockHandler::USER_CLICK) { |
xiyuan
2014/10/09 21:03:55
nit: Should we move this logic to Lockhandler::Set
tbarzic
2014/10/09 21:33:27
I plan to make a cl that removes FORCED_OFFLINE_PA
| |
279 ScreenlockBridge::LockHandler::USER_CLICK, | 280 screenlock_bridge_->lock_handler()->SetAuthType( |
280 l10n_util::GetStringUTF16( | 281 user_email_, |
281 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE)); | 282 ScreenlockBridge::LockHandler::USER_CLICK, |
283 l10n_util::GetStringUTF16( | |
284 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE)); | |
285 } | |
282 } else if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != | 286 } else if (screenlock_bridge_->lock_handler()->GetAuthType(user_email_) != |
283 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { | 287 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD) { |
284 screenlock_bridge_->lock_handler()->SetAuthType( | 288 screenlock_bridge_->lock_handler()->SetAuthType( |
285 user_email_, | 289 user_email_, |
286 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, | 290 ScreenlockBridge::LockHandler::OFFLINE_PASSWORD, |
287 base::string16()); | 291 base::string16()); |
288 } | 292 } |
289 } | 293 } |
OLD | NEW |