Chromium Code Reviews| 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/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/component_loader.h" | 14 #include "chrome/browser/extensions/component_loader.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/easy_unlock_service_factory.h" | 17 #include "chrome/browser/signin/easy_unlock_service_factory.h" |
| 17 #include "chrome/browser/ui/extensions/application_launch.h" | 18 #include "chrome/browser/ui/extensions/application_launch.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
| 22 #include "device/bluetooth/bluetooth_adapter.h" | |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 21 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 22 #include "extensions/common/one_shot_event.h" | 25 #include "extensions/common/one_shot_event.h" |
| 23 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
| 24 | 27 |
| 25 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
| 26 #include "chrome/browser/chromeos/login/users/user_manager.h" | 29 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 27 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 30 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 28 #endif | 31 #endif |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 extensions::ComponentLoader* GetComponentLoader( | 35 extensions::ComponentLoader* GetComponentLoader( |
| 33 content::BrowserContext* context) { | 36 content::BrowserContext* context) { |
| 34 extensions::ExtensionSystem* extension_system = | 37 extensions::ExtensionSystem* extension_system = |
| 35 extensions::ExtensionSystem::Get(context); | 38 extensions::ExtensionSystem::Get(context); |
| 36 ExtensionService* extension_service = extension_system->extension_service(); | 39 ExtensionService* extension_service = extension_system->extension_service(); |
| 37 return extension_service->component_loader(); | 40 return extension_service->component_loader(); |
| 38 } | 41 } |
| 39 | 42 |
| 40 } // namespace | 43 } // namespace |
| 41 | 44 |
| 42 // static | 45 // static |
| 43 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { | 46 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { |
| 44 return EasyUnlockServiceFactory::GetForProfile(profile); | 47 return EasyUnlockServiceFactory::GetForProfile(profile); |
| 45 } | 48 } |
| 46 | 49 |
| 50 class EasyUnlockService::BluetoothDetector | |
| 51 : public device::BluetoothAdapter::Observer { | |
| 52 public: | |
| 53 explicit BluetoothDetector(EasyUnlockService* service) | |
| 54 : service_(service), weak_ptr_factory_(this) {} | |
|
Ilya Sherman
2014/08/14 07:29:41
nit: One per line, please.
xiyuan
2014/08/14 20:27:43
Done. I personally like one-per-line style but "gi
| |
| 55 | |
| 56 virtual ~BluetoothDetector() { | |
| 57 if (adapter_) | |
| 58 adapter_->RemoveObserver(this); | |
| 59 } | |
| 60 | |
| 61 void Initialize() { | |
| 62 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | |
| 63 return; | |
| 64 | |
| 65 device::BluetoothAdapterFactory::GetAdapter( | |
| 66 base::Bind(&BluetoothDetector::OnAdapterInitialized, | |
| 67 weak_ptr_factory_.GetWeakPtr())); | |
| 68 } | |
| 69 | |
| 70 bool IsPresent() const { | |
| 71 return adapter_ && adapter_->IsPresent(); | |
| 72 } | |
| 73 | |
| 74 // device::BluetoothAdapter::Observer | |
| 75 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
| 76 bool present) OVERRIDE { | |
| 77 ReportAdapterPresent(); | |
|
Ilya Sherman
2014/08/14 07:29:41
If the adapter was removed from the system, should
xiyuan
2014/08/14 20:27:43
Nope. The BluetoothAdapter instance never changes
armansito
2014/08/14 20:55:05
Yep, exactly. We have a singleton BluetoothAdapter
| |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 82 adapter_ = adapter; | |
| 83 adapter_->AddObserver(this); | |
| 84 ReportAdapterPresent(); | |
| 85 } | |
| 86 | |
| 87 void ReportAdapterPresent() { | |
| 88 service_->OnBluetoothAdapterPresentChanged(); | |
| 89 } | |
| 90 | |
| 91 EasyUnlockService* service_; | |
|
Ilya Sherman
2014/08/14 07:29:41
nit: Please document lifetime expectations.
xiyuan
2014/08/14 20:27:43
Done.
| |
| 92 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 93 base::WeakPtrFactory<BluetoothDetector> weak_ptr_factory_; | |
| 94 DISALLOW_COPY_AND_ASSIGN(BluetoothDetector); | |
| 95 }; | |
| 96 | |
| 47 EasyUnlockService::EasyUnlockService(Profile* profile) | 97 EasyUnlockService::EasyUnlockService(Profile* profile) |
| 48 : profile_(profile), weak_ptr_factory_(this) { | 98 : profile_(profile), |
| 99 bluetooth_detector_(new BluetoothDetector(this)), | |
| 100 weak_ptr_factory_(this) { | |
| 49 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 101 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
| 50 FROM_HERE, | 102 FROM_HERE, |
| 51 base::Bind(&EasyUnlockService::Initialize, | 103 base::Bind(&EasyUnlockService::Initialize, |
| 52 weak_ptr_factory_.GetWeakPtr())); | 104 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 105 } |
| 54 | 106 |
| 55 EasyUnlockService::~EasyUnlockService() { | 107 EasyUnlockService::~EasyUnlockService() { |
| 56 } | 108 } |
| 57 | 109 |
| 58 // static | 110 // static |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 #if defined(OS_CHROMEOS) | 142 #if defined(OS_CHROMEOS) |
| 91 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) | 143 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) |
| 92 return false; | 144 return false; |
| 93 | 145 |
| 94 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) | 146 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) |
| 95 return false; | 147 return false; |
| 96 | 148 |
| 97 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) | 149 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) |
| 98 return false; | 150 return false; |
| 99 | 151 |
| 100 // It is only disabled when the trial exists and is in "Disable" group. | 152 // It is only disabled when the trial exists and is in "Disable" group. |
|
Ilya Sherman
2014/08/14 07:29:41
nit: Please update this comment.
xiyuan
2014/08/14 20:27:43
Removed "only".
| |
| 101 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; | 153 if (base::FieldTrialList::FindFullName("EasyUnlock") == "Disable") |
| 154 return false; | |
| 155 | |
| 156 if (!bluetooth_detector_->IsPresent()) | |
| 157 return false; | |
| 158 | |
| 159 return true; | |
|
Ilya Sherman
2014/08/14 07:29:41
Hmm, do we not have any command-line switches asso
xiyuan
2014/08/14 20:27:43
We used to have one but it is removed now when tur
| |
| 102 #else | 160 #else |
| 103 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. | 161 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. |
| 104 return false; | 162 return false; |
| 105 #endif | 163 #endif |
| 106 } | 164 } |
| 107 | 165 |
| 108 void EasyUnlockService::Initialize() { | 166 void EasyUnlockService::Initialize() { |
| 109 registrar_.Init(profile_->GetPrefs()); | 167 registrar_.Init(profile_->GetPrefs()); |
| 110 registrar_.Add( | 168 registrar_.Add( |
| 111 prefs::kEasyUnlockAllowed, | 169 prefs::kEasyUnlockAllowed, |
| 112 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); | 170 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); |
| 113 OnPrefsChanged(); | 171 OnPrefsChanged(); |
| 172 | |
| 173 bluetooth_detector_->Initialize(); | |
| 114 } | 174 } |
| 115 | 175 |
| 116 void EasyUnlockService::LoadApp() { | 176 void EasyUnlockService::LoadApp() { |
| 117 DCHECK(IsAllowed()); | 177 DCHECK(IsAllowed()); |
| 118 | 178 |
| 119 #if defined(GOOGLE_CHROME_BUILD) | 179 #if defined(GOOGLE_CHROME_BUILD) |
| 120 base::FilePath easy_unlock_path; | 180 base::FilePath easy_unlock_path; |
| 121 #if defined(OS_CHROMEOS) | 181 #if defined(OS_CHROMEOS) |
| 122 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); | 182 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); |
| 123 #endif // defined(OS_CHROMEOS) | 183 #endif // defined(OS_CHROMEOS) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 135 GetComponentLoader(profile_) | 195 GetComponentLoader(profile_) |
| 136 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); | 196 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); |
| 137 } | 197 } |
| 138 #endif // defined(GOOGLE_CHROME_BUILD) | 198 #endif // defined(GOOGLE_CHROME_BUILD) |
| 139 } | 199 } |
| 140 | 200 |
| 141 void EasyUnlockService::UnloadApp() { | 201 void EasyUnlockService::UnloadApp() { |
| 142 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); | 202 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); |
| 143 } | 203 } |
| 144 | 204 |
| 145 void EasyUnlockService::OnPrefsChanged() { | 205 void EasyUnlockService::CheckIsAllowed() { |
|
Ilya Sherman
2014/08/14 07:29:41
nit: Perhaps name this something more like "Update
xiyuan
2014/08/14 20:27:43
Done.
| |
| 146 if (IsAllowed()) | 206 if (IsAllowed()) |
| 147 LoadApp(); | 207 LoadApp(); |
| 148 else | 208 else |
| 149 UnloadApp(); | 209 UnloadApp(); |
| 150 } | 210 } |
| 211 | |
| 212 void EasyUnlockService::OnPrefsChanged() { | |
| 213 CheckIsAllowed(); | |
| 214 } | |
| 215 | |
| 216 void EasyUnlockService::OnBluetoothAdapterPresentChanged() { | |
| 217 CheckIsAllowed(); | |
| 218 } | |
| 219 | |
| OLD | NEW |