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_service.h" | 5 #include "chrome/browser/signin/easy_unlock_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" |
10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
11 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
12 #include "base/prefs/scoped_user_pref_update.h" | 13 #include "base/prefs/scoped_user_pref_update.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/extensions/component_loader.h" | 15 #include "chrome/browser/extensions/component_loader.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h" | 18 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h" |
18 #include "chrome/browser/signin/easy_unlock_service_factory.h" | 19 #include "chrome/browser/signin/easy_unlock_service_factory.h" |
19 #include "chrome/browser/signin/easy_unlock_service_observer.h" | 20 #include "chrome/browser/signin/easy_unlock_service_observer.h" |
20 #include "chrome/browser/signin/easy_unlock_toggle_flow.h" | 21 #include "chrome/browser/signin/easy_unlock_toggle_flow.h" |
21 #include "chrome/browser/signin/screenlock_bridge.h" | 22 #include "chrome/browser/signin/screenlock_bridge.h" |
22 #include "chrome/browser/ui/extensions/application_launch.h" | 23 #include "chrome/browser/ui/extensions/application_launch.h" |
23 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 27 #include "device/bluetooth/bluetooth_adapter.h" |
| 28 #include "device/bluetooth/bluetooth_adapter_factory.h" |
26 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
27 #include "extensions/common/one_shot_event.h" | 30 #include "extensions/common/one_shot_event.h" |
28 #include "grit/browser_resources.h" | 31 #include "grit/browser_resources.h" |
29 | 32 |
30 #if defined(OS_CHROMEOS) | 33 #if defined(OS_CHROMEOS) |
31 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 34 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
32 #include "components/user_manager/user_manager.h" | 35 #include "components/user_manager/user_manager.h" |
33 #endif | 36 #endif |
34 | 37 |
35 namespace { | 38 namespace { |
(...skipping 15 matching lines...) Expand all Loading... |
51 return extension_service->component_loader(); | 54 return extension_service->component_loader(); |
52 } | 55 } |
53 | 56 |
54 } // namespace | 57 } // namespace |
55 | 58 |
56 // static | 59 // static |
57 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { | 60 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { |
58 return EasyUnlockServiceFactory::GetForProfile(profile); | 61 return EasyUnlockServiceFactory::GetForProfile(profile); |
59 } | 62 } |
60 | 63 |
| 64 class EasyUnlockService::BluetoothDetector |
| 65 : public device::BluetoothAdapter::Observer { |
| 66 public: |
| 67 explicit BluetoothDetector(EasyUnlockService* service) |
| 68 : service_(service), |
| 69 weak_ptr_factory_(this) { |
| 70 } |
| 71 |
| 72 virtual ~BluetoothDetector() { |
| 73 if (adapter_) |
| 74 adapter_->RemoveObserver(this); |
| 75 } |
| 76 |
| 77 void Initialize() { |
| 78 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
| 79 return; |
| 80 |
| 81 device::BluetoothAdapterFactory::GetAdapter( |
| 82 base::Bind(&BluetoothDetector::OnAdapterInitialized, |
| 83 weak_ptr_factory_.GetWeakPtr())); |
| 84 } |
| 85 |
| 86 bool IsPresent() const { |
| 87 return adapter_ && adapter_->IsPresent(); |
| 88 } |
| 89 |
| 90 // device::BluetoothAdapter::Observer: |
| 91 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, |
| 92 bool present) OVERRIDE { |
| 93 service_->OnBluetoothAdapterPresentChanged(); |
| 94 } |
| 95 |
| 96 private: |
| 97 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { |
| 98 adapter_ = adapter; |
| 99 adapter_->AddObserver(this); |
| 100 service_->OnBluetoothAdapterPresentChanged(); |
| 101 } |
| 102 |
| 103 // Owner of this class and should out-live this class. |
| 104 EasyUnlockService* service_; |
| 105 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 106 base::WeakPtrFactory<BluetoothDetector> weak_ptr_factory_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(BluetoothDetector); |
| 109 }; |
| 110 |
61 EasyUnlockService::EasyUnlockService(Profile* profile) | 111 EasyUnlockService::EasyUnlockService(Profile* profile) |
62 : profile_(profile), | 112 : profile_(profile), |
| 113 bluetooth_detector_(new BluetoothDetector(this)), |
63 turn_off_flow_status_(IDLE), | 114 turn_off_flow_status_(IDLE), |
64 weak_ptr_factory_(this) { | 115 weak_ptr_factory_(this) { |
65 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 116 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
66 FROM_HERE, | 117 FROM_HERE, |
67 base::Bind(&EasyUnlockService::Initialize, | 118 base::Bind(&EasyUnlockService::Initialize, |
68 weak_ptr_factory_.GetWeakPtr())); | 119 weak_ptr_factory_.GetWeakPtr())); |
69 } | 120 } |
70 | 121 |
71 EasyUnlockService::~EasyUnlockService() { | 122 EasyUnlockService::~EasyUnlockService() { |
72 } | 123 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 #if defined(OS_CHROMEOS) | 157 #if defined(OS_CHROMEOS) |
107 if (!user_manager::UserManager::Get()->IsLoggedInAsRegularUser()) | 158 if (!user_manager::UserManager::Get()->IsLoggedInAsRegularUser()) |
108 return false; | 159 return false; |
109 | 160 |
110 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) | 161 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) |
111 return false; | 162 return false; |
112 | 163 |
113 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) | 164 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) |
114 return false; | 165 return false; |
115 | 166 |
116 // It is only disabled when the trial exists and is in "Disable" group. | 167 // It is disabled when the trial exists and is in "Disable" group. |
117 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; | 168 if (base::FieldTrialList::FindFullName("EasyUnlock") == "Disable") |
| 169 return false; |
| 170 |
| 171 if (!bluetooth_detector_->IsPresent()) |
| 172 return false; |
| 173 |
| 174 return true; |
118 #else | 175 #else |
119 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. | 176 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. |
120 return false; | 177 return false; |
121 #endif | 178 #endif |
122 } | 179 } |
123 | 180 |
124 EasyUnlockScreenlockStateHandler* | 181 EasyUnlockScreenlockStateHandler* |
125 EasyUnlockService::GetScreenlockStateHandler() { | 182 EasyUnlockService::GetScreenlockStateHandler() { |
126 if (!IsAllowed()) | 183 if (!IsAllowed()) |
127 return NULL; | 184 return NULL; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 turn_off_flow_.reset(); | 282 turn_off_flow_.reset(); |
226 SetTurnOffFlowStatus(IDLE); | 283 SetTurnOffFlowStatus(IDLE); |
227 } | 284 } |
228 | 285 |
229 void EasyUnlockService::Initialize() { | 286 void EasyUnlockService::Initialize() { |
230 registrar_.Init(profile_->GetPrefs()); | 287 registrar_.Init(profile_->GetPrefs()); |
231 registrar_.Add( | 288 registrar_.Add( |
232 prefs::kEasyUnlockAllowed, | 289 prefs::kEasyUnlockAllowed, |
233 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); | 290 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); |
234 OnPrefsChanged(); | 291 OnPrefsChanged(); |
| 292 |
| 293 bluetooth_detector_->Initialize(); |
235 } | 294 } |
236 | 295 |
237 void EasyUnlockService::LoadApp() { | 296 void EasyUnlockService::LoadApp() { |
238 DCHECK(IsAllowed()); | 297 DCHECK(IsAllowed()); |
239 | 298 |
240 #if defined(GOOGLE_CHROME_BUILD) | 299 #if defined(GOOGLE_CHROME_BUILD) |
241 base::FilePath easy_unlock_path; | 300 base::FilePath easy_unlock_path; |
242 #if defined(OS_CHROMEOS) | 301 #if defined(OS_CHROMEOS) |
243 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); | 302 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); |
244 #endif // defined(OS_CHROMEOS) | 303 #endif // defined(OS_CHROMEOS) |
245 | 304 |
246 #ifndef NDEBUG | 305 #ifndef NDEBUG |
247 // Only allow app path override switch for debug build. | 306 // Only allow app path override switch for debug build. |
248 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 307 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
249 if (command_line->HasSwitch(switches::kEasyUnlockAppPath)) { | 308 if (command_line->HasSwitch(switches::kEasyUnlockAppPath)) { |
250 easy_unlock_path = | 309 easy_unlock_path = |
251 command_line->GetSwitchValuePath(switches::kEasyUnlockAppPath); | 310 command_line->GetSwitchValuePath(switches::kEasyUnlockAppPath); |
252 } | 311 } |
253 #endif // !defined(NDEBUG) | 312 #endif // !defined(NDEBUG) |
254 | 313 |
255 if (!easy_unlock_path.empty()) { | 314 if (!easy_unlock_path.empty()) { |
256 GetComponentLoader(profile_) | 315 extensions::ComponentLoader* loader = GetComponentLoader(profile_); |
257 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); | 316 if (!loader->Exists(extension_misc::kEasyUnlockAppId)) |
| 317 loader->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); |
258 } | 318 } |
259 #endif // defined(GOOGLE_CHROME_BUILD) | 319 #endif // defined(GOOGLE_CHROME_BUILD) |
260 } | 320 } |
261 | 321 |
262 void EasyUnlockService::UnloadApp() { | 322 void EasyUnlockService::UnloadApp() { |
263 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); | 323 extensions::ComponentLoader* loader = GetComponentLoader(profile_); |
| 324 if (loader->Exists(extension_misc::kEasyUnlockAppId)) |
| 325 loader->Remove(extension_misc::kEasyUnlockAppId); |
264 } | 326 } |
265 | 327 |
266 void EasyUnlockService::OnPrefsChanged() { | 328 void EasyUnlockService::UpdateAppState() { |
267 if (IsAllowed()) { | 329 if (IsAllowed()) { |
268 LoadApp(); | 330 LoadApp(); |
269 } else { | 331 } else { |
270 UnloadApp(); | 332 UnloadApp(); |
271 // Reset the screenlock state handler to make sure Screenlock state set | 333 // Reset the screenlock state handler to make sure Screenlock state set |
272 // by Easy Unlock app is reset. | 334 // by Easy Unlock app is reset. |
273 screenlock_state_handler_.reset(); | 335 screenlock_state_handler_.reset(); |
274 } | 336 } |
275 } | 337 } |
276 | 338 |
| 339 void EasyUnlockService::OnPrefsChanged() { |
| 340 UpdateAppState(); |
| 341 } |
| 342 |
| 343 void EasyUnlockService::OnBluetoothAdapterPresentChanged() { |
| 344 UpdateAppState(); |
| 345 } |
| 346 |
277 void EasyUnlockService::SetTurnOffFlowStatus(TurnOffFlowStatus status) { | 347 void EasyUnlockService::SetTurnOffFlowStatus(TurnOffFlowStatus status) { |
278 turn_off_flow_status_ = status; | 348 turn_off_flow_status_ = status; |
279 FOR_EACH_OBSERVER( | 349 FOR_EACH_OBSERVER( |
280 EasyUnlockServiceObserver, observers_, OnTurnOffOperationStatusChanged()); | 350 EasyUnlockServiceObserver, observers_, OnTurnOffOperationStatusChanged()); |
281 } | 351 } |
282 | 352 |
283 void EasyUnlockService::OnTurnOffFlowFinished(bool success) { | 353 void EasyUnlockService::OnTurnOffFlowFinished(bool success) { |
284 turn_off_flow_.reset(); | 354 turn_off_flow_.reset(); |
285 | 355 |
286 if (!success) { | 356 if (!success) { |
287 SetTurnOffFlowStatus(FAIL); | 357 SetTurnOffFlowStatus(FAIL); |
288 return; | 358 return; |
289 } | 359 } |
290 | 360 |
291 ClearRemoteDevices(); | 361 ClearRemoteDevices(); |
292 SetTurnOffFlowStatus(IDLE); | 362 SetTurnOffFlowStatus(IDLE); |
293 | 363 |
294 if (GetComponentLoader(profile_)->Exists(extension_misc::kEasyUnlockAppId)) { | 364 if (GetComponentLoader(profile_)->Exists(extension_misc::kEasyUnlockAppId)) { |
295 extensions::ExtensionSystem* extension_system = | 365 extensions::ExtensionSystem* extension_system = |
296 extensions::ExtensionSystem::Get(profile_); | 366 extensions::ExtensionSystem::Get(profile_); |
297 extension_system->extension_service()->ReloadExtension( | 367 extension_system->extension_service()->ReloadExtension( |
298 extension_misc::kEasyUnlockAppId); | 368 extension_misc::kEasyUnlockAppId); |
299 } | 369 } |
300 } | 370 } |
OLD | NEW |